【问题标题】:Error: Transaction reverted: function call failed to execute错误:事务已恢复:函数调用未能执行
【发布时间】:2021-12-02 17:25:43
【问题描述】:

我目前正在开发一个 nft 市场,并希望对每笔销售收取 2.5% 的费用。我已经阅读并在线搜索了如何计算可靠性百分比并在这方面取得了成功,但问题是结果是一个巨大的数字,即 2.5 * 150/100 应该返回 3.75 但我得到了这个数字:3750000000000000000000000000000000000

我的测试

const auctionPrice = ethers.utils.parseUnits("1", "ether");

const [_, userAddress] = await ethers.getSigners();
    let fee = (2.5 * auctionPrice) / 100;
    const commission = ethers.utils.parseUnits(fee.toString(), "ether");
    let commissionValue = commission.toString();

    console.log(commission.toString());
    console.log(fee);

    await market
      .connect(userAddress)
      .createMarketSale(nftContractAddress, 1, commissionValue, {
        value: auctionPrice,
      });

结果

250000000000000000000000000000000

 1 failing

  1) NFTMarket
       Should create and execute market sales:
     Error: Transaction reverted: function call failed to execute
      at NFTMarket.createMarketSale (contracts/NFTMarket.sol:165)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at runNextTicks (node:internal/process/task_queues:65:3)
      at listOnTimeout (node:internal/timers:526:9)
      at processTimers (node:internal/timers:500:7)
      at HardhatNode._mineBlockWithPendingTxs (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:1602:23)
      at HardhatNode.mineBlock (node_modules\hardhat\src\internal\hardhat-network\provider\node.ts:435:16)
      at EthModule._sendTransactionAndReturnHash (node_modules\hardhat\src\internal\hardhat-network\provider\modules\eth.ts:1494:18)

所以我的问题是有没有办法把这个数字(3750000000000000000000000000000000000)变成一个小数或整数?我已经搜索过,但在网上找不到任何谈论这个的东西

【问题讨论】:

    标签: solidity nft hardhat


    【解决方案1】:

    其实你的计算没有问题。问题是函数ethers.utils.parseUnits()的使用。

    让我们先快速回顾一下:

    在底层,以太坊中的所有交易都以wei 支付,这是以太币的最小面额。所以,如果你向某个合约发送 1 ETH,你实际上是在发送 1,000,000,000,000,000,000 wei,因为 1 ETH = 10^18 wei。

    您的代码

    在测试中,当你使用ethers.utils.parseUnits("1", "ether")函数时,你其实是在说“我想发送1 ETH,所以请把这1 ETH兑换成wei,让我的生活更轻松”。然后这个函数只是在你的“1”字符串中添加 18 个零。

    最后,当您执行(2.5 * auctionPrice) / 100 的计算时,结果已经是正确的格式。所以您不必再次致电perseUnits,因为您添加了更多的 18 个零! =D

    【讨论】:

    • 但是当我想将该金额转移给合同所有者时为什么会出现错误```应付(所有者).transfer(listingPrice); ```
    猜你喜欢
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 2020-08-01
    • 2019-05-08
    • 2022-06-15
    相关资源
    最近更新 更多