0

I have created an NFT with the help of openzeppelin wizard. I got the following response on mintable method.

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]
    }
  ]
}

I am not sure how to get the tokenId from this response. I am running it to truffle console. I have tried different solutions from this stackexchange but as you see that i haven't received the topics[] arg under the log. I am using Solidity 0.8.2 version.

Contract:

//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);
    }
}

1 Answer 1

1

Okay, i have solved it and i hope it will help to other newbies too like me. The tokenId exists in the rawLogs data.

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'
  }
]

So get the tokenId i have used the web3 utility.

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

Note: I used the truffle console for all that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.