impermax-sdk 1.2.69 → 1.2.71
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/lib/offchain/account/lendingPool/borrowable.d.ts +14 -0
- package/lib/offchain/account/lendingPool/borrowable.js +73 -0
- package/lib/offchain/account/lendingPool/collateral.d.ts +12 -0
- package/lib/offchain/account/lendingPool/collateral.js +42 -0
- package/lib/offchain/account/lendingVault.d.ts +11 -0
- package/lib/offchain/account/lendingVault.js +38 -0
- package/lib/offchain/account/poolToken.d.ts +6 -0
- package/lib/offchain/account/poolToken.js +21 -0
- package/lib/offchain/initializer.d.ts +32 -0
- package/lib/offchain/initializer.js +627 -0
- package/lib/offchain/lendingPool/borrowable.d.ts +55 -0
- package/lib/offchain/lendingPool/borrowable.js +351 -0
- package/lib/offchain/lendingPool/collateral.d.ts +17 -0
- package/lib/offchain/lendingPool/collateral.js +72 -0
- package/lib/offchain/lendingVault.d.ts +22 -0
- package/lib/offchain/lendingVault.js +115 -0
- package/lib/offchain/offchainAPRHelper.d.ts +14 -3
- package/lib/offchain/offchainAPRHelper.js +98 -27
- package/lib/offchain/offchainPriceHelper.js +3 -0
- package/lib/offchain/poolToken.d.ts +20 -0
- package/lib/offchain/poolToken.js +120 -0
- package/lib/offchain/priceHelper.d.ts +21 -0
- package/lib/offchain/priceHelper.js +195 -0
- package/lib/offchain/query.js +247 -247
- package/lib/offchain/solidexHelper.d.ts +16 -0
- package/lib/offchain/solidexHelper.js +58 -0
- package/lib/offchain/types.d.ts +150 -0
- package/lib/offchain/types.js +3 -0
- package/lib/onchain/account/interactions/index.d.ts +28 -0
- package/lib/onchain/account/interactions/index.js +158 -0
- package/lib/onchain/account/interactions/lendingPool/borrowable.d.ts +16 -0
- package/lib/onchain/account/interactions/lendingPool/borrowable.js +49 -0
- package/lib/onchain/account/interactions/lendingPool/collateral.d.ts +15 -0
- package/lib/onchain/account/interactions/lendingPool/collateral.js +33 -0
- package/lib/onchain/account/interactions/lendingPool/index.d.ts +41 -0
- package/lib/onchain/account/interactions/lendingPool/index.js +120 -0
- package/lib/onchain/account/interactions/lendingVault.d.ts +10 -0
- package/lib/onchain/account/interactions/lendingVault.js +12 -0
- package/lib/onchain/account/interactions/poolToken.d.ts +33 -0
- package/lib/onchain/account/interactions/poolToken.js +180 -0
- package/lib/onchain/account/lendingPool/borrowable.d.ts +29 -0
- package/lib/onchain/account/lendingPool/borrowable.js +135 -0
- package/lib/onchain/account/lendingPool/collateral.d.ts +15 -0
- package/lib/onchain/account/lendingPool/collateral.js +66 -0
- package/lib/onchain/account/lendingVault.d.ts +10 -0
- package/lib/onchain/account/lendingVault.js +35 -0
- package/lib/onchain/account/poolToken.d.ts +24 -0
- package/lib/onchain/account/poolToken.js +84 -0
- package/lib/onchain/contractsHelper.d.ts +29 -0
- package/lib/onchain/contractsHelper.js +52 -0
- package/lib/onchain/impermaxFactory/lendingPool/borrowable.d.ts +36 -0
- package/lib/onchain/impermaxFactory/lendingPool/borrowable.js +153 -0
- package/lib/onchain/impermaxFactory/lendingPool/collateral.d.ts +17 -0
- package/lib/onchain/impermaxFactory/lendingPool/collateral.js +59 -0
- package/lib/onchain/interactions/lendingPool/borrowable.d.ts +14 -0
- package/lib/onchain/interactions/lendingPool/borrowable.js +47 -0
- package/lib/onchain/interactions/lendingPool/collateral.d.ts +13 -0
- package/lib/onchain/interactions/lendingPool/collateral.js +31 -0
- package/lib/onchain/interactions/lendingVault.d.ts +8 -0
- package/lib/onchain/interactions/lendingVault.js +15 -0
- package/lib/onchain/interactions/poolToken.d.ts +31 -0
- package/lib/onchain/interactions/poolToken.js +181 -0
- package/lib/onchain/lendingVault.d.ts +17 -0
- package/lib/onchain/lendingVault.js +56 -0
- package/lib/onchain/poolToken.d.ts +32 -0
- package/lib/onchain/poolToken.js +121 -0
- package/lib/onchain/types.d.ts +45 -0
- package/lib/onchain/types.js +14 -0
- package/lib/utils/price-from-reserves.d.ts +2 -0
- package/lib/utils/price-from-reserves.js +19 -0
- package/package.json +1 -1
|
@@ -12,44 +12,56 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
12
12
|
const types_1 = require("../config/types");
|
|
13
13
|
// Map of OUR chains to the chain names used by llama in their `poolsOld`
|
|
14
14
|
const LlamaChains = {
|
|
15
|
-
[types_1.Networks.Ropsten]:
|
|
16
|
-
[types_1.Networks.Mainnet]:
|
|
17
|
-
[types_1.Networks.Polygon]:
|
|
18
|
-
[types_1.Networks.Arbitrum]:
|
|
19
|
-
[types_1.Networks.Avalanche]:
|
|
20
|
-
[types_1.Networks.Moonriver]:
|
|
21
|
-
[types_1.Networks.Aurora]:
|
|
22
|
-
[types_1.Networks.Cronos]:
|
|
23
|
-
[types_1.Networks.Fantom]:
|
|
24
|
-
[types_1.Networks.Harmony]:
|
|
25
|
-
[types_1.Networks.Moonbeam]:
|
|
26
|
-
[types_1.Networks.Sxnetwork]:
|
|
27
|
-
[types_1.Networks.Canto]:
|
|
28
|
-
[types_1.Networks.ZksyncEra]:
|
|
29
|
-
[types_1.Networks.Base]:
|
|
30
|
-
[types_1.Networks.Mantle]:
|
|
31
|
-
[types_1.Networks.Scroll]:
|
|
32
|
-
[types_1.Networks.Optimism]:
|
|
15
|
+
[types_1.Networks.Ropsten]: "ropsten",
|
|
16
|
+
[types_1.Networks.Mainnet]: "ethereum",
|
|
17
|
+
[types_1.Networks.Polygon]: "polygon",
|
|
18
|
+
[types_1.Networks.Arbitrum]: "arbitrum",
|
|
19
|
+
[types_1.Networks.Avalanche]: "avalanche",
|
|
20
|
+
[types_1.Networks.Moonriver]: "moonriver",
|
|
21
|
+
[types_1.Networks.Aurora]: "aurora",
|
|
22
|
+
[types_1.Networks.Cronos]: "cronos",
|
|
23
|
+
[types_1.Networks.Fantom]: "fantom",
|
|
24
|
+
[types_1.Networks.Harmony]: "harmony",
|
|
25
|
+
[types_1.Networks.Moonbeam]: "moonbeam",
|
|
26
|
+
[types_1.Networks.Sxnetwork]: "sxnetwork",
|
|
27
|
+
[types_1.Networks.Canto]: "canto",
|
|
28
|
+
[types_1.Networks.ZksyncEra]: "zksync era",
|
|
29
|
+
[types_1.Networks.Base]: "base",
|
|
30
|
+
[types_1.Networks.Mantle]: "mantle",
|
|
31
|
+
[types_1.Networks.Scroll]: "scroll",
|
|
32
|
+
[types_1.Networks.Optimism]: "optimism",
|
|
33
33
|
};
|
|
34
34
|
// Class to get the APR from DefiLlama directly, only makes 1 call to api and caches all pools
|
|
35
35
|
class OffchainAPRHelper {
|
|
36
36
|
constructor(offchain) {
|
|
37
37
|
this.llamaAllPools = {};
|
|
38
38
|
this.initialized = null;
|
|
39
|
+
this.chainTvls = {};
|
|
40
|
+
this.tvlInitialized = null;
|
|
39
41
|
this.offchain = offchain;
|
|
40
42
|
}
|
|
41
43
|
cleanCache() {
|
|
42
44
|
this.llamaAllPools = {};
|
|
43
45
|
this.initialized = null;
|
|
46
|
+
this.chainTvls = {};
|
|
47
|
+
this.tvlInitialized = null;
|
|
48
|
+
}
|
|
49
|
+
// Blacklist certain projects which might report same pool as dex (aggregators, etc.)
|
|
50
|
+
isBlacklisted(project) {
|
|
51
|
+
// Names from llama's response.project
|
|
52
|
+
const blacklistedProjects = new Set(["extra-finance", "beefy"]);
|
|
53
|
+
return blacklistedProjects.has(project);
|
|
44
54
|
}
|
|
45
55
|
// Initializes all llama pools
|
|
46
|
-
|
|
56
|
+
initializeYields() {
|
|
47
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
48
58
|
try {
|
|
49
|
-
const response = yield fetch(
|
|
59
|
+
const response = yield fetch("https://yields.llama.fi/poolsOld").then((i) => i.json());
|
|
50
60
|
const pools = response.data;
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
const filteredPools = pools.filter((i) => !this.isBlacklisted(i.project));
|
|
62
|
+
// i.pool_old is the pool address with sometimes -{chain} separator if
|
|
63
|
+
// contracts exists on diff chains
|
|
64
|
+
this.llamaAllPools = filteredPools.reduce((acc, i) => {
|
|
53
65
|
const pool = i.pool_old.split("-")[0];
|
|
54
66
|
const llamaChain = i.chain;
|
|
55
67
|
const llamaId = pool.toLowerCase() + "-" + llamaChain.toLowerCase();
|
|
@@ -58,27 +70,55 @@ class OffchainAPRHelper {
|
|
|
58
70
|
}, {});
|
|
59
71
|
}
|
|
60
72
|
catch (err) {
|
|
61
|
-
console.warn("The llamas
|
|
73
|
+
console.warn("The llamas pools down?");
|
|
62
74
|
this.llamaAllPools = {};
|
|
63
75
|
}
|
|
64
76
|
});
|
|
65
77
|
}
|
|
66
|
-
|
|
78
|
+
// Initializes all our chain tvl and borrows from llama
|
|
79
|
+
initializeTvls() {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
try {
|
|
82
|
+
const response = yield fetch("https://api.llama.fi/protocol/impermax-finance").then((i) => i.json());
|
|
83
|
+
const currentChainTvls = response.currentChainTvls;
|
|
84
|
+
this.chainTvls = Object.entries(currentChainTvls).reduce((acc, [key, tvl]) => {
|
|
85
|
+
// Llama capitalizes these for some reason
|
|
86
|
+
acc[key.toLowerCase()] = tvl;
|
|
87
|
+
return acc;
|
|
88
|
+
}, {});
|
|
89
|
+
}
|
|
90
|
+
catch (err) {
|
|
91
|
+
console.warn("The llamas TVL down?");
|
|
92
|
+
this.chainTvls = {};
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
getLlamaYields() {
|
|
67
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
68
98
|
// First call initializes
|
|
69
99
|
if (!this.initialized)
|
|
70
|
-
this.initialized = this.
|
|
100
|
+
this.initialized = this.initializeYields();
|
|
71
101
|
// async mutex to only call llama api once
|
|
72
102
|
yield this.initialized;
|
|
73
103
|
return this.llamaAllPools;
|
|
74
104
|
});
|
|
75
105
|
}
|
|
76
|
-
|
|
106
|
+
getLlamaTvls() {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
// First call initializes
|
|
109
|
+
if (!this.tvlInitialized)
|
|
110
|
+
this.tvlInitialized = this.initializeTvls();
|
|
111
|
+
// async mutex to only call llama api once
|
|
112
|
+
yield this.tvlInitialized;
|
|
113
|
+
return this.chainTvls;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
// ---------------- Getters ---------------- //
|
|
77
117
|
getLlamaAPR(pool, chain) {
|
|
78
118
|
var _a;
|
|
79
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
80
120
|
// Get or initialize the pools
|
|
81
|
-
const llamaAllPools = yield this.
|
|
121
|
+
const llamaAllPools = yield this.getLlamaYields();
|
|
82
122
|
// Map to llama chain names, if not supported, then fetch native way
|
|
83
123
|
const llamaChain = LlamaChains[chain];
|
|
84
124
|
if (!llamaChain)
|
|
@@ -91,5 +131,36 @@ class OffchainAPRHelper {
|
|
|
91
131
|
return apy || 0;
|
|
92
132
|
});
|
|
93
133
|
}
|
|
134
|
+
getLlamaTvlAndBorrows(chain) {
|
|
135
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
136
|
+
// Get or initialize the tvl for all chains
|
|
137
|
+
const llamaTvls = yield this.getLlamaTvls();
|
|
138
|
+
const llamaChain = LlamaChains[chain];
|
|
139
|
+
if (!llamaChain)
|
|
140
|
+
return { tvl: 0, borrowed: 0 };
|
|
141
|
+
const tvl = llamaTvls[llamaChain];
|
|
142
|
+
const borrowed = llamaTvls[llamaChain.concat("-borrowed")];
|
|
143
|
+
return { tvl, borrowed };
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
// Overall Cross-chain stats
|
|
147
|
+
getImpermaxBorrows() {
|
|
148
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
149
|
+
const llamaTvls = yield this.getLlamaTvls();
|
|
150
|
+
return llamaTvls.borrowed;
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
getImpermaxTvl() {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
const llamaTvls = yield this.getLlamaTvls();
|
|
156
|
+
let crossChainTvl = 0;
|
|
157
|
+
// LLama key is `{chain}` or `{chain}-borrowed`, need to manually add the tvl
|
|
158
|
+
for (const [chain, tvl] of Object.entries(llamaTvls)) {
|
|
159
|
+
if (!chain.includes("borrowed"))
|
|
160
|
+
crossChainTvl += tvl;
|
|
161
|
+
}
|
|
162
|
+
return crossChainTvl;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
94
165
|
}
|
|
95
166
|
exports.default = OffchainAPRHelper;
|
|
@@ -42,6 +42,7 @@ const coingecko_ids = {
|
|
|
42
42
|
"0xa25b25548b4c98b0c7d3d27dca5d5ca743d68b7f": "wrapped-rseth",
|
|
43
43
|
"0x80137510979822322193fc997d400d5a6c747bf7": "stakestone-ether",
|
|
44
44
|
"0x2b6a85cd35d15691357eea61d88cb3f401a92fc3": "surveyor-dao",
|
|
45
|
+
"0x7dbd14115baf4240aab8c4ef31bf71f76c708420": "uk-real-estate",
|
|
45
46
|
// base
|
|
46
47
|
"0x940181a94a35a4569e4529a3cdfb74e38fd98631": "aerodrome-finance",
|
|
47
48
|
"0x4ed4E862860beD51a9570b96d89aF5E1B0Efefed": "degen-base",
|
|
@@ -50,6 +51,8 @@ const coingecko_ids = {
|
|
|
50
51
|
"0xa3d1a8deb97b111454b294e2324efad13a9d8396": "overnight-finance",
|
|
51
52
|
"0x4154550f4db74dc38d1fe98e1f3f28ed6dad627d": "jarvis-synthetic-euro",
|
|
52
53
|
"0xa61beb4a3d02decb01039e378237032b351125b4": "eura",
|
|
54
|
+
"0x7002458b1df59eccb57387bc79ffc7c29e22e6f7": "origin-protocol",
|
|
55
|
+
"0xdbfefd2e8460a6ee4955a68582f85708baea60a3": "super-oeth",
|
|
53
56
|
// optimism
|
|
54
57
|
"0x9560e827af36c94d2ac33a39bce1fe78631088db": "velodrome-finance",
|
|
55
58
|
"0x4200000000000000000000000000000000000042": "optimism",
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Address } from '../config/types';
|
|
2
|
+
import { PoolTokenData } from './types';
|
|
3
|
+
import Offchain from './index';
|
|
4
|
+
export default abstract class PoolToken {
|
|
5
|
+
abstract getOffchain(): Offchain;
|
|
6
|
+
abstract getPoolTokenData(): Promise<PoolTokenData>;
|
|
7
|
+
protected getPoolTokenParam(param: string): Promise<string>;
|
|
8
|
+
protected getPoolTokenParamFloat(param: string): Promise<number>;
|
|
9
|
+
protected getUnderlyingParam(param: string): Promise<string>;
|
|
10
|
+
getName(): Promise<string>;
|
|
11
|
+
getSymbol(): Promise<string>;
|
|
12
|
+
getDecimals(): Promise<number>;
|
|
13
|
+
getExchangeRate(): Promise<number>;
|
|
14
|
+
getUnderlyingAddress(): Promise<Address>;
|
|
15
|
+
getTotalBalance(): Promise<number>;
|
|
16
|
+
getTotalBalanceUSD(): Promise<number>;
|
|
17
|
+
calculateCollateralPrice(reserve0: number, reserve1: number, totalSupply: number, price0: number, price1: number): number;
|
|
18
|
+
getTokenPriceFast(fallback?: boolean): Promise<number>;
|
|
19
|
+
abstract getTokenPriceAccurate(): Promise<number>;
|
|
20
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const eth_1 = require("../config/eth");
|
|
13
|
+
const weths_1 = require("../config/contracts/weths");
|
|
14
|
+
class PoolToken {
|
|
15
|
+
// Helpers
|
|
16
|
+
getPoolTokenParam(param) {
|
|
17
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
const poolTokenData = yield this.getPoolTokenData();
|
|
19
|
+
return poolTokenData[param];
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
getPoolTokenParamFloat(param) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
return parseFloat(yield this.getPoolTokenParam(param));
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
getUnderlyingParam(param) {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
const poolTokenData = yield this.getPoolTokenData();
|
|
30
|
+
return poolTokenData.underlying[param];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
// Name
|
|
34
|
+
getName() {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const underlying = yield this.getUnderlyingAddress();
|
|
37
|
+
if (underlying.toLowerCase() === weths_1.WETH[this.getOffchain().network].toLowerCase())
|
|
38
|
+
return eth_1.ETH_NAME[this.getOffchain().network];
|
|
39
|
+
if (underlying.toLowerCase() === "0xa3fa99a148fa48d14ed51d610c367c61876997f1")
|
|
40
|
+
return "MIMATIC";
|
|
41
|
+
if (underlying.toLowerCase() === "0x2e9a6df78e42a30712c10a9dc4b1c8656f8f2879")
|
|
42
|
+
return "Maker";
|
|
43
|
+
return this.getUnderlyingParam("name");
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// Symbol
|
|
47
|
+
getSymbol() {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const underlying = yield this.getUnderlyingAddress();
|
|
50
|
+
if (underlying.toLowerCase() === weths_1.WETH[this.getOffchain().network].toLowerCase())
|
|
51
|
+
return eth_1.ETH_SYMBOL[this.getOffchain().network];
|
|
52
|
+
if (underlying.toLowerCase() === "0xa3fa99a148fa48d14ed51d610c367c61876997f1")
|
|
53
|
+
return "MAI";
|
|
54
|
+
if (underlying.toLowerCase() === "0x2e9a6df78e42a30712c10a9dc4b1c8656f8f2879")
|
|
55
|
+
return "MKR";
|
|
56
|
+
if (underlying.toLowerCase() === "0xc165d941481e68696f43ee6e99bfb2b23e0e3114")
|
|
57
|
+
return "OXDv1";
|
|
58
|
+
if (underlying.toLowerCase() === "0xc5a9848b9d145965d821aaec8fa32aaee026492d")
|
|
59
|
+
return "OXDv2";
|
|
60
|
+
if (underlying.toLowerCase() === "0x6ae7dfc73e0dde2aa99ac063dcf7e8a63265108c")
|
|
61
|
+
return "JPYCv1";
|
|
62
|
+
if (underlying.toLowerCase() === "0x431d5dff03120afa4bdf332c61a6e1766ef37bdb")
|
|
63
|
+
return "JPYCv2";
|
|
64
|
+
return this.getUnderlyingParam("symbol");
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// Decimals
|
|
68
|
+
getDecimals() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
return parseInt(yield this.getUnderlyingParam("decimals"));
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
// ExchangeRate
|
|
74
|
+
getExchangeRate() {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
return this.getPoolTokenParamFloat("exchangeRate");
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
// Underlying Address
|
|
80
|
+
getUnderlyingAddress() {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
82
|
+
return this.getUnderlyingParam("id");
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
// Total balance
|
|
86
|
+
getTotalBalance() {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
return this.getPoolTokenParamFloat("totalBalance");
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
getTotalBalanceUSD() {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const totalBalance = yield this.getTotalBalance();
|
|
94
|
+
const tokenPrice = yield this.getTokenPriceFast();
|
|
95
|
+
return totalBalance * tokenPrice;
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
// Calculate collateral price -> returns the price of the collateral, not necessarily of the underlying LP
|
|
99
|
+
calculateCollateralPrice(reserve0, reserve1, totalSupply, price0, price1) {
|
|
100
|
+
if (totalSupply === 0) {
|
|
101
|
+
console.error("Warning: totalSupply is 0");
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
const reserve0USD = reserve0 * price0;
|
|
105
|
+
const reserve1USD = reserve1 * price1;
|
|
106
|
+
// adjust reserves taking the latest price passed as parameters
|
|
107
|
+
const adjustedReserveUSD = Math.sqrt(reserve0USD * reserve1USD) * 2;
|
|
108
|
+
return adjustedReserveUSD / totalSupply;
|
|
109
|
+
}
|
|
110
|
+
// Token price getters
|
|
111
|
+
getTokenPriceFast(fallback = true) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
if (fallback) {
|
|
114
|
+
return this.getTokenPriceAccurate();
|
|
115
|
+
}
|
|
116
|
+
return 0;
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
exports.default = PoolToken;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Offchain from "./index";
|
|
2
|
+
import { Address } from '../config/types';
|
|
3
|
+
export default class PriceHelper {
|
|
4
|
+
offchain: Offchain;
|
|
5
|
+
subgraphTokensPrice: Promise<{
|
|
6
|
+
[key in Address]?: number;
|
|
7
|
+
}> | null;
|
|
8
|
+
debankTokensPrice: {
|
|
9
|
+
[key in Address]?: Promise<number>;
|
|
10
|
+
};
|
|
11
|
+
constructor(offchain: Offchain);
|
|
12
|
+
cleanCache(): void;
|
|
13
|
+
private addPriceIfMissing;
|
|
14
|
+
private initializeSubgraphTokenPrice;
|
|
15
|
+
getSubgraphTokensPrice(): Promise<{
|
|
16
|
+
[key in Address]: number | undefined;
|
|
17
|
+
}>;
|
|
18
|
+
private getCoingeckoTokenPrice;
|
|
19
|
+
private initializeDebankTokenPrice;
|
|
20
|
+
getDebankTokenPrice(tokenAddress: Address): Promise<number>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const debank_ids_1 = require("../config/debank-ids");
|
|
13
|
+
// coingecko has priority over debank for initialized tokens
|
|
14
|
+
const coingecko_ids = {
|
|
15
|
+
// Arbitrum
|
|
16
|
+
"0xb348b87b23d5977e2948e6f36ca07e1ec94d7328": "swapfish",
|
|
17
|
+
"0x2f2a2543b76a4166549f7aab2e75bef0aefc5b0f": "wrapped-bitcoin",
|
|
18
|
+
"0x463913d3a3d3d291667d53b8325c598eb88d3b0e": "solidlizard",
|
|
19
|
+
"0xaaa6c1e32c55a7bfa8066a6fae9b42650f262418": "ramses-exchange",
|
|
20
|
+
"0xd5954c3084a1ccd70b4da011e67760b8e78aee84": "arbitrum-exchange",
|
|
21
|
+
"0xff191514a9baba76bfd19e3943a4d37e8ec9a111": "auragi",
|
|
22
|
+
"0x15b2fb8f08e4ac1ce019eadae02ee92aedf06851": "chronos-finance",
|
|
23
|
+
"0xaae0c3856e665ff9b3e2872b6d75939d810b7e40": "yieldfarming-index",
|
|
24
|
+
"0xe80772eaf6e2e18b651f160bc9158b2a5cafca65": "usd",
|
|
25
|
+
// Avalanche
|
|
26
|
+
"0x3712871408a829c5cd4e86da1f4ce727efcd28f6": "glacier",
|
|
27
|
+
// ZkSync
|
|
28
|
+
"0x503234f203fc7eb888eec8513210612a43cf6115": "liquity-usd",
|
|
29
|
+
"0x85d84c774cf8e9ff85342684b0e795df72a24908": "velocore",
|
|
30
|
+
"0x5756a28e2aae01f600fc2c01358395f5c1f8ad3a": "vesync",
|
|
31
|
+
};
|
|
32
|
+
class PriceHelper {
|
|
33
|
+
constructor(offchain) {
|
|
34
|
+
this.offchain = offchain;
|
|
35
|
+
this.debankTokensPrice = {};
|
|
36
|
+
}
|
|
37
|
+
cleanCache() {
|
|
38
|
+
this.subgraphTokensPrice = null;
|
|
39
|
+
this.debankTokensPrice = {};
|
|
40
|
+
}
|
|
41
|
+
// First source of token price: Offchain (less accurate, faster)
|
|
42
|
+
addPriceIfMissing(offchainTokenPrice, token) {
|
|
43
|
+
if (offchainTokenPrice[token.id])
|
|
44
|
+
return;
|
|
45
|
+
offchainTokenPrice[token.id] = parseFloat(token.derivedUSD);
|
|
46
|
+
}
|
|
47
|
+
initializeSubgraphTokenPrice() {
|
|
48
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
+
const lendingPoolsData = yield this.offchain.getLendingPoolsData();
|
|
50
|
+
const offchainTokenPrice = {};
|
|
51
|
+
for (const factory in lendingPoolsData) {
|
|
52
|
+
for (const id in lendingPoolsData[factory]) {
|
|
53
|
+
const lendingPool = lendingPoolsData[factory][id];
|
|
54
|
+
this.addPriceIfMissing(offchainTokenPrice, lendingPool.borrowable0.underlying);
|
|
55
|
+
this.addPriceIfMissing(offchainTokenPrice, lendingPool.borrowable1.underlying);
|
|
56
|
+
for (const reward of lendingPool.pair.rewards) {
|
|
57
|
+
this.addPriceIfMissing(offchainTokenPrice, reward.rewardsToken);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//fix for IMX
|
|
62
|
+
offchainTokenPrice["0x9c67ee39e3c4954396b9142010653f17257dd39c"] = 0;
|
|
63
|
+
offchainTokenPrice["0x7b35ce522cb72e4077baeb96cb923a5529764a00"] = 0;
|
|
64
|
+
offchainTokenPrice["0xea6887e4a9cda1b77e70129e5fba830cdb5cddef"] = 0;
|
|
65
|
+
offchainTokenPrice["0x60bb3d364b765c497c8ce50ae0ae3f0882c5bd05"] = 0;
|
|
66
|
+
offchainTokenPrice["0x900f1ec5819fa087d368877cd03b265bf1802667"] = 0;
|
|
67
|
+
offchainTokenPrice["0xea38f1ccf77bf43f352636241b05dd8f6f5f52b2"] = 0;
|
|
68
|
+
return offchainTokenPrice;
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
getSubgraphTokensPrice() {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
if (!this.subgraphTokensPrice)
|
|
74
|
+
this.subgraphTokensPrice = this.initializeSubgraphTokenPrice();
|
|
75
|
+
return this.subgraphTokensPrice;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
// Second source of token price: Debank (more accurate, slower)
|
|
79
|
+
getCoingeckoTokenPrice(tokenAddress) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
try {
|
|
82
|
+
const tokenId = coingecko_ids[tokenAddress];
|
|
83
|
+
const response = yield fetch("https://api.coingecko.com/api/v3/simple/price?ids=" + tokenId + "&vs_currencies=usd");
|
|
84
|
+
if (response.status != 200)
|
|
85
|
+
return 0;
|
|
86
|
+
const data = yield response.json();
|
|
87
|
+
if (!data)
|
|
88
|
+
return 0;
|
|
89
|
+
return data[tokenId].usd ? data[tokenId].usd : 0;
|
|
90
|
+
}
|
|
91
|
+
catch (_a) {
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
initializeDebankTokenPrice(tokenAddress) {
|
|
97
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
98
|
+
if (Object.keys(coingecko_ids).includes(tokenAddress.toLowerCase())) {
|
|
99
|
+
const result = yield this.getCoingeckoTokenPrice(tokenAddress.toLowerCase());
|
|
100
|
+
if (result)
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
try {
|
|
104
|
+
const response = yield fetch("https://openapi.debank.com/v1/token?chain_id=" + debank_ids_1.DEBANK_IDS[this.offchain.network] + "&id=" + tokenAddress);
|
|
105
|
+
/*const response = await fetch(
|
|
106
|
+
"https://pro-openapi.debank.com/v1/token?chain_id=" + debank_chain_ids[this.offchain.network] + "&id=" + tokenAddress,
|
|
107
|
+
{headers: {"AccessKey": "0ac0fba6aeb0905b4c9a1c5cfd9d48d74fbc60e0"}}
|
|
108
|
+
);*/
|
|
109
|
+
if (response.status != 200)
|
|
110
|
+
return 0;
|
|
111
|
+
const data = yield response.json();
|
|
112
|
+
if (!data)
|
|
113
|
+
return 0;
|
|
114
|
+
return data.price ? data.price : 0;
|
|
115
|
+
}
|
|
116
|
+
catch (_a) {
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
getDebankTokenPrice(tokenAddress) {
|
|
122
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
123
|
+
if (tokenAddress.toLowerCase() === '0xf28164a485b0b2c90639e47b0f377b4a438a16b1') { // fix for dQuick
|
|
124
|
+
return (yield this.getDebankTokenPrice('0x831753dd7087cac61ab5644b308642cc1c33dc13')) * 1.68;
|
|
125
|
+
}
|
|
126
|
+
if (tokenAddress.toLowerCase() === '0x63468133ed352e602beb61dd254d6060ad2fe419') { // fix for sTHO
|
|
127
|
+
return (yield this.getDebankTokenPrice('0xae4aa155d2987b454c29450ef4f862cf00907b61')) * 1.1;
|
|
128
|
+
}
|
|
129
|
+
if (tokenAddress.toLowerCase() === '0xfcef8a994209d6916eb2c86cdd2afd60aa6f54b1') { // fix for fBEETS
|
|
130
|
+
return (yield this.getDebankTokenPrice('0xf24bcf4d1e507740041c9cfd2dddb29585adce1e')) * 0.93;
|
|
131
|
+
}
|
|
132
|
+
if (tokenAddress.toLowerCase() === '0x5979d7b546e38e414f7e9822514be443a4800529') { // TEMPORARY fix for WSTETH
|
|
133
|
+
return (yield this.getDebankTokenPrice('0x82af49447d8a07e3bd95bd0d56f35241523fbab1')) / 0.92;
|
|
134
|
+
}
|
|
135
|
+
if (tokenAddress.toLowerCase() === '0x3f56e0c36d275367b8c502090edf38289b3dea0d') { // TEMPORARY fix for MAI
|
|
136
|
+
return 1;
|
|
137
|
+
}
|
|
138
|
+
if (tokenAddress.toLowerCase() === '0xc71aaf8e486e3f33841bb56ca3fd2ac3fa8d29a8') { // TEMPORARY fix for WSTETH Canto
|
|
139
|
+
return (yield this.getDebankTokenPrice('0x5fd55a1b9fc24967c4db09c513c3ba0dfa7ff687')) * 1.12;
|
|
140
|
+
}
|
|
141
|
+
if (tokenAddress.toLowerCase() === '0x152b9d0fdc40c096757f570a51e494bd4b943e50') { // TEMPORARY fix for BTC.b AVAX
|
|
142
|
+
return (yield this.getDebankTokenPrice('0x50b7545627a5162f82a992c33b87adc75187b218'));
|
|
143
|
+
}
|
|
144
|
+
if (tokenAddress.toLowerCase() === '0x9fc3104f6fc188fee65c85bbc4b94a48282ae76d') { // TEMPORARY fix for SATIN
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
if ([
|
|
148
|
+
"0x9c67ee39e3c4954396b9142010653f17257dd39c",
|
|
149
|
+
"0x7b35ce522cb72e4077baeb96cb923a5529764a00",
|
|
150
|
+
"0xea6887e4a9cda1b77e70129e5fba830cdb5cddef",
|
|
151
|
+
"0x60bb3d364b765c497c8ce50ae0ae3f0882c5bd05",
|
|
152
|
+
"0x900f1ec5819fa087d368877cd03b265bf1802667",
|
|
153
|
+
"0xea38f1ccf77bf43f352636241b05dd8f6f5f52b2",
|
|
154
|
+
].includes(tokenAddress.toLowerCase())) { // fix for IMX
|
|
155
|
+
return 0;
|
|
156
|
+
}
|
|
157
|
+
if (tokenAddress.toLowerCase() === '0xda0053f0befcbcac208a3f867bb243716734d809') { // TEMPORARY fix for oxSOLID
|
|
158
|
+
return (yield this.getDebankTokenPrice('0x888ef71766ca594ded1f0fa3ae64ed2941740a20')) * 0.88;
|
|
159
|
+
}
|
|
160
|
+
if (tokenAddress.toLowerCase() === '0x431d5dff03120afa4bdf332c61a6e1766ef37bdb') { // TEMPORARY fix for JPYCv2
|
|
161
|
+
return (yield this.getDebankTokenPrice('0x6ae7dfc73e0dde2aa99ac063dcf7e8a63265108c'));
|
|
162
|
+
}
|
|
163
|
+
if (!this.debankTokensPrice[tokenAddress])
|
|
164
|
+
this.debankTokensPrice[tokenAddress] = this.initializeDebankTokenPrice(tokenAddress);
|
|
165
|
+
const tokenPrice = yield this.debankTokensPrice[tokenAddress];
|
|
166
|
+
if (tokenPrice === 0) {
|
|
167
|
+
//manual fallback
|
|
168
|
+
if (tokenAddress.toLowerCase() === '0xaaa6c1e32c55a7bfa8066a6fae9b42650f262418') { // fix for RAM
|
|
169
|
+
return 0.02;
|
|
170
|
+
}
|
|
171
|
+
if (tokenAddress.toLowerCase() === '0xd5954c3084a1ccd70b4da011e67760b8e78aee84') { // fix for ARX
|
|
172
|
+
return 0.5;
|
|
173
|
+
}
|
|
174
|
+
if (tokenAddress.toLowerCase() === '0xe80772eaf6e2e18b651f160bc9158b2a5cafca65') { // fix for USD+
|
|
175
|
+
return 1;
|
|
176
|
+
}
|
|
177
|
+
if (tokenAddress.toLowerCase() === '0x3712871408a829c5cd4e86da1f4ce727efcd28f6') { // fix for GLCR
|
|
178
|
+
return 0.011;
|
|
179
|
+
}
|
|
180
|
+
if (tokenAddress.toLowerCase() === '0x5756a28e2aae01f600fc2c01358395f5c1f8ad3a') { // fix for VS
|
|
181
|
+
return 0.05;
|
|
182
|
+
}
|
|
183
|
+
if (tokenAddress.toLowerCase() === '0x1c28edf9e0d66f3124f141a3cf3ef1217f7019c4') { // fix for SLNA
|
|
184
|
+
return 0.00045;
|
|
185
|
+
}
|
|
186
|
+
// try fallback on offchain
|
|
187
|
+
const subgraphTokensPrice = yield this.getSubgraphTokensPrice();
|
|
188
|
+
const subgraphTokenPrice = subgraphTokensPrice[tokenAddress];
|
|
189
|
+
return subgraphTokenPrice ? subgraphTokenPrice : 0;
|
|
190
|
+
}
|
|
191
|
+
return tokenPrice;
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.default = PriceHelper;
|