1

I have this simple contract which has a payable method.

contract Betting is usingOraclize {
  function Betting() {
    //...
  }
  function sendEther() payable returns (uint) {
    return msg.value;
  }

The expected behavior is to accept ether and store it in the contract address. Everything works fine including the transaction except that the balance doesn't show up. It remains zero. Here is the etherscan details of the contract.

The last 2 transactions were made using the sendEther method invoked used web3.js. Still the balance of the contract is zero.

1

To accept the Ether from the wallet address you have to create a default fallback function which will be invoked default while your transferring Ether to it.

contract Betting is usingOraclize {
/* This event is triggered when this contract receives Ether */
event Transfer(address _from, uint256 _value);
function Betting() {
//...
}
// This function will get triggered when you send Ether to this contract address 
function () payable {
   Transfer(msg.sender, msg.value);
}

}

For more reference, refer under fallback function in solidity docs

| improve this answer | |
  • Thanks a lot for the response! But I am already using a payable function and transferred ether by invoking the function. from the documentation it says, if the transaction is not handled properly then the ether will be returned to the sender and transaction will fail. In my case the transaction succeeded yet the balance remains zero. – Kannan Ravindran Sep 20 '17 at 17:58
  • You solution worked. But I am still confused how a fallback function works but the same payable function does not work. I do not want users to send ethers directly to the contract I need to record the amount they are sending and do some logic while they pay the ether. the fallback function allows even normal transactions to occur with the contract address – Kannan Ravindran Sep 20 '17 at 20:27
  • To store the value of ether you can use mapping function like, mapping (address => uint256) public fundValue; function () payable{ fundValue[msg.sender] = msg.value; } Then you can access the mapping function like fundValue[address]; – Karthikeyan Thangavel Sep 21 '17 at 3:28

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.