首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建带有铸币费的智能合同

如何创建带有铸币费的智能合同
EN

Stack Overflow用户
提问于 2022-02-12 05:25:21
回答 2查看 407关注 0票数 0

我有一个nft市场,就像开敞式跑步一样。我想收费任何艺术家造币在网站上,造币费类似%5每项目被铸币,就像开放和交换nft平台。

我的市场代码:

代码语言:javascript
复制
// 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();
  }
}

我的NFT集合代码

代码语言:javascript
复制
// 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;

    }
}

我的移民法:

代码语言:javascript
复制
// 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;
  }
}

我在这里的问题是,我如何修改市场代码,以增加造币费,比方说,每个项目5%的薄荷?或者,假设在上传一个项目之前收取5美元。你能帮我拿一下示例代码吗?

EN

回答 2

Stack Overflow用户

发布于 2022-05-22 17:56:20

你把它设为区块链:

代码语言:javascript
复制
  uint public listingPrice=0.025 ether;

当您的造币券和买方想再次出售它时,您需要添加一个require语句:

代码语言:javascript
复制
require(msg.value==listingPrice,"Price must be equal to listing fee");

您可以编写一个函数来更改将来的值。

代码语言:javascript
复制
function setListingPrice(uint newPrice) external onlyOwner{
    require(newPrice>0,"Price must be at least 1 wei");
    listingPrice=newPrice;
  }
票数 0
EN

Stack Overflow用户

发布于 2022-02-17 13:04:39

在我看来,你应该把它写在智能合同上。因为我认为如果每个人都能看到现在的费用是多少(透明度),并避免人为错误从代码中设置费用,那就太好了。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71089222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档