@curvefi/llamalend-api 2.0.22 → 2.0.24
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/external-api.d.ts +12 -1
- package/lib/external-api.js +12 -7
- package/lib/index.d.ts +6 -2
- package/lib/index.js +1 -2
- package/lib/lendMarkets/fetch/fetchLendMarkets.js +0 -13
- package/lib/llamalend.d.ts +8 -4
- package/lib/llamalend.js +107 -179
- package/lib/mintMarkets/fetch/fetchMintMarkets.d.ts +3 -0
- package/lib/mintMarkets/fetch/fetchMintMarkets.js +138 -0
- package/package.json +1 -1
- package/src/external-api.ts +28 -12
- package/src/index.ts +1 -3
- package/src/lendMarkets/fetch/fetchLendMarkets.ts +0 -14
- package/src/llamalend.ts +124 -201
- package/src/mintMarkets/fetch/fetchMintMarkets.ts +151 -0
package/lib/external-api.d.ts
CHANGED
|
@@ -16,8 +16,19 @@ export declare const _getUserCollateralForce: (network: INetworkName, controller
|
|
|
16
16
|
export declare const _getUserCollateralCrvUsd: ((network: INetworkName, controller: string, user: string) => Promise<string>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<string>>;
|
|
17
17
|
export declare const _getUserCollateralCrvUsdFull: ((network: INetworkName, controller: string, user: string) => Promise<UserCollateral>) & memoize.Memoized<(network: INetworkName, controller: string, user: string) => Promise<UserCollateral>>;
|
|
18
18
|
export declare const _getMarketsData: ((network: INetworkName) => Promise<IMarketData>) & memoize.Memoized<(network: INetworkName) => Promise<IMarketData>>;
|
|
19
|
+
export interface ICrvUsdMarketAPI {
|
|
20
|
+
address: string;
|
|
21
|
+
llamma: string;
|
|
22
|
+
amm_a: number;
|
|
23
|
+
monetary_policy_address: string;
|
|
24
|
+
collateral_token: {
|
|
25
|
+
symbol: string;
|
|
26
|
+
address: string;
|
|
27
|
+
decimals: number;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
export declare const _getCrvUsdMarketsData: (() => Promise<ICrvUsdMarketAPI[]>) & memoize.Memoized<() => Promise<ICrvUsdMarketAPI[]>>;
|
|
19
31
|
export declare function _getQuoteOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string, pathVizImage: boolean, slippage?: number): Promise<IQuoteOdos>;
|
|
20
32
|
export declare function _getExpectedOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string): Promise<string>;
|
|
21
33
|
export declare function _assembleTxOdos(this: Llamalend, pathId: string): Promise<string>;
|
|
22
|
-
export declare const _getHiddenPools: (() => Promise<any>) & memoize.Memoized<() => Promise<any>>;
|
|
23
34
|
export {};
|
package/lib/external-api.js
CHANGED
|
@@ -138,6 +138,18 @@ export const _getMarketsData = memoize((network) => __awaiter(void 0, void 0, vo
|
|
|
138
138
|
promise: true,
|
|
139
139
|
maxAge: 10 * 1000, // 10s
|
|
140
140
|
});
|
|
141
|
+
export const _getCrvUsdMarketsData = memoize(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
142
|
+
const url = 'https://prices.curve.finance/v1/crvusd/markets/ethereum';
|
|
143
|
+
const response = yield fetch(url, { headers: { "accept": "application/json" } });
|
|
144
|
+
if (response.status !== 200) {
|
|
145
|
+
throw Error(`Fetch error: ${response.status} ${response.statusText}`);
|
|
146
|
+
}
|
|
147
|
+
const { data } = yield response.json();
|
|
148
|
+
return data;
|
|
149
|
+
}), {
|
|
150
|
+
promise: true,
|
|
151
|
+
maxAge: 10 * 1000, // 10s
|
|
152
|
+
});
|
|
141
153
|
// --- ODOS ---
|
|
142
154
|
export function _getQuoteOdos(fromToken_1, toToken_1, _amount_1, blacklist_1, pathVizImage_1) {
|
|
143
155
|
return __awaiter(this, arguments, void 0, function* (fromToken, toToken, _amount, blacklist, pathVizImage, slippage = 0.5) {
|
|
@@ -182,13 +194,6 @@ export function _assembleTxOdos(pathId) {
|
|
|
182
194
|
return _assembleTxOdosMemoized(this.constants.ALIASES.leverage_zap, pathId);
|
|
183
195
|
});
|
|
184
196
|
}
|
|
185
|
-
export const _getHiddenPools = memoize(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
186
|
-
const response = yield fetch(`https://api.curve.finance/api/getHiddenPools`);
|
|
187
|
-
return (yield response.json()).data;
|
|
188
|
-
}), {
|
|
189
|
-
promise: true,
|
|
190
|
-
maxAge: 5 * 60 * 1000, // 5m
|
|
191
|
-
});
|
|
192
197
|
function fetchJson(url) {
|
|
193
198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
194
199
|
var _a;
|
package/lib/index.d.ts
CHANGED
|
@@ -50,9 +50,11 @@ export declare function createLlamalend(): {
|
|
|
50
50
|
getGasPriceFromL1: () => Promise<number>;
|
|
51
51
|
getGasPriceFromL2: () => Promise<number>;
|
|
52
52
|
getGasInfoForL2: () => Promise<Record<string, number>>;
|
|
53
|
-
fetchStats: (amms: string[], controllers: string[], vaults: string[], borrowed_tokens: string[], collateral_tokens: string[], version?: "v1" | "v2") => Promise<void>;
|
|
54
53
|
mintMarkets: {
|
|
55
54
|
getMarketList: () => string[];
|
|
55
|
+
fetchMintMarkets: ({ useApi }?: {
|
|
56
|
+
useApi?: boolean;
|
|
57
|
+
}) => Promise<void>;
|
|
56
58
|
};
|
|
57
59
|
lendMarkets: {
|
|
58
60
|
fetchMarkets: ({ useApi, version }?: {
|
|
@@ -152,9 +154,11 @@ declare const _default: {
|
|
|
152
154
|
getGasPriceFromL1: () => Promise<number>;
|
|
153
155
|
getGasPriceFromL2: () => Promise<number>;
|
|
154
156
|
getGasInfoForL2: () => Promise<Record<string, number>>;
|
|
155
|
-
fetchStats: (amms: string[], controllers: string[], vaults: string[], borrowed_tokens: string[], collateral_tokens: string[], version?: "v1" | "v2") => Promise<void>;
|
|
156
157
|
mintMarkets: {
|
|
157
158
|
getMarketList: () => string[];
|
|
159
|
+
fetchMintMarkets: ({ useApi }?: {
|
|
160
|
+
useApi?: boolean;
|
|
161
|
+
}) => Promise<void>;
|
|
158
162
|
};
|
|
159
163
|
lendMarkets: {
|
|
160
164
|
fetchMarkets: ({ useApi, version }?: {
|
package/lib/index.js
CHANGED
|
@@ -43,11 +43,10 @@ export function createLlamalend() {
|
|
|
43
43
|
getGasPriceFromL1: getGasPriceFromL1.bind(llamalend),
|
|
44
44
|
getGasPriceFromL2: getGasPriceFromL2.bind(llamalend),
|
|
45
45
|
getGasInfoForL2: getGasInfoForL2.bind(llamalend),
|
|
46
|
-
// Core methods
|
|
47
|
-
fetchStats: llamalend.fetchStats.bind(llamalend),
|
|
48
46
|
// Market lists
|
|
49
47
|
mintMarkets: {
|
|
50
48
|
getMarketList: llamalend.getMintMarketList.bind(llamalend),
|
|
49
|
+
fetchMintMarkets: llamalend.fetchMintMarkets.bind(llamalend),
|
|
51
50
|
},
|
|
52
51
|
lendMarkets: {
|
|
53
52
|
fetchMarkets: llamalend.fetchLendMarkets.bind(llamalend),
|
|
@@ -75,13 +75,6 @@ export const fetchOneWayMarketsByBlockchain = (llamalend_1, ...args_1) => __awai
|
|
|
75
75
|
llamalend.constants.DECIMALS[c] = COIN_DATA[c].decimals;
|
|
76
76
|
}
|
|
77
77
|
registerMarkets(llamalend, names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges, COIN_DATA, version);
|
|
78
|
-
if (version === 'v2') {
|
|
79
|
-
llamalend.constants.ONE_WAY_MARKETS_V2 = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS_V2);
|
|
80
|
-
}
|
|
81
|
-
else {
|
|
82
|
-
llamalend.constants.ONE_WAY_MARKETS = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS);
|
|
83
|
-
}
|
|
84
|
-
yield llamalend.fetchStats(amms, controllers, vaults, borrowed_tokens, collateral_tokens, version);
|
|
85
78
|
});
|
|
86
79
|
export const fetchOneWayMarketsByAPI = (llamalend_1, ...args_1) => __awaiter(void 0, [llamalend_1, ...args_1], void 0, function* (llamalend, version = 'v1') {
|
|
87
80
|
const { names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges } = yield getFactoryMarketDataByAPI(llamalend);
|
|
@@ -90,10 +83,4 @@ export const fetchOneWayMarketsByAPI = (llamalend_1, ...args_1) => __awaiter(voi
|
|
|
90
83
|
llamalend.constants.DECIMALS[c] = COIN_DATA[c].decimals;
|
|
91
84
|
}
|
|
92
85
|
registerMarkets(llamalend, names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges, COIN_DATA, version);
|
|
93
|
-
if (version === 'v2') {
|
|
94
|
-
llamalend.constants.ONE_WAY_MARKETS_V2 = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS_V2);
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
llamalend.constants.ONE_WAY_MARKETS = yield llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS);
|
|
98
|
-
}
|
|
99
86
|
});
|
package/lib/llamalend.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ethers, Networkish, BigNumberish, Numeric } from "ethers";
|
|
2
|
-
import { Provider as MulticallProvider } from '@curvefi/ethcall';
|
|
2
|
+
import { Provider as MulticallProvider, Contract as MulticallContract } from '@curvefi/ethcall';
|
|
3
3
|
import { IChainId, ILlamalend, ILlamma, IDict, INetworkName, ICurveContract, IOneWayMarket, ICoin } from "./interfaces.js";
|
|
4
4
|
import { MintMarketTemplate } from "./mintMarkets";
|
|
5
5
|
import { LendMarketTemplate } from "./lendMarkets";
|
|
@@ -67,21 +67,25 @@ declare class Llamalend implements ILlamalend {
|
|
|
67
67
|
maxPriorityFeePerGas?: number;
|
|
68
68
|
chainId?: number;
|
|
69
69
|
}): Promise<void>;
|
|
70
|
-
|
|
70
|
+
initContract: (address: string, abi: any, provider: ethers.BrowserProvider | ethers.JsonRpcProvider | ethers.Signer) => ethers.Contract;
|
|
71
|
+
initMulticallContract: (address: string, abi: any) => MulticallContract;
|
|
72
|
+
setContract(address: string | undefined, abi: any): void;
|
|
71
73
|
setCustomFeeData(customFeeData: {
|
|
72
74
|
gasPrice?: number;
|
|
73
75
|
maxFeePerGas?: number;
|
|
74
76
|
maxPriorityFeePerGas?: number;
|
|
75
77
|
}): void;
|
|
76
|
-
|
|
78
|
+
_setupMintMarketContracts(useApi?: boolean): Promise<void>;
|
|
77
79
|
getLendMarketList: () => string[];
|
|
78
80
|
getMintMarketList: () => string[];
|
|
79
81
|
fetchLendMarkets: ({ useApi, version }?: {
|
|
80
82
|
useApi?: boolean;
|
|
81
83
|
version?: "v1" | "v2";
|
|
82
84
|
}) => Promise<void>;
|
|
85
|
+
fetchMintMarkets: ({ useApi }?: {
|
|
86
|
+
useApi?: boolean;
|
|
87
|
+
}) => Promise<void>;
|
|
83
88
|
getCoins: (collateral_tokens: string[], borrowed_tokens: string[], useApi?: boolean) => Promise<IDict<ICoin>>;
|
|
84
|
-
fetchStats: (amms: string[], controllers: string[], vaults: string[], borrowed_tokens: string[], collateral_tokens: string[], version?: "v1" | "v2") => Promise<void>;
|
|
85
89
|
formatUnits(value: BigNumberish, unit?: string | Numeric): string;
|
|
86
90
|
parseUnits(value: string, unit?: string | Numeric): bigint;
|
|
87
91
|
updateFeeData(): Promise<void>;
|
package/lib/llamalend.js
CHANGED
|
@@ -24,10 +24,7 @@ import gasOracleBlobABI from './constants/abis/gas_oracle_optimism_blob.json' wi
|
|
|
24
24
|
// crvUSD ABIs
|
|
25
25
|
import llammaABI from "./constants/abis/crvUSD/llamma.json" with { type: 'json' };
|
|
26
26
|
import controllerABI from "./constants/abis/crvUSD/controller.json" with { type: 'json' };
|
|
27
|
-
import controllerV2ABI from "./constants/abis/crvUSD/controller_v2.json";
|
|
28
27
|
import PegKeeper from "./constants/abis/crvUSD/PegKeeper.json" with { type: 'json' };
|
|
29
|
-
import FactoryABI from "./constants/abis/crvUSD/Factory.json" with { type: 'json' };
|
|
30
|
-
import MonetaryPolicy2ABI from "./constants/abis/crvUSD/MonetaryPolicy2.json" with { type: 'json' };
|
|
31
28
|
import HealthCalculatorZapABI from "./constants/abis/crvUSD/HealthCalculatorZap.json" with { type: 'json' };
|
|
32
29
|
import LeverageZapCrvUSDABI from "./constants/abis/crvUSD/LeverageZap.json" with { type: 'json' };
|
|
33
30
|
import DeleverageZapABI from "./constants/abis/crvUSD/DeleverageZap.json" with { type: 'json' };
|
|
@@ -36,10 +33,24 @@ import { COINS_ETHEREUM, COINS_OPTIMISM, COINS_ARBITRUM, COINS_FRAXTAL, COINS_SO
|
|
|
36
33
|
import { LLAMMAS } from "./constants/llammas.js";
|
|
37
34
|
import { L2Networks } from "./constants/L2Networks.js";
|
|
38
35
|
import { createCall, handleMultiCallResponse } from "./utils.js";
|
|
39
|
-
import {
|
|
40
|
-
import { _getMarketsData, _getHiddenPools } from "./external-api.js";
|
|
41
|
-
import { extractDecimals } from "./constants/utils.js";
|
|
36
|
+
import { _getMarketsData, _getCrvUsdMarketsData } from "./external-api.js";
|
|
42
37
|
import { fetchOneWayMarketsByBlockchain, fetchOneWayMarketsByAPI } from "./lendMarkets/fetch/fetchLendMarkets.js";
|
|
38
|
+
import { fetchMintMarketsByBlockchain, fetchMintMarketsByAPI } from "./mintMarkets/fetch/fetchMintMarkets.js";
|
|
39
|
+
const memoizeByAddress = (factory) => {
|
|
40
|
+
return () => {
|
|
41
|
+
const cache = {};
|
|
42
|
+
return (address, abi, ...args) => {
|
|
43
|
+
if (address in cache) {
|
|
44
|
+
return cache[address];
|
|
45
|
+
}
|
|
46
|
+
const result = factory(address, abi, ...args);
|
|
47
|
+
cache[address] = result;
|
|
48
|
+
return result;
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const memoizedContract = memoizeByAddress((address, abi, provider) => new Contract(address, abi, provider));
|
|
53
|
+
const memoizedMulticallContract = memoizeByAddress((address, abi) => new MulticallContract(address, abi));
|
|
43
54
|
export const NETWORK_CONSTANTS = {
|
|
44
55
|
1: {
|
|
45
56
|
NAME: 'ethereum',
|
|
@@ -71,6 +82,8 @@ export const NETWORK_CONSTANTS = {
|
|
|
71
82
|
};
|
|
72
83
|
class Llamalend {
|
|
73
84
|
constructor() {
|
|
85
|
+
this.initContract = memoizedContract();
|
|
86
|
+
this.initMulticallContract = memoizedMulticallContract();
|
|
74
87
|
this.getLendMarketList = () => Object.keys(Object.assign(Object.assign({}, this.constants.ONE_WAY_MARKETS), this.constants.ONE_WAY_MARKETS_V2));
|
|
75
88
|
this.getMintMarketList = () => Object.keys(this.constants.LLAMMAS);
|
|
76
89
|
this.fetchLendMarkets = (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ useApi = true, version = 'v1' } = {}) {
|
|
@@ -84,6 +97,12 @@ class Llamalend {
|
|
|
84
97
|
yield fetchOneWayMarketsByBlockchain(this, version);
|
|
85
98
|
}
|
|
86
99
|
});
|
|
100
|
+
this.fetchMintMarkets = (...args_1) => __awaiter(this, [...args_1], void 0, function* ({ useApi = true } = {}) {
|
|
101
|
+
yield Promise.all([
|
|
102
|
+
this._setupMintMarketContracts(useApi),
|
|
103
|
+
useApi ? fetchMintMarketsByAPI(this) : fetchMintMarketsByBlockchain(this),
|
|
104
|
+
]);
|
|
105
|
+
});
|
|
87
106
|
this.getCoins = (collateral_tokens_1, borrowed_tokens_1, ...args_1) => __awaiter(this, [collateral_tokens_1, borrowed_tokens_1, ...args_1], void 0, function* (collateral_tokens, borrowed_tokens, useApi = false) {
|
|
88
107
|
const coins = new Set([...collateral_tokens, ...borrowed_tokens]);
|
|
89
108
|
const COINS_DATA = {};
|
|
@@ -134,46 +153,6 @@ class Llamalend {
|
|
|
134
153
|
}
|
|
135
154
|
return COINS_DATA;
|
|
136
155
|
});
|
|
137
|
-
this.fetchStats = (amms_1, controllers_1, vaults_1, borrowed_tokens_1, collateral_tokens_1, ...args_1) => __awaiter(this, [amms_1, controllers_1, vaults_1, borrowed_tokens_1, collateral_tokens_1, ...args_1], void 0, function* (amms, controllers, vaults, borrowed_tokens, collateral_tokens, version = 'v1') {
|
|
138
|
-
cacheStats.clear();
|
|
139
|
-
const marketCount = controllers.length;
|
|
140
|
-
const calls = [];
|
|
141
|
-
for (let i = 0; i < marketCount; i++) {
|
|
142
|
-
calls.push(createCall(this.contracts[controllers[i]], 'total_debt', []));
|
|
143
|
-
calls.push(createCall(this.contracts[vaults[i]], 'totalAssets', []));
|
|
144
|
-
calls.push(createCall(this.contracts[borrowed_tokens[i]], 'balanceOf', [controllers[i]]));
|
|
145
|
-
calls.push(createCall(this.contracts[amms[i]], 'rate', []));
|
|
146
|
-
calls.push(createCall(this.contracts[borrowed_tokens[i]], 'balanceOf', [amms[i]]));
|
|
147
|
-
if (version === 'v1') {
|
|
148
|
-
calls.push(createCall(this.contracts[amms[i]], 'admin_fees_x', []));
|
|
149
|
-
calls.push(createCall(this.contracts[amms[i]], 'admin_fees_y', []));
|
|
150
|
-
}
|
|
151
|
-
calls.push(createCall(this.contracts[collateral_tokens[i]], 'balanceOf', [amms[i]]));
|
|
152
|
-
}
|
|
153
|
-
const res = yield this.multicallProvider.all(calls);
|
|
154
|
-
for (let i = 0; i < marketCount; i++) {
|
|
155
|
-
if (version === 'v1') {
|
|
156
|
-
cacheStats.set(cacheKey(controllers[i], 'total_debt'), res[(i * 8) + 0]);
|
|
157
|
-
cacheStats.set(cacheKey(vaults[i], 'totalAssets', controllers[i]), res[(i * 8) + 1]);
|
|
158
|
-
cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', controllers[i]), res[(i * 8) + 2]);
|
|
159
|
-
cacheStats.set(cacheKey(amms[i], 'rate'), res[(i * 8) + 3]);
|
|
160
|
-
cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', amms[i]), res[(i * 8) + 4]);
|
|
161
|
-
cacheStats.set(cacheKey(amms[i], 'admin_fees_x'), res[(i * 8) + 5]);
|
|
162
|
-
cacheStats.set(cacheKey(amms[i], 'admin_fees_y'), res[(i * 8) + 6]);
|
|
163
|
-
cacheStats.set(cacheKey(collateral_tokens[i], 'balanceOf', amms[i]), res[(i * 8) + 7]);
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
cacheStats.set(cacheKey(controllers[i], 'total_debt'), res[(i * 6) + 0]);
|
|
167
|
-
cacheStats.set(cacheKey(vaults[i], 'totalAssets', controllers[i]), res[(i * 6) + 1]);
|
|
168
|
-
cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', controllers[i]), res[(i * 6) + 2]);
|
|
169
|
-
cacheStats.set(cacheKey(amms[i], 'rate'), res[(i * 6) + 3]);
|
|
170
|
-
cacheStats.set(cacheKey(borrowed_tokens[i], 'balanceOf', amms[i]), res[(i * 6) + 4]);
|
|
171
|
-
cacheStats.set(cacheKey(amms[i], 'admin_fees_x'), BigInt(0)); // Always 0 for v2
|
|
172
|
-
cacheStats.set(cacheKey(amms[i], 'admin_fees_y'), BigInt(0)); // Always 0 for v2
|
|
173
|
-
cacheStats.set(cacheKey(collateral_tokens[i], 'balanceOf', amms[i]), res[(i * 6) + 5]);
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
156
|
this.address = '00000';
|
|
178
157
|
this.crvUsdAddress = COINS_ETHEREUM.crvusd;
|
|
179
158
|
this.provider = null;
|
|
@@ -241,55 +220,47 @@ class Llamalend {
|
|
|
241
220
|
],
|
|
242
221
|
WETH: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2".toLowerCase(),
|
|
243
222
|
};
|
|
244
|
-
|
|
223
|
+
let signerPromise;
|
|
245
224
|
if (providerType.toLowerCase() === 'JsonRpc'.toLowerCase()) {
|
|
246
225
|
providerSettings = providerSettings;
|
|
247
226
|
let jsonRpcApiProviderOptions;
|
|
248
227
|
if (providerSettings.batchMaxCount) {
|
|
249
|
-
jsonRpcApiProviderOptions = {
|
|
250
|
-
batchMaxCount: providerSettings.batchMaxCount,
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
if (providerSettings.url) {
|
|
254
|
-
this.provider = new ethers.JsonRpcProvider(providerSettings.url, undefined, jsonRpcApiProviderOptions);
|
|
255
|
-
}
|
|
256
|
-
else {
|
|
257
|
-
this.provider = new ethers.JsonRpcProvider('http://localhost:8545/', undefined, jsonRpcApiProviderOptions);
|
|
228
|
+
jsonRpcApiProviderOptions = { batchMaxCount: providerSettings.batchMaxCount };
|
|
258
229
|
}
|
|
230
|
+
this.provider = new ethers.JsonRpcProvider(providerSettings.url || 'http://localhost:8545/', undefined, jsonRpcApiProviderOptions);
|
|
259
231
|
if (providerSettings.privateKey) {
|
|
260
|
-
|
|
232
|
+
signerPromise = Promise.resolve(new ethers.Wallet(providerSettings.privateKey, this.provider));
|
|
261
233
|
}
|
|
262
234
|
else if (!((_a = providerSettings.url) === null || _a === void 0 ? void 0 : _a.startsWith("https://rpc.gnosischain.com"))) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
this.signer = null;
|
|
268
|
-
}
|
|
235
|
+
signerPromise = this.provider.getSigner().catch(() => null);
|
|
236
|
+
}
|
|
237
|
+
else {
|
|
238
|
+
signerPromise = Promise.resolve(null);
|
|
269
239
|
}
|
|
270
|
-
// Web3 provider
|
|
271
240
|
}
|
|
272
241
|
else if (providerType.toLowerCase() === 'Web3'.toLowerCase()) {
|
|
273
242
|
providerSettings = providerSettings;
|
|
274
243
|
this.provider = new ethers.BrowserProvider(providerSettings.externalProvider);
|
|
275
|
-
|
|
276
|
-
// Infura provider
|
|
244
|
+
signerPromise = this.provider.getSigner();
|
|
277
245
|
}
|
|
278
246
|
else if (providerType.toLowerCase() === 'Infura'.toLowerCase()) {
|
|
279
247
|
providerSettings = providerSettings;
|
|
280
248
|
this.provider = new ethers.InfuraProvider(providerSettings.network, providerSettings.apiKey);
|
|
281
|
-
|
|
282
|
-
// Alchemy provider
|
|
249
|
+
signerPromise = Promise.resolve(null);
|
|
283
250
|
}
|
|
284
251
|
else if (providerType.toLowerCase() === 'Alchemy'.toLowerCase()) {
|
|
285
252
|
providerSettings = providerSettings;
|
|
286
253
|
this.provider = new ethers.AlchemyProvider(providerSettings.network, providerSettings.apiKey);
|
|
287
|
-
|
|
254
|
+
signerPromise = Promise.resolve(null);
|
|
288
255
|
}
|
|
289
256
|
else {
|
|
290
257
|
throw Error('Wrong providerType');
|
|
291
258
|
}
|
|
292
|
-
const network = yield
|
|
259
|
+
const [signer, network] = yield Promise.all([
|
|
260
|
+
signerPromise,
|
|
261
|
+
this.provider.getNetwork(),
|
|
262
|
+
]);
|
|
263
|
+
this.signer = signer;
|
|
293
264
|
this.chainId = Number(network.chainId) === 133 || Number(network.chainId) === 31337 ? 1 : Number(network.chainId);
|
|
294
265
|
console.log("CURVE-LLAMALEND-JS IS CONNECTED TO NETWORK:", { name: network.name.toUpperCase(), chainId: Number(this.chainId) });
|
|
295
266
|
if (this.chainId === 42161) {
|
|
@@ -307,7 +278,7 @@ class Llamalend {
|
|
|
307
278
|
try {
|
|
308
279
|
this.signerAddress = yield this.signer.getAddress();
|
|
309
280
|
}
|
|
310
|
-
catch (
|
|
281
|
+
catch (_b) {
|
|
311
282
|
this.signer = null;
|
|
312
283
|
}
|
|
313
284
|
}
|
|
@@ -315,7 +286,6 @@ class Llamalend {
|
|
|
315
286
|
this.signerAddress = '';
|
|
316
287
|
}
|
|
317
288
|
this.feeData = { gasPrice: options.gasPrice, maxFeePerGas: options.maxFeePerGas, maxPriorityFeePerGas: options.maxPriorityFeePerGas };
|
|
318
|
-
yield this.updateFeeData();
|
|
319
289
|
// oneWayMarkets contracts
|
|
320
290
|
this.setContract(this.constants.ALIASES['one_way_factory'], OneWayLendingFactoryABI);
|
|
321
291
|
if (this.constants.ALIASES['one_way_factory_v2'] && this.constants.ALIASES['one_way_factory_v2'] !== this.constants.ZERO_ADDRESS) {
|
|
@@ -342,104 +312,12 @@ class Llamalend {
|
|
|
342
312
|
this.setContract(this.constants.ALIASES.gauge_factory, GaugeFactorySidechainABI);
|
|
343
313
|
}
|
|
344
314
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
this.
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
this.setContract(llamma.controller_address, controllerABI);
|
|
352
|
-
const monetary_policy_address = yield this.contracts[llamma.controller_address].contract.monetary_policy(this.constantOptions);
|
|
353
|
-
llamma.monetary_policy_address = monetary_policy_address.toLowerCase();
|
|
354
|
-
this.setContract(llamma.monetary_policy_address, llamma.monetary_policy_abi);
|
|
355
|
-
if (llamma.collateral_address === "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
|
|
356
|
-
this.setContract(this.constants.WETH, ERC20ABI);
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
this.setContract(llamma.collateral_address, ERC20ABI);
|
|
360
|
-
}
|
|
361
|
-
this.setContract(llamma.leverage_zap, LeverageZapCrvUSDABI);
|
|
362
|
-
this.setContract(llamma.deleverage_zap, DeleverageZapABI);
|
|
363
|
-
if (llamma.health_calculator_zap)
|
|
364
|
-
this.setContract(llamma.health_calculator_zap, HealthCalculatorZapABI);
|
|
365
|
-
}
|
|
366
|
-
for (const pegKeeper of this.constants.PEG_KEEPERS) {
|
|
367
|
-
this.setContract(pegKeeper, PegKeeper);
|
|
368
|
-
}
|
|
369
|
-
}
|
|
370
|
-
// TODO Put it in a separate method
|
|
371
|
-
// Fetch new llammas
|
|
372
|
-
if (this.chainId === 1) {
|
|
373
|
-
this.setContract(this.constants.FACTORY, FactoryABI);
|
|
374
|
-
const factoryContract = this.contracts[this.constants.FACTORY].contract;
|
|
375
|
-
const factoryMulticallContract = this.contracts[this.constants.FACTORY].multicallContract;
|
|
376
|
-
const N1 = Object.keys(this.constants.LLAMMAS).length;
|
|
377
|
-
const N2 = yield factoryContract.n_collaterals(this.constantOptions);
|
|
378
|
-
let calls = [];
|
|
379
|
-
for (let i = N1; i < N2; i++) {
|
|
380
|
-
calls.push(factoryMulticallContract.collaterals(i), factoryMulticallContract.amms(i), factoryMulticallContract.controllers(i));
|
|
381
|
-
}
|
|
382
|
-
const res = (yield this.multicallProvider.all(calls)).map((c) => c.toLowerCase());
|
|
383
|
-
const collaterals = res.filter((a, i) => i % 3 == 0);
|
|
384
|
-
const amms = res.filter((a, i) => i % 3 == 1);
|
|
385
|
-
const controllers = res.filter((a, i) => i % 3 == 2);
|
|
386
|
-
if (collaterals.length > 0) {
|
|
387
|
-
for (const collateral of collaterals)
|
|
388
|
-
this.setContract(collateral, ERC20ABI);
|
|
389
|
-
calls = [];
|
|
390
|
-
for (const collateral of collaterals) {
|
|
391
|
-
calls.push(this.contracts[collateral].multicallContract.symbol(), this.contracts[collateral].multicallContract.decimals());
|
|
392
|
-
}
|
|
393
|
-
const res = (yield this.multicallProvider.all(calls)).map((x) => {
|
|
394
|
-
if (typeof x === "string")
|
|
395
|
-
return x.toLowerCase();
|
|
396
|
-
return x;
|
|
397
|
-
});
|
|
398
|
-
calls = [];
|
|
399
|
-
for (const amm of amms) {
|
|
400
|
-
this.setContract(amm, llammaABI);
|
|
401
|
-
calls.push(this.contracts[amm].multicallContract.A());
|
|
402
|
-
}
|
|
403
|
-
const AParams = (yield this.multicallProvider.all(calls)).map((x) => {
|
|
404
|
-
return Number(x);
|
|
405
|
-
});
|
|
406
|
-
for (let i = 0; i < collaterals.length; i++) {
|
|
407
|
-
const is_eth = collaterals[i] === this.constants.WETH;
|
|
408
|
-
const [collateral_symbol, collateral_decimals] = res.splice(0, 2);
|
|
409
|
-
if (i >= collaterals.length - 3) {
|
|
410
|
-
this.setContract(controllers[i], controllerV2ABI);
|
|
411
|
-
}
|
|
412
|
-
else {
|
|
413
|
-
this.setContract(controllers[i], controllerABI);
|
|
414
|
-
}
|
|
415
|
-
const monetary_policy_address = (yield this.contracts[controllers[i]].contract.monetary_policy(this.constantOptions)).toLowerCase();
|
|
416
|
-
this.setContract(monetary_policy_address, MonetaryPolicy2ABI);
|
|
417
|
-
const _llammaId = is_eth ? "eth" : collateral_symbol.toLowerCase();
|
|
418
|
-
let llammaId = _llammaId;
|
|
419
|
-
let j = 2;
|
|
420
|
-
while (llammaId in this.constants.LLAMMAS)
|
|
421
|
-
llammaId = _llammaId + j++;
|
|
422
|
-
this.constants.LLAMMAS[llammaId] = {
|
|
423
|
-
amm_address: amms[i],
|
|
424
|
-
controller_address: controllers[i],
|
|
425
|
-
monetary_policy_address,
|
|
426
|
-
collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collaterals[i],
|
|
427
|
-
leverage_zap: this.constants.ALIASES.leverage_zap,
|
|
428
|
-
deleverage_zap: "0x0000000000000000000000000000000000000000",
|
|
429
|
-
collateral_symbol: is_eth ? "ETH" : collateral_symbol,
|
|
430
|
-
collateral_decimals,
|
|
431
|
-
min_bands: 4,
|
|
432
|
-
max_bands: 50,
|
|
433
|
-
default_bands: 10,
|
|
434
|
-
A: AParams[i],
|
|
435
|
-
monetary_policy_abi: MonetaryPolicy2ABI,
|
|
436
|
-
is_deleverage_supported: true,
|
|
437
|
-
index: N1 + i,
|
|
438
|
-
};
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
this.constants.DECIMALS = Object.assign(Object.assign({}, extractDecimals(this.constants.LLAMMAS)), { [this.crvUsdAddress]: 18, [this.constants.ALIASES.crv]: 18, [this.constants.ALIASES.crvUSD]: 18, [this.constants.ALIASES.st_crvUSD]: 18 });
|
|
315
|
+
this.constants.DECIMALS = {
|
|
316
|
+
[this.crvUsdAddress]: 18,
|
|
317
|
+
[this.constants.ALIASES.crv]: 18,
|
|
318
|
+
[this.constants.ALIASES.crvUSD]: 18,
|
|
319
|
+
[this.constants.ALIASES.st_crvUSD]: 18,
|
|
320
|
+
};
|
|
443
321
|
if (L2Networks.includes(this.chainId)) {
|
|
444
322
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
445
323
|
const lendingInstance = this;
|
|
@@ -479,20 +357,70 @@ class Llamalend {
|
|
|
479
357
|
setContract(address, abi) {
|
|
480
358
|
if (address === this.constants.ZERO_ADDRESS || address === undefined)
|
|
481
359
|
return;
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
360
|
+
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
361
|
+
const llamalendInstance = this;
|
|
362
|
+
const proxyHandler = {
|
|
363
|
+
get: function (target, name) {
|
|
364
|
+
if (name === 'contract') {
|
|
365
|
+
return llamalendInstance.initContract(target['address'], target['abi'], llamalendInstance.signer || llamalendInstance.provider);
|
|
366
|
+
}
|
|
367
|
+
else if (name === 'multicallContract') {
|
|
368
|
+
return llamalendInstance.initMulticallContract(target['address'], target['abi']);
|
|
369
|
+
}
|
|
370
|
+
else {
|
|
371
|
+
return target[name];
|
|
372
|
+
}
|
|
373
|
+
},
|
|
374
|
+
};
|
|
375
|
+
const coreContract = {
|
|
376
|
+
address,
|
|
377
|
+
abi,
|
|
487
378
|
};
|
|
379
|
+
this.contracts[address] = new Proxy(coreContract, proxyHandler);
|
|
488
380
|
}
|
|
489
381
|
setCustomFeeData(customFeeData) {
|
|
490
382
|
this.feeData = Object.assign(Object.assign({}, this.feeData), customFeeData);
|
|
491
383
|
}
|
|
492
|
-
|
|
493
|
-
return __awaiter(this,
|
|
494
|
-
|
|
495
|
-
|
|
384
|
+
_setupMintMarketContracts() {
|
|
385
|
+
return __awaiter(this, arguments, void 0, function* (useApi = true) {
|
|
386
|
+
this.setContract(this.crvUsdAddress, ERC20ABI);
|
|
387
|
+
if (this.chainId !== 1)
|
|
388
|
+
return;
|
|
389
|
+
this.setContract(this.constants.COINS.crvusd.toLowerCase(), ERC20ABI);
|
|
390
|
+
const llammas = Object.values(this.constants.LLAMMAS);
|
|
391
|
+
for (const llamma of llammas) {
|
|
392
|
+
this.setContract(llamma.amm_address, llammaABI);
|
|
393
|
+
this.setContract(llamma.controller_address, controllerABI);
|
|
394
|
+
}
|
|
395
|
+
if (useApi) {
|
|
396
|
+
const apiData = yield _getCrvUsdMarketsData();
|
|
397
|
+
const monetaryPolicyMap = new Map(apiData.map((m) => [m.address.toLowerCase(), m.monetary_policy_address.toLowerCase()]));
|
|
398
|
+
for (const llamma of llammas) {
|
|
399
|
+
const fresh = monetaryPolicyMap.get(llamma.controller_address);
|
|
400
|
+
if (fresh)
|
|
401
|
+
llamma.monetary_policy_address = fresh;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
const monetaryPolicies = (yield this.multicallProvider.all(llammas.map((l) => this.contracts[l.controller_address].multicallContract.monetary_policy()))).map((a) => a.toLowerCase());
|
|
406
|
+
llammas.forEach((llamma, i) => { llamma.monetary_policy_address = monetaryPolicies[i]; });
|
|
407
|
+
}
|
|
408
|
+
for (const llamma of llammas) {
|
|
409
|
+
this.setContract(llamma.monetary_policy_address, llamma.monetary_policy_abi);
|
|
410
|
+
if (llamma.collateral_address === "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") {
|
|
411
|
+
this.setContract(this.constants.WETH, ERC20ABI);
|
|
412
|
+
}
|
|
413
|
+
else {
|
|
414
|
+
this.setContract(llamma.collateral_address, ERC20ABI);
|
|
415
|
+
}
|
|
416
|
+
this.setContract(llamma.leverage_zap, LeverageZapCrvUSDABI);
|
|
417
|
+
this.setContract(llamma.deleverage_zap, DeleverageZapABI);
|
|
418
|
+
if (llamma.health_calculator_zap)
|
|
419
|
+
this.setContract(llamma.health_calculator_zap, HealthCalculatorZapABI);
|
|
420
|
+
}
|
|
421
|
+
for (const pegKeeper of this.constants.PEG_KEEPERS) {
|
|
422
|
+
this.setContract(pegKeeper, PegKeeper);
|
|
423
|
+
}
|
|
496
424
|
});
|
|
497
425
|
}
|
|
498
426
|
formatUnits(value, unit) {
|