Skip to content

Can't call functions with arguments #53

@SpencerMiller23

Description

@SpencerMiller23

I am working on a contract that makes calls to other contracts, so naturally I wanted to use the Calls.huff from Huffmate. However, I noticed in testing that the calls kept reverting. I started narrowing down the issue by copying the Calls.huff file to my local directory, which still reverted, and then copying the STATICCALL function to my contract file. In the end, it seems that there is an issue with passing arguments to the macro

Contract.huff:

/// @title Contract
/// @notice SPDX-License-Identifier: BSL-1.1

/* Interface */
#define function callContract(address) nonpayable returns (uint256, uint256)

/* Methods */
#define macro CALL_CONTRACT() = takes(0) returns (0) {
    __FUNC_SIG("getReserves()") 0x00 mstore

    STATICCALL(0x00, 0x00, 0x04, 0x1C, 0x04 calldataload, gas)

    // store returndata in memory
    returndatasize      // [returndatasize, success]
    0x00                // [mem_offset, returndatasize, success]
    0x00                // [destOffset, mem_offset, returndatasize, success]
    returndatacopy      // [success] // this stores the return data in memory

    // return the returndata from memory to the msg.sender
    0x40                // [returndatasize, success]
    0x00                // [mem_offset, returndatasize, success]
    return
}

#define macro STATICCALL(
    ret_size,
    ret_offset,
    arg_size,
    arg_offset,
    to,
    maxgas
) = takes (0) returns (1) {
    <ret_size>              // [retSize]
    <ret_offset>            // [retOffset, retSize]
    <arg_size>              // [argSize, retOffset, retSize]
    <arg_offset>            // [argOffset, argSize, retOffset, retSize]
    <to>                    // [to, argOffset, argSize, retOffset, retSize]
    <maxgas>                // [gas, to, argOffset, argSize, retOffset, retSize]
    staticcall              // [success]
}

#define macro MAIN() = takes (0) returns (0) {
    // Identify which function is being called.
    0x00 calldataload 0xE0 shr
    
    dup1 __FUNC_SIG(callContract) eq callContract jumpi

    0x00 0x00 revert

    callContract:
        CALL_CONTRACT()
}

foundry.toml:

[profile.default]
evm_version = 'shanghai'
optimizer = true
optimizer_runs = 200 # Default amount
ffi = true
fuzz_runs = 1_000
remappings = [
  "forge-std=lib/forge-std/src/",
  "foundry-huff=lib/foundry-huff/src/",
  "huffmate=lib/huffmate/src/",
]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions