The following is an example contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
struct Param {
uint256 stakedTokens; //准入token数量
bytes32 topic; //投票主题
address tokenAddr; //token合约地址
address[] voters; //所有可参与的投票人
}
contract TestStructParam {
uint256 public stakedTokens; //准入token数量
bytes32 public topic; //投票主题
address public tokenAddr; //token合约地址
address[] public voters; //所有可参与的投票人
constructor(Param memory _param) {
stakedTokens = _param.stakedTokens;
topic = _param.topic;
tokenAddr = _param.tokenAddr;
voters = _param.voters;
}
}
In Remix, when struct is passed in as a parameter, it is a tuple, and brackets [] need to be used to enclose all parameters
When deploying the above contract, you need to pass the struct parameter, and the correct value of the passed parameter is
[10,"0x68656c6c6f000000000000000000000000000000000000000000000000000000","0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",["0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"]]
Note: The bytes32 type and address type in the parameters need to be added with double quotes , and the test is shown in the following figure:
The following is the wrong way of writing, the bytes32 type and the address type do not add double quotes, and an error will be reported:
[10,0x68656c6c6f000000000000000000000000000000000000000000000000000000,0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266,[0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0]]
error message
creation of TestStructParam errored: Error encoding arguments: Error: types/values length mismatch (count={"types":1,"values":2}, value={"types":["tuple(uint256,bytes32,address,address[])"],"values":["[10,",",0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266,[0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0]]"]}, code=INVALID_ARGUMENT, version=abi/5.5.0)
As shown below: