@strkfarm/sdk 2.0.0-dev.4 → 2.0.0-dev.40

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.
Files changed (77) hide show
  1. package/dist/cli.js +190 -36
  2. package/dist/cli.mjs +188 -34
  3. package/dist/index.browser.global.js +116018 -90768
  4. package/dist/index.browser.mjs +12769 -10876
  5. package/dist/index.d.ts +2222 -1947
  6. package/dist/index.js +13171 -11077
  7. package/dist/index.mjs +13076 -11004
  8. package/package.json +3 -3
  9. package/src/data/avnu.abi.json +840 -0
  10. package/src/data/ekubo-price-fethcer.abi.json +265 -0
  11. package/src/data/redeem-request-nft.abi.json +752 -0
  12. package/src/data/universal-vault.abi.json +8 -7
  13. package/src/dataTypes/_bignumber.ts +13 -4
  14. package/src/dataTypes/bignumber.browser.ts +10 -1
  15. package/src/dataTypes/bignumber.node.ts +10 -1
  16. package/src/dataTypes/index.ts +3 -2
  17. package/src/dataTypes/mynumber.ts +141 -0
  18. package/src/global.ts +96 -41
  19. package/src/index.browser.ts +2 -1
  20. package/src/interfaces/common.tsx +212 -5
  21. package/src/modules/apollo-client-config.ts +28 -0
  22. package/src/modules/avnu.ts +21 -12
  23. package/src/modules/ekubo-pricer.ts +79 -0
  24. package/src/modules/ekubo-quoter.ts +48 -30
  25. package/src/modules/erc20.ts +17 -0
  26. package/src/modules/harvests.ts +43 -29
  27. package/src/modules/index.ts +1 -1
  28. package/src/modules/pragma.ts +23 -8
  29. package/src/modules/pricer-from-api.ts +156 -15
  30. package/src/modules/pricer-lst.ts +1 -1
  31. package/src/modules/pricer.ts +40 -4
  32. package/src/modules/pricerBase.ts +2 -1
  33. package/src/node/deployer.ts +36 -1
  34. package/src/node/pricer-redis.ts +2 -1
  35. package/src/strategies/base-strategy.ts +168 -16
  36. package/src/strategies/constants.ts +8 -3
  37. package/src/strategies/ekubo-cl-vault.tsx +1044 -351
  38. package/src/strategies/factory.ts +199 -0
  39. package/src/strategies/index.ts +5 -3
  40. package/src/strategies/registry.ts +262 -0
  41. package/src/strategies/sensei.ts +353 -9
  42. package/src/strategies/svk-strategy.ts +125 -30
  43. package/src/strategies/token-boosted-xstrk-carry-strategy.tsx +1225 -0
  44. package/src/strategies/types.ts +4 -0
  45. package/src/strategies/universal-adapters/adapter-utils.ts +4 -1
  46. package/src/strategies/universal-adapters/avnu-adapter.ts +196 -272
  47. package/src/strategies/universal-adapters/baseAdapter.ts +263 -251
  48. package/src/strategies/universal-adapters/common-adapter.ts +206 -203
  49. package/src/strategies/universal-adapters/index.ts +10 -8
  50. package/src/strategies/universal-adapters/svk-troves-adapter.ts +511 -0
  51. package/src/strategies/universal-adapters/token-transfer-adapter.ts +200 -0
  52. package/src/strategies/universal-adapters/vesu-adapter.ts +120 -82
  53. package/src/strategies/universal-adapters/vesu-modify-position-adapter.ts +525 -0
  54. package/src/strategies/universal-adapters/vesu-multiply-adapter.ts +1098 -712
  55. package/src/strategies/universal-adapters/vesu-position-common.ts +258 -0
  56. package/src/strategies/universal-adapters/vesu-supply-only-adapter.ts +18 -3
  57. package/src/strategies/universal-lst-muliplier-strategy.tsx +551 -405
  58. package/src/strategies/universal-strategy.tsx +1487 -1173
  59. package/src/strategies/vesu-rebalance.tsx +252 -152
  60. package/src/strategies/yoloVault.ts +1084 -0
  61. package/src/utils/cacheClass.ts +11 -2
  62. package/src/utils/health-factor-math.ts +33 -1
  63. package/src/utils/index.ts +3 -1
  64. package/src/utils/logger.browser.ts +22 -4
  65. package/src/utils/logger.node.ts +259 -24
  66. package/src/utils/starknet-call-parser.ts +1036 -0
  67. package/src/utils/strategy-utils.ts +61 -0
  68. package/src/modules/ExtendedWrapperSDk/index.ts +0 -62
  69. package/src/modules/ExtendedWrapperSDk/types.ts +0 -311
  70. package/src/modules/ExtendedWrapperSDk/wrapper.ts +0 -395
  71. package/src/strategies/universal-adapters/extended-adapter.ts +0 -661
  72. package/src/strategies/universal-adapters/unused-balance-adapter.ts +0 -109
  73. package/src/strategies/vesu-extended-strategy/services/operationService.ts +0 -34
  74. package/src/strategies/vesu-extended-strategy/utils/config.runtime.ts +0 -77
  75. package/src/strategies/vesu-extended-strategy/utils/constants.ts +0 -49
  76. package/src/strategies/vesu-extended-strategy/utils/helper.ts +0 -372
  77. package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +0 -1140
