Overview - Tools you need to work with Chainbase

To automatically retrieve all the transfers of an ERC20 token using its contract address, you can utilize Chainbase API’s getTokenTransfers function. This article will guide you through the necessary steps to achieve this.

Outline of the Article

  1. Introduction
  1. Setting up a free account at Chainbase
  2. Writing a script using Chainbase API
  3. Printing metadata of an ERC20 token
  4. API Reference
  5. Support
  6. Conclusion
  7. FAQs

1. Introduction

When working with ERC20 tokens, it can be beneficial to track and analyze the token transfers associated with a specific contract address. Chainbase API offers a convenient solution to retrieve all the transfers of an ERC20 token efficiently.

2. Setting up a free account at Chainbase

To fully leverage the capabilities of Chainbase api, you need to create a free account on our platform. Follow these steps:

  • Visit the Chainbase website and sign up for a free account.
  • Once logged in, navigate to the dashboard to get an overview of the available features.
  • Create a new project in the console to obtain an API key.

3**. Writing a script using Chainbase API**

To retrieve all the transfers of an ERC20 token, you can use the Chainbase API in your script. Here are examples of how to accomplish this in JavaScript:

Using fetch:

network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
contract_addr = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0'; // Take Matic Token's contract address as an example.

fetch(`https://api.chainbase.online/v1/token/transfers?chain_id=${network_id}&contract_address=${contract_addr}&page=1&limit=5`, {
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
}).then(response => response.json())
    .then(data => console.log(data.data))
    .catch(error => console.error(error));

Using axios:

You need to install axios using npm install axios --save in the terminal first.

network_id = '1'; // See https://docs.chainbase.com/reference/supported-chains to get the id of different chains.
contract_addr = '0x7d1afa7b718fb893db30a3abc0cfc608aacfebb0'; // Take Matic Token's contract address as an example.

const axios = require('axios');
const options = {
    url: `https://api.chainbase.online/v1/token/transfers?chain_id=${network_id}&contract_address=${contract_addr}&page=1&limit=5`,
    method: 'GET',
    headers: {
        'x-api-key': CHAINBASE_API_KEY, // Replace the field with your API key.
        'accept': 'application/json'
    }
};
axios(options)
    .then(response => console.log(response.data.data))
    .catch(error => console.log(error));

4**. Printing metadata of an ERC20 token**

By using the Chainbase API’s getTokenTransfers function with the appropriate parameters, you can obtain the transfers related to a specific ERC20 token. Additionally, if you provide an address, you can filter the transfers to a specific address. The returned data will include information such as block number, timestamp, addresses involved, transaction hash, transaction index, transaction fee, and transfer value.

To get data printed, run command node .js in the terminal. In this case, the data returned looks as follows.

{
  "block_number": 17182749,
  "block_timestamp": "2023-05-03T21:06:23Z",
  "from_address": "0x25feaf3f5f36b44d0cb5a9c735d205e66f94437c",
  "log_index": 292,
  "to_address": "0xa3fd5a303020f2eeee79d9fc4ae468bca6bccc56",
  "transaction_hash": "0x443b17a0ea6418b4b0e467dd7c495034bf13c2be65dc8fca958f4a8ec7c53513",
  "transaction_index": 137,
  "tx_fee": 3918874072581520,
  "tx_type": 2,
  "value": "188992828092557071964"
},
{
  "block_number": 17182747,
  "block_timestamp": "2023-05-03T21:05:59Z",
  "from_address": "0x0979850acd0133fbf330e3d2f2540e111b77e969",
  "log_index": 329,
  "to_address": "0x1522900b6dafac587d499a862861c0869be6e428",
  "transaction_hash": "0xf1208710548fe0ac4ad9d6e3a2157adc741f8eeca0914d2cb680a18cb7ac42fd",
  "transaction_index": 139,
  "tx_fee": 4530117915638504,
  "tx_type": 2,
  "value": "2178406783020000000000"
},
{
  "block_number": 17182742,
  "block_timestamp": "2023-05-03T21:04:59Z",
  "from_address": "0xd9a506404e71e1f461a1d532a1f748aa1a52c437",
  "log_index": 259,
  "to_address": "0x51a9f80cdfe9c6996d95e5f6b6d029ed7ddd5001",
  "transaction_hash": "0x0d823b7261b741474d7635913abc315c4f188756b0e793b2eb5276f463efcb63",
  "transaction_index": 95,
  "tx_fee": 4091630857640924,
  "tx_type": 2,
  "value": "419123260000000000000"
},
{
  "block_number": 17182734,
  "block_timestamp": "2023-05-03T21:03:11Z",
  "from_address": "0x21a31ee1afc51d94c2efccaa2092ad1028285549",
  "log_index": 169,
  "to_address": "0x0979850acd0133fbf330e3d2f2540e111b77e969",
  "transaction_hash": "0x5b08286a38964c59925019f1f57b9b49270481cc97875bcc866d3eadb1f6a5cc",
  "transaction_index": 66,
  "tx_fee": 4178649496106592,
  "tx_type": 2,
  "value": "2178406783020000000000"
},
{
  "block_number": 17182733,
  "block_timestamp": "2023-05-03T21:02:59Z",
  "from_address": "0x28c6c06298d514db089934071355e5743bf21d60",
  "log_index": 216,
  "to_address": "0xf23500fec72b26535a2d61383a665439919f287f",
  "transaction_hash": "0xa135997a18e3aa2413b1e2cf262db44fa0418758c57dc0b5b1e8456fcedfba32",
  "transaction_index": 72,
  "tx_fee": 4095379462808136,
  "tx_type": 2,
  "value": "4133504000000000000000"
}

5**. API Reference**

If you want to know more details on the endpoint and optional parameters, check out:

6**. Support**

If you encounter any difficulties while following this tutorial, don’t hesitate to reach out to our Chainbase community support on the Discord server. They provide 24/7 assistance and guidance.

7. Conclusion

In conclusion, retrieving all ERC20 transfers by contract address becomes effortless with the help of our Chainbase API. By setting up an account, writing a script using the API, and utilizing the getTokenTransfers function, you can access and analyze valuable transfer data for ERC20 tokens.

8. FAQs

Q1: Can I retrieve transfers from multiple ERC20 tokens using Chainbase API?

A1: Yes, you can retrieve transfers for any ERC20 token by providing its contract address as a parameter to the getTokenTransfers function.

Q2: Is the Chainbase API free to use?

A2: We offer a free account plan that gives you access to the API. We also have paid plans available for more advanced features and higher usage limits. You can find more information on our pricing page.

Q3: Can I filter transfers by a specific address using Chainbase API?

A3: Yes, you can specify an address in the request to filter transfers related to that specific address.

Q4: How frequently is the transfer data updated in Chainbase?

A4: The transfer data is typically updated in real-time or with a minimal delay, depending on the blockchain network’s performance.

Q5: Can I integrate Chainbase API into my own application or project?

A5: Absolutely! Our Chainbase API is designed to be integrated into various applications, projects, and services that require access to ERC20 transfer data.


About Chainbase

Chainbase is an all-in-one data infrastructure for Web3 that allows you to index, transform, and use on-chain data at scale. By leveraging enriched on-chain data and streaming computing technologies across one data infrastructure, Chainbase automates the indexing and querying of blockchain data, enabling developers to accomplish more with less effort.

Want to learn more about Chainbase?

Visit our website chainbase.com Sign up for a free account, and Check out our documentation.

WebsiteBlogTwitterDiscordLink3