2

I want to be able to find the TokenID of a Crypto Kitty being sold.

The transaction returned from by the etherscan API is such:

{
  blockNumber: '11711690',
  timeStamp: '1611403229',
  hash: '0x8f8d4e163e90c8db91672f443b0896f157d45fc32c3ccf74d131137eedbdd896',
  nonce: '4',
  blockHash: '0xbdd67604d6cc7844f151a38360753ad96fa234bd9cf199c6cf14317ebb4fd0ad',
  transactionIndex: '147',
  from: '0x65274ae6d0d4f88818b600c4b3435c868f5dad4c',
  to: '0xb1690c08e213a35ed9bab7b318de14420fb57d8c',
  value: '2527858781343926',
  gas: '164016',
  gasPrice: '43000000000',
  isError: '1',
  txreceipt_status: '0',
  input: '0x454a2ab3000000000000000000000000000000000000000000000000000000000005a5a3',
  contractAddress: '',
  cumulativeGasUsed: '9793966',
  gasUsed: '27036',
  confirmations: '5702'
}

By looking at the transaction on etherscan I can see that the TokenID is 587058 - https://etherscan.io/tx/0xc7e09024a41841d6acb5933ab983a68bb4433a94e3167a2a893be3ea4387fad6

However that is not recorded anywhere on the transaction... Is there any way that I can derive the token ID? Or, is there an alternative way that I can find the token programmatically/via API? I intend to use the TokenID to then obtain further information about the particular Kitty.

2 Answers 2

0

Very easy.

Get the Transfer events from the transaction hash. using tronscan api.

And find the token ID from the event.

You can use this API: https://api.etherscan.io/api?module=logs&action=getLogs&address=0x06012c8cf97bead5deae237070f9587f8e7a266d&topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef&apikey=YourApiKeyToken

It will give all the latest transactions. You may sort it through it according to your particular transaction.

Enjoy :)

3
  • 1
    Thanks! So what I take from this is that the TokenID is transmitted as event logs against the transaction hash? Does this mean I could also obtain the event logs using a library such as web3js?
    – genzr
    Jan 24, 2021 at 10:18
  • Yes you can get Transfer event using web3js and get token ID from it :) Jan 27, 2021 at 6:00
  • 1
    @Yogesh Next question: how? I can't see anything about token id on the transaction: { address: '0x5c5c53D13aD8725cFE551D6E9dB93e9a9D38D78D', blockHash: '0x3f546e5caa9142d89dc3a87e7d369ba175629b480840d8bf88bd146f205e62de', blockNumber: 6572543, logIndex: 5, removed: false, transactionHash: '0x7de4476522402eb1769fcb93425c63aa0d14a9fbafec6bf2d7dcb7eafc76fb01', transactionIndex: 6, id: 'log_bb3a4fc6', returnValues: Result {}, event: undefined, signature: null, raw: { data: '0x', topics: [Array] } }, Apr 7, 2021 at 1:57
2

I got the solution using Web3.

Full code:

var abi_ = [
        {"inputs":[{"indexed":true,"name":"from","type":"address"},
                {"indexed":true,"name":"to","type":"address"},
                {"indexed":true,"name":"tokenId","type":"uint256"}],
        "name":"Transfer","type":"event"}
    ];

    const Web3Eth = require('web3-eth')
    const API_KEY = "f3e2ba..."
    const URL = "wss://rinkeby.infura.io/ws/v3/" + API_KEY
    const web3Eth = new Web3Eth(Web3Eth.givenProvider || URL);
    const smartContractAddress = "0xab6a8c463d7fecd3ab65761769b7ccbb83ecb597"
    const contract = new web3Eth.Contract(abi_, smartContractAddress)

    contract.getPastEvents('Transfer', {
            fromBlock: 0,
            toBlock: 'latest'
        }, function(error, events){
            if(!error){
                for(var i=0;i<events.length;i++){
                    console.log(events[i].returnValues.tokenId)
                }
            }
        })    

Output:

0
1
2
3

Your Answer

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

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