4

I'm using Web3j library to work with the blockchain. I want to resolve what tokens are transferred during specific transactions. What I have already tried:

  • Call Function with the name supportsInterface to check whether it supports NFT standards (ERC721, ERC1155 and etc.). Have not succeeded.
  • Tried to decoded Transaction Logs, found out how to retrieve Token ID, but I can't do anything with this information.

Any suggestions on this?

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
wollenS
  • 67
  • 5

1 Answers1

1

You cannot get what token is used from the transaction hash: Here is my answer to see what information you can get from trnasaction hash if you decode it: How to know the cryptocurrency used in a transaction through the transaction hash?

However you can get which interface is used.

supportsInterface is used for this:

// I did not use web3j. I am not sure how you call methods in web3j
const is721 = await contract.methods.supportsInterface('0x80ac58cd').call();
if(is721) {
    return "ERC721";
}

In order to call this your contract has to inherit from ERC165. if you are using contract from openzeppelin, it already inherits and it should work:

contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {}

But if you are implementing your own ERC721, you have to make sure you inherit from ERC165. ERC165 is for detecting what interfaces a smart contract implements. ERC165 has only one function used to check if the contract signature meets the specified interface signature.

interface ERC165 {
/// @notice Query whether a contract implements some interface or
not
/// @param interfaceID Interface ID specified by ERC-165 standard
/// @dev Interface ID is defined in ERC-165 standard.
/// This function use GAS less than 30,000 gas.
/// @return If contract implements the specified interface, then
return
/// `true`
/// and `interfaceID` is not 0xffffffff, or return `false`
function supportsInterface(bytes4 interfaceID) external view
returns (bool);
}
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • 1
    Thank you (+1). Let's say, I have a transaction object or a transaction receipt, how do I load its contract to check that it is ERC165? I am sorry, if it is something obvious, I am new to web3. – Sasha Shpota Feb 08 '22 at 16:46
  • @SashaShpota check this out: https://stackoverflow.com/questions/70935158/how-to-know-the-cryptocurrency-used-in-a-transaction-through-the-transaction-has/70940099#70940099 – Yilmaz Feb 08 '22 at 18:43
  • Thanks, identified how to it earlier, but forgot to write the answer. Basically, to check if it's NFT. Method `supportsInterface` should be called to check whether it supports `0x80ac58cd` (ERC721) and `0xd9b67a26` (ERC1155) – wollenS Feb 09 '22 at 08:40
  • @Yilmaz thank you for pointing out the link. However I still don't get how I reach from having a transaction hash to obtaining a contract. Can you please clarify? It might be completely obvious but I am new to web3 and just see no links. – Sasha Shpota Feb 09 '22 at 18:01
  • @wollenS can you please post your solution? – Sasha Shpota Feb 09 '22 at 18:02
  • @SashaShpota you cannot get the token from transaction hash. I just realized that question is also asking about it. – Yilmaz Feb 09 '22 at 18:04
  • Becasue question is asking about if transaction is nft. question body is asking another question – Yilmaz Feb 09 '22 at 18:05
  • @Yilmaz I am confused. From what I understood from your answers, in order to understand if a transaction is ERC721 one has to check its contract. But if I have transaction hash and I cannot get its contract, how can I understand if the transaction is ERC721? – Sasha Shpota Feb 09 '22 at 18:08
  • @SashaShpota since nft contract is inheriting from `ERC165`, you can call the methods inside `ERC165` which has only one method and this method's only job is to detect which interface the current contract implemented from. So I am calling `supportsInterface` passing the interface id for I721. – Yilmaz Feb 09 '22 at 18:12