description | layout | |||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Learn how to use Airstack to fetch all ERC20/721/1155 token mints data, from or to user(s), across Ethereum, Base, Degen Chain, and other Airstack supported chains. |
|
All tokens minted are essentially token transfers from a null address (0x00...00) to a user address that are executed by the receiving user itself. Thus, with Airstack, you can use the TokenTransfers
API to fetch all user's token mints by specifying the input as follows:
Input Filter | Value | Description |
---|---|---|
operator | user's 0x address, ENS, cb.id, Lens, or Farcaster | Executor of the transaction. |
from | 0x0000000000000000000000000000000000000000 | Sender in the ERC20/721/1155 token transfers. |
to | user's 0x address, ENS, cb.id, Lens, or Farcaster | Receiver in the ERC20/721/1155 token transfers. |
You also have the option to add blockTimestamp
filter to your query to fetch all tokens that are minted during a specified period of time:
Input Filter | Value | Description |
---|---|---|
blockTimestamp |
Specific time period to fetch token mints, use |
Allows entering blockTimestamp to filter transactions which happened in specific periods. |
In this guide you will learn how to use Airstack to:
- Get NFT Mints By A User
- Get ERC20 Token Mints By A User
- Get NFT Mints By A User in a Specified Period
- Get ERC20 Token Mints By A User in a Specified Period
- Get Current Balance of Minted NFTs
- Get Current Balance of Minted ERC20 Tokens
- An Airstack account
- Basic knowledge of GraphQL
If you are using JavaScript/TypeScript or Python, Install the Airstack SDK:
{% tabs %} {% tab title="npm" %} React
npm install @airstack/airstack-react
Node
npm install @airstack/node
{% endtab %}
{% tab title="yarn" %} React
yarn add @airstack/airstack-react
Node
yarn add @airstack/node
{% endtab %}
{% tab title="pnpm" %} React
pnpm install @airstack/airstack-react
Node
pnpm install @airstack/node
{% endtab %}
{% tab title="pip" %}
pip install airstack
{% endtab %} {% endtabs %}
Then, add the following snippets to your code:
{% tabs %} {% tab title="React" %}
import { init, useQuery } from "@airstack/airstack-react";
init("YOUR_AIRSTACK_API_KEY");
const query = `YOUR_QUERY`; // Replace with GraphQL Query
const Component = () => {
const { data, loading, error } = useQuery(query);
if (data) {
return <p>Data: {JSON.stringify(data)}</p>;
}
if (loading) {
return <p>Loading...</p>;
}
if (error) {
return <p>Error: {error.message}</p>;
}
};
{% endtab %}
{% tab title="Node" %}
import { init, fetchQuery } from "@airstack/node";
init("YOUR_AIRSTACK_API_KEY");
const query = `YOUR_QUERY`; // Replace with GraphQL Query
const { data, error } = await fetchQuery(query);
console.log("data:", data);
console.log("error:", error);
{% endtab %}
{% tab title="Python" %}
import asyncio
from airstack.execute_query import AirstackClient
api_client = AirstackClient(api_key="YOUR_AIRSTACK_API_KEY")
query = """YOUR_QUERY""" # Replace with GraphQL Query
async def main():
execute_query_client = api_client.create_execute_query_object(
query=query)
query_response = await execute_query_client.execute_query()
print(query_response.data)
asyncio.run(main())
{% endtab %} {% endtabs %}
To access the Airstack APIs in other languages, you can use https://api.airstack.xyz/gql as your GraphQL endpoint.
🤖 AI Natural Language
Airstack provides an AI solution for you to build GraphQL queries to fulfill your use case easily. You can find the AI prompt of each query in the demo's caption or title for yourself to try.
Airstack AI (Demo)
You can fetch all NFTs minted by a user, e.g. betashop.eth
, across multiple chains, such as Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenTransfers
API:
{% embed url="https://app.airstack.xyz/query/1cpqz1wo5D" %} Show me all NFTs minted by betashop.eth {% endembed %}
{% tabs %} {% tab title="Query" %}
query MyQuery {
Ethereum: TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "betashop.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "betashop.eth"},
# Get only NFTs (ERC721/1155)
tokenType: {_in: [ERC721, ERC1155]},
},
blockchain: ethereum,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
tokenAddress
tokenId
tokenNft {
metaData {
name
}
contentValue {
image {
medium
}
}
}
tokenType
}
}
Base: TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "betashop.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "betashop.eth"},
# Get only NFTs (ERC721/1155)
tokenType: {_in: [ERC721, ERC1155]},
},
blockchain: base,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
tokenAddress
tokenId
tokenNft {
metaData {
name
}
contentValue {
image {
medium
}
}
}
tokenType
}
}
Zora: TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "betashop.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "betashop.eth"},
# Get only NFTs (ERC721/1155)
tokenType: {_in: [ERC721, ERC1155]},
},
blockchain: zora,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
tokenAddress
tokenId
tokenNft {
metaData {
name
}
contentValue {
image {
medium
}
}
}
tokenType
}
}
}
{% endtab %}
{% tab title="Response" %}
{
"data": {
"Ethereum": {
"TokenTransfer": [
{
"blockchain": "ethereum",
"formattedAmount": 1,
"tokenAddress": "0x0f92612c5f539c89dc379e8aa42e3ba862a34b7e",
"tokenId": "8",
"tokenNft": {
"metaData": {
"name": "Venture Club Alpha Membership NFT"
},
"contentValue": {
"image": {
"medium": "https://assets.uat.airstack.xyz/image/nft/li5d4XIGDPxtahI+EMjNmOiSymdFmCR0OsRC2p13nDaZQijmEYVpYlbV0t57GD8K/medium.jpg"
}
}
},
"tokenType": "ERC721"
}
// Other NFTs minted by betashop.eth on Ethereum
]
},
"Base": {
"TokenTransfer": [
{
"blockchain": "base",
"formattedAmount": 1,
"tokenAddress": "0xba5e05cb26b78eda3a2f8e3b3814726305dcac83",
"tokenId": "118",
"tokenNft": {
"metaData": {
"name": null
},
"contentValue": {
"image": null
}
},
"tokenType": "ERC1155"
}
]
},
"Zora": {
"TokenTransfer": null // No NFT mints by betashop.eth on Zora
}
}
}
{% endtab %} {% endtabs %}
You can fetch all ERC20 tokens minted by a user, e.g. ipeciura.eth
, across multiple chains, such as Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenTransfers
API:
{% embed url="https://app.airstack.xyz/query/RsCkWHyHjV" %} Show me all ERC20 tokens minted by ipeciura.eth {% endembed %}
{% tabs %} {% tab title="Query" %}
query MyQuery {
Ethereum: TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC20 tokens
tokenType: {_eq: ERC20},
},
blockchain: ethereum,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
tokenAddress
token {
name
}
}
}
Base: TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC20 tokens
tokenType: {_eq: ERC20},
},
blockchain: base,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
tokenAddress
token {
name
}
}
}
Zora: TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC20 tokens
tokenType: {_eq: ERC20},
},
blockchain: zora,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
tokenAddress
token {
name
}
}
}
}
{% endtab %}
{% tab title="Response" %}
{
"data": {
"Ethereum": {
"TokenTransfer": [
{
"blockchain": "ethereum",
"formattedAmount": 0.01697896348647009,
"tokenAddress": "0xadbf1854e5883eb8aa7baf50705338739e558e5b",
"token": {
"name": "Uniswap V2"
}
},
// Other ERC20 tokens minted by ipeciura.eth on Ethereum
]
},
"Base": {
"TokenTransfer": null // No ERC20 token minted on Base
},
"Zora": {
"TokenTransfer": null // No ERC20 token minted on Zora
}
}
}
{% endtab %} {% endtabs %}
You can fetch all NFTs minted during a specified period of time by a user, e.g. ipeciura.eth
, across different chains, e.g. Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenTransfers
API:
{% embed url="https://app.airstack.xyz/query/8ksF52mYYI" %} Show me ERC721/1155 Ethereum NFT mints by ipeciura.eth in 2023 {% endembed %}
{% tabs %} {% tab title="Query" %}
query MyQuery {
TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC721/1155 NFTs
tokenType: {_in: [ERC721, ERC1155]},
# Specify time stamp (in 2023)
blockTimestamp: {
_gt: "2023-01-01T00:00:00Z", # (greater than - after Jan 1, 2023)
_lt: "2024-01-01T00:00:00Z" # (less than - before Jan 1, 2024)
}
},
blockchain: ethereum,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
blockTimestamp
tokenAddress
tokenNft {
metaData {
name
}
contentValue {
image {
medium
}
}
}
tokenType
}
}
}
{% endtab %}
{% tab title="Response" %}
{
"data": {
"TokenTransfers": {
"TokenTransfer": [
{
"blockchain": "ethereum",
"formattedAmount": 1,
"blockTimestamp": "2023-11-09T13:26:53Z",
"tokenAddress": "0xd3b4de0d85c44c57993b3b18d42b00de81809eea",
"tokenNft": {
"metaData": {
"name": "Unveiling Airstack's Onchain Graph #1"
},
"contentValue": {
"image": {
"medium": "https://assets.airstack.xyz/image/nft/N3xsCM4U2UsyVojt4p4OUOsbwHHkyl1MPU9iEHe4mr0FqR+Vy4cMTaTFc03m2rVApXUy38ASkynABWL/M3lLaQ==/medium.jpg"
}
}
},
"tokenType": "ERC721"
}
]
}
}
}
{% endtab %} {% endtabs %}
You can fetch all ERC20 tokens minted during a specified period of time by a user, e.g. ipeciura.eth
, across different chains, e.g. Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenTransfers
API:
{% embed url="https://app.airstack.xyz/query/BnRXgwEgRm" %} show me all Ethereum ERC20 token mints by ipeciura.eth in 2022 {% endembed %}
{% tabs %} {% tab title="Query" %}
query MyQuery {
TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC20 tokens
tokenType: {_eq: ERC20}
# Specify time stamp (in 2022)
blockTimestamp: {
_gt: "2022-01-01T00:00:00Z", # (greater than - before Jan 1, 2022)
_lt: "2023-01-01T00:00:00Z" # (less than - before Jan 1, 2023)
}
},
blockchain: base,
order: {blockTimestamp: DESC}
}
) {
TokenTransfer {
blockchain
formattedAmount
blockTimestamp
tokenAddress
token {
name
}
}
}
}
{% endtab %}
{% tab title="Response" %}
{
"data": {
"TokenTransfers": {
"TokenTransfer": [
{
"blockchain": "base",
"formattedAmount": 0.01697896348647009,
"blockTimestamp": "2022-07-15T15:37:02Z",
"tokenAddress": "0xadbf1854e5883eb8aa7baf50705338739e558e5b",
"token": {
"name": "Uniswap V2"
}
},
{
"blockchain": "base",
"formattedAmount": 0.9908239030713007,
"blockTimestamp": "2022-07-15T14:45:54Z",
"tokenAddress": "0x9928340f9e1aaad7df1d95e27bd9a5c715202a56",
"token": {
"name": "Balancer B-stMATIC-STABLE RewardGauge Deposit"
}
}
// Other ERC20 tokens minted by ipeciura.eth on Base in 2022
]
}
}
}
{% endtab %} {% endtabs %}
You can fetch the current NFT balances of minted NFTs, e.g. ipeciura.eth
, across different chains, e.g. Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenTransfers
API:
{% embed url="https://app.airstack.xyz/query/rWLjAJASsE" %} Show current NFT balances of Minted NFTs on Ethereum by ipeciura.eth {% endembed %}
{% tabs %} {% tab title="Query" %}
query MyQuery {
TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC721/1155 NFTs
tokenType: {_in: [ERC1155, ERC721]}
},
order: {blockTimestamp: DESC},
blockchain: ethereum
}
) {
TokenTransfer {
blockTimestamp
type
transactionHash
tokenNft {
contentValue {
image {
medium
}
}
}
token {
name
address
type
tokenBalances(input: {filter: {owner: {_eq: "ipeciura.eth"}}}) {
formattedAmount
}
}
}
}
}
{% endtab %}
{% tab title="Response" %}
{
"data": {
"TokenTransfers": {
"TokenTransfer": [
{
"blockTimestamp": "2023-11-09T13:26:53Z",
"type": "MINT",
"transactionHash": "0x633ce3c1c2050b2477d9b8e3ee4a8de41bfb15a2b8adc3f59cc7ba002a782b2f",
"tokenNft": {
"contentValue": {
"image": {
"medium": "https://assets.airstack.xyz/image/nft/N3xsCM4U2UsyVojt4p4OUOsbwHHkyl1MPU9iEHe4mr0FqR+Vy4cMTaTFc03m2rVApXUy38ASkynABWL/M3lLaQ==/medium.jpg"
}
}
},
"token": {
"name": "Unveiling Airstack's Onchain Graph",
"address": "0xd3b4de0d85c44c57993b3b18d42b00de81809eea",
"type": "ERC721",
"tokenBalances": [
{
"formattedAmount": 1 // Shows that ipeciura.eth current balance for this NFT
}
]
}
},
// Other minted NFTs by ipeciura.eth on Ethereum
]
}
}
}
{% endtab %} {% endtabs %}
To get the list of minted NFTs and their current balance in a flat array, use the following format function:
{% tabs %} {% tab title="TypeScript" %}
interface Token {
name: string;
type: string;
address: string;
tokenBalances: { formattedAmount?: string }[];
}
interface TokenTransfer {
token: Token;
}
interface Data {
TokenTransfers?: {
TokenTransfer: TokenTransfer[];
};
}
/**
* @description Formats the NFT mints data.
*
* @example
* import { fetchQuery } from "@airstack/node";
*
* const { data } = await fetchQuery(query, variables);
* const res = formatFunction(data);
*
* @param {Data} data - The data result from NFT mints query.
* @returns {Object[]} An array of formatted minted NFTs' current balance
*/
const formatFunction = (data: Data) =>
data?.TokenTransfers?.TokenTransfer?.map(({ token }) => {
const { name, type, address, tokenBalances } = token;
const formattedAmount = tokenBalances[0]?.formattedAmount ?? 0;
return { name, type, address, formattedAmount };
})?.filter(
(arr, index, self) =>
index === self.findIndex((t) => t.address === arr.address)
);
{% endtab %}
{% tab title="JavaScript" %}
/**
* @description Formats the NFT mints data.
*
* @example
* import { fetchQuery } from "@airstack/node";
*
* const { data } = await fetchQuery(query, variables);
* const res = formatFunction(data);
*
* @param {Data} data - The data result from NFT mints query.
* @returns {Object[]} An array of formatted minted NFTs' current balance
*/
const formatFunction = (data) =>
data?.TokenTransfers?.TokenTransfer?.map(({ token }) => {
const { name, type, address, tokenBalances } = token;
const formattedAmount = tokenBalances[0]?.formattedAmount ?? 0;
return { name, type, address, formattedAmount };
})?.filter(
(arr, index, self) =>
index === self.findIndex((t) => t.address === arr.address)
);
{% endtab %}
{% tab title="Python" %}
from typing import List, Dict, Optional, Any
class Token:
def __init__(self, name: str, type: str, address: str, tokenBalances: List[Dict[str, Any]]):
self.name = name
self.type = type
self.address = address
self.tokenBalances = tokenBalances
class TokenTransfer:
def __init__(self, token: Token):
self.token = token
class Data:
def __init__(self, TokenTransfers: Optional[Dict[str, List[TokenTransfer]]]):
self.TokenTransfers = TokenTransfers
def format_function(data: Optional[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Format and flatten NFT mint data to show all minted NFTs
and their current balance hold by a user
"""
if not data or "TokenTransfers" not in data or not data["TokenTransfers"]:
return []
token_transfers = data["TokenTransfers"].get("TokenTransfer")
if not token_transfers:
return []
unique = {}
formatted_data = []
for transfer in token_transfers:
token = transfer.get("token", {})
token_balances = token.get("tokenBalances", [])
if token_balances:
formatted_amount = token_balances[0].get("formattedAmount", 0)
else:
formatted_amount = 0
if token.get("address") and token.get("address") not in unique:
unique[token.get("address")] = True
formatted_data.append({
"name": token.get("name"),
"type": token.get("type"),
"address": token.get("address"),
"formattedAmount": formatted_amount
})
return formatted_data
{% endtab %} {% endtabs %}
With this format function, you will get the following result that you can directly add to your application:
[
{
"name": "Unveiling Airstack's Onchain Graph",
"type": "ERC721",
"address": "0xd3b4de0d85c44c57993b3b18d42b00de81809eea",
"formattedAmount": 1
},
{
"name": "MIAMI ESPORTS",
"type": "ERC721",
"address": '0x15b120343928019be964a353cf4f7dae474fb579",
"formattedAmount": 1
}
]
You can fetch the current ERC20 token balances of minted ERC20 tokens that are still hold by a user, e.g. ipeciura.eth
, across different chains, e.g. Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenTransfers
API:
{% embed url="https://app.airstack.xyz/query/i4vZl3D8Fa" %} Show current ERC20 token balances of Minted ERC20 tokens on Ethereum by ipeciura.eth {% endembed %}
{% tabs %} {% tab title="Query" %}
query MyQuery {
TokenTransfers(
input: {
filter: {
# Only get mints that are executed by the same user
operator: {_eq: "ipeciura.eth"},
# Mints are token transfers that has null address as `from`
from: {_eq: "0x0000000000000000000000000000000000000000"},
# Set this to the user that receive the token mints
to: {_eq: "ipeciura.eth"},
# Get only ERC20 tokens
tokenType: {_eq: ERC20}
},
order: {blockTimestamp: DESC},
blockchain: ethereum}
) {
TokenTransfer {
blockTimestamp
type
transactionHash
token {
name
address
tokenBalances(input: {filter: {owner: {_eq: "ipeciura.eth"}}}) {
formattedAmount
}
}
}
}
}
{% endtab %}
{% tab title="Response" %}
{
"data": {
"TokenTransfers": {
"TokenTransfer": [
{
"blockTimestamp": "2022-07-15T14:45:54Z",
"type": "MINT",
"transactionHash": "0x074efa071dd2be61286f411c091cf157246ccbe7fd6b24f6a8af86bcf52a0c98",
"token": {
"name": "Balancer B-stMATIC-STABLE RewardGauge Deposit",
"address": "0x9928340f9e1aaad7df1d95e27bd9a5c715202a56",
"tokenBalances": [
{
"formattedAmount": 0.9908239030713007 // ipeciura.eth currently hold this ERC20 token
}
]
}
},
{
"blockTimestamp": "2022-07-15T15:37:02Z",
"type": "MINT",
"transactionHash": "0x985ff4181c7fec054fcf5eb20d841f4372745f80cf51a112444337166e7417f9",
"token": {
"name": "Uniswap V2",
"address": "0xadbf1854e5883eb8aa7baf50705338739e558e5b",
"tokenBalances": [] // Empty array indicates that the minted ERC20 token have 0 balance (no longer held)
}
},
// Other ERC20 tokens minted by ipeciura.eth on Ethereum
]
}
}
}
{% endtab %} {% endtabs %}
To get the list of minted ERC20 tokens and their current balance in a flat array, use the following format function:
{% tabs %} {% tab title="TypeScript" %}
interface Token {
name: string;
address: string;
tokenBalances: { formattedAmount?: string }[];
}
interface TokenTransfer {
token: Token;
}
interface Data {
TokenTransfers?: {
TokenTransfer: TokenTransfer[];
};
}
/**
* @description Formats the ERC20 token mints data.
*
* @example
* import { fetchQuery } from "@airstack/node";
*
* const { data } = await fetchQuery(query, variables);
* const res = formatFunction(data);
*
* @param {Data} data - The data result from ERC20 token mints query.
* @returns {Object[]} An array of formatted minted ERC20 tokens' current balance
*/
const formatFunction = (data: Data) =>
data?.TokenTransfers?.TokenTransfer?.map(({ token }) => {
const { name, address, tokenBalances } = token;
const formattedAmount = tokenBalances[0]?.formattedAmount ?? 0;
return { name, address, formattedAmount };
})?.filter(
(arr, index, self) =>
index === self.findIndex((t) => t.address === arr.address)
);
{% endtab %}
{% tab title="JavaScript" %}
/**
* @description Formats the ERC20 token mints data.
*
* @example
* import { fetchQuery } from "@airstack/node";
*
* const { data } = await fetchQuery(query, variables);
* const res = formatFunction(data);
*
* @param {Data} data - The data result from ERC20 token mints query.
* @returns {Object[]} An array of formatted minted ERC20 tokens' current balance
*/
const formatFunction = (data) =>
data?.TokenTransfers?.TokenTransfer?.map(({ token }) => {
const { name, address, tokenBalances } = token;
const formattedAmount = tokenBalances[0]?.formattedAmount ?? 0;
return { name, address, formattedAmount };
})?.filter(
(arr, index, self) =>
index === self.findIndex((t) => t.address === arr.address)
);
{% endtab %}
{% tab title="Python" %}
from typing import List, Dict, Optional, Any
class Token:
def __init__(self, name: str, address: str, tokenBalances: List[Dict[str, Any]]):
self.name = name
self.address = address
self.tokenBalances = tokenBalances
class TokenTransfer:
def __init__(self, token: Token):
self.token = token
class Data:
def __init__(self, TokenTransfers: Optional[Dict[str, List[TokenTransfer]]]):
self.TokenTransfers = TokenTransfers
def format_function(data: Optional[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Format and flatten ERC20 token mint data to show all minted ERC20 tokens
and their current balance hold by a user
"""
if not data or "TokenTransfers" not in data or not data["TokenTransfers"]:
return []
token_transfers = data["TokenTransfers"].get("TokenTransfer")
if not token_transfers:
return []
unique = {}
formatted_data = []
for transfer in token_transfers:
token = transfer.get("token", {})
token_balances = token.get("tokenBalances", [])
if token_balances:
formatted_amount = token_balances[0].get("formattedAmount", 0)
else:
formatted_amount = 0
if token.get("address") and token.get("address") not in unique:
unique[token.get("address")] = True
formatted_data.append({
"name": token.get("name"),
"address": token.get("address"),
"formattedAmount": formatted_amount
})
return formatted_data
{% endtab %} {% endtabs %}
With this format function, you will get the following result that you can directly add to your application:
[
{
"name": "Balancer B-stMATIC-STABLE RewardGauge Deposit",
"address": "0x9928340f9e1aaad7df1d95e27bd9a5c715202a56",
"formattedAmount": 0.9908239030713007
},
{
"name": "Uniswap V2",
"address": "0xadbf1854e5883eb8aa7baf50705338739e558e5b",
"formattedAmount": 0
}
// Other minted ERC20 tokens
]
If you have any questions or need help regarding fetching token mints data into your application, please join our Airstack's Telegram group.