1

I am unable to send ether from the following contract to a recipient address.

contract MoneyFountain{

    address owner;
    uint256 sendAmount;

    function MoneyFountain(){
        owner = msg.sender;
        sendAmount = 100000000000000000;
    }

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

    function sendWei(address recp) returns (bool){        
        recp.send(sendAmount);
    }
}

I am able to put this contract on a private blockchain and also populate the contract balance to 10 ethers to begin with.

web3.fromWei(eth.getBalance("0x171fa8740cbd7ffaec96109b5d35172556a96886"),"e}

10

After the successful execution of the function sendWei(), if I check the balance of the recipient it doesn't show the balance to be transferred. The contract and the balance of the recipient remain the same.

2

try

if (!recp.send(sendAmount))
throw;

instead recp.send(sendAmount);

and use this.balance without address

also check the balance before sending

if(address(this).balance>sendAmount){
    if (!recp.send(sendAmount)){
        throw;
    return false;
    } 
    else {
    return true;
    }

to check you could use an event :

  event send_amount(address indexed _to,uint _value);

so the code will look like :

contract MoneyFountain{

address owner;
uint256 sendAmount;

function MoneyFountain(){
    owner = msg.sender;
    sendAmount = 100000000000000000;
}

function getBalance() returns (uint){
    return address(this).balance;
}    
event send_amount(address indexed _to,uint indexed _value);

    function sendWei(address recp) returns (bool){

    if(this.balance>sendAmount){
            if (!recp.send(sendAmount)){
                throw;
            return false;
            } 
            else {
               send_amount(recp,sendAmount);
            return true;
            }
    }
    }
  • actually, I stripped down to the recp.send from the above code snippets as no exceptions were being thrown and still the ether weren't getting transferred. – Japp Wilson Oct 13 '16 at 16:01
  • are you working on a private chain? if so you need to mine the transaction – Badr Bellaj Oct 13 '16 at 16:21
  • Yes, these are mined. That is how i know they are successful, they give valid transaction hashes. – Japp Wilson Oct 13 '16 at 19:01
  • Result: { "blockHash": "0xa4ae150e0c84aafbf1b5bd477354e0c0c9c9718b159c1a9b6023b97cd8a1f7bd", "blockNumber": 2243, "contractAddress": null, "cumulativeGasUsed": 54787, "from": "0x50e6abb4972d92d025b2da4fc11fe4e9148a7c14", "gasUsed": 54787, "logs": [], "root": "33a0a75d15529838ecc004265d6cbf4de40732a1ab2c5929bfca2430fdf1782d", "to": "0x29e3f0899fbf98fccf2f7ffdd926733c84c18b15", "transactionHash": "0x19b52c643b659e97537f849a4cb77dc6221a1f43ae0c0a0054bdce605a6f66ac", "transactionIndex": 0 } Transaction cost: 54787 gas. – Japp Wilson Oct 13 '16 at 19:01
  • i will test it localy and give you the result – Badr Bellaj Oct 13 '16 at 19:54

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.