1

I have 2 contracts, FloorInfo and Elevator.

I want to call the goTo(uint) in the Elevator contract from the FloorInfo contract using a low-level call, this happens in the go() function inside the FloorInfo contract. Executing go() causes a revert.

It only does not revert if I make an interface for the Elevator contract, attach it to InstanceAddr and reimplement go() as :

Elevator(InstanceAddr).goTo(1)

Why is this the case?

FloorInfo.sol

contract FloorInfo {
    address private InstanceAddr = 0x804a8a48135EBdfb99e56811F157722A825AD823;
    int isLastFloorCallCount;

    //to be called by contract at *InstanceAddr*
    function isLastFloor(uint _floor) public returns (bool) {
        isLastFloorCallCount++;
        
        if(isLastFloorCallCount == 1)
            return false;
        return true;
    }

    function go() external {
        (InstanceAddr).call{value:0}(
              abi.encodeWithSignature("goTo(uint)", 1)
        );
    }
}

Elevator.sol

interface FloorInfoInterface {
  function isLastFloor(uint) external returns (bool);
}


contract Elevator {
  bool public top;
  uint public floor;

  function goTo(uint _floor) public {
    FloorInfoInterface floorInfo = FloorInfoInterface(msg.sender);

    if (! floorInfo.isLastFloor(_floor)) {
      floor = _floor;
      top = floorInfo.isLastFloor(floor);
    }
  }
}

1 Answer 1

1
  1. You don't have to set the value to 0 when you call the go() function .
  2. call low level function returns two values , a boolean and bytes . Try to store these two values in variables .
function go() external {
           (bool success , bytes memory data) = InstanceAddr.call(
                 abi.encodeWithSignature("goTo(uint)", 1)
           );
            require(success , "call failed");

       }
  1. Make sure the instance address is correct .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.