【问题标题】:How to add ETH as parameter when calling solidity contract function on web3web3上调用solidity合约函数时如何添加ETH作为参数
【发布时间】:2021-08-20 03:01:46
【问题描述】:

我已经创建了带有函数的智能合约:

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 发送到合约函数调用呢?

【问题讨论】:

    标签: blockchain ethereum solidity smartcontracts web3


    【解决方案1】:

    您可以将其作为名为@9​​87654323@ 的属性传递给send() 函数参数。它的值是要发送的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
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-16
      • 2022-08-20
      • 2019-03-28
      • 2021-10-17
      • 2022-01-17
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多