@strkfarm/sdk 2.0.0-staging.1 → 2.0.0-staging.11

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.
@@ -5,6 +5,7 @@ import { IConfig } from "@/interfaces/common";
5
5
  import { Web3Number } from "@/dataTypes";
6
6
  import { PricerBase } from "./pricerBase";
7
7
  import { logger } from "@/utils/logger";
8
+ import { AvnuWrapper } from "./avnu";
8
9
  import { BlockIdentifier } from "starknet";
9
10
 
10
11
  export interface PriceInfo {
@@ -22,13 +23,14 @@ export class Pricer extends PricerBase {
22
23
 
23
24
  // code populates this map during runtime to determine which method to use for a given token
24
25
  // The method set will be the first one to try after first attempt
25
- protected methodToUse: {[tokenSymbol: string]: 'Ekubo' | 'Coinbase' | 'Coinmarketcap'} = {};
26
+ protected methodToUse: {[tokenSymbol: string]: 'Ekubo' | 'Coinbase' | 'Coinmarketcap' | 'Avnu'} = {};
26
27
 
27
28
  /**
28
29
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
29
30
  */
31
+ // ! switch to USDC (new) later
30
32
  protected PRICE_API = `https://api.coinbase.com/v2/prices/{{PRICER_KEY}}/buy`;
31
- protected EKUBO_API = 'https://quoter-mainnet-api.ekubo.org/{{AMOUNT}}/{{TOKEN_ADDRESS}}/0x053c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8'; // e.g. ETH/USDC
33
+ protected EKUBO_API = 'https://prod-api-quoter.ekubo.org/23448594291968334/{{AMOUNT}}/{{TOKEN_ADDRESS}}/0x033068F6539f8e6e6b131e6B2B814e6c34A5224bC66947c47DaB9dFeE93b35fb'; // e.g. ETH/USDC
32
34
 
33
35
  constructor(config: IConfig, tokens: TokenInfo[], refreshInterval = 30000, staleTime = 60000) {
34
36
  super(config, tokens);
@@ -94,7 +96,7 @@ export class Pricer extends PricerBase {
94
96
  let retry = 0;
95
97
  while (retry < MAX_RETRIES) {
96
98
  try {
97
- if (token.symbol === 'USDT') {
99
+ if (token.symbol === 'USDT' || token.symbol === 'USDC') {
98
100
  this.prices[token.symbol] = {
99
101
  price: 1,
100
102
  timestamp: new Date()
@@ -175,6 +177,15 @@ export class Pricer extends PricerBase {
175
177
  console.warn(`Ekubo: price err [${token.symbol}]: `, Object.keys(error));
176
178
  // do nothing, try next
177
179
  }
180
+ case 'Avnu':
181
+ try {
182
+ const result = await this._getAvnuPrice(token, new Web3Number(token.priceCheckAmount ? token.priceCheckAmount : 1, token.decimals));
183
+ this.methodToUse[token.symbol] = 'Avnu';
184
+ return result;
185
+ } catch (error: any) {
186
+ console.warn(`Avnu: price err [${token.symbol}]: `, error.message);
187
+ console.warn(`Avnu: price err [${token.symbol}]: `, Object.keys(error));
188
+ }
178
189
  }
179
190
 
180
191
  // if methodToUse is the default one, pass Coinbase to try all from start
@@ -201,7 +212,31 @@ export class Pricer extends PricerBase {
201
212
  throw new Error("Not implemented");
202
213
  }
203
214
 
215
+ async _getAvnuPrice(token: TokenInfo, amountIn = new Web3Number(1, token.decimals), retry = 0): Promise<number> {
216
+ logger.verbose(`Getting price of ${token.symbol} using Ekubo, amountIn: ${amountIn.toWei()}`);
217
+
218
+ const avnuWrapper = new AvnuWrapper();
219
+ const usdcAddress = '0x033068F6539f8e6e6b131e6B2B814e6c34A5224bC66947c47DaB9dFeE93b35fb';
220
+ const quote = await avnuWrapper.getQuotes(token.address.toString(), usdcAddress, amountIn.toWei(), '0x1');
221
+ const multiplier = 1 / amountIn.toNumber();
222
+ const outputUSDC = Number(Web3Number.fromWei(quote.buyAmount.toString(), 6).toFixed(6)) * multiplier;
223
+ logger.verbose(`Avnu: ${token.symbol} -> USDC: ${outputUSDC}, retry: ${retry}`);
224
+ if (outputUSDC === 0 && retry < 3) {
225
+ // try again with a higher amount
226
+ const amountIn = new Web3Number(100, token.decimals); // 100 unit of token
227
+ return await this._getAvnuPrice(token, amountIn, retry + 1);
228
+ }
229
+
230
+ // if usdc depegs, it will not longer be 1 USD
231
+ // so we need to get the price of USDC in USD
232
+ // and then convert the outputUSDC to USD
233
+ const usdcPrice = 1; // (await this.getPrice('USDC')).price;
234
+ logger.verbose(`USDC Price: ${usdcPrice}`);
235
+ return outputUSDC * usdcPrice;
236
+ }
237
+
204
238
  async _getPriceEkubo(token: TokenInfo, amountIn = new Web3Number(1, token.decimals), retry = 0): Promise<number> {
239
+ logger.verbose(`Getting price of ${token.symbol} using Ekubo, amountIn: ${amountIn.toWei()}`);
205
240
  const url = this.EKUBO_API.replace("{{TOKEN_ADDRESS}}", token.address.toString()).replace("{{AMOUNT}}", amountIn.toWei());
206
241
  const result = await axios.get(url);
207
242
  const data: any = result.data;
@@ -23,6 +23,16 @@ export interface DualTokenInfo {
23
23
  token1: SingleTokenInfo
24
24
  }
25
25
 
26
+ export interface NetAPYSplit {
27
+ apy: number;
28
+ id: string;
29
+ }
30
+
31
+ export interface NetAPYDetails {
32
+ net: number;
33
+ splits: NetAPYSplit[];
34
+ }
35
+
26
36
  interface CacheData {
27
37
  timestamp: number;
28
38
  ttl: number;
@@ -57,7 +67,22 @@ export class BaseStrategy<TVLInfo, ActionInfo> extends CacheClass {
57
67
  throw new Error("Not implemented");
58
68
  }
59
69
 
70
+ async netAPY(
71
+ blockIdentifier?: BlockIdentifier,
72
+ sinceBlocks?: number,
73
+ timeperiod?: "24h" | "7d" | "30d" | "3m"
74
+ ): Promise<number | NetAPYDetails> {
75
+ throw new Error("Not implemented");
76
+ }
77
+
60
78
  async getPendingRewards(): Promise<HarvestInfo[]> {
61
79
  return [];
62
80
  }
81
+
82
+ async getUserRealizedAPY(
83
+ blockIdentifier?: BlockIdentifier,
84
+ sinceBlocks?: number
85
+ ): Promise<number> {
86
+ throw new Error("Not implemented");
87
+ }
63
88
  }