OpenSea交易NFT藏品系統模式開發技術及詳情分析

I76搭2o72開發9II9發表於2023-04-17

OpenSea是一個以太坊上的去中心化交易市場,旨在為數字藏品、遊戲物品和其他NFT提供交易服務。在本文中,我們將

介紹如何使用OpenSea交易NFT藏品,並提供相關的程式設計程式碼示例。


OpenSea交易NFT藏品


OpenSea是一個去中心化的市場,可以用於買賣各種型別的數字藏品,包括但不限於NFT(非同質化代幣)。NFT是一種特

殊的數字資產,具有獨特性和不可替代性。在OpenSea上,使用者可以釋出自己的NFT藏品,也可以瀏覽市場上其他使用者釋出

的NFT藏品。當一個NFT藏品被出售時,交易將在以太坊上完成,並且賣家將收到出售金額的付款。


基於Solidity的OpenSea交易NFT藏品的程式碼示例:

typescriptCopy codepragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721URIStorage, Ownable {    
    constructor() ERC721("MyNFT", "MNFT") {}    
    function mint(address to, uint256 tokenId, string memory tokenURI) public onlyOwner {  
          _safeMint(to, tokenId); 
                 _setTokenURI(tokenId, tokenURI);
    }    
    function setTokenURI(uint256 tokenId, string memory tokenURI) public {
            require(_isApprovedOrOwner(msg.sender, tokenId), "Caller is not owner nor approved"); 
                   _setTokenURI(tokenId, tokenURI);
    }    
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {  
          require(_exists(tokenId), "URI query for nonexistent token");
                  return _tokenURI(tokenId);
    }    
    function sell(uint256 tokenId, uint256 price) public {       
     require(_isApprovedOrOwner(msg.sender, tokenId), "Caller is not owner nor approved");      
       require(price > 0, "Price must be greater than zero");      
         approve(address(this), tokenId);     
            OpenSeaStore.storeNewOrder(address(this), tokenId, price);
    }    
    function buy(uint256 tokenId) public payable {
        uint256 price = OpenSeaStore.getOrderPrice(address(this), tokenId);    
            require(price > 0, "Token not for sale");       
             require(msg.value == price, "Incorrect value sent");
        address owner = ownerOf(tokenId);
        address payable payableOwner = payable(owner);
        payableOwner.transfer(msg.value);    
           safeTransferFrom(owner, msg.sender, tokenId);     
           OpenSeaStore.deleteOrder(address(this), tokenId);
    }    
    function cancelSell(uint256 tokenId) public {    
        require(_isApprovedOrOwner(msg.sender, tokenId), "Caller is not owner nor approved");     
           OpenSeaStore.deleteOrder(address(this), tokenId);
    }
}
contract OpenSeaStore {
    
    struct Order {
        address tokenAddress;
        uint256 tokenId;
        uint256 price;
    }    
    Order[] orders; 
       mapping(address => mapping(uint256 => uint256)) tokenOrders;    
    function storeNewOrder(address tokenAddress, uint256


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70028031/viewspace-2946224/,如需轉載,請註明出處,否則將追究法律責任。

相關文章