Solidity

如何從 NFT 合約中獲取代幣 ID

  • January 12, 2022

我在 openzeppelin 嚮導的幫助下創建了一個 NFT。我得到了關於 mintable 方法的以下回复。

token.safeMint('0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266', 'http://wplugins.localhost/contract.json')
{
 tx: '0xb493554e41ccb3f6af221bf563353114d452b0086f925c9ad1b91bcb6f163706',
 receipt: {
   transactionHash: '0xb493554e41ccb3f6af221bf563353114d452b0086f925c9ad1b91bcb6f163706',
   transactionIndex: 0,
   blockHash: '0xa2ccf6978b7b433bba7d57d981dea8a3d7efcaa341ffb4c30bb35540932ee17c',
   blockNumber: 95,
   from: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
   to: '0x49fd2be640db2910c2fab69bb8531ab6e76127ff',
   cumulativeGasUsed: 161855,
   gasUsed: 161855,
   contractAddress: null,
   logs: [ [Object] ],
   logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000008000000000000000000000000000000000000000000000000020000000000000100000800000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000200000000000000000000000002000000000000000000020002000000000000000000000000000000040000000000000000000000000000000',
   type: '0x2',
   status: true,
   effectiveGasPrice: '0x9503070d',
   rawLogs: [ [Object] ]
 },
 logs: [
   {
     removed: false,
     logIndex: 0,
     transactionIndex: 0,
     transactionHash: '0xb493554e41ccb3f6af221bf563353114d452b0086f925c9ad1b91bcb6f163706',
     blockHash: '0xa2ccf6978b7b433bba7d57d981dea8a3d7efcaa341ffb4c30bb35540932ee17c',
     blockNumber: 95,
     address: '0x49fd2BE640DB2910c2fAb69bB8531Ab6E76127ff',
     id: 'log_ff516e78',
     event: 'Transfer',
     args: [Result]
   }
 ]
}

我不確定如何從此響應中獲取 tokenId。我將它執行到松露控制台。我已經嘗試了與此 stackexchange 不同的解決方案,但正如您所見,我沒有收到topics[]日誌下的 arg。我正在使用 Solidity 0.8.2 版本。

契約:

//SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract Token is ERC721, ERC721URIStorage, Ownable {
   using Counters for Counters.Counter;

   Counters.Counter private _tokenIdCounter;

   constructor() ERC721("My Test Token", "MTT") {}

   function safeMint(address _to, string memory _tokenURI) 
       public 
       onlyOwner 
   {
       uint256 newItemId = _tokenIdCounter.current();
       _tokenIdCounter.increment();
       _safeMint(_to, newItemId);
       _setTokenURI(newItemId, _tokenURI);
   }

   function _burn(uint256 _tokenId) 
       internal 
       override(ERC721, ERC721URIStorage) 
   {
       super._burn(_tokenId);
   }

   function tokenURI(uint256 tokenId)
       public
       view
       override(ERC721, ERC721URIStorage)
       returns (string memory)
   {
       return super.tokenURI(tokenId);
   }
}

好的,我已經解決了,我希望它對像我這樣的其他新手也有幫助。tokenId存在於rawLogs數據中**。**

tx.then(t => t.receipt.rawLogs)
[
 {
   removed: false,
   logIndex: 0,
   transactionIndex: 0,
   transactionHash: '0x494f65b3c78455501d5a8950f77c0b8031e7591cebedc9afa5496647a9b6ce2b',
   blockHash: '0xafec3731344cf08d04eb621fc3d1c44e3539530698af630ed34fbbb7839695a2',
   blockNumber: 96,
   address: '0x49fd2BE640DB2910c2fAb69bB8531Ab6E76127ff',
   data: '0x',
   topics: [
     '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
     '0x0000000000000000000000000000000000000000000000000000000000000000',
     '0x000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266',
     '0x0000000000000000000000000000000000000000000000000000000000000001'
   ],
   id: 'log_d73f58e2'
 }
]

所以獲取我使用過的 web3 實用程序的 tokenId。

web3.utils.hexToNumber(t.receipt.rawLogs[0].topics[3])

注意:我使用了 truffle 控制台來完成這一切。

引用自:https://ethereum.stackexchange.com/questions/118673