簡體   English   中英

如何使用版稅調用 Openzeppelin safeTransferFrom() function 以避免錯誤,ERC721:所有者查詢不存在的令牌?

[英]How to call Openzeppelin safeTransferFrom() function with Royalty to avoid error, ERC721: owner query for nonexistent token?

我正在使用 Openzeppelin NFT 標准和從 Tatum 復制的代碼創建智能合約,其中safeTransferFrom function 看起來像這樣,

function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata dataBytes
    ) public payable virtual override {
        uint256 index;
        uint256 value; // price 1000 matic
        uint256 percentSum;
        IERC20 token;
        (index, value) = _bytesCheck(dataBytes);

        // return error if token id is odd
        if (tokenId % 2 == 1) {
            
            //if block time is earlier than 2023 march 1 timestamp
            if (block.timestamp < 1677628800) {
                revert("You have to wait until 2023 March 1 to mint this token");
            }
          
        }

        if (_customToken[tokenId] != address(0)) {
            token = IERC20(_customToken[tokenId]);
        }
        if (_cashbackRecipients[tokenId].length > 0) {
            for (uint256 i = 0; i < _cashbackValues[tokenId].length; i++) {
                uint256 iPercent = (_cashbackValues[tokenId][i] * value) /
                    10000;
                if (iPercent >= _fixedValues[tokenId][i]) {
                    percentSum += iPercent;
                } else {
                    percentSum += _fixedValues[tokenId][i];
                }
            }
            if (_customToken[tokenId] == address(0)) {
                if (percentSum > msg.value) {
                    payable(from).transfer(msg.value);
                    revert(
                        "Value should be greater than or equal to cashback value"
                    );
                }
            } else {
                if (percentSum > token.allowance(to, address(this))) {
                    revert(
                        "Insufficient ERC20 allowance balance for paying for the asset."
                    );
                }
            }
            for (uint256 i = 0; i < _cashbackRecipients[tokenId].length; i++) {
                // transferring cashback to authors
                uint256 cbvalue = (_cashbackValues[tokenId][i] * value) / 10000;
                if (_customToken[tokenId] == address(0)) {
                    cbvalue = _cashbackCalculator(
                        cbvalue,
                        _fixedValues[tokenId][i]
                    );
                    payable(_cashbackRecipients[tokenId][i]).transfer(cbvalue);
                } else {
                    cbvalue = _cashbackCalculator(
                        cbvalue,
                        _fixedValues[tokenId][i]
                    );

                    token.transferFrom(
                        to,
                        _cashbackRecipients[tokenId][i],
                        cbvalue
                    );
                }
            }
            if (_customToken[tokenId] != address(0) && msg.value > 0) {
                payable(from).transfer(msg.value);
            }
            if (_customToken[tokenId] == address(0) && msg.value > percentSum) {
                payable(from).transfer(msg.value - percentSum);
            }
        }
        _safeTransfer(from, to, tokenId, dataBytes);
        string calldata dataString = string(dataBytes);
        _appendTokenData(tokenId, dataString);
        emit TransferWithProvenance(tokenId, to, dataString[:index], value);
    }

它使用 ERC721 標准支付版稅。 我這樣稱呼這個 function,

import { ethers } from "hardhat";

const contractAddressRoyalty = "0x1903344651b356ce3b755458008c0fe74f8cc1c9";

const trans = async () => {

  const CannesRoyalty = await ethers.getContractAt(
    "Cant",
    contractAddressRoyalty
  );

  const name = await CannesRoyalty.symbol();

  //   // transfer
  const safeTransferFrom = await CantRoyalty[
    "safeTransferFrom(address,address,uint256)"
  ](
    "0x888a7E4DAE1d9009694dEdf240F976EC498D7D90",
    "0x7542A9d09589B21b5a671e14254318D2016A0A0c",
    4,
    {
      from: "0x888a7E4DAE1d9009694dEdf240F976EC498D7D90",
      gasLimit: 2000000,
      gasPrice: ethers.utils.parseUnits("10", "gwei"),
      value: ethers.utils.parseEther("0"),
    }
  );
  console.log(safeTransferFrom);
  console.log(name);
};

trans();

我收到此錯誤, https://mumbai.polygonscan.com/tx/0xdefbb122ab45bb85c29ffda0a14b61b77564f2748ece65f87067e24b4624cfd5

消息 -失敗並出現錯誤“ERC721:所有者查詢不存在的令牌”

但是令牌是鑄造的。 我也同意了。 我不知道我是否弄亂了輸入值。 我試過 tokenId、4 和“4”。 但似乎沒有運氣。

您嘗試轉移的令牌似乎“不存在”。 這通常意味着它還沒有被鑄造。 當您使用 tokenId 4 調用公共 function tokenUri 時,您能否驗證 tokenId 4 是否返回任何內容?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM