From 844dac87ed28fd10206915c5ad0c4a9545a08c1f Mon Sep 17 00:00:00 2001 From: Alan Xu Date: Sun, 3 Nov 2024 22:18:21 +0800 Subject: [PATCH] docs(38): fix nftswap.sol bug (#831) * docs(38): fix nftswap.sol bug fix nftswap.sol bug --- 38_NFTSwap/NFTSwap.sol | 5 +++-- 38_NFTSwap/readme.md | 5 +++-- Languages/en/38_NFTSwap_en/NFTSwap.sol | 10 +++++---- Languages/en/38_NFTSwap_en/readme.md | 30 ++++++++++++++------------ 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/38_NFTSwap/NFTSwap.sol b/38_NFTSwap/NFTSwap.sol index 4a1bc170f..56de4ca43 100644 --- a/38_NFTSwap/NFTSwap.sol +++ b/38_NFTSwap/NFTSwap.sol @@ -67,9 +67,10 @@ contract NFTSwap is IERC721Receiver { // 将NFT转给买家 _nft.safeTransferFrom(address(this), msg.sender, _tokenId); - // 将ETH转给卖家,多余ETH给买家退款 + // 将ETH转给卖家 + payable(_order.owner).transfer(_order.price); + // 多余ETH给买家退款 if (msg.value > _order.price) { - payable(_order.owner).transfer(_order.price); payable(msg.sender).transfer(msg.value - _order.price); } diff --git a/38_NFTSwap/readme.md b/38_NFTSwap/readme.md index 147cdaf9c..78ccaf476 100644 --- a/38_NFTSwap/readme.md +++ b/38_NFTSwap/readme.md @@ -160,9 +160,10 @@ contract NFTSwap is IERC721Receiver { // 将NFT转给买家 _nft.safeTransferFrom(address(this), msg.sender, _tokenId); - // 将ETH转给卖家,多余ETH给买家退款 + // 将ETH转给卖家 + payable(_order.owner).transfer(_order.price); + // 多余ETH给买家退款 if (msg.value > _order.price) { - payable(_order.owner).transfer(_order.price); payable(msg.sender).transfer(msg.value - _order.price); } diff --git a/Languages/en/38_NFTSwap_en/NFTSwap.sol b/Languages/en/38_NFTSwap_en/NFTSwap.sol index 48e09abf5..76458cb93 100644 --- a/Languages/en/38_NFTSwap_en/NFTSwap.sol +++ b/Languages/en/38_NFTSwap_en/NFTSwap.sol @@ -69,12 +69,14 @@ contract NFTSwap is IERC721Receiver { _nft.safeTransferFrom(address(this), msg.sender, _tokenId); // Transfer ETH to the seller, and refund the excess ETH to the buyer payable(_order.owner).transfer(_order.price); - payable(msg.sender).transfer(msg.value - _order.price); + if (msg.value > _order.price) { + payable(msg.sender).transfer(msg.value - _order.price); + } + + // Release the Purchase event + emit Purchase(msg.sender, _nftAddr, _tokenId, _order.price); delete nftList[_nftAddr][_tokenId]; // delete order - - // Release the Purchase event - emit Purchase(msg.sender, _nftAddr, _tokenId, msg.value); } // Cancellation: The seller cancels the pending order diff --git a/Languages/en/38_NFTSwap_en/readme.md b/Languages/en/38_NFTSwap_en/readme.md index f6fab5365..547904579 100644 --- a/Languages/en/38_NFTSwap_en/readme.md +++ b/Languages/en/38_NFTSwap_en/readme.md @@ -139,25 +139,27 @@ function revoke(address _nftAddr, uint256 _tokenId) public { - Purchase: The buyer pays with `ETH` to purchase the `NFT` on the order, and triggers the `Purchase` event. The parameters are the `NFT` contract address `_nftAddr` and the corresponding `_tokenId` of the `NFT`. Upon success, the `ETH` will be transferred to the seller and the `NFT` will be transferred from the `NFTSwap` contract to the buyer. ```solidity - // Purchase: A buyer purchases an NFT with ETH attached, the contract address is _nftAddr, tokenId is _tokenId - function purchase(address _nftAddr, uint256 _tokenId) payable public { - Order storage _order = nftList[_nftAddr][_tokenId]; // Get Order - require(_order.price > 0, "Invalid Price"); // The NFT price is greater than 0 - require(msg.value >= _order.price, "Increase price"); // The purchase price is greater than the asking price - // Declare IERC721 interface contract variable + // Purchase: The buyer purchases NFT, the contract is _nftAddr, the tokenId is _tokenId, and ETH is required when calling the function + function purchase(address _nftAddr, uint256 _tokenId) public payable { + Order storage _order = nftList[_nftAddr][_tokenId]; // get Order + require(_order.price > 0, "Invalid Price"); // NFT price is greater than 0 + require(msg.value >= _order.price, "Increase price"); // The purchase price is greater than the list price + // Declare IERC721 interface contract variables IERC721 _nft = IERC721(_nftAddr); - require(_nft.ownerOf(_tokenId) == address(this), "Invalid Order"); // The NFT is in the contract + require(_nft.ownerOf(_tokenId) == address(this), "Invalid Order"); // NFT is in the contract - // Transfer the NFT to the buyer + // Transfer NFT to buyer _nft.safeTransferFrom(address(this), msg.sender, _tokenId); - // Transfer ETH to the seller, refund any excess ETH to the buyer + // Transfer ETH to the seller, and refund the excess ETH to the buyer payable(_order.owner).transfer(_order.price); - payable(msg.sender).transfer(msg.value-_order.price); - - delete nftList[_nftAddr][_tokenId]; // Delete order + if (msg.value > _order.price) { + payable(msg.sender).transfer(msg.value - _order.price); + } + + // Release the Purchase event + emit Purchase(msg.sender, _nftAddr, _tokenId, _order.price); - // Release Purchase event - emit Purchase(msg.sender, _nftAddr, _tokenId, msg.value); + delete nftList[_nftAddr][_tokenId]; // delete order } ```