简体   繁体   English

web3上调用solidity合约function时如何添加ETH作为参数

[英]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.

相关问题 使用web3 js调用智能合约功能 - calling a smart contract function with web3 js Web3功能的回拨问题-牢固性 - Web3 Call Back Issue for Function - Solidity 如何在合约中设置 ETH 的接收者,以在 remix IDE 中使用solidity usign call() 从一个地址向另一个地址发送ETH - How to set the receiver of ETH in a contract to send ETH from one address to another with solidity usign call() in remix IDE 如何解决[web3.eth.Contract不是构造函数] - How to solve [web3.eth.Contract is not a constructor] Web3 1.0:创建合同后,`web3.eth.call(tx)`返回什么? - Web3 1.0: What does `web3.eth.call(tx)` return for a contract creation? 松露固体-从不同合约调用功能 - Truffle Solidity - Calling function from different contract Solidity function 在混音中返回真,但在 web3 function 调用中返回假 - Solidity function returning true in remix but false in web3 function call 可靠性:调用另一个合约的函数时出错。 错误:发送值时应向构造函数付款 - Solidity: Error when calling a function of another contract. Error: The constructor should be payable when you send value 如何在ganache / truffle / web3中解锁合同地址,以便我可以使用from作为调用函数? - How to unlock a contract address in ganache/truffle/web3 so that I can use it as from to call a function? 如何使用solidity和web3将以太币存入账户? - How to deposit ether to an account using solidity and web3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM