A platform for users to sell and trade any standard NFT and any ERC20 transaction type (leading cryptocurrencies, USDT, etc.).
Trade Settings
You need to specify a number and token address, (leading cryptocurrency will be 3, which does not need to be set and cannot be set), to set the transaction type in advance.
Copy function newTradeType(uint id_, address bank_) external onlyOwner returns (bool) {
require(id_ != 3 && tradeType[id_] == address(0),"Wrong trade type");
tradeType[id_] = bank_;
return true;
}
To further set the transaction of NFT. the contract address of NFT should be registered in order to support NFT transaction, as follows:
Copy function newGoodsInfo(uint types_, address place_, string memory name_) external onlyOwner returns (bool) {
require(place_ != address(0) && address(goodsInfos[types_].place) == address(0), "New goods");
goodsInfos[types_] = GoodsInfo({
name:name_,
place:ERC721Upgradeable(place_)
});
emit NewGoods(types_, name_, place_);
return true;
}
After completing the two steps above, users can directly list their NFTs on the market.
NFT listings
When listing, enter the NFT type, Token ID, and the type and price of the token you want to sell, then NFT market listing is completed, waiting for users to buy.
Copy function sell(uint goodsType_, uint tokenId_, uint tradeType_, uint price_) public {
require(price_ >= 1e15 && price_ < 1e26, "Price");
require(address(goodsInfos[goodsType_].place) != address(0), "Wrong goods");
if (tradeType_ != 3) {
require(tradeType[tradeType_] != address(0), "Invalid trade type");
}
goodsInfos[goodsType_].place.safeTransferFrom(_msgSender(), address(this), tokenId_);
sellingGoods[goodsType_][tokenId_] = Goods ({
id : _goodsSeq,
tradeType : tradeType_,
price : price_,
owner: _msgSender()
});
_sellingList[goodsType_][_msgSender()].push(tokenId_);
emit UpdateGoodsStatus(_goodsSeq, true, goodsType_, tokenId_, tradeType_, price_);
_goodsSeq += 1;
}
NFT listings can also be cancelled
Copy function cancelSell(uint goodsType_, uint index_) public {
uint arrLen = _sellingList[goodsType_][_msgSender()].length;
require(index_ < arrLen, "Invalid index");
uint tokenId = _sellingList[goodsType_][_msgSender()][index_];
if (arrLen > 2 && index_ + 1 < arrLen) {
_sellingList[goodsType_][_msgSender()][index_] = _sellingList[goodsType_][_msgSender()][arrLen - 1];
}
_sellingList[goodsType_][_msgSender()].pop();
goodsInfos[goodsType_].place.safeTransferFrom(address(this), _msgSender(), tokenId);
uint goodsId = sellingGoods[goodsType_][tokenId].id;
delete sellingGoods[goodsType_][tokenId];
emit UpdateGoodsStatus(goodsId, false, goodsType_, tokenId, 0, 0);
}
MainCoinPurchase
There will be two functions since the properties of leading cryptocurrency and other tokens are different at the time of purchase. However, they share the same interaction procedure: select the desired NFT type, Token ID, and get the purchased NFT after the transaction is completed.
Purchase leading cryptocurrency
Copy function mainCoinPurchase(uint goodsType_, uint tokenId_) public payable {
Goods memory info = sellingGoods[goodsType_][tokenId_];
require(info.id > 0, "Not selling");
require(info.tradeType == 3, "Main coin");
require(info.price == msg.value, "Value");
require(info.owner != _msgSender(), "Own");
uint handleFee = info.price / 100 * fee;
uint amount = info.price - handleFee;
payable(info.owner).transfer(amount);
purchaseProcess(info.id, info.owner, goodsType_, tokenId_, info.tradeType, info.price);
}
Purchase any tokens
Copy function erc20Purchase(uint goodsType_, uint tokenId_) public {
Goods memory info = sellingGoods[goodsType_][tokenId_];
require(info.id > 0, "Not selling");
require(info.tradeType != 3, "erc20");
require(info.owner != _msgSender(), "Own");
uint handleFee = info.price / 100 * fee;
uint amount = info.price - handleFee;
address banker = tradeType[info.tradeType];
IERC20Upgradeable(banker).safeTransferFrom(_msgSender(), info.owner, amount);
IERC20Upgradeable(banker).safeTransferFrom(_msgSender(), address(this), handleFee);
purchaseProcess(info.id, info.owner, goodsType_, tokenId_, info.tradeType, info.price);
}