1

我创建了一个智能合约,铸造了一个 nft,现在尝试转移它。我的问题是交易完成得很好——我可以在 etherscan 等中看到它,但 nft 没有被转移。可能的根本原因是什么?

async function transferNFT() {
    const nonce = await web3.eth.getTransactionCount(PUBLIC_KEY, 'latest'); //get latest nonce

  //the transaction
    const tx = {
      'from': sender,
      'to': receiver,
      'nonce': nonce,
      'gas': 500000,
      'data': myContract.methods.transferFrom(sender,receiver, 1).encodeABI()
    }

    const signPromise = web3.eth.accounts.signTransaction(tx, PRIVATE_KEY)
    signPromise
    .then((signedTx) => {
        web3.eth.sendSignedTransaction(
        signedTx.rawTransaction,
        function (err, hash) {
            if (!err) {
            console.log(
                "The hash of your transaction is: ",
                hash,
                "\nCheck Alchemy's Mempool to view the status of your transaction!"
            )
            } else {
            console.log(
                "Something went wrong when submitting your transaction:",
                err
            )
            }
        }
        )
    })
    .catch((err) => {
        console.log(" Promise failed:", err)
    })

}
transferNFT()

还有合同。我实际上并没有调用传输函数,因为我期望使用 openzeppelin transferFrom 函数。但是如果我使用合同中的转移函数 - 结果是一样的:

  • 交易被执行

  • NFT 不会被转移。

     contract MyNFTv2 is ERC721URIStorage, Ownable {
         using Counters for Counters.Counter;
         Counters.Counter private _tokenIds;
         event NftBought(address _seller, address _buyer, uint256 _price);
         event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
    
     mapping (uint256 => uint256) public tokenIdToPrice;
     mapping(uint256 => address) internal idToOwner;
    
    
     constructor() public ERC721("MyNFTv2", "NFT") {}
    
     function mintNFT(address recipient, string memory tokenURI)
         public onlyOwner
         returns (uint256)
     {
         _tokenIds.increment();
    
         uint256 newItemId = _tokenIds.current();
         _mint(recipient, newItemId);
         _setTokenURI(newItemId, tokenURI);
    
         return newItemId;
     }
    
     function allowBuy(uint256 _tokenId, uint256 _price) external {
         require(msg.sender == ownerOf(_tokenId), 'Not owner of this token');
         require(_price > 0, 'Price zero');
         tokenIdToPrice[_tokenId] = _price;
     }
    
     function disallowBuy(uint256 _tokenId) external {
         require(msg.sender == ownerOf(_tokenId), 'Not owner of this token');
         tokenIdToPrice[_tokenId] = 0;
     }
    
     function buy(uint256 _tokenId) external payable {
         uint256 price = tokenIdToPrice[_tokenId];
         require(price > 0, 'This token is not for sale');
         require(msg.value == price, 'Incorrect value');
    
         address seller = ownerOf(_tokenId);
         _transfer(seller, msg.sender, _tokenId);
         tokenIdToPrice[_tokenId] = 0; // not for sale anymore
         payable(seller).transfer(msg.value); // send the ETH to the seller
    
         emit NftBought(seller, msg.sender, msg.value);
     }
    
     function transfer(address _to, uint256 _tokenId) public {
         require(msg.sender == idToOwner[_tokenId]);
         idToOwner[_tokenId] = _to;
         emit Transfer(msg.sender, _to, _tokenId);
     }
    

    }

4

1 回答 1

1
const tx = {
    'from': sender,
    'to': receiver,

您将交易发送给代币接收者,这是一个不包含任何智能合约的常规地址。

因此,当您将data字段传递给他们时,没有处理它的合同,交易只是通过忽略该data字段。

解决方案:to交易的字段设置为 NFT 集合合约。然后它将能够处理在字段中tranferFrom()传递的函数及其参数。data请注意,receiver已经在函数的第二个参数中传递了。

于 2021-10-15T20:19:38.917 回答