0

I have 2 contract

contract A{


uint bidValue;

   address bidder;

   function bid() public payable{

   bidValue = msg.value;    

   bidder = tx.origin;

   }

}

And contract B :

import A.sol;

contract B{    

  function aBid() public payable{


    A a = A(address);    

    a.bid.value(msg.sender);

  }

}

Some how when i call aBid() from contract B and send ether along with it contract A still get msg.value = 0 and tx.origin = address(0). After checking balance of contract A and B seem like eth value stuck in contract B.

Edit : the problem is the "()" after a.bid.value(msg.sender) but when i add it to my contract the gas fee needed to call that function increase to near infinity. For now all i can do is set gasPrice to 1 so it work like normal :(

1

I flattened it into a single file in Remix for simplicity. As separate files, B.sol will need import "./A.sol"; as in your original code.

I fiddled around with it until it worked. There is a fair bit to sort out on a line-by-line basis. This works as expected:

pragma solidity 0.5.2;

contract A {

   uint public bidValue;
   address public bidder;

   function bid() public payable {
     bidValue = msg.value;    
     bidder = msg.sender;
   }
}

contract B {    

  function aBid(address aAddress) public payable{
    A a = A(aAddress);    
    a.bid.value(msg.value)();
  }
}
  1. Deploy A
  2. Copy A's address the clipboard
  3. Deploy B, passing A's address into the constructor
  4. Send some funds to B.aBid()
  5. Inspect A's bidValue() and bidder() functions.

You can improve B a little bit by passing in A's address just one time on deployment instead of every single time.

contract B {    

  A a; // An "A" known as "a" with the address stored in the contract

  constructor(address aAddress) public {
      a = A(aAddress);
  }

  function aBid() public payable{
    a.bid.value(msg.value)();
  }
}

Following SMarx's suggestion, B can be more concise:

contract B {    

  A a;

  constructor(A _a) public {
      a = _a;
  }

  function aBid() public payable{
    a.bid.value(msg.value)();
  }
}

There is also the interface concept which will be worth looking into as an optimization. I only mention that for completeness.

Hope it helps.

  • My problem seem to be that "()" after .value(msg.value) but when i apply it to my code the gas price jump though the roof. Now im really confused. – dlq May 31 at 5:18
  • 1
    constructor(A _a) public { a = _a; } would be better. No need to bypass type checking. – user19510 May 31 at 6:13
0

This line:

a.bid.value(msg.sender);

should be:

a.bid.value(msg.value)();

You were passing msg.sender instead of msg.value, and you were missing the parentheses to actually call the function.

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.