@strkfarm/sdk 1.0.6 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -37,6 +37,7 @@ declare class Pricer {
37
37
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
38
38
  */
39
39
  protected PRICE_API: string;
40
+ protected client: any;
40
41
  constructor(config: IConfig, tokens: TokenInfo[]);
41
42
  isReady(): boolean;
42
43
  waitTillReady(): Promise<void>;
@@ -45,6 +46,9 @@ declare class Pricer {
45
46
  assertNotStale(timestamp: Date, tokenName: string): void;
46
47
  getPrice(tokenName: string): Promise<PriceInfo>;
47
48
  protected _loadPrices(onUpdate?: (tokenSymbol: string) => void): void;
49
+ _getPrice(token: TokenInfo): Promise<number>;
50
+ _getPriceCoinbase(token: TokenInfo): Promise<number>;
51
+ _getPriceCoinMarketCap(token: TokenInfo): Promise<number>;
48
52
  }
49
53
 
50
54
  declare class Pragma {
package/dist/index.js CHANGED
@@ -168,6 +168,7 @@ var Global = class {
168
168
  };
169
169
 
170
170
  // src/modules/pricer.ts
171
+ var CoinMarketCap = require("coinmarketcap-api");
171
172
  var Pricer = class {
172
173
  constructor(config, tokens) {
173
174
  this.tokens = [];
@@ -176,6 +177,8 @@ var Pricer = class {
176
177
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
177
178
  */
178
179
  this.PRICE_API = `https://api.coinbase.com/v2/prices/{{PRICER_KEY}}/buy`;
180
+ // backup oracle
181
+ this.client = new CoinMarketCap(process.env.COINMARKETCAP_KEY);
179
182
  this.config = config;
180
183
  this.tokens = tokens;
181
184
  }
@@ -239,13 +242,7 @@ var Pricer = class {
239
242
  onUpdate(token.symbol);
240
243
  return;
241
244
  }
242
- if (!token.pricerKey) {
243
- throw new FatalError(`Pricer key not found for ${token.name}`);
244
- }
245
- const url = this.PRICE_API.replace("{{PRICER_KEY}}", token.pricerKey);
246
- const result = await import_axios.default.get(url);
247
- const data = result.data;
248
- const price = Number(data.data.amount);
245
+ const price = await this._getPrice(token);
249
246
  this.prices[token.symbol] = {
250
247
  price,
251
248
  timestamp: /* @__PURE__ */ new Date()
@@ -272,6 +269,30 @@ var Pricer = class {
272
269
  });
273
270
  }
274
271
  }
272
+ async _getPrice(token) {
273
+ try {
274
+ return await this._getPriceCoinbase(token);
275
+ } catch (error) {
276
+ }
277
+ try {
278
+ return await this._getPriceCoinMarketCap(token);
279
+ } catch (error) {
280
+ }
281
+ throw new FatalError(`Price not found for ${token.name}`);
282
+ }
283
+ async _getPriceCoinbase(token) {
284
+ if (!token.pricerKey) {
285
+ throw new FatalError(`Pricer key not found for ${token.name}`);
286
+ }
287
+ const url = this.PRICE_API.replace("{{PRICER_KEY}}", token.pricerKey);
288
+ const result = await import_axios.default.get(url);
289
+ const data = result.data;
290
+ return Number(data.data.amount);
291
+ }
292
+ async _getPriceCoinMarketCap(token) {
293
+ const result = await this.client.getQuotes({ symbol: token.symbol });
294
+ return result.data[token.symbol].quote.USD.price;
295
+ }
275
296
  };
276
297
 
277
298
  // src/modules/pragma.ts
package/dist/index.mjs CHANGED
@@ -1,3 +1,10 @@
1
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
2
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
3
+ }) : x)(function(x) {
4
+ if (typeof require !== "undefined") return require.apply(this, arguments);
5
+ throw Error('Dynamic require of "' + x + '" is not supported');
6
+ });
7
+
1
8
  // src/modules/pricer.ts
2
9
  import axios from "axios";
3
10
 
@@ -114,6 +121,7 @@ var Global = class {
114
121
  };
115
122
 
116
123
  // src/modules/pricer.ts
124
+ var CoinMarketCap = __require("coinmarketcap-api");
117
125
  var Pricer = class {
118
126
  constructor(config, tokens) {
119
127
  this.tokens = [];
@@ -122,6 +130,8 @@ var Pricer = class {
122
130
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
123
131
  */
124
132
  this.PRICE_API = `https://api.coinbase.com/v2/prices/{{PRICER_KEY}}/buy`;
133
+ // backup oracle
134
+ this.client = new CoinMarketCap(process.env.COINMARKETCAP_KEY);
125
135
  this.config = config;
126
136
  this.tokens = tokens;
127
137
  }
@@ -185,13 +195,7 @@ var Pricer = class {
185
195
  onUpdate(token.symbol);
186
196
  return;
187
197
  }
188
- if (!token.pricerKey) {
189
- throw new FatalError(`Pricer key not found for ${token.name}`);
190
- }
191
- const url = this.PRICE_API.replace("{{PRICER_KEY}}", token.pricerKey);
192
- const result = await axios.get(url);
193
- const data = result.data;
194
- const price = Number(data.data.amount);
198
+ const price = await this._getPrice(token);
195
199
  this.prices[token.symbol] = {
196
200
  price,
197
201
  timestamp: /* @__PURE__ */ new Date()
@@ -218,6 +222,30 @@ var Pricer = class {
218
222
  });
219
223
  }
220
224
  }
