购买商品合约代码:
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