- A+
所属分类: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发币教程下载:
pdf+视频币安智能链BSC发币教程下载地址:
本文是全系列中第3 / 5篇:NFT Market
- OpenSea 是如何成为最受欢迎的 NFT 市场的?
- 现代汽车集团宣布进军基于社区的 NFT 市场
- 币安BSC智能链NFT Market搭建教程——搭建基于BSC链的NFT Market合约实现【pdf+视频BSC NFT教程下载】
- 波场链TRX上NFT发行教程——NFT中元数据合约源代码实现【pdf+视频TRX链NFT发行教程下载】
- 波场链TRX上NFT发行教程——部署波场链上的NFT合约【pdf+视频TRX链NFT发行教程下载】
- 我的微信
- 这是我的微信扫一扫
-
- App下载
- 漫兮网App下载
-