我正在尝试可靠地执行一个简单的函数,在其中我将一个值传递给一个set()
函数,并将其存储在智能合约中的一个预先声明的变量中。
提供的初始代码truffle unbox react
工作正常。您将其传递给它int
,它会返回事务哈希。但是,如果我将其传递给字符串,则会引发错误。
这是由提供的初始代码/合同truffle unbox react
react.pragma solidity ^0.5.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
我这样称呼它
componentDidMount = async () => {
try {
const web3 = await getWeb3();
const accounts = await web3.eth.getAccounts();
const networkId = await web3.eth.net.getId();
const deployedNetwork = SimpleStorageContract.networks[networkId];
const contract = new web3.eth.Contract(
SimpleStorageContract.abi,
deployedNetwork && deployedNetwork.address
);
this.setState({ web3, accounts, contract });
} catch (error) {
alert(
`Failed to load web3, accounts, or contract. Check console for details.`
);
}
};
这是功能,
contract.methods.set(5).send({ from: accounts[0] });
const response = await contract.methods.get().call();
这是新合同:
pragma solidity ^0.5.0;
contract SimpleStorage {
string storedData;
function set(string memory x) public {
storedData = x;
}
function get() public view returns (string memory) {
return storedData;
}
}
我这样称呼,
contract.methods.set("5").send({ from: accounts[0] });
const response = await contract.methods.get().call();
我收到以下错误
errors.js:85 Uncaught (in promise) Error: insufficient data for dynamicBytes length (arg="", coderType="dynamicBytes", value="0x000000000000000000000000000000000000000000000000000000000005", version=4.0.33)
我究竟做错了什么?