Adapter

Balancer Fees - Clone

Sub-Adapters 4

Preview and test each sub adapter.

Balancer - Version 1 (balancer-v1)

Balancer - Version 2 (balancerv2)

Balancer - Polygon (balancerv2-polygon)

Balancer - Arbitrum (balancerv2-arbitrum)

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Balancer Fees - Clone';
2export const version = '0.2.0';
3export const license = 'MIT';
4
5const SEC_IN_DAY = 86400;
6
7export function setup(sdk: Context) {
8  async function getBalancerV1Data(
9    date: string,
10    subgraphName: string,
11    chain = 'ethereum'
12  ): Promise<number> {
13    const todayBlock = await sdk.chainData.getBlockNumber(sdk.date.offsetDaysFormatted(date, 1), chain);
14    const yesterdayBlock = await sdk.chainData.getBlockNumber(date, chain);
15
16    const data = await sdk.graph.query(
17      `balancer-labs/${subgraphName}`,
18      `{
19        now: balancer(id: "1", block: {number: ${todayBlock}}) {
20          totalSwapFee
21        }
22        yesterday: balancer(id: "1", block: {number: ${yesterdayBlock}}) {
23          totalSwapFee
24        }
25      }`
26    );
27
28    if (!data.now || !data.yesterday) {
29      return null;
30    }
31
32    return parseFloat(data.now.totalSwapFee) - parseFloat(data.yesterday.totalSwapFee);
33  };
34
35  async function getBalancerV2Data(
36    date: string,
37    subgraphName: string
38  ): Promise<number> {
39    const dayId = Math.floor(sdk.date.dateToTimestamp(date) / SEC_IN_DAY);
40
41    const graphQuery = `query fees($dayId: String!, $yesterdayId: String!) {
42      now: balancerSnapshot(id: $dayId) {
43        totalSwapFee
44      }
45      yesterday: balancerSnapshot(id: $yesterdayId) {
46        totalSwapFee
47      }
48    }`;
49
50    const data = await sdk.graph.query(
51      `balancer-labs/${subgraphName}`,
52      graphQuery,
53      {
54        dayId: `2-${dayId}`,
55        yesterdayId: `2-${dayId - 1}`
56      }
57    );
58
59    if (!data.now || !data.yesterday) {
60      return null;
61    }
62
63    return parseFloat(data.now.totalSwapFee) - parseFloat(data.yesterday.totalSwapFee);
64  };
65
66  const metadata = {
67    category: 'dex',
68    name: 'Balancer',
69    description: 'Balancer is a decentralized exchange & asset pool balancer.',
70    feeDescription: 'Trading fees are paid by traders to liquidity providers',
71    source: 'The Graph Protocol',
72    website: 'https://balancer.fi',
73    blockchain: 'Ethereum',
74    tokenLaunch: '2020-06-20',
75    tokenTicker: 'BAL',
76    tokenCoingecko: 'balancer',
77    icon: sdk.ipfs.getDataURILoader('Qmb3u8knknnGYyLBVrw5ZTqYUue2LC1zCkDWsfctBHJBHN', 'image/svg+xml'),
78    protocolLaunch: '2020-02-27',
79  };
80
81  sdk.register({
82    id: 'balancer-v1',
83    bundle: 'balancer',
84    queries: {
85      oneDayTotalFees: (date: string) => getBalancerV1Data(date, 'balancer'),
86    },
87    metadata: {
88      ...metadata,
89      subtitle: 'Version 1',
90      protocolLaunch: '2020-02-27',
91    },
92  });
93
94  sdk.register({
95    id: 'balancerv2',
96    bundle: 'balancer',
97    queries: {
98      oneDayTotalFees: (date: string) => getBalancerV2Data(date, 'balancer-v2'),
99    },
100    metadata: {
101      ...metadata,
102      subtitle: 'Version 2',
103      protocolLaunch: '2021-05-11',
104    },
105  });
106
107  sdk.register({
108    id: 'balancerv2-polygon',
109    bundle: 'balancer',
110    queries: {
111      oneDayTotalFees: (date: string) => getBalancerV2Data(date, 'balancer-polygon-v2'),
112    },
113    metadata: {
114      ...metadata,
115      subtitle: 'Polygon',
116      blockchain: 'Polygon',
117      protocolLaunch: '2021-07-01',
118    },
119  });
120
121  sdk.register({
122    id: 'balancerv2-arbitrum', 
123    bundle: 'balancer',
124    queries: {
125      oneDayTotalFees: (date: string) => getBalancerV2Data(date, 'balancer-arbitrum-v2'),
126    },
127    metadata: {
128      ...metadata,
129      subtitle: 'Arbitrum',
130      blockchain: 'Arbitrum One',
131      protocolLaunch: '2021-08-31',
132    },
133  });
134
135  sdk.registerBundle('balancer', metadata);
136}
137

It's something off?

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

Adapter Info

Version

0.2.0

License

MIT

IPFS CID

QmXqi6MmWDPpEcjJLCJwSMMo3JRrNLZrtWH37B8uw4EU2g

CID (source)

QmRij41y4iJLQaEJxBm5QPd1J8UEn93zq8RBxWHnh9BKCk

Collections

fees

Author

0x4780c2e30CF6b88AdD511bAc516DCbFB444Fd48a