0

I am trying to test out this sample code:

contract MyContract {


function foo(address a) returns (uint) {

    // send ether with default 21,000 gas
    // likely causes OOG in callee
    a.send(1 ether);

    // send ether with all remaining gas
    // but no success check!
    a.call.value(1 ether)();

    // RECOMMENDED
    // send all remaining gas
    // explicitly handle callee throw
    if(a.call.value(1 ether)()) throw;
    return a.balance;
}

Very simple, just send 1 ether to a specific address, then return address balance, however I always get 0 as output meaning the sending failed:

enter image description here

what's wrong?

| improve this question | | | | |
  • You should send some ether to smart contract before or call foo with some Ethereum. On screen above I see value 0 wei. How can you send Ether if there is any on smart contract account? – Lukasz Zuchowski Sep 28 '17 at 18:19
  • its not 0, it was 100 ether, there were some bug with JS VM, it initialized to be 100 ether, after I clicked foo several times it becomes a very small number close to 0 – Zixuan Zhang Sep 28 '17 at 18:22
0

What is missing is the payable modifier.

I tested this pice of code:

contract MyContract {

function foo(address a) payable returns (uint) {
   a.call.value(1 ether)();
   return a.balance;
 }
}

I deployed it and then run with Value set to 2 (is means 2 Ether) and it works. Here is the output: enter image description here

The output is 1 ETH in Wei.

| improve this answer | | | | |
  • Thanks Lukasz, it works! What does value set to 1 mean here? – Zixuan Zhang Sep 28 '17 at 18:38
  • I edited my answer. Value was set to 2, it means send 2 Ether (from my account to my smart contract address). The main issue here was lack of payable modifier. – Lukasz Zuchowski Sep 28 '17 at 18:46

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.