3

I've come across a use-case to need to pass a struct into a Solidity function. I know this can be done via tuples. However, I need to sign and send this transaction from my node.js back-end using Infura as a provider.

Currently, I use encodeABI() from web3.js to encode the function call. However, in this case where it has a struct as a parameter using encodeABI() doesn't properly do it, and I'm assuming it's because encodeABI() only supports V1 of AbiEncoder.

Is there any functions in web3.js or ethers.js etc. that do what encodeABI() does, but using the V2 of AbiEncoder?

I would just pass a flattened struct to get around this problem but I run into the infamous stack to deep error, since the struct has 10 values, and I can't separate the function into many functions since the only code is forming the struct inside the function.

EDIT: I realized I could just pass the values in arrays to solve the flattening problem. However, I'm still curious on how to properly encode the data.

Thanks for any help or suggestions!

1 Answer 1

5

You can use the Ethers.js utils to pack the data how the v2 AbiEncoder would:

https://docs.ethers.io/v5/api/utils/hashing/#utils--solidity-hashing

Example from the docs:

let result = utils.solidityKeccak256([ 'int8', 'bytes1', 'string' ], [ -1, '0x42', 'hello' ]);
console.log(result);
// '0x52d7e6a62ca667228365be2143375d0a2a92a3bd4325dd571609dfdc7026686e'

result = utils.soliditySha256([ 'int8', 'bytes1', 'string' ], [ -1, '0x42', 'hello' ]);
console.log(result);
// '0x1eaebba7999af2691d823bf0c817e635bbe7e89ec7ed32a11e00ca94e86cbf37'

result = utils.solidityPack([ 'int8', 'bytes1', 'string' ], [ -1, '0x42', 'hello' ]);
console.log(result);
// '0xff4268656c6c6f'
0

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.