1

My source below

pragma solidity ^0.4.17;

contract EtherBox {
    uint balance;

    constructor () public payable  {}

    function SetBalance() public payable {
        balance = balance + 10;
    }

    function GetBalance() public payable returns(uint) {
       return balance;
    }
}

contract UsingCall {
    constructor () public payable  {}

    function SimpleCall(address addr) public payable returns (uint) {
        bool status = true;
        //EtherBox eb = new EtherBox();
        EtherBox eb = EtherBox(addr);
        address myaddr = address(eb);
        status = myaddr.call(bytes4(keccak256("SetBalance()")));
        return eb.GetBalance();
    }

    function SimpleCallwithGas() public returns (bool) {
        bool status = true;
        EtherBox eb = new EtherBox();
        address myaddr = address(eb);
        status = myaddr.call.gas(200000)(bytes4(keccak256("GetBalance()")));
        return status;
    }

    function SimpleCallwithGasAndValue() public returns (bool) {
        bool status = true;
        EtherBox eb = new EtherBox();
        address myaddr = address(eb);
        status = myaddr.call.gas(200000).value(1)(bytes4(keccak256("GetBalance()")));
        return status;
    }

}

It was fine and still working very well. But I have a question. When I execute SimpleCallwithGasAndValue() method, I want to see changes of value and gas in my Geth program. How could I do that? help me awesome guys!!

0

If you have the interface of the contract you want to call then it is much easier to call it directly.

function SimpleCallwithGasAndValue() public returns (bool) {
    bool status = true;
    EtherBox eb = new EtherBox();
    uint balance = eb.GetBalance.gas(200000).value(1)();
    return status;
}

Solidity allow passing an explicit .value() and .gas() to indicate gas and ether passed to called contract, and you have access to the returned value. A limitation is that an error in the called contract will cause the whole transaction to revert.

If you want both to access the returned value and be able to detect an error in the called contract the only solution is to use a low level call from assembly.

| improve this answer | |
  • Oh, I see. I will try the order side. – graychocobo Sep 11 '18 at 3:33

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.