APE Price: $1.00 (-1.55%)

Contract Diff Checker

Contract Name:
NFTMarketplace

Contract Source Code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface IERC721 {
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    function ownerOf(uint256 tokenId) external view returns (address);

    function getApproved(uint256 tokenId) external view returns (address);

    function isApprovedForAll(
        address owner,
        address operator
    ) external view returns (bool);

    function approve(address to, uint256 tokenId) external;
}

contract NFTMarketplace {
    struct Listing {
        address seller;
        uint256 price;
    }

    // 存储每个NFT的上架信息
    mapping(uint256 => Listing) public listings;

    // NFT合约地址
    IERC721 public nftContract;

    // 市场事件
    event ItemListed(
        address indexed seller,
        uint256 indexed tokenId,
        uint256 price
    );
    event ItemDelisted(address indexed seller, uint256 indexed tokenId);
    event ItemPurchased(
        address indexed buyer,
        address indexed seller,
        uint256 indexed tokenId,
        uint256 price
    );

    constructor(address _nftContract) {
        require(_nftContract != address(0), "Invalid NFT contract address");
        nftContract = IERC721(_nftContract);
    }

    // 上架NFT
    function listItem(uint256 tokenId, uint256 price) external {
        require(price > 0, "Price must be greater than zero");
        require(
            nftContract.ownerOf(tokenId) == msg.sender,
            "You are not the owner"
        );
        require(
            nftContract.getApproved(tokenId) == address(this) ||
                nftContract.isApprovedForAll(msg.sender, address(this)),
            "Marketplace not approved"
        );

        listings[tokenId] = Listing(msg.sender, price);

        emit ItemListed(msg.sender, tokenId, price);
    }

    // 下架NFT
    function delistItem(uint256 tokenId) external {
        Listing memory listing = listings[tokenId];
        require(listing.seller == msg.sender, "You are not the seller");

        delete listings[tokenId];

        emit ItemDelisted(msg.sender, tokenId);
    }

    // 购买NFT
    function buyItem(uint256 tokenId) external payable {
        Listing memory listing = listings[tokenId];
        require(listing.price > 0, "Item not listed");
        require(msg.value == listing.price, "Incorrect payment amount");

        // 转账给卖家
        payable(listing.seller).transfer(msg.value);

        // 转移NFT到买家
        nftContract.safeTransferFrom(listing.seller, msg.sender, tokenId);

        // 从列表中移除
        delete listings[tokenId];

        emit ItemPurchased(msg.sender, listing.seller, tokenId, msg.value);
    }

    // 获取NFT的上架信息
    function getListing(
        uint256 tokenId
    ) external view returns (Listing memory) {
        return listings[tokenId];
    }
}

Please enter a contract address above to load the contract details and source code.

Context size (optional):