1

Every transaction that modifies blockchain state costs gas. However, I have a doubt and would like to ask. Does it cost gas when sending ether from a contract to an address? If it does, how to implement deposit/withdraw scenario? After a user deposits some ethers to the contract, he/she likely would not withdraw the same amount of ethers because he/she needs to pay the transaction fee. So, how deposit/withdraw works anyway?

2

It breaks down like this.

First, there are two kinds of address, externally owned (regular) accounts (EOA) and contracts.

Every EOA has a signing key and can sign and send a transaction to anyone it wants, including a contract, any time it wants.

Contracts do not have signing keys and can never initiate a transaction. They have code and must execute their function if an EOA or another contract sends a transaction.

All transactions begin with a transaction signed by an EOA. If it goes to a contract, that contract may do stuff. The signer pays for gas. That contract may send a message to another contract making it do stuff. The signer still pays for the gas. And so on. No matter how long the chain of contracts and messages is, the original signer pays for all gas.

Suppose Alice wants to deposit, then withdraw from a contract that works like an ATM.

First, Alice deposits funds into contract 0x123. The contract function looks something like:

function acceptDeposit() public payable {
  balances[msg.sender] += msg.value; 
}

Then, Alice wants to withdraw her money. The 'withdrawal' pattern is good practice. In the contract, it would look something like:

function withdrawFunds(uint amount) public {
  require(balances[msg.sender] >= amount;
  balances[msg.sender] -= amount;
  msg.sender.transfer(amount);
}

In both cases, Alice starts the process, signs the transaction and pays for gas. First, the "processing fee" to accept her deposit. Then, the "processing fee" to process her withdrawal.

Hope it helps.

  • Thank Rob but please consider this scenario. Assume that after Alice deposits her money to the smart contract, on the monthly basis, the smart contract automatically pays the interest to her. From your answer, I know that whenever the Alice wants to withdraw the interest, she must initiate a transaction and then, she should pay the fee. However, this is not my use case because I want the smart contract to pay Alice automatically without her intervention. In this case, it seems that the smart contract needs to pay the transaction itself. Is it correct? – Long Truong Mar 11 '18 at 23:10
  • I understand what you want to accomplish. You'll have to adapt to new patterns in order to make it happen. For example, it's possible for Alice to call a read function that can calculate her interest entitlement as well as last stored balance. It's also possible to clean up the bookkeeping the next time Alice signs a transaction to move funds and agrees to pay for gas. There are also some novel approaches to cron-like jobs, and a trusted bookkeeper could be charged with updating all the accounts (at considerable cost). I would not consider the obvious batch process ideal for blockchain. – Rob Hitchens Mar 12 '18 at 0:51
  • So, if Alice has 100 Ethers and after she deposits all 100 Ethers to the smart contract, she will not be able to withdraw the money because her balance is zero and hence, can't pay for the transaction fee. – Long Truong Mar 12 '18 at 2:41
  • She would find she has to pay 100 ether, plus gas to make the deposit, so she can only afford to deposit 99.99... max. When it's time to withdraw from the contract, she will need a little gas money in her wallet. – Rob Hitchens Mar 12 '18 at 2:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.