如何将 string[] 作为参数传递给solidity函数?

问题描述

我正在尝试将 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();

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...