[英]how to create smart contract with minting fees
I have an nft market place just like opensea running.我有一个 nft 市场,就像 opensea 跑步一样。 I want to charge any artist minting on the site, minting fees of like %5 per item been minted, just like opensea and barkeryswap nft platform.
我想向网站上的任何艺术家收取铸币费,每件铸币的铸币费为 %5,就像 opensea 和 barkeryswap nft 平台一样。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "./NFTCollection.sol";
contract NFTMarketplace {
uint count;
uint public offerCount;
mapping (uint => _Offer) public offers;
mapping (address => uint) public userFunds;
mapping(uint => Seller) public sellers;
NFTCollection nftCollection;
struct _Offer {
uint offerId;
uint id;
address user;
uint price;
bool fulfilled;
bool cancelled;
}
struct Seller {
address userAddres;
uint balance;
}
event Offer(
uint offerId,
uint id,
address user,
uint price,
bool fulfilled,
bool cancelled
);
event OfferFilled(uint offerId, uint id, address newOwner);
event OfferCancelled(uint offerId, uint id, address owner);
event ClaimFunds(address user, uint amount);
constructor(address _nftCollection) {
nftCollection = NFTCollection(_nftCollection);
}
function makeOffer(uint _id, uint _price) public {
nftCollection.transferFrom(msg.sender, address(this), _id);
offerCount ++;
offers[offerCount] = _Offer(offerCount, _id, msg.sender, _price, false, false);
emit Offer(offerCount, _id, msg.sender, _price, false, false);
}
function fillOffer(uint _offerId) public payable {
_Offer storage _offer = offers[_offerId];
require(_offer.offerId == _offerId, 'The offer must exist');
require(_offer.user != msg.sender, 'The owner of the offer cannot fill it');
require(!_offer.fulfilled, 'An offer cannot be fulfilled twice');
require(!_offer.cancelled, 'A cancelled offer cannot be fulfilled');
require(msg.value == _offer.price, 'The BNB amount should match with the NFT Price');
nftCollection.transferFrom(address(this), msg.sender, _offer.id);
_offer.fulfilled = true;
userFunds[_offer.user] += msg.value;
sellers[count].userAddres = _offer.user;
sellers[count].balance = msg.value;
nftCollection.setTrack(msg.sender, _offer.id);
count++;
emit OfferFilled(_offerId, _offer.id, msg.sender);
}
function cancelOffer(uint _offerId) public {
_Offer storage _offer = offers[_offerId];
require(_offer.offerId == _offerId, 'The offer must exist');
require(_offer.user == msg.sender, 'The offer can only be canceled by the owner');
require(_offer.fulfilled == false, 'A fulfilled offer cannot be cancelled');
require(_offer.cancelled == false, 'An offer cannot be cancelled twice');
nftCollection.transferFrom(address(this), msg.sender, _offer.id);
_offer.cancelled = true;
emit OfferCancelled(_offerId, _offer.id, msg.sender);
}
function claimFunds() public {
require(userFunds[msg.sender] > 0, 'This user has no funds to be claimed');
payable(msg.sender).transfer(userFunds[msg.sender]);
emit ClaimFunds(msg.sender, userFunds[msg.sender]);
userFunds[msg.sender] = 0;
}
function getSellers() public view returns (address[] memory, uint[] memory){
address[] memory userAddress = new address[](count);
uint[] memory balances = new uint[](count);
for(uint i = 0; i < count; i++){
userAddress[i] = sellers[i].userAddres;
balances[i] = sellers[i].balance;
}
return (userAddress, balances);
}
// Fallback: reverts if Ether is sent to this smart-contract by mistake
fallback () external {
revert();
}
}
My question here is, how do I modify the marketplace code to add minting fees let say %5 per item been mint?
我的问题是,如何修改市场代码以添加铸币费,比如说每件铸币 %5? OR, Let say charge $5 before uploading an item.
或者,假设在上传商品之前收取 5 美元。 Can you help with sample code please?
你能帮忙提供示例代码吗?
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../client/node_modules/@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "../client/node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
contract NFTCollection is ERC721, ERC721Enumerable {
string[] public tokenURIs;
mapping(string => bool) _tokenURIExists;
mapping(uint => string) _tokenIdToTokenURI;
mapping(uint => address[]) _itemTrack;
constructor()
ERC721("MTL Collection", "MTL")
{
}
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
super._beforeTokenTransfer(from, to, tokenId);
}
function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
return super.supportsInterface(interfaceId);
}
function tokenURI(uint256 tokenId) public override view returns (string memory) {
require(_exists(tokenId), 'ERC721Metadata: URI query for nonexistent token');
return _tokenIdToTokenURI[tokenId];
}
function safeMint(string memory _tokenURI) public {
require(!_tokenURIExists[_tokenURI], 'The token URI should be unique');
tokenURIs.push(_tokenURI);
uint _id = tokenURIs.length;
_tokenIdToTokenURI[_id] = _tokenURI;
setTrack(msg.sender, _id);
_safeMint(msg.sender, _id);
_tokenURIExists[_tokenURI] = true;
}
function setTrack(address _address, uint _id) public returns(bool){
_itemTrack[_id].push(_address);
return true;
}
function getTrack(uint _id) public view returns(address[] memory){
address[] memory users;
users = _itemTrack[_id];
return users;
}
}
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract Migrations {
address public owner = msg.sender;
uint public last_completed_migration;
modifier restricted() {
require(
msg.sender == owner,
"This function is restricted to the contract's owner"
);
_;
}
function setCompleted(uint completed) public restricted {
last_completed_migration = completed;
}
}
My question here is, how do I modify the marketplace code to add minting fees let say %5 per item been mint?
我的问题是,如何修改市场代码以添加铸币费,比如说每件铸币 %5? OR, Let say charge $5 before uploading an item.
或者,假设在上传商品之前收取 5 美元。 Can you help with sample code please?
你能帮忙提供示例代码吗?
In my opinion, you should write it on the Smart Contract.在我看来,你应该把它写在智能合约上。 because I think it would be great if everyone can see how much a fee is for now (transparency) and avoid human error to set fees from the code.
因为我认为如果每个人都能看到现在的费用是多少(透明度)并避免人为错误从代码中设置费用,那就太好了。
You set it on blockchain:您将其设置在区块链上:
uint public listingPrice=0.025 ether;
and when you mint token and when buyer wants to put it on sale again, you add a require statement:当你铸造代币并且当买家想要再次出售它时,你添加一个需求声明:
require(msg.value==listingPrice,"Price must be equal to listing fee");
You can write a function to change the value in the future您可以编写一个函数来更改将来的值
function setListingPrice(uint newPrice) external onlyOwner{
require(newPrice>0,"Price must be at least 1 wei");
listingPrice=newPrice;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.