簡體   English   中英

使用 Remix IDE 在 Solidity 中購買代幣交易時出現“資金不足”

[英]"Not Enough Fund" when doing buying token transaction in Solidity with Remix IDE

我對這個區塊鏈世界完全陌生,所以我想嘗試學習它。 當我按照教程創建一個簡單的 ERC-1155 合約來鑄造代幣並設定其價格時。 當我嘗試從另一個地址運行 buyToken function 時,它一直說“資金不足”。 當我調試時,msg.value 值為 0,即使我的每個地址上都有 100 以太幣余額。 當我將令牌數量更改為 0 時,它會成功運行。 我需要做什么才能進行交易?

日志

這是我修改過的合同:

// Contract based on https://docs.openzeppelin.com/contracts/4.x/erc1155
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";

/**
 * @title NFTMinter
 * @dev NFT Contract Minter
 * @custom:dev-run-script ./scripts/deploy_with_ethers.ts
 */
contract NFTMinter is ERC1155 {
  constructor() ERC1155("https://raw.githubusercontent.com/noopmood/TutorialNFTInGo/main/metadata/{id}.json") payable {}
  
  // Define the mapping of addresses to balances
  mapping(address => mapping(uint256 => uint256)) public _balances;

  // Define the mapping of address to tokenIds owned
  mapping(address => uint256[]) public _tokenIds;

  // Define the mapping of tokenId to price
  mapping(uint256 => uint256) public tokenPrice;

  // Define the sender to address payable type
  address payable public sender;

  struct Token {
    uint256 tokenId;
    uint256 balance;
  }

  function mintCaller(uint256 tokenId, uint256 amount) public {
    _mint(msg.sender, tokenId, amount, "");
  }

  // Mints new tokens and sets the price for each token.
  function mintAddress(uint256 tokenId, uint256 amount, address addr, uint256 price) public{
    _mint(addr, tokenId, amount, "");
    // Update the balance of the recipient
    _balances[addr][tokenId] += amount;
     // Add the tokenId to the address
    _tokenIds[addr].push(tokenId);
    // Set the price of the token
    tokenPrice[tokenId] = price;
  }

  // Get all tokenIds from its owner address
  function getTokenIdsByAddress(address addr) public view returns (uint[] memory) {
    return _tokenIds[addr];
  }

  // Get the balance / amount of the tokenId
  function getTokenByIdAndAddress(address addr, uint256 tokenId) public view returns (Token memory) {
    Token memory result;
    result.tokenId = tokenId;
    result.balance = _balances[addr][tokenId];
    return result;
  }

  // Get the tokenIds along with its corresponding balances/amount
  function getAllTokenByAddress(address holder) public view returns (Token[] memory) {
    Token[] memory result = new Token[](_tokenIds[holder].length);
    for (uint i = 0; i < _tokenIds[holder].length; i++) {
      result[i].tokenId = _tokenIds[holder][i];
      result[i].balance = _balances[holder][_tokenIds[holder][i]];
    }
    return result;
  }

  // Transfers the tokens from one address to another.
  function transfer(address addr, uint256 tokenId, uint256 amount) public {
    require(_balances[msg.sender][tokenId] >= amount, "Not enough balance");
    // Transfer the token
    _safeTransferFrom(msg.sender, addr, tokenId, amount, "");
    // Update the sender's balance
    _balances[msg.sender][tokenId] -= amount;
    // Update the recipient's balance
    _balances[addr][tokenId] += amount;
  }

  // Allows a buyer to purchase a token by sending the required amount to the contract and updating the balance of the buyer.
  function buyToken(uint256 tokenId, uint256 amount) public payable {
    require(msg.value >= amount * tokenPrice[tokenId], "Not enough funds");
    // Deduct the amount from the buyer
    sender = payable(msg.sender);
    sender.transfer(amount * tokenPrice[tokenId]);
    // Transfer the token to the buyer
    _safeTransferFrom(address(0), msg.sender, tokenId, amount, "");
    // Update the buyer's balance
    _balances[msg.sender][tokenId] += amount;
  }
}

關於重現此步驟的步驟:

  • 部署有價值的合同,對我來說,我通過了 20 Ether
  • 通過傳遞運行 mintAddress function,tokenId:1,數量:10,價格:10,地址:帳戶地址
  • 通過傳遞地址運行轉賬 function:您要將代幣轉入的帳戶地址,tokenId:1,金額:10
  • 把上半部分的賬戶改成你轉賬的地址function
  • 運行buyToken function,tokenId:1,數量:10
  • 它會說沒有足夠的資金

購買代幣

到目前為止我嘗試了什么:

  • 我在另一篇文章中讀到,我應該部署包含其中價值的合同。 但是還是不行
  • 我已經檢查過我在 remix 中的賬戶余額有 100 個以太幣,但是當我嘗試用它購買代幣時,它仍然說“資金不足”

我期待的是:能夠以鑄造代幣時設定的價格購買代幣。

你的錯誤是從這里失敗的:

// require is like if statement. if condition does not satisfied, it shows the passed message
require(msg.value >= amount * tokenPrice[tokenId], "Not enough funds")

當用戶在交易的同時發送資金時,而不是作為 function 參數,solidity 將使用msg.value捕獲它。 看起來您沒有在交易的同時發送任何價值或沒有發送足夠的資金。 在混音中 IDE

在此處輸入圖像描述

您需要在上面的紅色邊框框中傳遞必要的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM