[英]ERC20: transfer amount exceeds allowance Smart contract with brownie and NFT
您好,我有兩個智能合約,一個是 ERC1155 合約,它從 Moralis IPFS 服務器鑄造一個 NFT,另一個是 ERC20 代幣。 我希望用戶能夠使用 ERC20 代幣為鑄造的 NFT 付款,但我在transferfrom()
function: brownie.exceptions.VirtualMachineError: revert: ERC20: transfer amount exceeded permit 上收到錯誤。 我做了一些研究,但到目前為止沒有任何幫助。
這是我的 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))
感謝您提供任何幫助,我還應該支付 function minPomobot() 嗎?
在調用mintPomoBot
function 之前,使用相同的帳戶( msg.sender
)您需要在RoboToken
智能合約上調用approve(spender, amount)
function ,其中spender = NFTPomoBots' address
和amount = 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.