@curvefi/llamalend-api 2.0.21 → 2.0.23
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/constants/abis/ControllerV2.json +0 -4
- 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/lendMarkets/modules/v2/loanV2.js +1 -1
- package/lib/llamalend.d.ts +8 -4
- package/lib/llamalend.js +101 -177
- package/lib/mintMarkets/fetch/fetchMintMarkets.d.ts +3 -0
- package/lib/mintMarkets/fetch/fetchMintMarkets.js +135 -0
- package/package.json +1 -1
- package/src/constants/abis/ControllerV2.json +0 -4
- package/src/external-api.ts +28 -12
- package/src/index.ts +1 -3
- package/src/lendMarkets/fetch/fetchLendMarkets.ts +0 -14
- package/src/lendMarkets/modules/v2/loanV2.ts +1 -1
- package/src/llamalend.ts +124 -199
- package/src/mintMarkets/fetch/fetchMintMarkets.ts +146 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { _getCrvUsdMarketsData } from "../../external-api.js";
|
|
11
|
+
import ERC20ABI from '../../constants/abis/ERC20.json' with { type: 'json' };
|
|
12
|
+
import llammaABI from "../../constants/abis/crvUSD/llamma.json" with { type: 'json' };
|
|
13
|
+
import controllerABI from "../../constants/abis/crvUSD/controller.json" with { type: 'json' };
|
|
14
|
+
import controllerV2ABI from "../../constants/abis/crvUSD/controller_v2.json";
|
|
15
|
+
import FactoryABI from "../../constants/abis/crvUSD/Factory.json" with { type: 'json' };
|
|
16
|
+
import MonetaryPolicy2ABI from "../../constants/abis/crvUSD/MonetaryPolicy2.json" with { type: 'json' };
|
|
17
|
+
export const fetchMintMarketsByAPI = (llamalend) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
if (llamalend.chainId !== 1)
|
|
19
|
+
return;
|
|
20
|
+
const data = yield _getCrvUsdMarketsData();
|
|
21
|
+
const existingControllers = new Set(Object.values(llamalend.constants.LLAMMAS).map((l) => l.controller_address));
|
|
22
|
+
const newMarkets = data.filter((m) => !existingControllers.has(m.address.toLowerCase()));
|
|
23
|
+
if (newMarkets.length === 0)
|
|
24
|
+
return;
|
|
25
|
+
const N1 = Object.keys(llamalend.constants.LLAMMAS).length;
|
|
26
|
+
const controllers = newMarkets.map((m) => m.address.toLowerCase());
|
|
27
|
+
const amms = newMarkets.map((m) => m.llamma.toLowerCase());
|
|
28
|
+
const collaterals = newMarkets.map((m) => m.collateral_token.address.toLowerCase());
|
|
29
|
+
for (const collateral of collaterals)
|
|
30
|
+
llamalend.setContract(collateral, ERC20ABI);
|
|
31
|
+
for (const amm of amms)
|
|
32
|
+
llamalend.setContract(amm, llammaABI);
|
|
33
|
+
for (const controller of controllers)
|
|
34
|
+
llamalend.setContract(controller, controllerABI);
|
|
35
|
+
for (let i = 0; i < newMarkets.length; i++) {
|
|
36
|
+
const market = newMarkets[i];
|
|
37
|
+
const collateral_address = collaterals[i];
|
|
38
|
+
const is_eth = collateral_address === llamalend.constants.WETH;
|
|
39
|
+
const collateral_symbol = market.collateral_token.symbol;
|
|
40
|
+
const monetary_policy_address = market.monetary_policy_address.toLowerCase();
|
|
41
|
+
llamalend.setContract(monetary_policy_address, MonetaryPolicy2ABI);
|
|
42
|
+
const _llammaId = is_eth ? "eth" : collateral_symbol.toLowerCase();
|
|
43
|
+
let llammaId = _llammaId;
|
|
44
|
+
let j = 2;
|
|
45
|
+
while (llammaId in llamalend.constants.LLAMMAS)
|
|
46
|
+
llammaId = _llammaId + j++;
|
|
47
|
+
llamalend.constants.LLAMMAS[llammaId] = {
|
|
48
|
+
amm_address: amms[i],
|
|
49
|
+
controller_address: controllers[i],
|
|
50
|
+
monetary_policy_address,
|
|
51
|
+
collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collateral_address,
|
|
52
|
+
leverage_zap: llamalend.constants.ALIASES.leverage_zap,
|
|
53
|
+
deleverage_zap: "0x0000000000000000000000000000000000000000",
|
|
54
|
+
collateral_symbol: is_eth ? "ETH" : collateral_symbol,
|
|
55
|
+
collateral_decimals: market.collateral_token.decimals,
|
|
56
|
+
min_bands: 4,
|
|
57
|
+
max_bands: 50,
|
|
58
|
+
default_bands: 10,
|
|
59
|
+
A: market.amm_a,
|
|
60
|
+
monetary_policy_abi: MonetaryPolicy2ABI,
|
|
61
|
+
is_deleverage_supported: true,
|
|
62
|
+
index: N1 + i,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
export const fetchMintMarketsByBlockchain = (llamalend) => __awaiter(void 0, void 0, void 0, function* () {
|
|
67
|
+
if (llamalend.chainId !== 1)
|
|
68
|
+
return;
|
|
69
|
+
llamalend.setContract(llamalend.constants.FACTORY, FactoryABI);
|
|
70
|
+
const factoryContract = llamalend.contracts[llamalend.constants.FACTORY].contract;
|
|
71
|
+
const factoryMulticallContract = llamalend.contracts[llamalend.constants.FACTORY].multicallContract;
|
|
72
|
+
const N1 = Object.keys(llamalend.constants.LLAMMAS).length;
|
|
73
|
+
const N2 = yield factoryContract.n_collaterals(llamalend.constantOptions);
|
|
74
|
+
let calls = [];
|
|
75
|
+
for (let i = N1; i < N2; i++) {
|
|
76
|
+
calls.push(factoryMulticallContract.collaterals(i), factoryMulticallContract.amms(i), factoryMulticallContract.controllers(i));
|
|
77
|
+
}
|
|
78
|
+
const res = (yield llamalend.multicallProvider.all(calls)).map((c) => c.toLowerCase());
|
|
79
|
+
const collaterals = res.filter((a, i) => i % 3 == 0);
|
|
80
|
+
const amms = res.filter((a, i) => i % 3 == 1);
|
|
81
|
+
const controllers = res.filter((a, i) => i % 3 == 2);
|
|
82
|
+
if (collaterals.length === 0)
|
|
83
|
+
return;
|
|
84
|
+
for (const collateral of collaterals)
|
|
85
|
+
llamalend.setContract(collateral, ERC20ABI);
|
|
86
|
+
calls = [];
|
|
87
|
+
for (const collateral of collaterals) {
|
|
88
|
+
calls.push(llamalend.contracts[collateral].multicallContract.symbol(), llamalend.contracts[collateral].multicallContract.decimals());
|
|
89
|
+
}
|
|
90
|
+
const collateralData = (yield llamalend.multicallProvider.all(calls)).map((x) => {
|
|
91
|
+
if (typeof x === "string")
|
|
92
|
+
return x.toLowerCase();
|
|
93
|
+
return x;
|
|
94
|
+
});
|
|
95
|
+
calls = [];
|
|
96
|
+
for (const amm of amms) {
|
|
97
|
+
llamalend.setContract(amm, llammaABI);
|
|
98
|
+
calls.push(llamalend.contracts[amm].multicallContract.A());
|
|
99
|
+
}
|
|
100
|
+
const AParams = (yield llamalend.multicallProvider.all(calls)).map((x) => Number(x));
|
|
101
|
+
for (let i = 0; i < collaterals.length; i++) {
|
|
102
|
+
const is_eth = collaterals[i] === llamalend.constants.WETH;
|
|
103
|
+
const [collateral_symbol, collateral_decimals] = collateralData.splice(0, 2);
|
|
104
|
+
if (i >= collaterals.length - 3) {
|
|
105
|
+
llamalend.setContract(controllers[i], controllerV2ABI);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
llamalend.setContract(controllers[i], controllerABI);
|
|
109
|
+
}
|
|
110
|
+
const monetary_policy_address = (yield llamalend.contracts[controllers[i]].contract.monetary_policy(llamalend.constantOptions)).toLowerCase();
|
|
111
|
+
llamalend.setContract(monetary_policy_address, MonetaryPolicy2ABI);
|
|
112
|
+
const _llammaId = is_eth ? "eth" : collateral_symbol.toLowerCase();
|
|
113
|
+
let llammaId = _llammaId;
|
|
114
|
+
let j = 2;
|
|
115
|
+
while (llammaId in llamalend.constants.LLAMMAS)
|
|
116
|
+
llammaId = _llammaId + j++;
|
|
117
|
+
llamalend.constants.LLAMMAS[llammaId] = {
|
|
118
|
+
amm_address: amms[i],
|
|
119
|
+
controller_address: controllers[i],
|
|
120
|
+
monetary_policy_address,
|
|
121
|
+
collateral_address: is_eth ? "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee" : collaterals[i],
|
|
122
|
+
leverage_zap: llamalend.constants.ALIASES.leverage_zap,
|
|
123
|
+
deleverage_zap: "0x0000000000000000000000000000000000000000",
|
|
124
|
+
collateral_symbol: is_eth ? "ETH" : collateral_symbol,
|
|
125
|
+
collateral_decimals,
|
|
126
|
+
min_bands: 4,
|
|
127
|
+
max_bands: 50,
|
|
128
|
+
default_bands: 10,
|
|
129
|
+
A: AParams[i],
|
|
130
|
+
monetary_policy_abi: MonetaryPolicy2ABI,
|
|
131
|
+
is_deleverage_supported: true,
|
|
132
|
+
index: N1 + i,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
});
|
package/package.json
CHANGED
package/src/external-api.ts
CHANGED
|
@@ -169,6 +169,34 @@ export const _getMarketsData = memoize(
|
|
|
169
169
|
}
|
|
170
170
|
)
|
|
171
171
|
|
|
172
|
+
export interface ICrvUsdMarketAPI {
|
|
173
|
+
address: string;
|
|
174
|
+
llamma: string;
|
|
175
|
+
amm_a: number;
|
|
176
|
+
monetary_policy_address: string;
|
|
177
|
+
collateral_token: {
|
|
178
|
+
symbol: string;
|
|
179
|
+
address: string;
|
|
180
|
+
decimals: number;
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const _getCrvUsdMarketsData = memoize(
|
|
185
|
+
async (): Promise<ICrvUsdMarketAPI[]> => {
|
|
186
|
+
const url = 'https://prices.curve.finance/v1/crvusd/markets/ethereum';
|
|
187
|
+
const response = await fetch(url, { headers: { "accept": "application/json" } });
|
|
188
|
+
if (response.status !== 200) {
|
|
189
|
+
throw Error(`Fetch error: ${response.status} ${response.statusText}`);
|
|
190
|
+
}
|
|
191
|
+
const { data } = await response.json() as { data: ICrvUsdMarketAPI[] };
|
|
192
|
+
return data;
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
promise: true,
|
|
196
|
+
maxAge: 10 * 1000, // 10s
|
|
197
|
+
}
|
|
198
|
+
)
|
|
199
|
+
|
|
172
200
|
// --- ODOS ---
|
|
173
201
|
|
|
174
202
|
export async function _getQuoteOdos(this: Llamalend, fromToken: string, toToken: string, _amount: bigint, blacklist: string, pathVizImage: boolean, slippage = 0.5): Promise<IQuoteOdos> {
|
|
@@ -214,18 +242,6 @@ export async function _assembleTxOdos(this: Llamalend, pathId: string): Promise<
|
|
|
214
242
|
return _assembleTxOdosMemoized(this.constants.ALIASES.leverage_zap, pathId);
|
|
215
243
|
}
|
|
216
244
|
|
|
217
|
-
export const _getHiddenPools = memoize(
|
|
218
|
-
async () => {
|
|
219
|
-
const response = await fetch(`https://api.curve.finance/api/getHiddenPools`)
|
|
220
|
-
|
|
221
|
-
return (await response.json() as any).data
|
|
222
|
-
},
|
|
223
|
-
{
|
|
224
|
-
promise: true,
|
|
225
|
-
maxAge: 5 * 60 * 1000, // 5m
|
|
226
|
-
}
|
|
227
|
-
)
|
|
228
|
-
|
|
229
245
|
async function fetchJson(url: string): Promise<any> {
|
|
230
246
|
const response = await fetch(url);
|
|
231
247
|
return await response.json() ?? {};
|
package/src/index.ts
CHANGED
|
@@ -86,12 +86,10 @@ export function createLlamalend() {
|
|
|
86
86
|
getGasPriceFromL2: getGasPriceFromL2.bind(llamalend),
|
|
87
87
|
getGasInfoForL2: getGasInfoForL2.bind(llamalend),
|
|
88
88
|
|
|
89
|
-
// Core methods
|
|
90
|
-
fetchStats: llamalend.fetchStats.bind(llamalend),
|
|
91
|
-
|
|
92
89
|
// Market lists
|
|
93
90
|
mintMarkets: {
|
|
94
91
|
getMarketList: llamalend.getMintMarketList.bind(llamalend),
|
|
92
|
+
fetchMintMarkets: llamalend.fetchMintMarkets.bind(llamalend),
|
|
95
93
|
},
|
|
96
94
|
lendMarkets: {
|
|
97
95
|
fetchMarkets: llamalend.fetchLendMarkets.bind(llamalend),
|
|
@@ -87,14 +87,6 @@ export const fetchOneWayMarketsByBlockchain = async (llamalend: Llamalend, versi
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
registerMarkets(llamalend, names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges, COIN_DATA, version);
|
|
90
|
-
|
|
91
|
-
if (version === 'v2') {
|
|
92
|
-
llamalend.constants.ONE_WAY_MARKETS_V2 = await llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS_V2);
|
|
93
|
-
} else {
|
|
94
|
-
llamalend.constants.ONE_WAY_MARKETS = await llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
await llamalend.fetchStats(amms, controllers, vaults, borrowed_tokens, collateral_tokens, version);
|
|
98
90
|
};
|
|
99
91
|
|
|
100
92
|
export const fetchOneWayMarketsByAPI = async (llamalend: Llamalend, version: 'v1' | 'v2' = 'v1') => {
|
|
@@ -105,10 +97,4 @@ export const fetchOneWayMarketsByAPI = async (llamalend: Llamalend, version: 'v1
|
|
|
105
97
|
}
|
|
106
98
|
|
|
107
99
|
registerMarkets(llamalend, names, amms, controllers, borrowed_tokens, collateral_tokens, monetary_policies, vaults, gauges, COIN_DATA, version);
|
|
108
|
-
|
|
109
|
-
if (version === 'v2') {
|
|
110
|
-
llamalend.constants.ONE_WAY_MARKETS_V2 = await llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS_V2);
|
|
111
|
-
} else {
|
|
112
|
-
llamalend.constants.ONE_WAY_MARKETS = await llamalend._filterHiddenMarkets(llamalend.constants.ONE_WAY_MARKETS);
|
|
113
|
-
}
|
|
114
100
|
};
|
|
@@ -67,7 +67,7 @@ export class LoanV2Module extends LoanBaseModule implements ILoanV2 {
|
|
|
67
67
|
const _debt = parseUnits(debt);
|
|
68
68
|
|
|
69
69
|
const contract = this.llamalend.contracts[this.market.addresses.controller].contract;
|
|
70
|
-
const _health = await contract.repay_health_preview(0, _debt, address,
|
|
70
|
+
const _health = await contract.repay_health_preview(0, _debt, address, shrink, full, this.llamalend.constantOptions) as bigint;
|
|
71
71
|
|
|
72
72
|
return formatUnits(_health * BigInt(100));
|
|
73
73
|
}
|