2

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 Answers 2

6

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.

4
  • 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? Mar 11, 2018 at 23:10
  • 1
    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. Mar 12, 2018 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. Mar 12, 2018 at 2:41
  • 1
    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. Mar 12, 2018 at 2:55
2

Think of it like this,

  1. Who call the function/initialize a transaction will pay for the gas fee
  2. A smartcontract's function can not just run by itself, either another smartcontract or an EOA must call it first

which means that the first one to call a smartcontract's function must be an EOA, because no smartcontract's function can call another smartcontract's function by itself. So the EOA which initialize the first call of the call chain will pay for the gas fee.

For example, consider the following call chain:

Alice => smartcontractA => smartcontractB => smartcontractC=>.....

You can see that, only a smartcontract or an EOA can call a smartcontract's function. And for any call chain, when you trace to its root, it must be an EOA who start the chain, then that EOA will pay for all the fee of the call chain not the smartcontract itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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