问题描述
我已经创建了具有功能的智能合约:
function putOrder() external payable {
require(msg.value == itemPrice);
(bool sent,bytes memory data) = shopManager.call{value: msg.value}("");
require(sent,"Failed to purchase");
}
这只是检查 eth/bnb 值是否正确传递给函数,然后将其发送到管理器地址。
这就是我在 web3 上使用 react 的函数的样子:
const putOrder() = async () => {
...
window.contract.methods.orderStuff().send({from: accounts[0]}).on(
'receipt',function(){
processOrder();
}
);
...
}
显然,我收到一个错误,指出 itemPrice 不符合要求。那么如何通过 eth/bnb 值通过 web3 发送到合约函数调用?
解决方法
您可以将其作为名为 send()
的属性传递给 value
函数参数。它的值是要发送的wei数量(不是ETH数量)。
它只是对 transaction 参数(执行合约功能的交易)的覆盖。因此,如果需要,您还可以使用它来覆盖 gas
值、nonce
和其他参数。
.send({
from: accounts[0],value: 1 // 1 wei
})
.send({
from: accounts[0],value: web3.utils.toWei(1,'ether') // 1 ETH == 10^18 wei
})