'How to get ERC-721 tokenID?
I have created a ERC-721 contract deployed on ropston network. Using contract I'm creating NFT's and its totally working fine.
Now for the transfer part I need to get tokenID of any NFT and transfer to to other address but I'm not able get the tokenID whenever I fetch transaction details from etherscan or using web3.
I want to store the tokenID in DB so it can be utilized while transferring to other address.
I have encircled the exact tokenID required in above image.
Im using following code :
window.ethereum
.request({
method: 'eth_sendTransaction',
params: [
{
from: fromAddress,
to: contractAddress,
gas: '50000',
data: nftContract.methods.transferFrom(fromAddress, toAddress, tokenNumber).encodeABI()
},
],
})
I just want to get tokenID when NFT was created and store into DB for reference and perform business logic.
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;
}
Above is the solidity function responsible for creating the NFT.
Solution 1:[1]
Your mintNFT() function doesn't emit any event containing the newItemId.
solidity is using the standard definition of transfer
There's no "standard definition", the ERC-721 standard only defines an interface and few other rules - and the actual implementation (of the interface) is on each developer. However I'm assuming that by the "standard definition" you mean the OpenZeppelin implementation, which is a widely used implementation of the ERC-721 standard and is used by many people who start coding in Solidity.
You can see in the linked implementation, that the OZ _mint() function emits the Transfer() event, where the 3rd argument is the minted token ID.
So executing your mintNFT() function effectively emits the Transfer() event that contains the newly minted token ID as a value of the 3rd parameter.
After you've executed the mintNFT() contract function from your JS code, it returns a PromiEvent object, that you can use to catch its receipt event.
The receipt contains the emited logs, where you can find the Transfer() log as well.
const tx = nftContract.methods.mintNFT(...).send({from: ...});
tx.on('receipt', function(receipt){
console.log(logs[0].topics[3]); // this prints the hex value of the tokenId
// you can use `web3.utils.hexToNumber()` to convert it to decimal
});
If you want to get the token ID from an already existing transaction (using the tx hash), you can use this snippet:
web3.eth.getTransactionReceipt('0x258a6d35445814d091ae67ec01cf60f87a4a58fa5ac1de25d0746edf8472f189').then(function(data){
let transaction = data;
let logs = data.logs;
console.log(logs);
console.log(web3.utils.hexToNumber(logs[0].topics[3]));
});
You can find more details in the web3 docs for the send() method and the receipt.
Solution 2:[2]
You can try:
const receipt = await web3.eth.getTransactionReceipt(hash)
const tokenId = Web3.utils.hexToNumber(receipt.logs[0].topics[3])
I check hash from ropsten testnet: https://ropsten.etherscan.io/tx/0x59928012c3e0605b9346215c24654e84be29f2bf47949a2284aecf9991996a28
and output is 11
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | P. Huy |

