4

In a deployed contract (in sepolia) there is a method readTimelock(address,uint256) that returns a TimeLock, which is a struct like the following:

   struct TimeLock{
     uint160 amount,
     uint64 time,
     bool withdrawn
    }

I would like to make a call to the contract and get that timelock using foundry cast call. I'm trying with something like:

cast call 0x243B56EDb7BD8B0Aa6fE554A4860405717b8CC4a "readTimeLock(address,uint256)(uint160,uint64,bool)" 0x37aFaa6c1B0cD3c92DE24C09451FD046fA7CbfC3 0 --rpc-url $SEPOLIA_URL

The error I get is the following:

(code: -32000, message: execution reverted, data: None)

Unfortunately, in the documentation nothing is said about function returning structs. Thanks. https://book.getfoundry.sh/reference/cast/cast-call

Other errors I get trying different compositions is simply "Invalid data"

2 Answers 2

2

SOLVED.

My apologies, it turns out that the syntax call in the question was actually correct. I misspelled the function readTimeLock() which should have been instead of readTimelock().

I guess the lesson here is that when cast throws the error:

 (code: -32000, message: execution reverted, data: None)

it indicates that the method is not part of the code deployed at that contract address, so either a wrong address, a wrong network, or a misspelling of the function.

Anyways, this is the right call:

cast call 0x243B56EDb7BD8B0Aa6fE554A4860405717b8CC4a --rpc-url $SEPOLIA_RPC_URL "readTimelock(address,uint256)(uint160,uint64,bool)" 0x37aFaa6c1B0cD3c92DE24C09451FD046fA7CbfC3 0

which returns three values corresponding to (uint160,uint64,bool):

600
1683570501
false
1
+50

You can see an example of working with a struct or custom type here in the foundry abi-encode method.

Example:

cast abi-encode "someFunc((string,uint256))" "(myString,1)"

For your TimeLock example:

struct TimeLock{
    uint160 amount,
    uint64 time,
    bool withdrawn
}
cast call 0x243B56EDb7BD8B0Aa6fE554A4860405717b8CC4a "readTimeLock(address,uint256,(uint160,uint64,bool))" 0x37aFaa6c1B0cD3c92DE24C09451FD046fA7CbfC3 0 "(the_uint160,the_uint64,the_bool)" --rpc-url $SEPOLIA_URL
3
  • Thanks for taking the time, Patrick. However, my issue is not how to call with a struct as input argument, but how to parse (or decode) an output struct. The function from the question returns a struct, and when I try to call the readTimelock method I get errors like "Invalid data". Commented May 18, 2023 at 18:01
  • So are you all set? I'm not sure I follow your current answer. If this is your new question, could you either edit your original question or create a new one and link it here? Happy to help with a newly formatted question or if you edit the current one. Commented May 19, 2023 at 12:24
  • I am all set, thanks! I will update the answer so that it is less vague Commented May 21, 2023 at 18:02

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.