0

New to Solidity...

I am studying how to withdraw Ether from a smart contract by setting a parameter in a function. Based on the value the function receives, an amount of Ether is sent to a contract wallet address. My goal is to use address.transfer(amount in wei) as discussed here...

http://solidity.readthedocs.io/en/v0.4.21/units-and-global-variables.html?highlight=.transfer

The issue is I am not able to execute the transfer of Ether from the contract address. I am using Remix in conjunction with a local blockchain, and I have confirmed that all my accounts are unlocked, so I should be able to send ether from this contract.

Below is the source that I am studying with...

The idea is that if a value of 1 - 3 is provided, then 24, 12, or 6 Ether is sent to the address held in variable remMem. I set x = y to verify that the value was received by the function.

pragma solidity ^0.4.18;

contract ETHTEST {

    uint256 public y;
    address public remMem = 0x4152B21f407Ba23c6118c1641821EC4250DBf3B1;

    function transferETH(uint256 x) public payable returns(uint256){
    // y = x;
    // return;   

        if (x == 1){

            y = x;
            remMem.transfer(24000000000000000000);
            return;
        }

        if (x == 2){

            y = x;
            remMem.transfer(12000000000000000000);
            return;
        }

        if (x == 3){

            y = x;
            remMem.transfer(6000000000000000000);
            return;
        }

        if (x >=4 || x < 0){  //works
            revert();
            return;
        }

    }

}

How would I code the transfer of Ether successfully? Useful guidance would be most appreciated.

1

You need to add a payable function to accept Ether

function () public payable {
    // this function enables the contract to receive funds
}

It doesn't need to do anything, but you can add logic in there if you need some.

  • Thank you for the guidance, it solved my understanding gap. – 29A Apr 15 '18 at 1:42

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.