Sending ether from a contract to another fails

tcimolitcimoli Cagliari - ItalyMember Posts: 35
Hi, I am trying to send some ether from a contract to another, like this:
... function sendSomeMoney(uint n){ secondContract.ping(); if (!secondContractAddr.send(n)) error = true; else error = false; }
secondContract is the destination contract, its address is secondContractAddr. Calling ping on secondContract works just fine, while the send returns false.

How do I know which is the problem? Is there a log to know it? For which causes a send may fail?
Thank you

Comments

  • smalltalksmalltalk Member Posts: 16
    The best way I have found for debugging is adding events on each branch of an if statement and that way you can trace the code. Crude I know but the best way I found so far. Although probably won't help you in this case.

    To send ethers to another contract you don't need to send to the contract address:

    secondContract.call.value(1 ether);

    or if you want to send it via a function call you can do this:

    secondContract.ping.value(1 ether);

    If you have an argument to the function call then the syntax is:

    secondContract.ping.value(1 ether) (arg1);

  • tcimolitcimoli Cagliari - ItalyMember Posts: 35
    Thank you, it worked.
    In the fist case, usign the "call" methode, how could I also invoque the ping function?
    This is not working:
    sec.call.ping.value(1).gas(4)();
  • smalltalksmalltalk Member Posts: 16
    Not sure what you are trying to do. The call is only required if you want to send it directly to the contract. If you want to do it via the ping function then you don't need the call, so:

    sec.ping.value(1).gas(4);

  • tcimolitcimoli Cagliari - ItalyMember Posts: 35
    edited May 2016
    Yes, thank you. I just wanted to better understand the use of call: is it possible to call a methode on a contract using call?

    I was interested also because with call, it does not throw an exception in case of failure, but it just returns a bool (so I have been told...). And I am trying to figure out how exceptions and gas consuption are managed.
Sign In or Register to comment.