问题描述
我正在尝试将 string[] 从 node.js 传递给 solidity 函数:
function test(string[] memory options) public {}
像这样:
const options = ["a","b","c"]
const contract = this.getContract();
const testFunc = contract.functions['test'];
await testFunc(options);
但得到错误:数组的值无效;
solidity 附注:
pragma solidity 0.6.5; pragma Experimental ABIEncoderV2;
此外,如果我从 Etherscan 调用具有相同值的合同,它会起作用
解决方法
contract.functions
用于 ethers.js
(docs)。由于您的问题被标记为 web3
,因此我的回答是使用 web3
库。
假设 this.getContract()
返回 Contract
实例,您可以使用 contract.methods
(docs) 对象执行合约方法。
使用 .send()
从您配置的钱包发送读写交易,或使用 .call()
进行只读调用。
const options = ["a","b","c"]
const contract = this.getContract();
await contract.methods.test(options).send();