I'm trying to create the calldata offchain for an on-chain low-level call to a method that accepts an array of structs as an argument. I'm not sure how to encode the calldata for this.

Here's a simplified version of what I'm trying to call

Contract A:

    enum Operation {
    struct OrderItem {
        address token;
        uint256 weightIfBuyAmountIfSell;
        uint256 minEthIfSell;
        uint256 deadline;
        Operation op;
    function placeOrder(OrderItem[] memory _order) public payable{
        // handles the OrderItem[]

Contract B:

    function execute(address dest, bytes calldata _calldata) external {
        // The below function will call the placeOrder method of contract A
        (bool success, ) = dest.call(_calldata);

I need to know how to (off-chain) create the _calldata parameter for contract B

Here's what I've got so far:

// the next two lines are the first part I'm unsure of, but here's my best guess
let functionCallTypes = 'tuple[]'
let callFunction = 'placeOrder' + '(' + functionCallTypes.join() + ')'
let callFunctionHash = web3.utils.sha3(callFunction)
let callFunctionSignature = callFunctionHash.substring(0, 10) // 0x + 4 bytes

//The next line lines of web3 code is the second bit I'm unsure about
let functionCallParams = [{I'll include the actual orders here}]

// Once I've got those two variables properly set, the below code should finish the job :) 
let encodedCallParams = web3.eth.abi.encodeParameters(functionCallTypes, functionCallParams)
let data = await web3.utils.toHex(callFunctionSignature + encodedCallParams.substring(2));
  • I know structs are broken into tuples, so it should be something like an array of tuples, but I don't think tuple is a valid solidity type (based on: solidity.readthedocs.io/en/v0.5.3/abi-spec.html#types where it says "Types can be combined to a tuple by enclosing them inside parentheses, separated by commas:" but doesn't actually include tuple as a type itself in the list. But then again, the chart below shows tuple as a type recognized by the ABI encoder, so maybe something like: tuple[]?
    – a94
    Oct 28 '19 at 0:04
  • From solidity.readthedocs.io/en/v0.5.3/abi-spec.html#types it appears you have encode it as (address,uint256,uint256,uint256,uint8)[].
    – Ismael
    Oct 28 '19 at 14:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.