Sub-Adapters 3
Preview and test each sub adapter.
Synthetix Mainnet (synthetix-mainnet)
Synthetix Optimism (synthetix-optimism)
Synthetix Optimism Perpetual Futures (synthetix-optimism-perpsV2)
Adapter Code
Check the entire code written for the Adapter.
Source code
Showing TS source.
1export const name = 'Synthetix';
2export const version = '1.0.4';
3export const license = 'MIT';
4
5function flatten(arr, depth = 1) {
6 return arr.reduce(function (acc, val) {
7 return acc.concat(Array.isArray(val) ? flatten(val, depth - 1) : val);
8 }, []);
9};
10
11export function setup(sdk: Context) {
12
13 const getFeesL1 = async (date: string): Promise<number> => {
14 const currentDayTimestamp = sdk.date.dateToTimestamp(date);
15 const nextDayTimestamp = sdk.date.dateToTimestamp(sdk.date.offsetDaysFormatted(date, 1))
16
17 const queryExchange = `
18 query Totals($timestamp: BigInt) {
19 totals(subgraphError:deny, first: 2, orderBy: timestamp, orderDirection: asc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_gte: $timestamp, product: "exchange" }) {
20 totalFeesGeneratedInUSD
21 }
22 }`;
23
24 const exchangeData = await sdk.graph.query(
25 'synthetixio-team/mainnet-main',
26 queryExchange,
27 { timestamp: currentDayTimestamp },
28 );
29
30 const queryAtomic = `
31 query AtomicSynthExchange($currentDayTimestamp: BigInt, $nextDayTimestamp: BigInt) {
32 atomicSynthExchanges(where: { timestamp_gt: $currentDayTimestamp, timestamp_lt: $nextDayTimestamp }) {
33 feesInUSD
34 }
35 }`
36
37 const atomicData = await sdk.graph.query(
38 'synthetixio-team/mainnet-main',
39 queryAtomic,
40 { currentDayTimestamp, nextDayTimestamp },
41 );
42
43 let fees = 0
44
45 if (exchangeData.totals.length > 0) {
46 fees += exchangeData.totals.reduce(
47 (accumulator, currentValue) => accumulator + parseInt(currentValue.totalFeesGeneratedInUSD),
48 0
49 );
50 }
51
52 if (atomicData.atomicSynthExchanges.length > 0) {
53 fees += atomicData.atomicSynthExchanges.reduce(
54 (accumulator, currentValue) => accumulator + parseInt(currentValue.feesInUSD),
55 0
56 );
57 }
58
59 return fees;
60 }
61
62
63 const getFeesL2 = async (date: string): Promise<number> => {
64 const currentDayTimestamp = sdk.date.dateToTimestamp(date);
65
66 const queryExchange = `
67 query Totals($timestamp: BigInt) {
68 totals(subgraphError:deny, first: 2, orderBy: timestamp, orderDirection: asc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_gte: $timestamp, product: "exchange" }) {
69 totalFeesGeneratedInUSD
70 }
71 }`;
72
73 const exchangeData = await sdk.graph.query(
74 'synthetixio-team/optimism-main',
75 queryExchange,
76 { timestamp: currentDayTimestamp },
77 );
78
79 let fees = 0
80
81 if (exchangeData.totals.length > 0) {
82 fees += exchangeData.totals.reduce(
83 (accumulator, currentValue) => accumulator + parseInt(currentValue.totalFeesGeneratedInUSD),
84 0
85 );
86 }
87
88 return fees
89 }
90
91 const getFeesPerpsV2 = async (date: string): Promise<number> => {
92 const currentDayTimestamp = sdk.date.dateToTimestamp(date);
93 const nextDayTimestamp = sdk.date.dateToTimestamp(sdk.date.offsetDaysFormatted(date, 1))
94
95 const getData = async (resolve, reject, skip, totalData): Promise<any[]> => {
96 const query = `
97 query PerpsV2Fees($currentDayTimestamp: BigInt, $nextDayTimestamp: BigInt, $skip: Int) {
98 futuresTrades(subgraphError:deny, where:{timestamp_gt: $currentDayTimestamp, timestamp_lt: $nextDayTimestamp }, first: 1000, skip: $skip) {
99 feesPaidToSynthetix
100 }
101 }`;
102
103 const data = await sdk.graph.query('fritzschoff/perps-dashboard', query, {
104 nextDayTimestamp,
105 currentDayTimestamp,
106 skip
107 });
108
109 skip += 1000;
110
111 if (data.futuresTrades.length !== 0) {
112 totalData.push(data.futuresTrades);
113 getData(resolve, reject, skip, totalData);
114 } else {
115 return resolve(totalData);
116 }
117 };
118
119 const totalData = await new Promise((r, j) => getData(r, j, 0, []));
120
121 const fees = flatten(totalData, 1)
122 .map((item) => parseInt(item.feesPaidToSynthetix) / 1e18)
123 .reduce((accumulator, current) => {
124 return accumulator + current;
125 }, 0);
126
127 return fees
128 }
129
130
131 const metadata = {
132 name: 'Synthetix',
133 icon: sdk.ipfs.getDataURILoader('QmVccgm2dm5QfvutX2oqLbp3gQiv4jFY4drvar1EKb7j68', 'image/svg+xml'),
134 category: 'Derivatives',
135 description: 'Synthetix is a decentralised derivative liquidity protocol powering spot and synthetic assets.',
136 feeDescription: 'Fees are paid to SNX token stakers.',
137 source: 'The Graph Protocol',
138 website: 'https://synthetix.io',
139 tokenTicker: 'SNX',
140 tokenCoingecko: 'havven',
141 protocolLaunch: '2018-06-08',
142 }
143
144 sdk.register({
145 id: 'synthetix-mainnet',
146 queries: {
147 oneDayTotalFees: getFeesL1,
148 },
149 metadata: {
150 ...metadata,
151 name: 'Synthetix Mainnet',
152 blockchain: 'Ethereum'
153 },
154 })
155
156 sdk.register({
157 id: 'synthetix-optimism',
158 queries: {
159 oneDayTotalFees: getFeesL2,
160 },
161 metadata: {
162 ...metadata,
163 name: 'Synthetix Optimism',
164 blockchain: 'Optimism'
165 },
166 })
167
168 sdk.register({
169 id: 'synthetix-optimism-perpsV2',
170 queries: {
171 oneDayTotalFees: getFeesPerpsV2,
172 },
173 metadata: {
174 ...metadata,
175 name: 'Synthetix Optimism Perpetual Futures',
176 blockchain: 'Optimism'
177 },
178 })
179}
180
It's something off?
Report it to the discussion board on Discord, we will take care of it.