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.2.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 oneDayProtocolFees: getFeeLoader(chainIDs.P),
85 },
86 metadata: {
87 ...metadata,
88 subtitle: 'P Chain',
89 },
90 });
91
92 sdk.register({
93 id: 'avalanche-c-chain',
94 bundle: 'avalanche',
95 queries: {
96 oneDayTotalFees: getCChainFees,
97 oneDayProtocolFees: getCChainFees,
98 },
99 metadata: {
100 ...metadata,
101 subtitle: 'C Chain',
102 },
103 });
104
105 sdk.register({
106 id: 'avalanche-x-chain',
107 bundle: 'avalanche',
108 queries: {
109 oneDayTotalFees: getFeeLoader(chainIDs.X),
110 oneDayProtocolFees: getFeeLoader(chainIDs.X),
111 },
112 metadata: {
113 ...metadata,
114 subtitle: 'X Chain',
115 },
116 });
117}
118
It's something off?
Report it to the discussion board on Discord, we will take care of it.