0

This gives a contract the same ability to interact with other contracts (with its address as msg.sender), as an externally-owned account, right?

function call(address _contract, bytes calldata _calldata)
    external returns (bool, bytes memory)
{
    return _contract.call(_calldata);
}
0

To my understanding, Yes! Of course you have to do the abi.encodeWithSignature beforehand and add b as input as type address. You can also specify the amount of gas and the msg.value as shown in the code below.

Side note: When you call abi.encodeWithSignature the signature has to exactly match, e.g. uint instead of uint256 will lead to a failed call, although it is usually implied.

This contract for example enables you to call a function with one uint as input and specify the msg.value and gas:

pragma solidity "0.8.4";

contract externalcaller{
  function encodeforcall(string calldata signature, uint input)
    pure
    public
    returns(bytes memory)
  {
    return abi.encodeWithSignature(signature, input);
  }

  function call(string calldata signature, uint input, address b, 
                uint _gas, uint _weiamount)
    external
    payable
    returns
    (
      bool,
      bytes memory  
    )
  {
   bytes memory _calldata = encodeforcall(signature, input);
   (bool success, bytes memory data) = 
   b.call{gas: _gas, value: weiamount}(_calldata); 
   return (success, data);
  } 
}


contract callee{
  uint constant b = 1312;
  function getbplusa(uint256 a) public payable returns(uint){
    return b+a;
  }
} 
1
  • "b.call" was a typo, meant to have any contract address as input. what do mean abi.encodeWithSignature. _calldata is already formatted in my example. this is what gives the function the ability to call any other contract, that the formatting is done outside the contract, otherwise you limit yourself to what you describe in that answer
    – Anon
    Jul 26 at 13:39

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.