【问题标题】:How can I sell to my own nft market with my own crypto token如何使用自己的加密代币向自己的 nft 市场出售
【发布时间】:2022-11-12 00:02:03
【问题描述】:

我的目标是在我的市场上出售可以用我自己的加密货币购买的 nfts。为此,我使用市场合约中的 IERC20 接口访问我自己的代币。但我并不擅长坚固。如果我来到平原。当我在我的网站上单击购买按钮并付款时,如何获得使用用户钱包中的 BVC 代币(我的代币)购物的许可。

此 MARKET.sol 代码

// SPDX-License-Identifier: MIT LICENSE


pragma solidity ^0.8.9;

import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract Market is ReentrancyGuard, Ownable {
  struct TokenInfo{
    IERC20 paytoken;
    uint256 listingFee;
    uint256 mintingFee;
    uint256 price;
  }

  using Counters for Counters.Counter;
  Counters.Counter private _itemIds;
  Counters.Counter private _itemsSold;
  IERC20 public paytoken;


  TokenInfo[] public AllowedCrypto;

  address payable holder;
  //uint256 listingFee = 0.0025 ether;
  //uint256 mintingFee = 0.0075 ether;

  constructor() {
    holder = payable(msg.sender);
  }

  

  struct VaultItem {
    uint itemId;
    address nftContract;
    uint256 tokenId;
    address payable seller;
    address payable holder;
    uint256 price;
    bool sold;
  }

  mapping(uint256 => VaultItem) private idToVaultItem;

  event VaultItemCreated (
    uint indexed itemId,
    address indexed nftContract,
    uint256 indexed tokenId,
    address seller,
    address holder,
    uint256 price,
    bool sold
  );

  function AddCurrency (IERC20 _paytoken,uint256 _listingFee,uint256 _mintingFee,uint256 _price) public onlyOwner {
        AllowedCrypto.push(TokenInfo({
            paytoken:_paytoken,
            listingFee:_listingFee,
            mintingFee: _mintingFee,
            price:_price
        }));
    }
  
  function approveCRI (uint256 _pid) public payable nonReentrant{
    TokenInfo storage tokens = AllowedCrypto[_pid];
    paytoken = tokens.paytoken;
    uint256 amount = 200000000000000000000000000;
    paytoken.approve(msg.sender, amount);
  }

  function allowenceCRI (uint256 _pid) public  payable nonReentrant{
    TokenInfo storage tokens = AllowedCrypto[_pid];
    paytoken = tokens.paytoken;
    paytoken.allowance(address(this), msg.sender);
  }

  function getListingFee(uint256 _pid) public view returns (uint256) {
    TokenInfo storage tokens = AllowedCrypto[_pid];
    uint256 listingFee;
    listingFee = tokens.listingFee;
    return listingFee;
  }
  
  function createVaultItem(address nftContract,uint256 tokenId,uint256 _pid) public payable nonReentrant {
    TokenInfo storage tokens = AllowedCrypto[_pid];
    paytoken = tokens.paytoken;
    uint256 listingFee;
    listingFee = tokens.listingFee;
    uint256 price;
    price = tokens.price;
    require(price > 0, "Price cannot be zero");
    require(msg.value == listingFee, "Price cannot be listing fee");
    _itemIds.increment();
    uint256 itemId = _itemIds.current();
    idToVaultItem[itemId] =  VaultItem(itemId,nftContract,tokenId,payable(msg.sender),payable(address(0)),price,false);
    IERC721(nftContract).transferFrom(msg.sender, address(this), tokenId);
    emit VaultItemCreated(itemId,nftContract,tokenId,msg.sender,address(0),price,false);
    }

  function n2DMarketSale(address nftContract,uint256 itemId,uint256 _pid) public payable nonReentrant {
    TokenInfo storage tokens = AllowedCrypto[_pid];
    paytoken = tokens.paytoken;
    uint256 listingFee;
    listingFee = tokens.listingFee;
    uint price = tokens.price; //idToVaultItem[itemId].price;
    uint tokenId = idToVaultItem[itemId].tokenId;
    paytoken.approve(msg.sender, price);
    require(msg.value == paytoken.balanceOf(address(this)), "Not enough balance to complete transaction");
    idToVaultItem[itemId].seller.transfer(msg.value);
    IERC721(nftContract).transferFrom(address(this), msg.sender, tokenId);
    idToVaultItem[itemId].holder = payable(msg.sender);
    idToVaultItem[itemId].sold = true;
    _itemsSold.increment();
    payable(holder).transfer(listingFee);
  }

  function getAvailableNft() public view returns (VaultItem[] memory) {
    uint itemCount = _itemIds.current();
    uint unsoldItemCount = _itemIds.current() - _itemsSold.current();
    uint currentIndex = 0;

    VaultItem[] memory items = new VaultItem[](unsoldItemCount);
    for (uint i = 0; i < itemCount; i++) {
      if (idToVaultItem[i + 1].holder == address(0)) {
        uint currentId = i + 1;
        VaultItem storage currentItem = idToVaultItem[currentId];
        items[currentIndex] = currentItem;
        currentIndex += 1;
      }
    }
    return items;
  }

  function getMyNft() public view returns (VaultItem[] memory) {
    uint totalItemCount = _itemIds.current();
    uint itemCount = 0;
    uint currentIndex = 0;

    for (uint i = 0; i < totalItemCount; i++) {
      if (idToVaultItem[i + 1].holder == msg.sender) {
        itemCount += 1;
      }
    }

    VaultItem[] memory items = new VaultItem[](itemCount);
    for (uint i = 0; i < totalItemCount; i++) {
      if (idToVaultItem[i + 1].holder == msg.sender) {
        uint currentId = i + 1;
        VaultItem storage currentItem = idToVaultItem[currentId];
        items[currentIndex] = currentItem;
        currentIndex += 1;
      }
    }
    return items;
  }

  function getMyMarketNfts() public view returns (VaultItem[] memory) {
    uint totalItemCount = _itemIds.current();
    uint itemCount = 0;
    uint currentIndex = 0;

    for (uint i = 0; i < totalItemCount; i++) {
      if (idToVaultItem[i + 1].seller == msg.sender) {
        itemCount += 1;
      }
    }

    VaultItem[] memory items = new VaultItem[](itemCount);
    for (uint i = 0; i < totalItemCount; i++) {
      if (idToVaultItem[i + 1].seller == msg.sender) {
        uint currentId = i + 1;
        VaultItem storage currentItem = idToVaultItem[currentId];
        items[currentIndex] = currentItem;
        currentIndex += 1;
      }
    }
    return items;
  }

  function withdraw(uint256 _pid) public payable onlyOwner() {
    TokenInfo storage tokens = AllowedCrypto[_pid];
    paytoken = tokens.paytoken;
    require(msg.sender.balance == paytoken.balanceOf(address(this)));
    paytoken.transfer(msg.sender, paytoken.balanceOf(address(this)));
    }
}

【问题讨论】:

  • 请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。

标签: ethereum solidity nft ethers.js erc20


【解决方案1】:

必须调用批准直接地来自代币合约,因为批准是通过 msg.sender 执行的(用户)。

批准用于给予允许地址使用最多X来自另一个地址的令牌,所以批准应该作为输入市场合约地址最高金额将花费的代币。

通过 UIX,第一的通过调用approve函数让用户批准(地址市场,金额)直接来自代币合约,然后您将启用调用需要令牌的函数的按钮

【讨论】:

  • 感谢您提供有用的信息。在这种情况下,我的代币合约已经完好无损。我可以在合约的写选项卡下从 etherscan 调用审批功能吗?所以我想问的是,我可以请求批准其他已经发布的erc20代币,例如usdt吗?
  • 是的,当然可以,approve 函数是一个没有任何限制的公共函数,所以你可以为你想要的每个令牌调用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2022-08-17
  • 2022-11-28
  • 2013-01-28
  • 1970-01-01
  • 2011-05-24
  • 1970-01-01
相关资源
最近更新 更多