How to encode structs with a user defined type? #1301
-
Hey ya'll! Had a question about encoding structs with user defined types using ethers.js. With web3.js you can use web3.eth.abi.encodeParamters and pass in a JSON interface of a struct w/ a custom type, and I'm trying to figure out how to do the same with ethers. When I try the same syntax as I use with web3.js it gives me When I try to do this by turning a JsonFragmentType into a ParamType it gives me I see that the .from input needs to be a JsonFragmentType and will call fromObject. It looks like JsonFragmentType takes in an array of components of JsonFragmentType, so not sure how to encode the custom type here. For reference here is the code using web3.js:
And what I'm trying with ethers where I'm running into the above issues with encoding the Order[] type.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
The ABI coder takes in the same format that the Solidity copier spits out in the ABI, so you can just copy it from there. Here is what your examples would need: ethers.utils.defaultAbiCoder.encode(
[ 'address',
{
type: "tuple[]",
name: "orders",
components: [
{ name: "makerAddress", type: 'address' },
{ name: "takerAddress", type: 'address' },
{ name: "feeRecipientAddress", type:'address' },
{ name: "senderAddress", type:'address' },
{ name: "makerAssetAmount", type:'uint256' },
{ name: "takerAssetAmount", type:'uint256' },
{ name: "makerFee", type:'uint256' },
{ name: "takerFee", type:'uint256' },
{ name: "expirationTimeSeconds", type:'uint256' },
{ name: "salt", type:'uint256' },
{ name: "makerAssetData", type: 'bytes' },
{ name: "takerAssetData", type:'bytes' },
{ name: "makerFeeAssetData", type:'bytes' },
{ name: "takerFeeAssetData", type:'bytes' },
]
},
'uint256[]',
'bytes[]',
],
[ account, orders, amounts, signatures ],
) Keep in mind the Web3.js library just uses the ethers ABI coder under-the-hood, so I'm not sure where they got that format from, but they are coverting it to the ethers format (i.e. the Solidity format) somewhere in there before calling the ethers encode method. Do you have any info on that representation? If it is a common structure, I can look into adding a parser for it. For your signatures, you might also be interested in checking out EIP-2098, which uses two Let me know if you still have any issues. :) |
Beta Was this translation helpful? Give feedback.
The ABI coder takes in the same format that the Solidity copier spits out in the ABI, so you can just copy it from there.
Here is what your examples would need: