Adapter

Avalanche Fees

Sub-Adapters 3

Preview and test each sub adapter.

Avalanche - P Chain (avalanche-p-chain)

Avalanche - C Chain (avalanche-c-chain)

Avalanche - X Chain (avalanche-x-chain)

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Avalanche Fees';
2export const version = '0.1.0';
3export const license = 'MIT';
4
5const chainIDs = {
6  P: '11111111111111111111111111111111LpoYY',
7  C: '2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5',
8  X: '2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM',
9};
10
11export function setup(sdk: Context) {
12  const getChainFees = async (chainId: string, date: string) => {
13    const startDate = new Date(date).toISOString();
14    const endDate = new Date(sdk.date.offsetDaysFormatted(date, 1)).toISOString();
15    const dayAggregate = await sdk.http.get(
16      `https://explorerapi.avax.network/v2/txfeeAggregates?startTime=${startDate}&endTime=${endDate}&chainID=${chainId}`
17    );
18
19    return parseFloat(dayAggregate.aggregates.txfee) / 1e9;
20  };
21
22  const getBalance = async (block: string) => {
23    const res = await sdk.http.post('https://api.avax.network/ext/bc/C/rpc', {
24      id: 1,
25      jsonrpc: '2.0',
26      method: 'eth_getBalance',
27      params: ['0x0100000000000000000000000000000000000000', block],
28    });
29
30    return parseInt(res.result, 16) / 1e18;
31  };
32
33  const getCChainFees = async (date: string) => {
34    const [startOfDayBalance, endOfDayBalance, atomicFees, avaxPrice] = await Promise.all([
35      sdk.chainData.getBlockNumber(date, 'avalanche')
36        .then((blockStartOfDay: number) => getBalance('0x' + blockStartOfDay.toString(16))),
37      sdk.chainData.getBlockNumber(sdk.date.offsetDaysFormatted(date, 1), 'avalanche')
38        .then((blockEndOfDay: number) => getBalance('0x' + blockEndOfDay.toString(16))),
39      getChainFees(chainIDs.C, date),
40      sdk.coinGecko.getHistoricalPrice('avalanche-2', date),
41    ]);
42
43    const txFees = endOfDayBalance - startOfDayBalance;
44
45    return (txFees + atomicFees) * avaxPrice;
46  };
47
48  const getFeeLoader = (chain: string) => async (date: string) => {
49    const [fees, price] = await Promise.all([
50      getChainFees(chain, date),
51      sdk.coinGecko.getHistoricalPrice('avalanche-2', date),
52    ]);
53
54    return fees * price;
55  };
56
57  const metadata = {
58    name: 'Avalanche',
59    icon: sdk.ipfs.getDataURILoader('QmcWreeBT5HuurXsc5LbdDphmXUY3T4YrfnXvqt6y2no68', 'image/svg+xml'),
60    category: 'l1',
61    description: 'Avalanche is a platform for inter-connected blockchains.',
62    feeDescription: 'Transaction fees are burned.',
63    source: 'Ava Labs',
64    website: 'https://avalabs.org',
65    tokenTicker: 'AVAX',
66    tokenCoingecko: 'avalanche-2',
67    protocolLaunch: '2020-09-22',
68    tokenLaunch: '2020-09-22',
69    events: [
70      {
71        date: '2021-08-18',
72        description: '"Avalanche Rush" liquidity mining program announced',
73      },
74    ],
75  };
76
77  sdk.registerBundle('avalanche', metadata);
78
79  sdk.register({
80    id: 'avalanche-p-chain',
81    bundle: 'avalanche',
82    queries: {
83      oneDayTotalFees: getFeeLoader(chainIDs.P),
84    },
85    metadata: {
86      ...metadata,
87      subtitle: 'P Chain',
88    },
89  });
90
91  sdk.register({
92    id: 'avalanche-c-chain',
93    bundle: 'avalanche',
94    queries: {
95      oneDayTotalFees: getCChainFees,
96    },
97    metadata: {
98      ...metadata,
99      subtitle: 'C Chain',
100    },
101  });
102
103  sdk.register({
104    id: 'avalanche-x-chain',
105    bundle: 'avalanche',
106    queries: {
107      oneDayTotalFees: getFeeLoader(chainIDs.X),
108    },
109    metadata: {
110      ...metadata,
111      subtitle: 'X Chain',
112    },
113  });
114}
115

It's something off?

Report it to the discussion board on Discord, we will take care of it.

Adapter Info

Version

0.1.0

License

MIT

IPFS CID

QmXednKReDLYrpCRfrJ9Psrjrt9vjXb4bwdM8MHup5ZJKu

CID (source)

QmePmUtsZc2XDqzYpFUziDhU48DF4k53YJB8XzrnCCDPoF

Collections

fees

Author

mihal.eth