Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't call functions with arguments #53

Open
SpencerMiller23 opened this issue Nov 8, 2023 · 0 comments
Open

Can't call functions with arguments #53

SpencerMiller23 opened this issue Nov 8, 2023 · 0 comments

Comments

@SpencerMiller23
Copy link

SpencerMiller23 commented Nov 8, 2023

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/",
]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant