【问题标题】:ERC20: transfer amount exceeds allowance Smart contract with brownie and NFTERC20:转账金额超过限额 带有巧克力蛋糕和 NFT 的智能合约
【发布时间】:2021-12-28 19:48:36
【问题描述】:

您好,我有两个智能合约,一个是 ERC1155 合约,它从 Moralis IPFS 服务器铸造一个 NFT,另一个是 ERC20 代币。我希望用户能够使用 ERC20 令牌支付铸造的 NFT,但我在 transferfrom() 函数上收到错误消息:brownie.exceptions.VirtualMachineError:revert:ERC20:转账金额超过限额。我做了一些研究,但到目前为止没有任何帮助。

这是我的 ERC1155 合约


import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
// import "@openzeppelin/contracts/utils/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBase.sol";

contract NFTPomoBots is ERC1155, Ownable {
    IERC20 private _roboToken;

    constructor(IERC20 roboToken)
        ERC1155(
            "ipfs://QmcPjTnt33BRM5TPGyno7restoftheurl/({id}.json"
        )
    {
        _roboToken = roboToken;
    }

    function mintPomoBot(
        address account,
        uint256 id,
        uint256 amount
    ) public {
        require(_roboToken.transferFrom(msg.sender, address(this), 1));
        _mint(account, id, amount, "");
    }
}

这是 ERC20 合约

// SPDX-License-Identifier: MIT

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";

pragma solidity ^0.8.0;

contract RoboToken is ERC20, Ownable {
    uint256 public maxSupply = 100000 * 10**18;

    constructor() ERC20("Pomobots Token", "POMO") {
        _mint(msg.sender, maxSupply);
    }

    function transferToAccount(
        address to,
        uint256 amount,
        bool completedTime
    ) public onlyOwner {
        require(completedTime == true);
        _mint(to, amount);
    }

    function approveNFTContract(address _tokenAddress) public onlyOwner {
        approve(_tokenAddress, maxSupply);
    }
}

这是测试脚本

def test_can_mint_new_nft():

    # Arrange
    account1 = get_account()
    print(f"account 1 {account1.address}")
    account2 = get_account(1)
    print(f"account 2 {account2.address}")

    robo_token = deploy_erc20_token()
    pomobot_token = NFTPomoBots.deploy(robo_token, {"from": account1})
    # Act
    tx1 = robo_token.transferToAccount(account2, 10, True)
    tx1.wait(1)

    print(robo_token.balanceOf(account2.address))
    tx2 = robo_token.approve(account2.address, robo_token.balanceOf(account2.address))
    tx2.wait(1)

    # assert
    assert pomobot_token.mintPomoBot(account2.address, 20, 1, {"from": account2})
    print(f"nft minted to {account2.address}")
    print(robo_token.balanceOf(account2.address))

感谢您的帮助,我是否应该让函数 minPomobot() 支付?

【问题讨论】:

    标签: blockchain ethereum solidity smartcontracts brownie


    【解决方案1】:

    在调用mintPomoBot函数之前,使用同一个账户(msg.sender)你需要在RoboToken智能合约上调用approve(spender, amount)函数,其中spender = NFTPomoBots' addressamount = 1

    【讨论】:

    • 你拯救了我的一天,非常感谢
    猜你喜欢
    • 2022-01-08
    • 2021-11-07
    • 2022-12-05
    • 1970-01-01
    • 2021-11-30
    • 2021-04-26
    • 1970-01-01
    • 2022-08-12
    • 2022-01-20
    相关资源
    最近更新 更多