impermax-sdk 1.2.70 → 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.
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
import { Address, Networks } from
|
|
1
|
+
import { Address, Networks } from "../config/types";
|
|
2
2
|
import Offchain from "./index";
|
|
3
3
|
export default class OffchainAPRHelper {
|
|
4
4
|
private llamaAllPools;
|
|
5
5
|
private initialized;
|
|
6
|
+
private chainTvls;
|
|
7
|
+
private tvlInitialized;
|
|
6
8
|
offchain: Offchain;
|
|
7
9
|
constructor(offchain: Offchain);
|
|
8
10
|
cleanCache(): void;
|
|
9
11
|
private isBlacklisted;
|
|
10
|
-
private
|
|
11
|
-
private
|
|
12
|
+
private initializeYields;
|
|
13
|
+
private initializeTvls;
|
|
14
|
+
private getLlamaYields;
|
|
15
|
+
private getLlamaTvls;
|
|
12
16
|
getLlamaAPR(pool: Address, chain: Networks): Promise<number>;
|
|
17
|
+
getLlamaTvlAndBorrows(chain: Networks): Promise<{
|
|
18
|
+
tvl: number;
|
|
19
|
+
borrowed: number;
|
|
20
|
+
}>;
|
|
21
|
+
getImpermaxBorrows(): Promise<number>;
|
|
22
|
+
getImpermaxTvl(): Promise<number>;
|
|
13
23
|
}
|
|
@@ -12,50 +12,54 @@ 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;
|
|
44
48
|
}
|
|
45
49
|
// Blacklist certain projects which might report same pool as dex (aggregators, etc.)
|
|
46
50
|
isBlacklisted(project) {
|
|
47
51
|
// Names from llama's response.project
|
|
48
|
-
const blacklistedProjects = new Set([
|
|
52
|
+
const blacklistedProjects = new Set(["extra-finance", "beefy"]);
|
|
49
53
|
return blacklistedProjects.has(project);
|
|
50
54
|
}
|
|
51
55
|
// Initializes all llama pools
|
|
52
|
-
|
|
56
|
+
initializeYields() {
|
|
53
57
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
58
|
try {
|
|
55
|
-
const response = yield fetch(
|
|
59
|
+
const response = yield fetch("https://yields.llama.fi/poolsOld").then((i) => i.json());
|
|
56
60
|
const pools = response.data;
|
|
57
|
-
const filteredPools = pools.filter(i => !this.isBlacklisted(i.project));
|
|
58
|
-
// i.pool_old is the pool address with sometimes -{chain} separator if
|
|
61
|
+
const filteredPools = pools.filter((i) => !this.isBlacklisted(i.project));
|
|
62
|
+
// i.pool_old is the pool address with sometimes -{chain} separator if
|
|
59
63
|
// contracts exists on diff chains
|
|
60
64
|
this.llamaAllPools = filteredPools.reduce((acc, i) => {
|
|
61
65
|
const pool = i.pool_old.split("-")[0];
|
|
@@ -66,27 +70,55 @@ class OffchainAPRHelper {
|
|
|
66
70
|
}, {});
|
|
67
71
|
}
|
|
68
72
|
catch (err) {
|
|
69
|
-
console.warn("The llamas
|
|
73
|
+
console.warn("The llamas pools down?");
|
|
70
74
|
this.llamaAllPools = {};
|
|
71
75
|
}
|
|
72
76
|
});
|
|
73
77
|
}
|
|
74
|
-
|
|
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() {
|
|
75
97
|
return __awaiter(this, void 0, void 0, function* () {
|
|
76
98
|
// First call initializes
|
|
77
99
|
if (!this.initialized)
|
|
78
|
-
this.initialized = this.
|
|
100
|
+
this.initialized = this.initializeYields();
|
|
79
101
|
// async mutex to only call llama api once
|
|
80
102
|
yield this.initialized;
|
|
81
103
|
return this.llamaAllPools;
|
|
82
104
|
});
|
|
83
105
|
}
|
|
84
|
-
|
|
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 ---------------- //
|
|
85
117
|
getLlamaAPR(pool, chain) {
|
|
86
118
|
var _a;
|
|
87
119
|
return __awaiter(this, void 0, void 0, function* () {
|
|
88
120
|
// Get or initialize the pools
|
|
89
|
-
const llamaAllPools = yield this.
|
|
121
|
+
const llamaAllPools = yield this.getLlamaYields();
|
|
90
122
|
// Map to llama chain names, if not supported, then fetch native way
|
|
91
123
|
const llamaChain = LlamaChains[chain];
|
|
92
124
|
if (!llamaChain)
|
|
@@ -99,5 +131,36 @@ class OffchainAPRHelper {
|
|
|
99
131
|
return apy || 0;
|
|
100
132
|
});
|
|
101
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
|
+
}
|
|
102
165
|
}
|
|
103
166
|
exports.default = OffchainAPRHelper;
|