Sub-Adapters 2

Preview and test each sub adapter.

Synthetix - Synthetix Optimism (synthetix-optimism)

Synthetix - 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.1.1';
3export const license = 'MIT';
4
5export function setup(sdk: Context) {
6
7  // const getFeesL1 = async (date: string): Promise<number> => {
8  //   const currentDayTimestamp = sdk.date.dateToTimestamp(date);
9  //   const nextDayTimestamp = sdk.date.dateToTimestamp(sdk.date.offsetDaysFormatted(date, 1));
10  //   const twoDaysAhead = sdk.date.dateToTimestamp(sdk.date.offsetDaysFormatted(date, 1));
11    
12  //   const queryExchange = `
13  //     query Totals($timestamp: BigInt) {
14  //       totals(subgraphError:deny, first: 2, orderBy: timestamp, orderDirection: asc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_gte: $timestamp, product: "exchange" }) {
15  //         totalFeesGeneratedInUSD
16  //       }
17  //     }`;
18
19  //   const exchangeDataCurrent = await sdk.graph.query(
20  //     'synthetixio-team/mainnet-main',
21  //     queryExchange,
22  //     { timestamp: currentDayTimestamp },
23  //   );
24
25  //   const exchangeDataNext = await sdk.graph.query(
26  //     'synthetixio-team/mainnet-main',
27  //     queryExchange,
28  //     { timestamp: nextDayTimestamp },
29  //   );
30
31  //   const queryAtomic = `
32  //     query AtomicSynthExchange($currentDayTimestamp: BigInt, $nextDayTimestamp: BigInt) {
33  //       atomicSynthExchanges(where: { timestamp_gt: $currentDayTimestamp, timestamp_lt: $nextDayTimestamp }) {
34  //         feesInUSD
35  //       }
36  //   }`
37
38  //   const atomicDataCurrent = await sdk.graph.query(
39  //     'synthetixio-team/mainnet-main',
40  //     queryAtomic,
41  //     { currentDayTimestamp, nextDayTimestamp },
42  //   );
43
44  //   const atomicDataNext = await sdk.graph.query(
45  //     'synthetixio-team/mainnet-main',
46  //     queryAtomic,
47  //     { currentDayTimestamp: nextDayTimestamp, nextDayTimestamp: twoDaysAhead },
48  //   );
49
50  //   // If this condition is true we have not completed the current reporting period
51  //   // it is necessary to return null here to prevent cached results
52  //   if ((!atomicDataCurrent || !atomicDataNext) || (exchangeDataNext.totals.length === 0 && atomicDataNext.atomicSynthExchanges.length === 0)) {
53  //     return null
54  //   }
55
56  //   let fees = 0
57    
58  //   if (exchangeDataCurrent.totals.length > 0) {
59  //     fees += exchangeDataCurrent.totals.reduce(
60  //       (accumulator, currentValue) => accumulator + parseInt(currentValue.totalFeesGeneratedInUSD),
61  //       0
62  //     );
63  //   }
64
65  //   if (atomicDataCurrent.atomicSynthExchanges.length > 0) {
66  //     fees += atomicDataCurrent.atomicSynthExchanges.reduce(
67  //       (accumulator, currentValue) => accumulator + parseInt(currentValue.feesInUSD),
68  //       0
69  //     );
70  //   }
71
72  //   return fees;
73  // }
74
75
76  const getFeesL2 = async (date: string): Promise<number> => {
77    const currentDayTimestamp = sdk.date.dateToTimestamp(date);
78    const nextDayTimestamp = sdk.date.dateToTimestamp(sdk.date.offsetDaysFormatted(date, 1));
79
80    
81    const queryExchange = `
82      query Totals($timestamp: BigInt) {
83        totals(subgraphError:deny, first: 2, orderBy: timestamp, orderDirection: asc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_gte: $timestamp, product: "exchange" }) {
84          totalFeesGeneratedInUSD
85        }
86      }`;
87
88    const exchangeDataCurrent = await sdk.graph.query(
89      'synthetixio-team/optimism-main',
90      queryExchange,
91      { timestamp: currentDayTimestamp },
92    );
93
94    const exchangeDataNext = await sdk.graph.query(
95      'synthetixio-team/optimism-main',
96      queryExchange,
97      { timestamp: nextDayTimestamp },
98    );
99
100    if (!exchangeDataNext.totals || exchangeDataNext.totals.length === 0) {
101      return null
102    }
103    
104    let fees = 0
105
106    if (exchangeDataCurrent.totals.length > 0) {
107      fees += exchangeDataCurrent.totals.reduce(
108        (accumulator, currentValue) => accumulator + parseInt(currentValue.totalFeesGeneratedInUSD),
109        0
110      );
111    }
112
113    return fees
114  }
115  
116  const getFeesPerpsV2 = async (date: string): Promise<number> => {
117    const previous = sdk.date.getPreviousDay(date);
118
119    const data = await sdk.graph.query('synthetix-perps/perps', `
120      query DailyStats($yesterdayTimestamp: String, $todayTimestamp: String) {
121        dailyStatsToday: dailyStats(subgraphError:deny, where: { day: $yesterdayTimestamp }) {
122          fees
123        }
124        dailyStatsTomorrow: dailyStats(subgraphError: deny, where: { day: $todayTimestamp }) {
125          fees
126        }
127      }`
128    , {
129      yesterdayTimestamp: previous,
130      todayTimestamp: date
131    });
132
133    // Only return stats when the reporting period is completed (stats exist for tomorrow)
134    if (data.dailyStatsTomorrow.length === 0 || data.dailyStatsToday.length === 0) {
135      return null
136    } else {
137
138      return data.dailyStatsToday.reduce(
139        (accumulator, currentValue) => accumulator + parseInt(currentValue.fees) / 1e18,
140        0
141      )
142    }
143  }
144
145  const metadata = {
146      name: 'Synthetix',
147      bundle: 'synthetix',
148      icon: sdk.ipfs.getDataURILoader('QmVccgm2dm5QfvutX2oqLbp3gQiv4jFY4drvar1EKb7j68', 'image/svg+xml'),
149      category: 'Derivatives',
150      description: 'Synthetix is a decentralised derivative liquidity protocol powering spot and synthetic assets.',
151      feeDescription: 'Fees are paid to SNX token stakers.',
152      source: 'The Graph Protocol',
153      website: 'https://synthetix.io',
154      tokenTicker: 'SNX',
155      tokenCoingecko: 'havven', 
156      protocolLaunch: '2018-06-08',
157  }
158
159  // sdk.register({
160  //   id: 'synthetix-mainnet',
161  //   bundle: 'synthetix',
162  //   queries: {
163  //     oneDayTotalFees: getFeesL1,
164  //   },
165  //   metadata: {
166  //     ...metadata,
167  //     subtitle: 'Synthetix Mainnet',
168  //     blockchain: 'Ethereum'
169  //   },
170  // })
171
172  sdk.register({
173    id: 'synthetix-optimism',
174    bundle: 'synthetix',
175    queries: {
176      oneDayTotalFees: getFeesL2,
177    },
178    metadata: {
179      ...metadata,
180      subtitle: 'Synthetix Optimism',
181      blockchain: 'Optimism'
182    },
183  })
184
185  sdk.register({
186    id: 'synthetix-optimism-perpsV2',
187    bundle: 'synthetix',
188    queries: {
189      oneDayTotalFees: getFeesPerpsV2,
190    },
191    metadata: {
192      ...metadata,
193      subtitle: 'Synthetix Optimism Perpetual Futures',
194      blockchain: 'Optimism'
195    },
196  })
197
198  sdk.registerBundle('synthetix', metadata);
199}
200

It's something off?

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

Adapter Info

Version

1.1.1

License

MIT

IPFS CID

Qmc6u6vhw4DiQPyxXo1T2o5i8G9xr5gfctcv4nh6KzWWhA

CID (source)

QmVppmy2rdVso8zVTxzCwNZ2A7arT2EUhMoZRKd5525FBZ

Collections

Author

jmzwar.eth