30 部署合约购买商品的时候出现eth_estimateGas,调用出错,reverted。

购买商品合约代码:

function createMarketSale(address nftContract, uint256 id) public payable nonReentrant {

    MarketItem storage item = marketItems[id]; //should use storge!!!!
    uint price = item.price;
    uint tokenId = item.tokenId;
    address payable seller = item.seller;

    require(msg.value == price, "Please submit the asking price");
    require(IERC721(nftContract).getApproved(tokenId) == address(this), "NFT must be approved to market");
    require(msg.sender != seller, "you can't buy your own token");

    //将 NFT(tokenId)所有权从 item.seller 转移到  msg.sender
    IERC721(nftContract).transferFrom(item.seller, msg.sender, tokenId);
    item.buyer = payable(msg.sender);

    //marketowner 收取一笔上架费用 listingFee
    payable(marketowner).transfer(listingFee);

    //seller获取NFT价值
    seller.transfer(item.price);

    item.state = State.Release;
    _itemSoldCounter.increment();    

    emit MarketItemSold(
      id,
      nftContract,
      tokenId,
      item.seller,
      msg.sender,
      price,
      State.Release
    );    
  }

调用代码:

await market.createMarketSale(tokenAddress, 8, { value: auctionPrice });

其中market是合约地址,tokenAddress是nft地址,价格定义为:

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

报错信息

eth_estimateGas
  Contract call:       NFTMarketplace#createMarketSale
  From:                0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
  To:                  0x8f86403a4de0bb5791fa46b8e795c547942fe4cf
  Value:               1 ETH

  Error: Transaction reverted: function call failed to execute
      at NFTMarketplace.createMarketSale 
      at async EthModule._estimateGasAction
      at async HardhatNetworkProvider._sendWithLogging
请先 登录 后评论

1 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
  • 2 关注
  • 0 收藏,2302 浏览
  • Eeaga 提出于 2022-09-15 17:16