[英]How to add ETH as parameter when calling solidity contract function on web3
I have created smart contract with function:我用 function 创建了智能合约:
function putOrder() external payable {
require(msg.value == itemPrice);
(bool sent, bytes memory data) = shopManager.call{value: msg.value}("");
require(sent, "Failed to purchase");
}
This just checks if eth/bnb value is properly passed to the function and then sends it to manager address.这只是检查 eth/bnb 值是否正确传递给 function,然后将其发送到管理器地址。
This is how my function on web3 with react looks like:这就是我在 web3 上的 function 与反应的样子:
const putOrder() = async () => {
...
window.contract.methods.orderStuff().send({from: accounts[0]}).on(
'receipt', function(){
processOrder();
}
);
...
}
Obviously I get an error that itemPrice is not met.显然我得到一个错误,即 itemPrice 不符合。 So how do I pass eth/bnb value to send trough web3 to contract function call?那么我如何通过 eth/bnb 值发送槽 web3 来合同 function 调用?
You can pass it to the send()
function argument as a property named value
.您可以将其作为名为value
的属性传递给send()
function 参数。 Its value is the amount of wei (not the amount of ETH) to be sent.它的值是要发送的 wei 数量(不是 ETH 数量)。
It's just an override of the transaction params (the transaction that executes the contract function).它只是对交易参数(执行合约功能的交易)的覆盖。 So you could also use it to override gas
value, nonce
and other params if you need.因此,如果需要,您还可以使用它来覆盖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
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.