Adapter

Uniswap Fees & Volume

Get total trading fees and volume across various Uniswap deployments from The Graph

Sub-Adapters 6

Preview and test each sub adapter.

Uniswap - V1 (uniswap-v1)

Uniswap - V2 (uniswap-v2)

Uniswap - V3 (uniswap-v3)

Uniswap - Optimism (uniswap-optimism)

Uniswap - Arbitrum One (uniswap-arbitrum)

Uniswap - Polygon (uniswap-polygon)

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Uniswap Fees & Volume';
2export const version = '1.0.2';
3export const license = 'MIT';
4export const description = 'Get total trading fees and volume across various Uniswap deployments from The Graph';
5
6const blacklistAddresses = [
7  '0x7d7e813082ef6c143277c71786e5be626ec77b20',
8  '0xe5ffe183ae47f1a0e4194618d34c5b05b98953a8',
9  '0xf9c1fa7d41bf44ade1dd08d37cc68f67ae75bf92',
10  '0x23fe4ee3bd9bfd1152993a7954298bb4d426698f',
11  '0x382a9a8927f97f7489af3f0c202b23ed1eb772b5',
12];
13
14const SEC_IN_DAY = 86400;
15
16export function setup(sdk: Context) {
17  const uniV3Adapter = (subgraph: string, getFees?: boolean) => async (date: string): Promise<number> => {
18    const dateId = Math.floor(sdk.date.dateToTimestamp(date) / SEC_IN_DAY);
19
20    const graphQuery = `query fees($dateId: Int!, $nextDay: Int!) {
21      uniswapDayData(id: $dateId) {
22        feesUSD
23        volumeUSD
24      }
25      nextDay: uniswapDayData(id: $nextDay) {
26        feesUSD
27        volumeUSD
28      }
29    }`;
30
31    const data = await sdk.graph.query(
32      subgraph,
33      graphQuery,
34      { dateId, nextDay: dateId + 1 },
35    );
36
37    if (!data.nextDay) {
38      return null;
39    }
40
41    return parseFloat(getFees ? data.uniswapDayData.feesUSD : data.uniswapDayData.volumeUSD);
42  };
43
44  const getUniswapV2 = (getFees?: boolean) => async (date: string): Promise<number> => {
45    const dateId = Math.floor(sdk.date.dateToTimestamp(date) / SEC_IN_DAY);
46
47    const graphQuery = `query fees($date: Int!, $dateId: Int!, $nextDay: Int!, $blacklistAddresses: [Bytes!]!) {
48      uniswapDayData(id: $dateId) {
49        dailyVolumeUSD
50      }
51      nextDay: uniswapDayData(id: $nextDay) {
52        dailyVolumeUSD
53      }
54      blacklist: pairDayDatas(where: { pairAddress_in: $blacklistAddresses, date: $date }) {
55        dailyVolumeUSD
56      }
57    }`;
58
59    const data = await sdk.graph.query(
60      'uniswap/uniswap-v2',
61      graphQuery,
62      {
63        date: sdk.date.dateToTimestamp(date),
64        blacklistAddresses,
65        dateId,
66        nextDay: dateId + 1,
67      },
68    );
69
70    if (!data.nextDay) {
71      return null;
72    }
73
74    const blacklistVolume = data.blacklist.reduce(
75      (total: number, day: any) => total + parseFloat(day.dailyVolumeUSD),
76      0
77    );
78
79    const oneDayVolume = parseFloat(data.uniswapDayData.dailyVolumeUSD) - blacklistVolume;
80
81    return getFees ? oneDayVolume * 0.003 : oneDayVolume;
82  };
83
84  const getUniswapV1 = (getFees?: boolean) => async (date: string): Promise<number> => {
85    const dateId = Math.floor(sdk.date.dateToTimestamp(date) / SEC_IN_DAY);
86
87    const graphQuery = `query fees($dateId: Int!, $nextDay: Int!) {
88      uniswapDayData(id: $dateId) {
89        dailyVolumeInUSD
90      }
91      nextDay: uniswapDayData(id: $nextDay) {
92        dailyVolumeInUSD
93      }
94    }`;
95
96    const data = await sdk.graph.query('graphprotocol/uniswap', graphQuery, { dateId, nextDay: dateId + 1});
97
98    if (!data.nextDay) {
99      return null;
100    }
101
102    const oneDayVolume = parseFloat(data.uniswapDayData.dailyVolumeInUSD);
103
104    return getFees ? oneDayVolume * 0.003 : oneDayVolume;
105  };
106
107  const metadata = {
108    icon: sdk.ipfs.getDataURILoader('QmPXoiG66a9gCDX1NX51crWV7UAijoFd5wycHrfRKM6Y1n', 'image/svg+xml'),
109    name: 'Uniswap',
110    bundle: 'uniswap',
111    category: 'dex',
112    description: 'Uniswap is a permissionless, decentralized exchange',
113    feeDescription: 'Trading fees are paid by traders to liquidity providers',
114    website: 'https://uniswap.org',
115    blockchain: 'Ethereum',
116    source: 'The Graph Protocol',
117    tokenTicker: 'UNI',
118    tokenCoingecko: 'uniswap',
119    tokenLaunch: '2020-09-14',
120    protocolLaunch: '2020-11-02',
121  };
122
123  sdk.register({
124    id: 'uniswap-v1',
125    bundle: 'uniswap',
126    queries: {
127      oneDayTotalFees: getUniswapV1(true),
128      oneDayTotalVolumeUSD: getUniswapV1(),
129    },
130    metadata: {
131      ...metadata,
132      icon: sdk.ipfs.getDataURILoader('QmQZhDfjDjT1Tv9xfaWYcRkywD4GCavkhge7Yi4A74rJ4b', 'image/png'),
133      subtitle: 'V1',
134      protocolLaunch: '2020-11-02',
135    },
136  });
137
138  sdk.register({
139    id: 'uniswap-v2',
140    bundle: 'uniswap',
141    queries: {
142      oneDayTotalFees: getUniswapV2(true),
143      oneDayTotalVolumeUSD: getUniswapV2(),
144    },
145    metadata: {
146      ...metadata,
147      subtitle: 'V2',
148      protocolLaunch: '2020-05-04',
149    },
150  });
151
152  sdk.register({
153    id: 'uniswap-v3',
154    bundle: 'uniswap',
155    queries: {
156      oneDayTotalFees: uniV3Adapter('ianlapham/uniswap-v3-subgraph', true),
157      oneDayTotalVolumeUSD: uniV3Adapter('ianlapham/uniswap-v3-subgraph'),
158    },
159    metadata: {
160      ...metadata,
161      subtitle: 'V3',
162      protocolLaunch: '2021-05-05',
163    },
164  });
165
166  sdk.register({
167    id: 'uniswap-optimism',
168    bundle: 'uniswap',
169    queries: {
170      oneDayTotalFees: uniV3Adapter('ianlapham/optimism-post-regenesis', true),
171      oneDayTotalVolumeUSD: uniV3Adapter('ianlapham/optimism-post-regenesis'),
172    },
173    metadata: {
174      ...metadata,
175      subtitle: 'Optimism',
176      blockchain: 'Optimism',
177      protocolLaunch: '2021-11-11',
178    },
179  });
180
181  sdk.register({
182    id: 'uniswap-arbitrum',
183    bundle: 'uniswap',
184    queries: {
185      oneDayTotalFees: uniV3Adapter('ianlapham/arbitrum-minimal', true),
186      oneDayTotalVolumeUSD: uniV3Adapter('ianlapham/arbitrum-minimal'),
187    },
188    metadata: {
189      ...metadata,
190      subtitle: 'Arbitrum One',
191      blockchain: 'Arbitrum One',
192      protocolLaunch: '2021-08-31',
193    },
194  });
195
196  sdk.register({
197    id: 'uniswap-polygon',
198    bundle: 'uniswap',
199    queries: {
200      oneDayTotalFees: uniV3Adapter('ianlapham/uniswap-v3-polygon', true),
201      oneDayTotalVolumeUSD: uniV3Adapter('ianlapham/uniswap-v3-polygon'),
202    },
203    metadata: {
204      ...metadata,
205      subtitle: 'Polygon',
206      blockchain: 'Polygon',
207      protocolLaunch: '2021-12-21',
208    },
209  });
210
211  sdk.registerBundle('uniswap', metadata);
212}
213

It's something off?

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

Adapter Info

Version

1.0.2

License

MIT

IPFS CID

QmUfQCAvFTTZwWgxRiPYepWptcekhHkDdhFKWFxA3a4dFn

CID (source)

QmSrf27QWrGuPXQWmNRSMsTL3B2hEDhZd1HvJ4c9ESM6sq

Collections

Author

mihal.eth