Adapter

Chainlink

Chainlink decentralized oracle networks provide tamper-proof inputs, outputs, and computations to support advanced smart contracts on any blockchain.

Sub-Adapters 1

Preview and test each sub adapter.

Chainlink (chainlink)

Metadata

ID
chainlink
name

"Chainlink"

icon
category

"Network Protocol"

subcategory

"Oracle"

description

"Chainlink decentralized oracle networks provide tamper-proof inputs, outputs, and computations to support advanced smart contracts on any blockchain."

feeDescription

"LINK payments are made to node operators."

blockchain

"Ethereum"

source

"Powered by Market.Link"

protocolWebsite

"https://chain.link/"

sourceWebsite

"https://market.link/"

protocolLaunch

"2019-05-30"

tokenTicker

"LINK"

tokenCoingecko

"chainlink"

events

[]

Queries

Adapter Code

Check the entire code written for the Adapter.

Source code

Showing TS source.
1export const name = 'Chainlink';
2export const version = '1.2.0';
3export const license = 'MIT';
4
5interface DailyRevenue {
6  date: string; 
7  revenueT30d: number;
8}
9
10
11export function setup(sdk: Context) {
12
13  // Source: https://bobbyhadz.com/blog/javascript-get-all-dates-between-two-dates
14  const getDatesInRange = (startDate: string, endDate: string) => {
15    
16    const dateStart = new Date(startDate);
17    const dateEnd = new Date(endDate);
18
19    const dates = [];
20
21    while (dateStart <= dateEnd) {
22      dates.push(new Date(dateStart).toISOString().split('T')[0]);
23      dateStart.setDate(dateStart.getDate() + 1);
24    }
25
26    return dates;
27  }
28
29  const getFees = async (startDate: string, endDate: string): Promise<number> => {
30    const url = 'https://api.market.link/v1/metrics/';
31    const body = `
32      SELECT
33        sum(link_rewards."link_rewards") as "link_rewards",
34        sum(link_rewards."link_rewards" * link_usd."link_answer") as "link_rewards_usd"
35
36      FROM (
37        SELECT "interval" as time,
38          coalesce(sum(link_reward_sum), 0) as "link_rewards"
39        FROM feed_aggregates_by_1d
40        WHERE 
41          network_name = 'Mainnet' 
42          AND "interval" BETWEEN \'${startDate}\' AND \'${endDate}\'
43        GROUP BY time
44        ORDER BY time DESC
45      ) link_rewards
46
47      LEFT JOIN (
48          SELECT
49            "interval" as time,
50            last(answer, interval) as link_answer 
51          FROM
52            feed_answers_by_1d 
53          WHERE
54            feed_name = 'LINK / USD' 
55            AND network_name = 'Mainnet' 
56            AND "interval" BETWEEN \'${startDate}\' AND \'${endDate}\' 
57          GROUP BY
58            "interval" 
59          ORDER BY
60            "interval"
61        ) link_usd ON link_rewards.time = link_usd.time 
62      `;
63
64    let options = {
65      method: 'POST',
66      headers: {
67        'Content-Type': 'text/plain'
68      },
69      body: body
70    };
71
72
73    const data = await sdk.http.get(url, options);
74    const fees = data[0].link_rewards_usd;
75
76    return fees;
77  }
78
79  const getOneDayFees = (date: string) => {
80    const nextDay = sdk.date.offsetDaysFormatted(date, 1);
81    return getFees(date, nextDay);
82  }  
83
84
85  const getOneDayTotalFees = (date: string) => {
86    const nextDay = sdk.date.offsetDaysFormatted(date, 1);
87    return getFees(date, nextDay);
88  };  
89
90  const getT30DFees = async (date: string) => {
91    const prior30Day = sdk.date.offsetDaysFormatted(date, -30);
92    let revenueT30d = await getFees(prior30Day, date);
93    revenueT30d = revenueT30d === null ? 0 : revenueT30d;
94    return {date: date, shortdate: date, revenueT30d: revenueT30d };
95  }; 
96
97
98  const getFeesByDay = async (startDate: string, endDate: string) => {
99
100    let dailyRevenues = [];
101    let dates = getDatesInRange(startDate, endDate);
102   
103    for (let i = 0; i < dates.length; i++) {
104      let revenue = getT30DFees(dates[i]);
105      dailyRevenues.push(revenue);
106    }
107
108    return await Promise.all(dailyRevenues)
109      .then((dailyRevenues) => {
110        return dailyRevenues;
111      });
112  };
113
114  const getDailyData = async (startDate: string, endDate: string): Promise<Array<number>> => {
115
116    const url = 'https://api.market.link/v1/metrics/';
117    const body = `
118      SELECT
119          link_rewards.time as date,
120          sum(link_rewards."link_rewards") as "link_rewards",
121          sum(link_rewards."link_rewards" * link_usd."link_answer") as "link_rewards_usd"
122
123      FROM 
124
125      (
126            SELECT 
127              "interval" as time,
128              network_group,     
129              coalesce(sum(link_reward_sum), 0) as "link_rewards"
130            FROM feed_aggregates_by_1d
131            WHERE 
132              network_name = 'Mainnet'
133          AND "interval" BETWEEN \'${startDate}\' AND \'${endDate}\'
134
135            GROUP BY time, network_group
136            ORDER BY time, network_group DESC
137      ) link_rewards
138
139          LEFT JOIN
140            (
141              SELECT
142                "interval" as time,
143                last(answer, interval) as link_answer 
144              FROM
145                feed_answers_by_1d 
146              WHERE
147                feed_name = 'LINK / USD' 
148                AND network_name = 'Mainnet' 
149          AND "interval" BETWEEN \'${startDate}\' AND \'${endDate}\'
150              GROUP BY
151                "interval" 
152              ORDER BY
153                "interval"
154              ) link_usd ON link_rewards.time = link_usd.time 
155
156      WHERE link_rewards.time >= \'${startDate}\'
157
158      GROUP BY
159          date
160
161      `;
162
163    let options = {
164      method: 'POST',
165      headers: {
166        'Content-Type': 'text/plain'
167      },
168      body: body
169    };
170
171
172    const data = await sdk.http.get(url, options);
173    const fees = data.map((day) => {
174        return {date: day.date.slice(0, 10), revenue: day.link_rewards_usd}
175    });
176
177    return fees;
178  }  
179
180  sdk.register({
181    id: 'chainlink',
182    queries: {
183      oneDayTotalFees: getOneDayFees,
184      dateRangeTotalFees: getFees,
185      dateRangeFeesByDay: getFeesByDay,
186      dateRangeDailyRevenues: getDailyData,
187    },
188    metadata: {
189      name: 'Chainlink',
190      icon: sdk.ipfs.getDataURILoader('QmUdtBEt4zaex8NYA5St9iSdnq6JKtJw2ArHpyr49W4XuL', 'image/png'),
191      category: 'Network Protocol',
192      subcategory: 'Oracle',
193      description: 'Chainlink decentralized oracle networks provide tamper-proof inputs, outputs, and computations to support advanced smart contracts on any blockchain.',
194      feeDescription: 'LINK payments are made to node operators.',
195      blockchain: 'Ethereum',
196      source: 'Powered by Market.Link',
197      protocolWebsite: 'https://chain.link/',
198      sourceWebsite: 'https://market.link/',
199      protocolLaunch: '2019-05-30', 
200      tokenTicker: 'LINK',
201      tokenCoingecko: 'chainlink', 
202      events: [
203      ],
204    },
205  })
206}
207

It's something off?

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

Adapter Info

Version

1.2.0

License

MIT

IPFS CID

QmfJ8joY4GRUiHJRKzrXek2ycRetEfnXy6AMJLNSLaVWzB

CID (source)

QmcGYWhAq89iYaCXynqVsoViDBpfjc17e58frBv4WWU784

Collections

Author

0x1a171a91B4Aa1A669e2397D6670746DDcDd4fbBe