[Ethereum] How to do approve and transferFrom for Ether

ether

With regular ERC20 tokens we have method approve(to, amount) and so I can approve to someone to take specified amount of my tokens later with transferFrom(from, to, amount).

Is there any way to approve Ether to be taken from my account?

Best Answer

You could do it by coding pretty much the same logic as the token does for it's own balance.

  1. A deposit function takes ether from msg.sender and stores it in the contract while registering the amount deposited by that address in a mapping state variable.

  2. An approve function would set how much ether the contract is allowed to transfer from the origin address.

  3. A transferFrom function would modify the _from account balance and _to account balance accordingly (up to the amount that was approved).

  4. Here's a main difference with the token logic. You would have to have a withdraw function that allows the msg.sender to reduce its balance and generate an Ether transfer from the contract to msg.sender. (This would be done to prevent possible DoS attacks on the contract).

Related Topic