@@ -29,28 +29,36 @@ export class Harvests {
29
29
 
30
30
  async getUnHarvestedRewards(addr: ContractAddr) {
31
31
  const rewards = await this.getHarvests(addr);
32
+ logger.verbose(`${Harvests.name}: getUnHarvestedRewards => rewards length: ${rewards.length}`);
32
33
  if (rewards.length == 0) return [];
33
34
 
34
35
  const unClaimed: HarvestInfo[] = [];
35
36
 
36
37
  // use the latest one
37
- const reward = rewards.sort((a, b) => b.endDate.getTime() - a.endDate.getTime())[0];
38
-
39
- const cls = await this.config.provider.getClassAt(reward.rewardsContract.address);
40
- const contract = new Contract({abi: cls.abi, address: reward.rewardsContract.address, providerOrAccount: this.config.provider});
41
- const isClaimed = await contract.call('is_claimed', [reward.claim.id]);
42
- logger.verbose(`${Harvests.name}: isClaimed: ${isClaimed}`);
43
- if (isClaimed) {
44
- return unClaimed;
45
- }
46
- // rewards contract must have enough balance to claim
47
- const bal = await (new ERC20(this.config)).balanceOf(reward.token, reward.rewardsContract.address, 18);
48
- if (bal.lessThan(reward.claim.amount)) {
49
- logger.verbose(`${Harvests.name}: balance: ${bal.toString()}, amount: ${reward.claim.amount.toString()}`);
50
- return unClaimed;
38
+ const sortedRewards = rewards.sort((a, b) => b.endDate.getTime() - a.endDate.getTime());
39
+ if (sortedRewards.length == 0) {
40
+ logger.verbose(`${Harvests.name}: no rewards found`);
41
+ return [];
51
42
  }
52
43
 
53
- unClaimed.unshift(reward); // to ensure older harvest is first
44
+ const cls = await this.config.provider.getClassAt(sortedRewards[0].rewardsContract.address);
45
+
46
+ for (const reward of sortedRewards) {
47
+ const contract = new Contract({abi: cls.abi, address: reward.rewardsContract.address, providerOrAccount: this.config.provider});
48
+ const isClaimed = await contract.call('is_claimed', [reward.claim.id]);
49
+ logger.verbose(`${Harvests.name}: isClaimed: ${isClaimed}, claim id: ${reward.claim.id}, address: ${reward.rewardsContract.address}`);
50
+ if (isClaimed) {
51
+ continue;
52
+ }
53
+ // rewards contract must have enough balance to claim
54
+ const bal = await (new ERC20(this.config)).balanceOf(reward.token, reward.rewardsContract.address, 18);
55
+ if (bal.lessThan(reward.claim.amount)) {
56
+ logger.verbose(`${Harvests.name}: balance: ${bal.toString()}, amount: ${reward.claim.amount.toString()}`);
57
+ continue;
58
+ }
59
+
60
+ unClaimed.push(reward); // to ensure older harvest is first
61
+ }
54
62
  return unClaimed;
55
63
  }
56
64
  }
@@ -59,26 +67,28 @@ const STRK = '0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d'
59
67
 
60
68
  export class EkuboHarvests extends Harvests {
61
69
  async getHarvests(addr: ContractAddr) {
62
- const EKUBO_API = `https://starknet-mainnet-api.ekubo.org/airdrops/${addr.address}?token=${STRK}`
70
+ logger.verbose(`${EkuboHarvests.name}: getHarvests => addr: ${addr.address}`);
71
+ const EKUBO_API = `https://prod-api.ekubo.org/claims/${addr.address}`
63
72
  const resultEkubo = await fetch(EKUBO_API);
64
- const items = (await resultEkubo.json());
65
-
73
+ const data = (await resultEkubo.json());
74
+ const claims = data.claims || [];
75
+ logger.verbose(`${EkuboHarvests.name}: getHarvests => claims length: ${claims.length}`);
66
76
  const rewards: HarvestInfo[] = [];
67
- for (let i=0; i<items.length; ++i) {
68
- const info = items[i];
69
- assert(info.token == STRK, 'expected strk token only')
77
+ for (let i=0; i<claims.length; ++i) {
78
+ const claimData = claims[i];
79
+ assert(claimData.key.token == STRK, 'expected strk token only')
70
80
  rewards.push({
71
- rewardsContract: ContractAddr.from(info.contract_address),
81
+ rewardsContract: ContractAddr.from(claimData.dropAddress),
72
82
  token: ContractAddr.from(STRK),
73
- startDate: new Date(info.start_date),
74
- endDate: new Date(info.end_date),
83
+ startDate: new Date(0),
84
+ endDate: new Date(0),
75
85
  claim: {
76
- id: info.claim.id,
77
- amount: Web3Number.fromWei(info.claim.amount, 18),
78
- claimee: ContractAddr.from(info.claim.claimee)
86
+ id: claimData.claim.index,
87
+ amount: Web3Number.fromWei(claimData.claim.amount, 18),
88
+ claimee: ContractAddr.from(claimData.claim.account)
79
89
  },
80
- actualReward: Web3Number.fromWei(info.claim.amount, 18),
81
- proof: info.proof
90
+ actualReward: Web3Number.fromWei(claimData.claim.amount, 18),
91
+ proof: claimData.proof
82
92
  });
83
93
  }
84
94
  return rewards.sort((a, b) => b.endDate.getTime() - a.endDate.getTime());
@@ -101,6 +111,10 @@ export class VesuHarvests extends Harvests {
101
111
  logger.verbose(`${VesuHarvests.name}: claimed_amount: ${claimed_amount.toString()}`);
102
112
 
103
113
  const data = _data.data['defiSpring'];
114
+ if (!data) {
115
+ logger.verbose(`${VesuHarvests.name}: no defiSpring data found`);
116
+ return [];
117
+ }
104
118
 
105
119
  // get the actual reward
106
120
  const actualReward = Web3Number.fromWei(data.amount, 18).minus(claimed_amount);
@@ -10,4 +10,4 @@ export * from './pricer-lst';
10
10
  export * from './lst-apr';
11
11
  export * from './pricerBase';
12
12
  export * from './midas';
13
- export * from './ExtendedWrapperSDk';
13
+ export * from './ekubo-pricer';
@@ -1,22 +1,37 @@
1
- import { Contract, RpcProvider } from "starknet";
1
+ import { Contract, RpcProvider, BlockIdentifier } from "starknet";
2
2
  import PragmaAbi from '@/data/pragma.abi.json';
3
3
  import { logger } from "@/utils/logger";
4
+ import { PricerBase } from "./pricerBase";
5
+ import { IConfig, TokenInfo } from "@/interfaces";
6
+ import { PriceInfo } from "./pricer";
4
7
 
5
- export class Pragma {
8
+ export class Pragma extends PricerBase {
6
9
  contractAddr = '0x023fb3afbff2c0e3399f896dcf7400acf1a161941cfb386e34a123f228c62832';
7
10
  readonly contract: Contract;
8
11
 
9
- constructor(provider: RpcProvider) {
10
- this.contract = new Contract({abi: PragmaAbi, address: this.contractAddr, providerOrAccount: provider});
12
+ constructor(config: IConfig, tokens: TokenInfo[]) {
13
+ super(config, tokens);
14
+ this.contract = new Contract({
15
+ abi: PragmaAbi,
16
+ address: this.contractAddr,
17
+ providerOrAccount: config.provider as RpcProvider
18
+ });
11
19
  }
12
20
 
13
- async getPrice(tokenAddr: string) {
21
+ async getPrice(tokenAddr: string, blockIdentifier: BlockIdentifier = 'latest'): Promise<PriceInfo> {
14
22
  if (!tokenAddr) {
15
23
  throw new Error(`Pragma:getPrice - no token`)
16
24
  }
17
- const result: any = await this.contract.call('get_price', [tokenAddr]);
25
+ const result: any = await this.contract.call(
26
+ 'get_price',
27
+ [tokenAddr],
28
+ { blockIdentifier }
29
+ );
18
30
  const price = Number(result.price) / 10**8;
19
- logger.verbose(`Pragma:${tokenAddr}: ${price}`);
20
- return price;
31
+ logger.verbose(`Pragma:${tokenAddr}: ${price} at block ${blockIdentifier}`);
32
+ return {
33
+ price,
34
+ timestamp: new Date()
35
+ };
21
36
  }
22
37
  }
@@ -1,21 +1,64 @@
1
- import { PriceInfo } from "./pricer";
1
+ import { PriceInfo } from "./pricer";
2
2
  import axios from "axios";
3
3
  import { IConfig, TokenInfo } from "@/interfaces";
4
4
  import { PricerBase } from "./pricerBase";
5
5
  import { logger } from "@/utils/logger";
6
+ import { createApolloClient } from "./apollo-client-config";
7
+ import { gql } from "@apollo/client";
8
+ import { ContractAddr } from "@/dataTypes";
9
+ import { Pragma } from "./pragma";
10
+ import { EkuboPricer } from "./ekubo-pricer";
11
+ import { BlockIdentifier } from "starknet";
6
12
 
7
13
  export class PricerFromApi extends PricerBase {
14
+ private apolloClient: ReturnType<typeof createApolloClient>;
15
+ private pragma: Pragma;
16
+ private ekuboPricer: EkuboPricer;
17
+
18
+ // Tokens supported by Pragma and GraphQL for historical prices (standardized addresses)
19
+ // Note: xSTRK is not supported by Pragma RPC, so it will use Ekubo pricer instead
20
+ private readonly PRAGMA_SUPPORTED_TOKENS = [
21
+ ContractAddr.from('0x3fe2b97c1fd336e750087d68b9b867997fd64a2661ff3ca5a7c771641e8e7ac').address, // WBTC
22
+ ContractAddr.from('0x53c91253bc9682c04929ca02ed00b3e423f6710d2ee7e0d5ebb06f3ecf368a8').address, // USDC
23
+ ContractAddr.from('0x4718f5a0fc34cc1af16a1cdee98ffb20c31f5cd61d6ab07201858f4287c938d').address, // STRK
24
+ // xSTRK removed - not supported by Pragma RPC, will use Ekubo pricer
25
+ // ContractAddr.from('0x28d709c875c0ceac3dce7065bec5328186dc89fe254527084d1689910954b0a').address, // xSTRK
26
+ ContractAddr.from('0x68f5c6a61780768455de69077e07e89787839bf8166decfbf92b645209c0fb8').address, // USDT
27
+ ContractAddr.from('0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7').address, // ETH
28
+ ];
29
+
8
30
  constructor(config: IConfig, tokens: TokenInfo[]) {
9
31
  super(config, tokens);
32
+ this.apolloClient = createApolloClient(config);
33
+ this.pragma = new Pragma(config, tokens);
34
+ this.ekuboPricer = new EkuboPricer(config, tokens);
10
35
  }
11
36
 
12
- async getPrice(tokenSymbol: string): Promise<PriceInfo> {
37
+ async getPrice(tokenSymbol: string, blockNumber?: BlockIdentifier): Promise<PriceInfo> {
38
+ const tokenInfo = this.tokens.find(t => t.symbol === tokenSymbol);
39
+ if (!tokenInfo) {
40
+ throw new Error(`Token ${tokenSymbol} not found in configured tokens`);
41
+ }
42
+ const symbol = tokenInfo.priceProxySymbol || tokenInfo.symbol;
43
+ // If blockNumber is provided, fetch historical price from GraphQL
44
+ if (blockNumber !== undefined) {
45
+ try {
46
+ return await this.getHistoricalPrice(symbol, blockNumber);
47
+ } catch (e: any) {
48
+ logger.error(`Historical price fetch failed for ${tokenSymbol}: ${e.message}`);
49
+ throw new Error('Server Error in fetching historical price')
50
+ // Fall back to current price if historical price is not available
51
+ }
52
+ }
53
+
54
+ // Otherwise, fetch current price
13
55
  try {
14
- return await this.getPriceFromMyAPI(tokenSymbol);
56
+ return await this.getPriceFromMyAPI(symbol);
15
57
  } catch (e: any) {
16
- logger.warn('getPriceFromMyAPI error', e);
58
+ logger.warn(`API price fetch failed for ${tokenSymbol}: ${e.message}, trying coinbase`);
17
59
  }
18
- logger.info('getPrice coinbase', tokenSymbol);
60
+
61
+ logger.info(`Using Coinbase price for ${tokenSymbol}`);
19
62
  let retry = 0;
20
63
  const MAX_RETRIES = 5;
21
64
  for (retry = 1; retry < MAX_RETRIES + 1; retry++) {
@@ -41,22 +84,120 @@ export class PricerFromApi extends PricerBase {
41
84
  }
42
85
 
43
86
  async getPriceFromMyAPI(tokenSymbol: string) {
44
- logger.verbose(`getPrice from api: ${tokenSymbol}`);
45
87
  const endpoint = 'https://proxy.api.troves.fi'
46
88
  const url = `${endpoint}/api/price/${tokenSymbol}`;
47
89
  const priceInfoRes = await fetch(url);
48
90
  const priceInfo = await priceInfoRes.json();
49
- const now = new Date();
50
- const priceTime = new Date(priceInfo.timestamp);
51
- // if (now.getTime() - priceTime.getTime() > 900000) {
52
- // // 15 mins
53
- // logger.verbose(`Price is stale: ${tokenSymbol}, timestamp: ${priceInfo.timestamp}, price: ${priceInfo.price}`);
54
- // throw new Error('Price is stale');
55
- // }
56
91
  const price = Number(priceInfo.price);
57
92
  return {
58
93
  price,
59
94
  timestamp: new Date(priceInfo.timestamp)
60
95
  }
61
- }
62
- }
96
+ }
97
+
98
+ /**
99
+ * Fetches historical price for a token at a specific block number
100
+ * @param tokenSymbol - The token symbol to get price for
101
+ * @param blockNumber - The block number to query
102
+ * @returns PriceInfo with price at the closest block <= blockNumber
103
+ */
104
+ async getHistoricalPrice(tokenSymbol: string, blockNumber: BlockIdentifier): Promise<PriceInfo> {
105
+ const tokenInfo = this.tokens.find(t => t.symbol === tokenSymbol);
106
+ if (!tokenInfo) {
107
+ throw new Error(`Token ${tokenSymbol} not found in configured tokens`);
108
+ }
109
+
110
+ // If blockNumber is not a number, fall back to current price
111
+ if (typeof blockNumber !== 'number') {
112
+ logger.info(`Non-numeric blockIdentifier '${blockNumber}' provided, fetching current price for ${tokenSymbol}`);
113
+ return await this.getPriceFromMyAPI(tokenSymbol);
114
+ }
115
+
116
+ const standardizedAddress = ContractAddr.from(tokenInfo.address.address).address;
117
+ const isPragmaSupported = this.PRAGMA_SUPPORTED_TOKENS.includes(standardizedAddress);
118
+
119
+ // Step 1: Try GraphQL first for ALL tokens
120
+ let data;
121
+ try {
122
+ const result = await this.apolloClient.query({
123
+ query: gql`
124
+ query GetHistoricalPrice($asset: String!, $blockNumber: Int!) {
125
+ findFirstPrices(
126
+ where: {
127
+ asset: { equals: $asset },
128
+ block_number: { lte: $blockNumber }
129
+ }
130
+ orderBy: { block_number: desc }
131
+ ) {
132
+ price
133
+ timestamp
134
+ block_number
135
+ }
136
+ }
137
+ `,
138
+ variables: {
139
+ asset: standardizedAddress,
140
+ blockNumber: blockNumber,
141
+ },
142
+ });
143
+ data = result.data;
144
+ } catch (graphqlError: any) {
145
+ logger.error(`GraphQL query failed for ${tokenSymbol}: ${graphqlError.message}`);
146
+ }
147
+
148
+ // Step 2: If GraphQL has data, check block difference and use it if acceptable
149
+ if (data?.findFirstPrices) {
150
+ const priceData = data.findFirstPrices;
151
+ const blockDifference = blockNumber - priceData.block_number;
152
+ const MAX_BLOCK_DIFFERENCE = 6800; // ~6 hours worth of blocks
153
+
154
+ if (blockDifference <= MAX_BLOCK_DIFFERENCE) {
155
+ logger.info(`Using GraphQL price for ${tokenSymbol} at block ${priceData.block_number}`);
156
+ return {
157
+ price: Number(priceData.price),
158
+ timestamp: new Date(priceData.timestamp * 1000),
159
+ };
160
+ }
161
+
162
+ logger.info(
163
+ `Block difference ${blockDifference} exceeds limit for ${tokenSymbol}. ` +
164
+ `Will try fallback sources at block ${blockNumber}`
165
+ );
166
+ } else {
167
+ logger.info(`No GraphQL data for ${tokenSymbol} at block ${blockNumber}, trying fallback sources`);
168
+ }
169
+
170
+ // Step 3: Fallback to Pragma (for supported tokens) or Ekubo (for non-supported tokens)
171
+ if (isPragmaSupported) {
172
+ logger.info(`Attempting Pragma RPC fetch for ${tokenSymbol} at block ${blockNumber}`);
173
+ try {
174
+ const priceInfo = await this.pragma.getPrice(tokenInfo.address.address, blockNumber);
175
+ logger.info(`Using Pragma price for ${tokenSymbol}: ${priceInfo.price}`);
176
+ return {
177
+ price: priceInfo.price,
178
+ timestamp: priceInfo.timestamp,
179
+ };
180
+ } catch (error: any) {
181
+ logger.error(`Pragma RPC failed for ${tokenSymbol} at block ${blockNumber}: ${error.message}`);
182
+ throw new Error(`Server Error at Pragma token ${tokenSymbol} at block ${blockNumber}`);
183
+ }
184
+ } else {
185
+ // For non-Pragma tokens, use Ekubo pricer
186
+ logger.info(`Attempting Ekubo price fetch for ${tokenSymbol} at block ${blockNumber}`);
187
+ try {
188
+ const priceInfo = await this.ekuboPricer.getPrice(
189
+ tokenInfo.address.address,
190
+ blockNumber
191
+ );
192
+ logger.info(`Using Ekubo price for ${tokenSymbol}: ${priceInfo.price}`);
193
+ return {
194
+ price: priceInfo.price,
195
+ timestamp: priceInfo.timestamp,
196
+ };
197
+ } catch (error: any) {
198
+ logger.error(`Ekubo RPC failed for ${tokenSymbol} at block ${blockNumber}: ${error.message}`);
199
+ throw new Error(`Server Error at Ekubo token ${tokenSymbol} at block ${blockNumber}`);
200
+ }
201
+ }
202
+ }
203
+ }
@@ -6,7 +6,7 @@ import axios from "axios";
6
6
 
7
7
  export class PricerLST extends Pricer {
8
8
  private tokenMaps: {lst: TokenInfo, underlying: TokenInfo}[];
9
- protected EKUBO_API = 'https://quoter-mainnet-api.ekubo.org/{{AMOUNT}}/{{TOKEN_ADDRESS}}/{{UNDERLYING_ADDRESS}}'; // e.g. xSTRK/STRK
9
+ protected EKUBO_API = 'https://prod-api-quoter.ekubo.org/23448594291968334/{{AMOUNT}}/{{TOKEN_ADDRESS}}/{{UNDERLYING_ADDRESS}}'; // e.g. xSTRK/STRK
10
10
 
11
11
  constructor(config: IConfig, tokenMaps: {lst: TokenInfo, underlying: TokenInfo}[]) {
12
12
  const refreshInterval = 5000;
@@ -5,6 +5,8 @@ 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";
9
+ import { BlockIdentifier } from "starknet";
8
10
 
9
11
  export interface PriceInfo {
10
12
  price: number,
@@ -21,13 +23,14 @@ export class Pricer extends PricerBase {
21
23
 
22
24
  // code populates this map during runtime to determine which method to use for a given token
23
25
  // The method set will be the first one to try after first attempt
24
- protected methodToUse: {[tokenSymbol: string]: 'Ekubo' | 'Coinbase' | 'Coinmarketcap'} = {};
26
+ protected methodToUse: {[tokenSymbol: string]: 'Ekubo' | 'Coinbase' | 'Coinmarketcap' | 'Avnu'} = {};
25
27
 
26
28
  /**
27
29
  * TOKENA and TOKENB are the two token names to get price of TokenA in terms of TokenB
28
30
  */
31
+ // ! switch to USDC (new) later
29
32
  protected PRICE_API = `https://api.coinbase.com/v2/prices/{{PRICER_KEY}}/buy`;
30
- 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
31
34
 
32
35
  constructor(config: IConfig, tokens: TokenInfo[], refreshInterval = 30000, staleTime = 60000) {
33
36
  super(config, tokens);
@@ -81,7 +84,7 @@ export class Pricer extends PricerBase {
81
84
  Global.assert(!this.isStale(timestamp, tokenName), `Price of ${tokenName} is stale`);
82
85
 
83
86
  }
84
- async getPrice(tokenSymbol: string) {
87
+ async getPrice(tokenSymbol: string, blockNumber?: BlockIdentifier) {
85
88
  Global.assert(this.prices[tokenSymbol], `Price of ${tokenSymbol} not found`);
86
89
  this.assertNotStale(this.prices[tokenSymbol].timestamp, tokenSymbol);
87
90
  return this.prices[tokenSymbol];
@@ -93,7 +96,7 @@ export class Pricer extends PricerBase {
93
96
  let retry = 0;
94
97
  while (retry < MAX_RETRIES) {
95
98
  try {
96
- if (token.symbol === 'USDT') {
99
+ if (token.symbol === 'USDT' || token.symbol === 'USDC') {
97
100
  this.prices[token.symbol] = {
98
101
  price: 1,
99
102
  timestamp: new Date()
@@ -174,6 +177,15 @@ export class Pricer extends PricerBase {
174
177
  console.warn(`Ekubo: price err [${token.symbol}]: `, Object.keys(error));
175
178
  // do nothing, try next
176
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
+ }
177
189
  }
178
190
 
179
191
  // if methodToUse is the default one, pass Coinbase to try all from start
@@ -200,7 +212,31 @@ export class Pricer extends PricerBase {
200
212
  throw new Error("Not implemented");
201
213
  }
202
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
+
203
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()}`);
204
240
  const url = this.EKUBO_API.replace("{{TOKEN_ADDRESS}}", token.address.toString()).replace("{{AMOUNT}}", amountIn.toWei());
205
241
  const result = await axios.get(url);
206
242
  const data: any = result.data;
@@ -1,5 +1,6 @@
1
1
  import { IConfig, TokenInfo } from "@/interfaces";
2
2
  import { PriceInfo } from "./pricer";
3
+ import { BlockIdentifier } from "starknet";
3
4
 
4
5
  export abstract class PricerBase {
5
6
  readonly config: IConfig;
@@ -9,7 +10,7 @@ export abstract class PricerBase {
9
10
  this.tokens = tokens;
10
11
  }
11
12
 
12
- async getPrice(tokenSymbol: string): Promise<PriceInfo> {
13
+ async getPrice(tokenSymbol: string, blockNumber?: BlockIdentifier): Promise<PriceInfo> {
13
14
  throw new Error('Method not implemented');
14
15
  }
15
16
  }
@@ -4,6 +4,7 @@ import { readFileSync, existsSync, writeFileSync } from 'fs'
4
4
  import { IConfig } from '../interfaces';
5
5
  import { Store, getDefaultStoreConfig } from '../utils/store';
6
6
  import { add } from 'winston';
7
+ import { logger } from '@/utils';
7
8
 
8
9
  function getContracts() {
9
10
  const PATH = './contracts.json'
@@ -207,13 +208,47 @@ async function executeTransactions(
207
208
  return tx;
208
209
  }
209
210
 
211
+ async function myWaitForTransaction(
212
+ transaction_hash: string,
213
+ provider: RpcProvider,
214
+ retry = 0
215
+ ) {
216
+ const MAX_RETRIES = 60;
217
+ logger.verbose(`Waiting for transaction: ${transaction_hash}, retry: ${retry}`);
218
+ try {
219
+ const status = await provider.getTransactionStatus(transaction_hash);
220
+ logger.verbose(`Transaction status: ${JSON.stringify(status.execution_status)}`);
221
+ if (status.execution_status == TransactionExecutionStatus.SUCCEEDED) {
222
+ return true;
223
+ }
224
+ if (status.execution_status == TransactionExecutionStatus.REVERTED) {
225
+ throw new Error(`Transaction reverted: ${transaction_hash}`);
226
+ }
227
+ if (retry > MAX_RETRIES) {
228
+ throw new Error(`Transaction not found: ${transaction_hash}`);
229
+ }
230
+ await new Promise(resolve => setTimeout(resolve, 1000));
231
+ return myWaitForTransaction(transaction_hash, provider, retry + 1);
232
+ } catch (error) {
233
+ if (error instanceof Error && error.message.includes('Transaction reverted')) {
234
+ throw new Error(`Transaction reverted: ${transaction_hash}`);
235
+ }
236
+ if (retry > MAX_RETRIES) {
237
+ throw error;
238
+ }
239
+ await new Promise(resolve => setTimeout(resolve, 1000));
240
+ return myWaitForTransaction(transaction_hash, provider, retry + 1);
241
+ }
242
+ }
243
+
210
244
  const Deployer = {
211
245
  getAccount,
212
246
  myDeclare,
213
247
  deployContract,
214
248
  prepareMultiDeployContracts,
215
249
  executeDeployCalls,
216
- executeTransactions
250
+ executeTransactions,
251
+ myWaitForTransaction
217
252
  }
218
253
 
219
254
  export default Deployer;
@@ -4,6 +4,7 @@ import { PriceInfo, Pricer } from '@/modules/pricer';
4
4
  import { createClient } from 'redis';
5
5
  import type { RedisClientType } from 'redis'
6
6
  import { logger } from "@/utils/logger";
7
+ import { BlockIdentifier } from "starknet";
7
8
 
8
9
  export class PricerRedis extends Pricer {
9
10
  private redisClient: RedisClientType | null = null;
@@ -51,7 +52,7 @@ export class PricerRedis extends Pricer {
51
52
  }
52
53
 
53
54
  /** Returns price from redis */
54
- async getPrice(tokenSymbol: string) {
55
+ async getPrice(tokenSymbol: string, blockNumber?: BlockIdentifier) {
55
56
  const STALE_TIME = 60000;
56
57
  if (!this.redisClient) {
57
58
  throw new FatalError(`Redis client not initialised`);