币安BSC智能链NFT Market搭建教程——搭建基于BSC链的NFT Market合约实现【pdf+视频BSC NFT教程下载】

  • A+
所属分类:NFT
Bitop
pi network

币安BSC智能链NFT Market搭建教程——搭建基于BSC链的NFT Market合约实现【pdf+视频BSC NFT教程下载】

本文主要讲述搭建基于bsc链上的nft market教程,实现功能类似opensea。可以实现NFT的铸造,权限控制,交易,拍卖等功能。同时可以自定义拍卖和交易的手续费,原创作者收益等。相关功能都是基于BSC上的合约功能实现。大致的系统架构是类似于opensea,首先都nft进行colledction分类,不同的collection实现不同模式的nft功能。同一collection下的nft item具备相关的属性。同一collection下的的nft创建不同的分类加一区分,同时nft item关联不同的属性。

相关的合约如下:

ERC721接口类定义:

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

import "./IERC165.sol";

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
function balanceOf(address owner) external view returns (uint256 balance);
function ownerOf(uint256 tokenId) external view returns (address owner);
function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;
function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;
function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;
}


实现类:

contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
    using Address for address;
    using Strings for uint256;
function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);
    }
}

实现nft market核心功能的合约代码

contract ERC721Suika is ERC721Mochi, ReentrancyGuard {
    using SafeMath for uint256;
    using Address for address payable;
    using Address for address;
constructor(
        EIP20 _payment_token, address _owner, address payable _admin, 
        uint256 _commissionRate, uint256 _royaltiesCommissionRate, string memory name, string memory symbol, bool _anyoneCanMint) 
        ERC721Mochi(_owner, name, symbol, _anyoneCanMint) 
    {
        admin = _admin;
        contract_owner = _owner;
        require(_commissionRate<=100, "ERC721Suika: Commission rate has to be between 0 and 100");
        commissionRate = _commissionRate;
        royaltiesCommissionRate = _royaltiesCommissionRate;
        payment_token = _payment_token;
    }
function sell(uint256 tokenId, uint256 price, address payable wallet) public {

        // onlyOwner
        require(ownerOf(tokenId)==msg.sender, "ERC721Suika: Only owner can sell this item");

        // cannot set a price if auction is activated
        require(!auctions[tokenId].open, "ERC721Suika: Cannot sell an item which has an active auction");

        // set sell price for index
        sellBidPrice[tokenId] = price;

        // If price is zero, means not for sale
        if (price>0) {

            // approve the Index to the current contract
            approve(address(this), tokenId);
            
            // set wallet payment
            _wallets[tokenId] = wallet;
            
        }

    }
function buy(uint256 tokenId) public nonReentrant {

        // is on sale
        require(!auctions[tokenId].open && sellBidPrice[tokenId]>0, "ERC721Suika: The collectible is not for sale");

        // transfer ownership
        address owner = ownerOf(tokenId);

        require(msg.sender!=owner, "ERC721Suika: The seller cannot buy his own collectible");

        // we need to call a transferFrom from this contract, which is the one with permission to sell the NFT
        callOptionalReturn(this, abi.encodeWithSelector(this.transferFrom.selector, owner, msg.sender, tokenId));

        // calculate amounts
        uint256 amount4admin = sellBidPrice[tokenId].mul(commissionRate).div(100);
        uint256 amount4creator = sellBidPrice[tokenId].mul(royaltiesCommissionRate).div(100);
        uint256 amount4owner = sellBidPrice[tokenId].sub(amount4admin).sub(amount4creator);

        // to owner
        require(payment_token.transferFrom(msg.sender, _wallets[tokenId], amount4owner), "Transfer failed.");

        // to creator
        if (amount4creator>0) {
            require(payment_token.transferFrom(msg.sender, creators[tokenId], amount4creator), "Transfer failed.");
        }

        // to admin
        if (amount4admin>0) {
            require(payment_token.transferFrom(msg.sender, admin, amount4admin), "Transfer failed.");
        }

        emit Sale(tokenId, owner, msg.sender, sellBidPrice[tokenId]);
        emit Commission(tokenId, owner, sellBidPrice[tokenId], commissionRate, amount4admin);
        emit Royalty(tokenId, owner, sellBidPrice[tokenId], royaltiesCommissionRate, amount4creator);

        soldFor[tokenId] = sellBidPrice[tokenId];

        // close the sell
        sellBidPrice[tokenId] = 0;
        delete _wallets[tokenId];

    }
}

以上合约代码可以实现nft的铸造,权限控制,market买卖交易。

至此,完成币搭建基于BSC链的NFT Market合约实现。

pdf+视频币安智能链BSC发币教程下载:

币安BSC智能链NFT Market搭建教程——搭建基于BSC链的NFT Market合约实现【pdf+视频BSC NFT教程下载】

pdf+视频币安智能链BSC发币教程下载地址:

此处为隐藏的内容!
登录后才能查看!

添加VX或者telegram获取全程线上免费指导

币安BSC智能链NFT Market搭建教程——搭建基于BSC链的NFT Market合约实现【pdf+视频BSC NFT教程下载】

免责声明

发文时比特币价格:$41249

当前比特币价格:[crypto coins=”BTC” type=”text” show=”price”]

当前比特币涨幅:[crypto coins=”BTC” type=”text” show=”percent”]

免责声明:

本文不代表漫兮网立场,且不构成投资建议,请谨慎对待。用户由此造成的损失由用户自行承担,与漫兮网没有任何关系;

漫兮网不对网站所发布内容的准确性,真实性等任何方面做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,漫兮网对项目的真实性,准确性等任何方面均不做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,漫兮网不对其构成任何投资建议,用户由此造成的损失由用户自行承担,与漫兮网没有任何关系;

漫兮区块链研究院声明:漫兮区块链研究院内容由漫兮网发布,部分来源于互联网和行业分析师投稿收录,内容为漫兮区块链研究院加盟专职分析师独立观点,不代表漫兮网立场。

 

 

本文是全系列中第3 / 5篇:NFT Market

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • App下载
  • 漫兮网App下载
  • weinxin
漫兮

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: