How do I call another contract with huff?
And then bonus points... Return the data from the call.
Let's say I have a solidity-style contract with a function as such:
NumberGetter.sol
uint256 myNumber = 777;
function getNumber() public view returns (uint256) {
return myNumber;
}
How would I call this function from a huff
contract?
We'd need to use call
or staticcall
(let's use staticcall
for this example).
I think it would look something like this:
#define function getNumberFromOtherContract() view returns (uint256)
#define constant NUMBER_GETTER_ADDRESS = 0x0000000000000000000000000000000000000000 // Swap out for NumberGetter.sol contract address
#define macro GET_NUMBER_FROM_OTHER_CONTRACT() = takes (0) returns (0) {
// store the function to call in memory
__FUNC_SIG("getNumber()") 0x00 mstore
// return size = 0x20
// return offset = 0x00
// arguments size = 0x04 (function selector size)
// arguments offset = 0x00
// address = NUMBER_GETTER_ADDRESS
// gas = whatever gas is left = gas
// staticcall calls our NUMBER_GETTER_ADDRESS contract
0x20 0x00 0x04 0x00 [NUMBER_GETTER_ADDRESS] gas staticcall
// size = 0x20
// return data offset = 0x00
// destination in memory offset = 0x00
0x20 0x00 0x00 returndatacopy
0x20 0x00 return
}