225
+ async _getPrice(token) {
226
+ try {
227
+ return await this._getPriceCoinbase(token);
228
+ } catch (error) {
229
+ }
230
+ try {
231
+ return await this._getPriceCoinMarketCap(token);
232
+ } catch (error) {
233
+ }
234
+ throw new FatalError(`Price not found for ${token.name}`);
235
+ }
236
+ async _getPriceCoinbase(token) {
237
+ if (!token.pricerKey) {
238
+ throw new FatalError(`Pricer key not found for ${token.name}`);
239
+ }
240
+ const url = this.PRICE_API.replace("{{PRICER_KEY}}", token.pricerKey);
241
+ const result = await axios.get(url);
242
+ const data = result.data;
243
+ return Number(data.data.amount);
244
+ }
245
+ async _getPriceCoinMarketCap(token) {
246
+ const result = await this.client.getQuotes({ symbol: token.symbol });
247
+ return result.data[token.symbol].quote.USD.price;
248
+ }
221
249
  };
222
250
 
223
251
  // src/modules/pragma.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strkfarm/sdk",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "STRKFarm TS SDK (Meant for our internal use, but feel free to use it)",
5
5
  "typings": "dist/index.d.ts",
6
6
  "main": "dist/index.js",
@@ -48,6 +48,7 @@
48
48
  "bignumber.js": "4.0.4",
49
49
  "browser-assert": "^1.2.1",
50
50
  "chalk": "^4.1.2",
51
+ "coinmarketcap-api": "^3.1.1",
51
52
  "commander": "^12.1.0",
52
53
  "inquirer": "^10.1.2",
53
54
  "node-telegram-bot-api": "^0.66.0",
package/readme.md ADDED
@@ -0,0 +1,4 @@
1
+ # STRKFarm SDK
2
+ We are working on the documentation. It's not a priority right now, but we will get to it soon. If you have any questions, feel free to ask in our [Telegram](https://t.me/+HQ_eHaXmF-1lZDc1) in dev topic.
3
+
4
+ Also, if you are interested to help in the documentation, please let us know in the Telegram group. We appreciate open source contributions and have a reward system for contributors on onlydust.
@@ -2,6 +2,7 @@ import axios from "axios";
2
2
  import { FatalError, Global, logger } from "@/global";
3
3
  import { TokenInfo } from "@/interfaces/common";
4
4
  import { IConfig } from "@/interfaces/common";
5
+ const CoinMarketCap = require('coinmarketcap-api')
5
6
 
6
7
  export interface PriceInfo {
7
8
  price: number,
@@ -18,6 +19,10 @@ export class Pricer {
18
19
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
19
20
  */
20
21
  protected PRICE_API = `https://api.coinbase.com/v2/prices/{{PRICER_KEY}}/buy`;
22
+
23
+ // backup oracle
24
+ protected client = new CoinMarketCap(process.env.COINMARKETCAP_KEY!);
25
+
21
26
  constructor(config: IConfig, tokens: TokenInfo[]) {
22
27
  this.config = config;
23
28
  this.tokens = tokens;
@@ -89,13 +94,8 @@ export class Pricer {
89
94
  onUpdate(token.symbol);
90
95
  return;
91
96
  }
92
- if (!token.pricerKey) {
93
- throw new FatalError(`Pricer key not found for ${token.name}`);
94
- }
95
- const url = this.PRICE_API.replace("{{PRICER_KEY}}", token.pricerKey);
96
- const result = await axios.get(url);
97
- const data: any = result.data;
98
- const price = Number(data.data.amount);
97
+
98
+ const price = await this._getPrice(token);
99
99
  this.prices[token.symbol] = {
100
100
  price,
101
101
  timestamp: new Date()
@@ -122,4 +122,35 @@ export class Pricer {
122
122
  })
123
123
  }
124
124
  }
125
+
126
+ async _getPrice(token: TokenInfo) {
127
+ try {
128
+ return await this._getPriceCoinbase(token);
129
+ } catch (error) {
130
+ // do nothing, try next
131
+ }
132
+
133
+ try {
134
+ return await this._getPriceCoinMarketCap(token);
135
+ } catch (error) {
136
+ // do nothing, try next
137
+ }
138
+
139
+ throw new FatalError(`Price not found for ${token.name}`);
140
+ }
141
+
142
+ async _getPriceCoinbase(token: TokenInfo) {
143
+ if (!token.pricerKey) {
144
+ throw new FatalError(`Pricer key not found for ${token.name}`);
145
+ }
146
+ const url = this.PRICE_API.replace("{{PRICER_KEY}}", token.pricerKey);
147
+ const result = await axios.get(url);
148
+ const data: any = result.data;
149
+ return Number(data.data.amount);
150
+ }
151
+
152
+ async _getPriceCoinMarketCap(token: TokenInfo): Promise<number> {
153
+ const result = await this.client.getQuotes({symbol: token.symbol});
154
+ return result.data[token.symbol].quote.USD.price as number
155
+ }
125
156
  }