[英]Paying with ERC20 token
I want to build an NFT that I can paid with an ERC-20 token to mint it.我想建立一个 NFT,我可以用 ERC-20 代币支付来铸造它。 I'm using currently the Mumbai testnet on polygon, and I'm using the Dummy ERC20 token to test it out.
我目前正在多边形上使用孟买测试网,并且正在使用Dummy ERC20 令牌对其进行测试。
This is currently my constructor:这是目前我的构造函数:
ERC20 token;
constructor() ERC721("Token", "TKN") {
token = ERC20(0xfe4F5145f6e09952a5ba9e956ED0C25e3Fa4c7F1);
}
And this is my mint function:这是我的薄荷功能:
function mint() public returns (uint256) {
uint256 tokenId = _tokenIds.current();
require(tokenId <= MAX_TOKEN_ID);
token.approve(address(this), NFT_PRICE);
token.transfer(address(this), NFT_PRICE);
_mint(msg.sender, tokenId);
_setTokenURI(tokenId, TOKEN_URI);
_tokenIds.increment();
return tokenId;
}
If I remove those 2 lines the code works fine, it mints the NFT:如果我删除这 2 行代码可以正常工作,它会生成 NFT:
token.approve(address(this), NFT_PRICE);
token.transfer(address(this), NFT_PRICE);
But as soon as I add it, the code breaks, it gives me the following gas estimation error:但是一旦我添加它,代码就会中断,它会给我以下气体估计错误:
The transaction execution will likely fail. Do you want to force sending?
Internal JSON-RPC error. { "code": 3, "message": "execution reverted: ERC20: transfer amount exceeds balance", "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002645524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e63650000000000000000000000000000000000000000000000000000" }
As a troubleshooting step I've also added this inside my mint function to make sure I'm calling from my own wallet:作为故障排除步骤,我还在我的 mint 函数中添加了这个,以确保我是从自己的钱包中调用的:
sender = msg.sender;
And created this function:并创建了这个函数:
function tokenBalance(address addr) public view returns (uint256) {
return token.balanceOf(addr);
}
And if I grab the token balance of the sender address it gives me the value:如果我获取发件人地址的代币余额,它会给我以下值:
0: uint256: 2000000000000000000
It's because the logic is wrong.因为逻辑不对。 That
approve
function you call inside your mint
function is useless: the spender needs to call the approve
function from the the contract of your dummy ERC20.您在
mint
函数中调用的approve
函数是无用的:支出者需要从您的虚拟 ERC20 的合约中调用approve
函数。
And then you can call the transferFrom(msg.sender, address(this), NFT_PRICE)
from your mint function.然后你可以从你的 mint 函数中调用
transferFrom(msg.sender, address(this), NFT_PRICE)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.