-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract NFTMarket is ReentrancyGuard {
//0x83Ac5678414dB95910e41B8ae1D281D25EE5b22a 部署后合约地址
IERC20 public paymentToken;
constructor(address _address) {
paymentToken = IERC20(_address);
}
struct Listing {
address seller;
address nftContract;
uint256 tokenId;
uint256 price;
bool active;
}
mapping(address => mapping(uint256 => Listing)) public listings;
event NFTListed(
address indexed nftContract,
uint256 indexed tokenId,
address indexed seller,
uint256 price
);
event NFTPurchase(
address indexed buyer,
address indexed nftContract,
uint256 indexed tokenId,
uint256 price
);
function listNFT(
address _nftContract,
uint256 _tokenId,
uint256 _price //10000000000
) external {
IERC721 nft = IERC721(_nftContract);
require(
nft.ownerOf(_tokenId) == msg.sender,
"Not the owner of this NFT"
);
require(
nft.isApprovedForAll(msg.sender, address(this)),
"Contract is not approved"
);
listings[_nftContract][_tokenId] = Listing(
msg.sender,
_nftContract,
_tokenId,
_price,
true
);
emit NFTListed(_nftContract, _tokenId, msg.sender, _price);
}
function buyNFT(address _nftContract, uint256 _tokenId)
external
nonReentrant
{
Listing storage listing = listings[_nftContract][_tokenId];
require(listing.active, "NFT on listed for sale");
IERC721 nft = IERC721(_nftContract);
require(
paymentToken.transferFrom(
msg.sender,
listing.seller,
listing.price
),
"payment failed"
);
nft.safeTransferFrom(listing.seller, msg.sender, _tokenId);
// set listing active false
listing.active = false;
// watch sender listen
emit NFTPurchase(msg.sender, _nftContract, _tokenId, listing.price);
}
}
|
Beta Was this translation helpful? Give feedback.
Answered by
JasonStu
Jun 18, 2024
Replies: 3 comments 2 replies
-
我调用 listNFT 的时候metamask 上的具体块是这样的:https://sepolia.etherscan.io/tx/0x938b1537703e79819bd35126694ee5d8b14ecd0ed947e6e30dfeb8fc9427c0a5 |
Beta Was this translation helpful? Give feedback.
2 replies
-
代码是从哪里获取的 一个视频啥也没讲呀 |
Beta Was this translation helpful? Give feedback.
0 replies
-
我授权的地方不对 应该是要将nft授权到市场去处理 |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
JasonStu
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
我授权的地方不对 应该是要将nft授权到市场去处理