简体   繁体   English

我想铸造并转移 1 个 erc20(custom) 到铸造者本身,只是为了跟踪 erc20 交易

[英]I want to mint and transfer 1 erc20(custom) to the minter itself, just to track erc20 transaction

This is the code used, I am using polygon tes.net for testing, the approve function is working fine but transferFrom is not working(error: -32000)这是使用的代码,我正在使用多边形 tes.net 进行测试,approve function 工作正常但 transferFrom 不工作(错误:-32000)

// SPDX-License-Identifier: MIT
pragma solidity 0.8.0;

import "@0xcert/ethereum-erc721/src/contracts/tokens/nf-token-metadata.sol";
import "@0xcert/ethereum-erc721/src/contracts/ownership/ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract newNFT is NFTokenMetadata, Ownable {
    ERC20 KOOLToken;

    constructor() {
        KOOLToken=ERC20(0xxxxxxxxxxxxxxxxxxxxxxxxxxx);
        nftName = "Test NFT 123";
        nftSymbol = "TNFT321";
    }

    function approve() public onlyOwner {
      KOOLToken.approve(address(msg.sender), 1);
    }

function transferFrom() public onlyOwner{
  KOOLToken.transferFrom(msg.sender,msg.sender, 1);
}

function mint(address _to, uint256 _tokenId, string calldata _uri) public onlyOwner {
  super._mint(_to, _tokenId);
  super._setTokenUri(_tokenId, _uri);
}
}

Your -32000 error is caused by a revert in the ERC20 contract, because msg.sender does't have enough allowance to spend.您的-32000错误是由 ERC20 合同中的还原引起的,因为 msg.sender 没有足够的津贴来花费。

According to EIP20 , transferFrom provides:根据EIP20transferFrom提供:

The transferFrom method is used for a withdraw workflow, allowing contracts to transfer tokens on your behalf. transferFrom 方法用于提取工作流程,允许合约代表您转移代币。 This can be used for example to allow a contract to transfer tokens on your behalf and/or to charge fees in sub-currencies.例如,这可以用于允许合同代表您转移代币和/或以子货币收取费用。 The function SHOULD throw unless the _from account has deliberately authorized the sender of the message via some mechanism. function 应该抛出,除非 _from 帐户通过某种机制故意授权消息的发件人。

Which means,even though the msg.sender is the owner here, it is not a valid source address for transferFrom unless you call approve on it before.这意味着,即使msg.sender是此处的所有者,它也不是transferFrom的有效源地址,除非您之前对其调用approve

You can approve the msg.sender , or simply use transfer function.您可以批准msg.sender ,或者简单地使用transfer function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM