Token Concentration

In this example we will set a trigger on the balances mapping of a token contract to monitor how concentrated it is. To understand how to configure the trigger, see the storage trigger example.

import { ethers } from "https://cdn.skypack.dev/[email protected]";

const providerString = "http://chain-provider";

const erc20Abi = ["function totalSupply() view returns (uint256)"];

export async function triggerHandler(context, data) {
    if (data.keys.length !== 1) {
        console.error("Expected 1 key, got " + data.keys.length);
        return;
    }

    const walletAddress = data.keys[0];
    const currentEOAHoldings = Number(data.finalValue);

    const provider = new ethers.providers.JsonRpcProvider(providerString);
    const tokenAddress = data.storageAddress;
    const token = new ethers.Contract(tokenAddress, erc20Abi, provider);

    const totalSupply = await token
        .totalSupply({ blockTag: context.blockNumber })
        .then((result) => Number(result.toHexString()));

    const percentage = currentEOAHoldings / totalSupply;
    return {
        context: context,
        tokenAddress: tokenAddress,
        walletAddress: walletAddress,
        percentage: percentage,
        totalSupply: totalSupply,
        currentEOAHoldings: currentEOAHoldings,
    };
}