@strkfarm/sdk 2.0.0-dev.5 → 2.0.0-dev.50
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/cli.js +190 -36
- package/dist/cli.mjs +188 -34
- package/dist/index.browser.global.js +119400 -92821
- package/dist/index.browser.mjs +13293 -11146
- package/dist/index.d.ts +2281 -1938
- package/dist/index.js +13532 -11179
- package/dist/index.mjs +14165 -11836
- package/package.json +59 -60
- package/src/data/avnu.abi.json +840 -0
- package/src/data/ekubo-price-fethcer.abi.json +265 -0
- package/src/data/redeem-request-nft.abi.json +752 -0
- package/src/data/universal-vault.abi.json +8 -7
- package/src/dataTypes/_bignumber.ts +13 -4
- package/src/dataTypes/bignumber.browser.ts +10 -1
- package/src/dataTypes/bignumber.node.ts +10 -1
- package/src/dataTypes/index.ts +3 -2
- package/src/dataTypes/mynumber.ts +141 -0
- package/src/global.ts +279 -233
- package/src/index.browser.ts +2 -1
- package/src/interfaces/common.tsx +228 -6
- package/src/modules/apollo-client-config.ts +28 -0
- package/src/modules/avnu.ts +21 -12
- package/src/modules/ekubo-pricer.ts +80 -0
- package/src/modules/ekubo-quoter.ts +48 -30
- package/src/modules/erc20.ts +17 -0
- package/src/modules/harvests.ts +43 -29
- package/src/modules/index.ts +2 -1
- package/src/modules/pragma.ts +23 -8
- package/src/modules/pricer-avnu-api.ts +114 -0
- package/src/modules/pricer-from-api.ts +156 -15
- package/src/modules/pricer-lst.ts +1 -1
- package/src/modules/pricer.ts +107 -41
- package/src/modules/pricerBase.ts +2 -1
- package/src/modules/zkLend.ts +3 -2
- package/src/node/deployer.ts +36 -1
- package/src/node/pricer-redis.ts +3 -1
- package/src/strategies/base-strategy.ts +168 -16
- package/src/strategies/constants.ts +8 -3
- package/src/strategies/ekubo-cl-vault.tsx +1048 -355
- package/src/strategies/factory.ts +199 -0
- package/src/strategies/index.ts +5 -3
- package/src/strategies/registry.ts +262 -0
- package/src/strategies/sensei.ts +354 -10
- package/src/strategies/svk-strategy.ts +292 -31
- package/src/strategies/token-boosted-xstrk-carry-strategy.tsx +1261 -0
- package/src/strategies/types.ts +4 -0
- package/src/strategies/universal-adapters/adapter-utils.ts +4 -1
- package/src/strategies/universal-adapters/avnu-adapter.ts +196 -272
- package/src/strategies/universal-adapters/baseAdapter.ts +263 -251
- package/src/strategies/universal-adapters/common-adapter.ts +206 -203
- package/src/strategies/universal-adapters/index.ts +10 -8
- package/src/strategies/universal-adapters/svk-troves-adapter.ts +511 -0
- package/src/strategies/universal-adapters/token-transfer-adapter.ts +200 -0
- package/src/strategies/universal-adapters/vesu-adapter.ts +120 -82
- package/src/strategies/universal-adapters/vesu-modify-position-adapter.ts +525 -0
- package/src/strategies/universal-adapters/vesu-multiply-adapter.ts +866 -860
- package/src/strategies/universal-adapters/vesu-position-common.ts +258 -0
- package/src/strategies/universal-adapters/vesu-supply-only-adapter.ts +18 -3
- package/src/strategies/universal-lst-muliplier-strategy.tsx +895 -416
- package/src/strategies/universal-strategy.tsx +1332 -1173
- package/src/strategies/vesu-rebalance.tsx +254 -153
- package/src/strategies/yoloVault.ts +1096 -0
- package/src/utils/cacheClass.ts +11 -2
- package/src/utils/health-factor-math.ts +33 -1
- package/src/utils/index.ts +3 -1
- package/src/utils/logger.browser.ts +22 -4
- package/src/utils/logger.node.ts +259 -24
- package/src/utils/starknet-call-parser.ts +1036 -0
- package/src/utils/strategy-utils.ts +61 -0
- package/src/modules/ExtendedWrapperSDk/index.ts +0 -62
- package/src/modules/ExtendedWrapperSDk/types.ts +0 -311
- package/src/modules/ExtendedWrapperSDk/wrapper.ts +0 -395
- package/src/strategies/universal-adapters/extended-adapter.ts +0 -662
- package/src/strategies/universal-adapters/unused-balance-adapter.ts +0 -109
- package/src/strategies/vesu-extended-strategy/services/operationService.ts +0 -34
- package/src/strategies/vesu-extended-strategy/utils/config.runtime.ts +0 -77
- package/src/strategies/vesu-extended-strategy/utils/constants.ts +0 -49
- package/src/strategies/vesu-extended-strategy/utils/helper.ts +0 -372
- package/src/strategies/vesu-extended-strategy/vesu-extended-strategy.tsx +0 -1140
package/src/modules/harvests.ts
CHANGED
|
@@ -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
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
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<
|
|
68
|
-
const
|
|
69
|
-
assert(
|
|
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(
|
|
81
|
+
rewardsContract: ContractAddr.from(claimData.dropAddress),
|
|
72
82
|
token: ContractAddr.from(STRK),
|
|
73
|
-
startDate: new Date(
|
|
74
|
-
endDate: new Date(
|
|
83
|
+
startDate: new Date(0),
|
|
84
|
+
endDate: new Date(0),
|
|
75
85
|
claim: {
|
|
76
|
-
id:
|
|
77
|
-
amount: Web3Number.fromWei(
|
|
78
|
-
claimee: ContractAddr.from(
|
|
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(
|
|
81
|
-
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);
|
package/src/modules/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export * from './token-market-data';
|
|
2
2
|
export * from './pricer';
|
|
3
|
+
export * from './pricer-avnu-api';
|
|
3
4
|
export * from './pragma';
|
|
4
5
|
export * from './zkLend';
|
|
5
6
|
export * from './pricer-from-api';
|
|
@@ -10,4 +11,4 @@ export * from './pricer-lst';
|
|
|
10
11
|
export * from './lst-apr';
|
|
11
12
|
export * from './pricerBase';
|
|
12
13
|
export * from './midas';
|
|
13
|
-
export * from './
|
|
14
|
+
export * from './ekubo-pricer';
|
package/src/modules/pragma.ts
CHANGED
|
@@ -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(
|
|
10
|
-
|
|
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(
|
|
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
|
|
31
|
+
logger.verbose(`Pragma:${tokenAddr}: ${price} at block ${blockIdentifier}`);
|
|
32
|
+
return {
|
|
33
|
+
price,
|
|
34
|
+
timestamp: new Date()
|
|
35
|
+
};
|
|
21
36
|
}
|
|
22
37
|
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { TokenInfo } from "@/interfaces/common";
|
|
3
|
+
import { IConfig } from "@/interfaces/common";
|
|
4
|
+
import { PricerBase } from "./pricerBase";
|
|
5
|
+
import { PriceInfo } from "./pricer";
|
|
6
|
+
import { logger } from "@/utils/logger";
|
|
7
|
+
import { ContractAddr } from "@/dataTypes";
|
|
8
|
+
|
|
9
|
+
const AVNU_TOKENS_API = "https://starknet.impulse.avnu.fi/v3/tokens";
|
|
10
|
+
|
|
11
|
+
interface AvnuTokenApiEntry {
|
|
12
|
+
address: string;
|
|
13
|
+
symbol: string;
|
|
14
|
+
starknet?: {
|
|
15
|
+
usd?: number;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Polls Avnu impulse tokens API and keeps USD prices in memory for configured tokens.
|
|
21
|
+
* Price timestamp is set when each poll request completes.
|
|
22
|
+
*/
|
|
23
|
+
export class PricerAvnuApi extends PricerBase {
|
|
24
|
+
protected prices: { [key: string]: PriceInfo } = {};
|
|
25
|
+
|
|
26
|
+
readonly refreshInterval = 15_000;
|
|
27
|
+
readonly staleTime = 5 * 60 * 1000;
|
|
28
|
+
|
|
29
|
+
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
30
|
+
private loading = false;
|
|
31
|
+
|
|
32
|
+
constructor(config: IConfig, tokens: TokenInfo[]) {
|
|
33
|
+
super(config, tokens);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
start() {
|
|
37
|
+
this._loadPrices();
|
|
38
|
+
this.pollTimer = setInterval(() => {
|
|
39
|
+
this._loadPrices();
|
|
40
|
+
}, this.refreshInterval);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
stop() {
|
|
44
|
+
if (this.pollTimer) {
|
|
45
|
+
clearInterval(this.pollTimer);
|
|
46
|
+
this.pollTimer = null;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
isStale(timestamp: Date) {
|
|
51
|
+
return Date.now() - timestamp.getTime() > this.staleTime;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
hasPrice(tokenSymbol: string) {
|
|
55
|
+
const info = this.prices[tokenSymbol];
|
|
56
|
+
return !!info && !this.isStale(info.timestamp);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async getPrice(tokenSymbol: string): Promise<PriceInfo> {
|
|
60
|
+
const info = this.prices[tokenSymbol];
|
|
61
|
+
if (!info) {
|
|
62
|
+
throw new Error(`AvnuApi: price of ${tokenSymbol} not found`);
|
|
63
|
+
}
|
|
64
|
+
if (this.isStale(info.timestamp)) {
|
|
65
|
+
throw new Error(`AvnuApi: price of ${tokenSymbol} is stale`);
|
|
66
|
+
}
|
|
67
|
+
return info;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected async _loadPrices() {
|
|
71
|
+
if (this.loading) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
this.loading = true;
|
|
75
|
+
const timestamp = new Date();
|
|
76
|
+
try {
|
|
77
|
+
const result = await axios.get<AvnuTokenApiEntry[]>(AVNU_TOKENS_API);
|
|
78
|
+
const priceByAddress = new Map<string, number>();
|
|
79
|
+
for (const entry of result.data) {
|
|
80
|
+
const usd = entry.starknet?.usd;
|
|
81
|
+
if (usd != null && usd > 0) {
|
|
82
|
+
priceByAddress.set(ContractAddr.standardise(entry.address), usd);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
for (const token of this.tokens) {
|
|
87
|
+
if (token.symbol === "USDT" || token.symbol === "USDC") {
|
|
88
|
+
this.prices[token.symbol] = { price: 1, timestamp };
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const targetToken = token.priceProxySymbol
|
|
93
|
+
? this.tokens.find((t) => t.symbol === token.priceProxySymbol)
|
|
94
|
+
: token;
|
|
95
|
+
if (!targetToken) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const addr = targetToken.address.address;
|
|
100
|
+
const price = priceByAddress.get(addr);
|
|
101
|
+
if (price != null) {
|
|
102
|
+
this.prices[token.symbol] = { price, timestamp };
|
|
103
|
+
logger.verbose(
|
|
104
|
+
`AvnuApi: ${token.symbol} -> $${price}`,
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
} catch (error: any) {
|
|
109
|
+
logger.warn(`AvnuApi: failed to fetch tokens: ${error?.message ?? error}`);
|
|
110
|
+
} finally {
|
|
111
|
+
this.loading = false;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -1,21 +1,64 @@
|
|
|
1
|
-
|
|
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(
|
|
56
|
+
return await this.getPriceFromMyAPI(symbol);
|
|
15
57
|
} catch (e: any) {
|
|
16
|
-
logger.warn(
|
|
58
|
+
logger.warn(`API price fetch failed for ${tokenSymbol}: ${e.message}, trying coinbase`);
|
|
17
59
|
}
|
|
18
|
-
|
|
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://
|
|
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;
|