1

New to Solidity...

I am studying how to deposit Ether into a smart contract by setting a parameter in a function. Based on the value the function receives, the msg.sender is able to send a predetermined amount of Ether.

The idea is that if a value of 1 - 3 is provided, then 24, 12, or 6 Ether is sent from the msg.sender to the contract address. I set z = j to verify that the value was received by the function. Variable t is the value in Wei and is set to msg.value. It appears msg.value is properly set as the value of t gets updated according to the provided input value.

The issue I encounter is that Ether is not transferred when the function is called. Useful guidance would be most appreciated.

I am using Remix and have confirmed that all accounts have been unlocked.

Below is the source...

 pragma solidity ^0.4.18;

contract ETHTEST2 {
    mapping(address => uint256) public deposits; //depost

    uint256 public z;
    uint256 public t;

    function sendETHtoContract(uint256 j) public payable {  //msg.sender & msg.value test - Work in Progress

        if (j == 1){

            z = j;
            msg.value == t;
            t = 24000000000000000000 wei;
            address(this).transfer(msg.value);
            return;
        }

        if (j == 2){

            z = j;
            t = 12000000000000000000 wei;
            address(this).transfer(msg.value);
            return;
        }

        if (j == 3){

            z = j;
            t = 6000000000000000000 wei;
            address(this).transfer(msg.value);
            return;
        }

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

    }

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

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

}

1 Answer 1

5

A contract can't forcefully extract ETH from a wallet address, if you want to pay Ether into a contract, you must set the "Value" of the transaction when calling a function (or just send ETH to the contracts address if the fallback function allows it).

Using transfer will transfer from the contract to another address, so with address(this).transfer(msg.value), the contract is just taking the ETH provided by the msg.sender, and passing it to itself.

function sendETHtoContract(uint256 j) public payable {
    //msg.value is the amount of wei that the msg.sender sent with this transaction. 
    //If the transaction doesn't fail, then the contract now has this ETH.
}
3
  • 1
    Thank you AnAllergyToAnalogy for the clarification regarding transfer. I have a question pertaining to "if you want to pay Ether into a contract, you must set the "Value" of the transaction". Is msg.value the "Value" that needs to be set or is there another "Value" I need to research. If msg.value is correct, have I set it correctly?
    – 29A
    Apr 18, 2018 at 5:45
  • Yes, msg.value is the value sent with the transaction. But this can't be set by the contract, only read by it. This is set by the sender. In Remix, its in the top-right of the screen when then "Run" tab is selected. Apr 18, 2018 at 5:50
  • Thank you again for aiding my understanding AnAllergyToAnalogy, I appreciate it. Was wondering what that 'Value' parameter in Remix was used for ;)
    – 29A
    Apr 18, 2018 at 5:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.