0

I want to transfer ether to account on my Smart Contract like below.

function withdrawEther(uint amount, address sendTo) external {
    sendTo.transfer(amount);
    EtherWithdraw(amount, sendTo);
}

And I also want to test on Ropsten testnet working with REMIX IDE. What I want to know is that how the transfer works on smart contract? Who send the ether to sendToaddress? Can smart contract hold ether or erc20 token? so that the smart contract transfer ether to other account?

1

Yes, deployed contracts can store ether. To deploy contract with initial amount of ether you need to add constructor function to your code and mark it as payable. Then in Remix type some value to "Value" field and click "Create". Your contract will be initialized with the specified amount of ether (just remember that default unit in Remix is wei and you may not see the effect of your actions if you specify some low value. But you can change the unit to ether).

Then you can use transfer function to send ether from your contract to some address (transfer function also takes wei as argument, not ether).

You can test this behaviour with the code below.

pragma solidity ^0.4.21;

contract TransferTest {

        function TransferTest() payable{
        }

        function withdrawEther(uint amount, address sendTo) external {
            sendTo.transfer(amount);
        }

        function getBalance() public view returns (uint){
            return this.balance;
        }
}

If later on you would like to send more ether to your contract, you will need some other function marked as payable.

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.