Sub-Adapters 3

Preview and test each sub adapter.

Synthetix - Ethereum (synthetix-ethereum)

Synthetix - Optimism (synthetix-optimism)

Synthetix - Optimism Perpetual Futures (synthetix-optimism-futures)

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Synthetix';
2export const version = '1.0.2';
3export const license = 'MIT';
4
5interface NetInfo {
6  subgraph: string;
7  blockchain: string;
8  protocolLaunch: string;
9}
10
11const networks: { [network: string]: NetInfo } = {
12  ethereum: {
13    subgraph: 'synthetixio-team/mainnet-main',
14    blockchain: 'Ethereum',
15    protocolLaunch: '2018-06-08',
16  },
17  optimism: {
18    subgraph: 'synthetixio-team/optimism-main',
19    blockchain: 'Optimism',
20    protocolLaunch: '2021-11-11',
21  }
22}
23
24export function setup(sdk: Context) {
25  
26  const createFeeDataQuery = (subgraph: string, product: string) => async (date: string): Promise<number> => {
27
28    const startDateTimestamp = Math.floor(sdk.date.dateToTimestamp(date));
29    
30    const graphQuery = `query totals($startDateTimestamp: Int!, $product: String!){
31      totals(first: 2, orderBy: timestamp, orderDirection: asc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_gte: $startDateTimestamp, product: $product }) {
32        totalFeesGeneratedInUSD
33      }
34    }`;
35
36    const data = await sdk.graph.query(
37      subgraph,
38      graphQuery,
39      { startDateTimestamp, product },
40    );
41
42    if(data.totals.length < 2){
43      return null
44    }
45
46    return Number(data.totals[0].totalFeesGeneratedInUSD);
47  }
48
49  /*
50    Synthetix's subgraphs don't currently update an all-time aggregation on every exchange due to indexing efficiency concerns.
51    Here we add the values of all the the daily aggregations between the startDate and endDate.
52  */
53  const createFeeRangeQuery = (subgraph: string, product: string) => async (startDate: string, endDate: string): Promise<number> => {
54
55    const startDateTimestamp = Math.floor(sdk.date.dateToTimestamp(startDate));
56    const endDateTimestamp = Math.floor(sdk.date.dateToTimestamp(endDate));
57
58    if(endDateTimestamp - startDateTimestamp > 8.64e7){
59      throw('Cannot query a range greater than 1,000 days.');
60    }
61
62    const graphQuery = `query totals($startDateTimestamp: Int!, $endDateTimestamp: Int!, $product: String!){
63      totals(first: 1000, orderBy: timestamp, orderDirection: desc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_lte: $endDateTimestamp, timestamp_gte: $startDateTimestamp, product: $product }) {
64        totalFeesGeneratedInUSD
65      }
66    }`;
67
68    const data = await sdk.graph.query(
69      subgraph,
70      graphQuery,
71      { startDateTimestamp, endDateTimestamp, product },
72    );
73
74    return data.totals.reduce((accumulator, dailyTotal) => {
75      return accumulator + Number(dailyTotal.totalFeesGeneratedInUSD)
76    }, 0);
77  }
78
79  const metadata = {
80    icon: sdk.ipfs.getDataURILoader('QmYPqFXTqYcynD5hT9sZbsoPZXbvjSfL7WWQPL7EwYAyE5', 'image/svg+xml'),
81    category: 'dex',
82    name: 'Synthetix',
83    description: 'The Synthetix Exchange is a decentralized exchange for trading synthetic assets',
84    feeDescription: 'Trading fees are paid by users to SNX stakers',
85    source: 'The Graph Protocol',
86    tokenTicker: 'SNX',
87    tokenCoingecko: 'havven',
88    protocolLaunch: '2018-06-08',
89    website: 'https://synthetix.io',
90  };
91
92  sdk.registerBundle('synthetix', metadata);
93
94  Object.entries(networks).map(([network, { subgraph, blockchain, protocolLaunch }]: [string, NetInfo]) => {
95    sdk.register({
96      id: `synthetix-${network}`,
97      bundle: 'synthetix',
98      queries: {
99        oneDayTotalFees: createFeeDataQuery(subgraph, 'exchange'),
100        oneDayProtocolFees: createFeeDataQuery(subgraph, 'exchange'),
101        oneDayTotalVolumeUSD: createFeeDataQuery(subgraph, 'exchange'),
102        dateRangeTotalFees: createFeeRangeQuery(subgraph, 'exchange'),
103        dateRangeProtocolFees: createFeeRangeQuery(subgraph, 'exchange'),
104      },
105      metadata: {
106        ...metadata,
107        subtitle: blockchain,
108        blockchain,
109        protocolLaunch,
110      },
111    })
112  
113  if(network == 'optimism'){
114    sdk.register({
115      id: `synthetix-${network}-futures`,
116      bundle: 'synthetix',
117      queries: {
118        oneDayTotalFees: createFeeDataQuery(subgraph, 'futures'),
119        oneDayProtocolFees: createFeeDataQuery(subgraph, 'futures'),
120        oneDayTotalVolumeUSD: createFeeDataQuery(subgraph, 'futures'),
121        dateRangeTotalFees: createFeeRangeQuery(subgraph, 'futures'),
122        dateRangeProtocolFees: createFeeRangeQuery(subgraph, 'futures'),
123      },
124      metadata: {
125        ...metadata,
126        subtitle: blockchain + ' Perpetual Futures',
127        blockchain,
128        protocolLaunch,
129      },
130    })
131  }
132  
133  })
134
135}

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

QmQC3XsUtkWDktXexUPMg7dEmmQihEiQaRuQ3Ms6WUY7nn

CID (source)

QmR58ZJVoTPj4xho1zCwWwRBVTnW4v1FhHZcFDCfcTUce7

Collections

Author

noahlitvin.eth