1

I tried to build a basic contract from some resources but failed to deposit the ether into this contract. Following is my code, is it correct that "msg.value" and "address(this).balance" be 0 at any time? If so, how can I send ether to another address? If not so, how to deposit money in a correct method?

// can get balance
function getOnesBalance(address addr) public view returns (uint){
    return addr.balance;
}

// can get balance
function getMyBalance() public view returns (uint){
    return msg.sender.balance;
}

// 0
function getMyBalance2() public view returns (uint){
    return msg.value;
}
// 0
function getMyBalance3() public view returns (uint){
    return address(this).balance;
}

function deposit(uint _money) public payable {}

function getContractAddr() public view returns (address){
    return msg.sender;
}

function() payable {}
  • 1
    How are you transferring ether to the contract? Presumably, you're invoking either deposit or the fallback function. Are you doing that via code? If so, can you share that code? If not, can you tell us how you're transferring the ether? – user19510 Mar 25 '18 at 4:41
  • getContractAddr() is wrong, it will return the address of the one who called the function.. – Ayushya Mar 25 '18 at 4:46
2

is it correct that "msg.value" and "address(this).balance" be 0 at any time?

Yes, msg.value depends on each transaction. While address(this).balance will return the balance stored at the address of contract.

msg.value is number of wei sent with the transaction.

The contract given below will allow you to deposit ether to it, and then get the information who deposited how much:

contract MyContract {
    mapping(address => uint256) balances;

    function getOnesBalance(address addr) public view returns (uint){
        return balances[addr];
    }
    function getMyBalance() public view returns (uint){
        return balances[msg.sender];
    }
    function getContractBalance() public view returns (uint){
        return address(this).balance;
    }
    function getContractAddress() public view returns (address){
        return address(this)
    }
    function deposit() public payable {balances[msg.sender]+=msg.value;}
    function() public payable {deposit();}
}

Corrections done are as follows:

  • deposit: This function will be used to send ether to contract
  • function() public payable {deposit();}: This is the fallback function which is executed whenever the contract receives ether.
  • balances[]: You need to store the ether deposited by someone at a different variable. Here we are storing in the mapping named balances
  • getMyBalance: This function will return the amount of ether deposited by the person who calls this function.
  • getContractAddress: Contract address is retrieved by using address(this).

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.