Sub-Adapters 2

Preview and test each sub adapter.

Synthetix - Ethereum (synthetix-ethereum)

Synthetix - Optimism (synthetix-optimism)

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Synthetix';
2export const version = '1.0.0';
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) => async (date: string): Promise<number> => {
27
28    const dateTimestamp = Math.floor(sdk.date.dateToTimestamp(date));
29    
30    const graphQuery = `query totals($dateTimestamp: Int!){
31      totals(first: 1, orderBy: timestamp, orderDirection: desc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_lte: $dateTimestamp }) {
32        totalFeesGeneratedInUSD
33      }
34    }`;
35
36    const data = await sdk.graph.query(
37      subgraph,
38      graphQuery,
39      { dateTimestamp },
40    );
41
42    return Number(data.totals[0].totalFeesGeneratedInUSD);
43  }
44
45  /*
46    Synthetix's subgraphs don't currently update an all-time aggregation on every exchange due to indexing efficiency concerns.
47    Here we add the values of all the the daily aggregations between the startDate and endDate.
48  */
49  const createFeeRangeQuery = (subgraph: string) => async (startDate: string, endDate: string): Promise<number> => {
50
51    const startDateTimestamp = Math.floor(sdk.date.dateToTimestamp(startDate));
52    const endDateTimestamp = Math.floor(sdk.date.dateToTimestamp(endDate));
53
54    if(endDateTimestamp - startDateTimestamp > 8.64e7){
55      throw('Cannot query a range greater than 1,000 days.');
56    }
57
58    const graphQuery = `query totals($startDateTimestamp: Int!, $endDateTimestamp: Int!){
59      totals(first: 1000, orderBy: timestamp, orderDirection: desc, where: { period: 86400, bucketMagnitude: 0, synth: null, timestamp_lte: $endDateTimestamp, , timestamp_gte: $startDateTimestamp }) {
60        totalFeesGeneratedInUSD
61      }
62    }`;
63
64    const data = await sdk.graph.query(
65      subgraph,
66      graphQuery,
67      { startDateTimestamp, endDateTimestamp },
68    );
69
70    return data.totals.reduce((accumulator, dailyTotal) => {
71      return accumulator + Number(dailyTotal.totalFeesGeneratedInUSD)
72    }, 0);
73  }
74
75  const metadata = {
76    icon: sdk.ipfs.getDataURILoader('QmYPqFXTqYcynD5hT9sZbsoPZXbvjSfL7WWQPL7EwYAyE5', 'image/svg+xml'),
77    category: 'dex',
78    name: 'Synthetix',
79    description: 'The Synthetix Exchange is a decentralized exchange for trading synthetic assets',
80    feeDescription: 'Trading fees are paid by users to SNX stakers',
81    source: 'The Graph Protocol',
82    tokenTicker: 'SNX',
83    tokenCoingecko: 'havven',
84    protocolLaunch: '2018-06-08',
85    website: 'https://synthetix.io',
86  };
87
88  sdk.registerBundle('synthetix', metadata);
89
90  Object.entries(networks).map(([network, { subgraph, blockchain, protocolLaunch }]: [string, NetInfo]) => {
91    sdk.register({
92      id: `synthetix-${network}`,
93      bundle: 'synthetix',
94      queries: {
95        oneDayTotalFees: createFeeDataQuery(subgraph),
96        oneDayProtocolFees: createFeeDataQuery(subgraph),
97        oneDayTotalVolumeUSD: createFeeDataQuery(subgraph),
98        dateRangeTotalFees: createFeeRangeQuery(subgraph),
99        dateRangeProtocolFees: createFeeRangeQuery(subgraph),
100      },
101      metadata: {
102        ...metadata,
103        subtitle: blockchain,
104        blockchain,
105        protocolLaunch,
106      },
107    })
108  })
109}

It's something off?

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

Adapter Info

Version

1.0.0

License

MIT

IPFS CID

QmWU45rhdmomuNKttMSfCEeiVM7RTX6acepdCobv2e91JB

CID (source)

QmYYGNh3cDJ9JMY1iC8PrvdbzNnTG7PBVH2q1PZJsmgcv4

Author

noahlitvin.eth