Search code examples
ethereumsoliditysmartcontracts

TransferFrom() function for ETH(as native token not ERC20)?


is there a function something like the transferFrom() function but for ETH the native token not weth?

If not how would you approach making a function with the same functionality but for the native token.

Thanks!


Solution

  • Not possible to simulate the same functionality.

    Token approvals are stored in the token contract - as well as the token balance.

    In order to simulate the same functionality with the native ETH, there would also need to be an approval database on the same layer as the ETH balance is stored. But there's not.


    Unless you can use the WETH token (or any other custom token), you can only have the users to sent native ETH to an escrow contract. But then they won't be able to use the escrowed ETH until it's pulled from the contract.

    For simplicity, this example shows an escrow contract for only one user. But it can be scaled to keep track of funds of multiple users.

    pragma solidity ^0.8;
    
    contract Escrow {
        address holder;
        address admin;
    
        receive() external payable {}
    
        // only the holder and the admin contract
        // can pull funds from this escrow account
        function withdraw(uint256 _amount) external {
            require(msg.sender == holder || msg.sender == admin);
            payable(msg.sender).transfer(_amount);
        }
    }