5

Consider the following two contracts A and B. Suppose I store these in a and b. Then when I make a call and try to pass two addresses as follows:

a.reqswap(b.address,eth.accounts[0],eth.accounts[1])

it does NOT increment the counter. I make sure there is ether in the contracts a and b.

On the other hand, if I delete the address arguments x,y and all statements that contain x,y from the code and just call :

a.reqswap(b.address)

This time it works and the b.counter is incremented.

As I said, I do make sure eth.defaultAccount is set and that also there are ethers in the contracts (so as to provide gas).


contract A {

    address  public owner;
    uint     public counter ; 
    B b ;
    address public bc ;
    address public bo ; 

    modifier owneronly {
        if (msg.sender != owner)
            throw;
        _
    }

    function A() {
        owner = msg.sender;
        counter = 1 ; 
    }

    function increment(address x,address y) public {
       counter = counter + 1 ;
       bc = x ;
       bo = y ; 
    }

    function reqswap(address baddr,address x,address y)  public {
        b = B(baddr)  ; 
        b.increment(x,y) ; 
    }
}


contract B {

    address  public owner;
    uint     public counter ; 
    A a ;
    address public ac ; 
    address public ao ;

    modifier owneronly {
        if (msg.sender != owner)
            throw;
        _
    }

    function B() {
        owner = msg.sender;
        counter = 1 ; 
    }

    function increment(address x,address y) public {
       counter = counter + 1;
       ac = x ;
       ao = y ; 
    }

    function reqswap(address aaddr,address x,address y)  public {
        a = A(aaddr)  ; 
        a.increment(x,y) ; 
    }
}

3
  • Having enough Ether is still different from how much gas is in the transaction. Have you tried increasing gas, example {gas: 3000000... ? Otherwise, what is the output of eth.getTransactionReceipt ?
    – eth
    Jul 4 '16 at 22:26
  • Thanks a lot for the answer. I included increased gas as you suggested: a.reqswap(b.address, eth.accounts[0], eth.accounts[1], {gas:120000}) and it worked.
    – user813
    Jul 5 '16 at 17:52
  • No problem. Good job on making sure eth.defaultAccount is set and that the relevant accounts have Ether.
    – eth
    Jul 5 '16 at 18:04
3

Having enough Ether is still different from how much gas is specified in the transaction. This question was resolved by increasing the gas: a.reqswap(b.address, eth.accounts[0], eth.accounts[1], {gas:120000})


General tip: When things have been debugged, some scenarios work, and some don't, try increasing the gas in the transaction.

2
  • great tip eth!! Jul 5 '16 at 18:07
  • yeah giving gas during function invocation worked for me too, though the gas I supply when creating a new contract instance is enough for all my operations :/
    – NaN
    Aug 24 '17 at 12:34
0

While you fixed your issue by increasing the gas limit, the example is in general quite educational for seeing how to call a function in another contract. Below is the minimized version of the same functionality:

contract A {
    uint counter ; 
    function increment(uint add) {
       counter = counter + add;
    }
}

contract B {
    function delegateIncrement(address aaddr, uint add) {
        A(aaddr).increment(add) ; 
    }
}

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .