聚焦资讯
服务于区块链创业者

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

chatGPT账号

本文主要讲述搭建基于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+视频TRX波场链NFT发行教程及合约源代码下载:
pdf+视频TRX波场链NFT发行教程及合约源代码下载地址:
此内容仅供注册用户可见,请登录!

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

免责声明

发文时比特币价格:$23249

免责声明:

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

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

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

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

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

chatGPT账号
赞(0) 打赏
版权声明:本文采用知识共享 署名4.0国际许可协议 [BY-NC-SA] 进行授权,未经允许不得转载。
文章名称:《币安BSC智能链NFT Market搭建教程——搭建基于BSC链的NFT Market合约实现【pdf+视频BSC NFT教程下载】》
文章链接:https://www.xingzhi.io/nonfungible/501.html
本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

服务于区块链创业者

业务范围商务合作

觉得文章有用就打赏一下文章作者

非常感谢你的打赏,我们将继续给力更多优质内容,让我们一起创建更加美好的区块链世界!

支付宝扫一扫打赏

微信扫一扫打赏

登录后才能复制或下载网站内容