Search code examples
soliditytransfersmartcontractstruffle

How to transfer funds from smart contract to account OTHER than msg.sender?


I am trying to transfer part of funds stored in a smart contract to an account other than the one calling the function (msg.sender). My function is pretty much like this:

function getFunds(address addr, uint amount) public {
    require (address.this(balance)>= amount);
    addr.transfer(amount);
}

What I get when I compile in Truffle is:

Member "transfer" not found or not visible after argument-dependent lookup in address.

As if it is looking for members in a struct.

Thanks for any help.


Solution

  • Since solidity 0.5 addresses should be payable in order to transfer eth to them

    function getFunds(address payable addr, uint amount) public {
        require (address.this(balance)>= amount);
        addr.transfer(amount);
    }
    

    For mapping of a struct you can use like this

    contract testContract {
    
        mapping(uint256 => test) testAddressMapping; 
        struct test {
            address payable testAddress;
        }
    
        function testFunction() external{
            testAddressMapping[0].testAddress.transfer(100);
        }
    }