我遇到了一个用例,需要将一个结构传递到一个可靠的函数中。我知道这可以通过元组来完成。然而,我需要签署和发送这个交易从我的node.js后端使用英弗拉作为一个提供者。
目前,我使用web3.js中的encodeABI()对函数调用进行编码。但是,在这种情况下,使用encodeABI()作为参数的结构并不正确,我认为这是因为encodeABI()只支持V1 of AbiEncoder。
在web3.js或ethers.js等中,除了使用V2 of AbiEncoder之外,是否还有encodeABI()所做的功能?
为了解决这个问题,我只需要传递一个扁平的结构,但是我遇到了臭名昭著的stack to deep错误,因为这个结构有10个值,而且我不能将函数分离成许多函数,因为唯一的代码是在函数中形成结构。
编辑:我意识到我可以传递数组中的值来解决压扁问题。然而,我仍然对如何正确地编码数据感到好奇。
感谢您的帮助和建议!
发布于 2019-04-11 11:49:05
您可以使用Ethers.js实用程序来打包v2 AbiEncoder将如何打包数据:
https://docs.ethers.io/v5/api/utils/hashing/#utils--solidity-hashing
文档中的示例:
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'https://ethereum.stackexchange.com/questions/69520
复制相似问题