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

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

这是使用的代码,我正在使用多边形测试网进行测试,approve 功能正常但 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);
}
}

您的 -32000 错误是由 ERC20 合约中的还原引起的,因为 msg.sender 没有足够的津贴可以花费。

根据EIP20transferFrom规定:

The transferFrom method is used for a withdraw workflow, allowing contracts to transfer tokens on your behalf. 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.

这意味着,即使 msg.sender 是此处的所有者,它也不是 transferFrom 的有效源地址,除非您之前对其调用 approve

您可以批准msg.sender,或者直接使用transfer功能。