@curvefi/llamalend-api 1.0.2
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/.eslintrc.json +40 -0
- package/.github/workflows/lint.yml +15 -0
- package/.github/workflows/publish.yml +55 -0
- package/LICENSE +21 -0
- package/README.md +1976 -0
- package/lib/cache/index.d.ts +14 -0
- package/lib/cache/index.js +31 -0
- package/lib/constants/L2Networks.d.ts +1 -0
- package/lib/constants/L2Networks.js +1 -0
- package/lib/constants/abis/Controller.json +1027 -0
- package/lib/constants/abis/ERC20.json +222 -0
- package/lib/constants/abis/ERC4626.json +1674 -0
- package/lib/constants/abis/GaugeController.json +794 -0
- package/lib/constants/abis/GaugeFactoryMainnet.json +1 -0
- package/lib/constants/abis/GaugeFactorySidechain.json +475 -0
- package/lib/constants/abis/GaugeV5.json +958 -0
- package/lib/constants/abis/LeverageZap.json +35 -0
- package/lib/constants/abis/Llamma.json +984 -0
- package/lib/constants/abis/Minter.json +1 -0
- package/lib/constants/abis/MonetaryPolicy.json +221 -0
- package/lib/constants/abis/OneWayLendingFactoryABI.json +899 -0
- package/lib/constants/abis/SidechainGauge.json +939 -0
- package/lib/constants/abis/Vault.json +721 -0
- package/lib/constants/abis/crvUSD/DeleverageZap.json +248 -0
- package/lib/constants/abis/crvUSD/Factory.json +514 -0
- package/lib/constants/abis/crvUSD/HealthCalculatorZap.json +54 -0
- package/lib/constants/abis/crvUSD/LeverageZap.json +312 -0
- package/lib/constants/abis/crvUSD/MonetaryPolicy.json +294 -0
- package/lib/constants/abis/crvUSD/MonetaryPolicy2.json +299 -0
- package/lib/constants/abis/crvUSD/PegKeeper.json +411 -0
- package/lib/constants/abis/crvUSD/controller.json +991 -0
- package/lib/constants/abis/crvUSD/llamma.json +984 -0
- package/lib/constants/abis/gas_oracle_optimism.json +149 -0
- package/lib/constants/abis/gas_oracle_optimism_blob.json +203 -0
- package/lib/constants/aliases.d.ts +16 -0
- package/lib/constants/aliases.js +124 -0
- package/lib/constants/coins.d.ts +16 -0
- package/lib/constants/coins.js +24 -0
- package/lib/constants/llammas.d.ts +2 -0
- package/lib/constants/llammas.js +96 -0
- package/lib/constants/utils.d.ts +4 -0
- package/lib/constants/utils.js +27 -0
- package/lib/external-api.d.ts +13 -0
- package/lib/external-api.js +436 -0
- package/lib/index.d.ts +104 -0
- package/lib/index.js +123 -0
- package/lib/interfaces.d.ts +228 -0
- package/lib/interfaces.js +1 -0
- package/lib/lendMarkets/LendMarketTemplate.d.ts +510 -0
- package/lib/lendMarkets/LendMarketTemplate.js +4682 -0
- package/lib/lendMarkets/index.d.ts +3 -0
- package/lib/lendMarkets/index.js +3 -0
- package/lib/lendMarkets/lendMarketConstructor.d.ts +2 -0
- package/lib/lendMarkets/lendMarketConstructor.js +6 -0
- package/lib/llamalend.d.ts +80 -0
- package/lib/llamalend.js +878 -0
- package/lib/mintMarkets/MintMarketTemplate.d.ts +308 -0
- package/lib/mintMarkets/MintMarketTemplate.js +2998 -0
- package/lib/mintMarkets/index.d.ts +3 -0
- package/lib/mintMarkets/index.js +3 -0
- package/lib/mintMarkets/mintMarketConstructor.d.ts +2 -0
- package/lib/mintMarkets/mintMarketConstructor.js +4 -0
- package/lib/st-crvUSD.d.ts +35 -0
- package/lib/st-crvUSD.js +505 -0
- package/lib/utils.d.ts +58 -0
- package/lib/utils.js +661 -0
- package/package.json +42 -0
- package/src/cache/index.ts +41 -0
- package/src/constants/L2Networks.ts +1 -0
- package/src/constants/abis/Controller.json +1027 -0
- package/src/constants/abis/ERC20.json +222 -0
- package/src/constants/abis/ERC4626.json +1674 -0
- package/src/constants/abis/GaugeController.json +794 -0
- package/src/constants/abis/GaugeFactoryMainnet.json +1 -0
- package/src/constants/abis/GaugeFactorySidechain.json +475 -0
- package/src/constants/abis/GaugeV5.json +958 -0
- package/src/constants/abis/LeverageZap.json +35 -0
- package/src/constants/abis/Llamma.json +984 -0
- package/src/constants/abis/Minter.json +1 -0
- package/src/constants/abis/MonetaryPolicy.json +221 -0
- package/src/constants/abis/OneWayLendingFactoryABI.json +899 -0
- package/src/constants/abis/SidechainGauge.json +939 -0
- package/src/constants/abis/Vault.json +721 -0
- package/src/constants/abis/crvUSD/DeleverageZap.json +248 -0
- package/src/constants/abis/crvUSD/ERC20.json +222 -0
- package/src/constants/abis/crvUSD/Factory.json +514 -0
- package/src/constants/abis/crvUSD/HealthCalculatorZap.json +54 -0
- package/src/constants/abis/crvUSD/LeverageZap.json +312 -0
- package/src/constants/abis/crvUSD/MonetaryPolicy.json +294 -0
- package/src/constants/abis/crvUSD/MonetaryPolicy2.json +299 -0
- package/src/constants/abis/crvUSD/PegKeeper.json +411 -0
- package/src/constants/abis/crvUSD/controller.json +991 -0
- package/src/constants/abis/crvUSD/llamma.json +984 -0
- package/src/constants/abis/gas_oracle_optimism.json +149 -0
- package/src/constants/abis/gas_oracle_optimism_blob.json +203 -0
- package/src/constants/aliases.ts +141 -0
- package/src/constants/coins.ts +41 -0
- package/src/constants/llammas.ts +99 -0
- package/src/constants/utils.ts +33 -0
- package/src/external-api.ts +325 -0
- package/src/index.ts +128 -0
- package/src/interfaces.ts +237 -0
- package/src/lendMarkets/LendMarketTemplate.ts +3022 -0
- package/src/lendMarkets/index.ts +7 -0
- package/src/lendMarkets/lendMarketConstructor.ts +7 -0
- package/src/llamalend.ts +785 -0
- package/src/mintMarkets/MintMarketTemplate.ts +1781 -0
- package/src/mintMarkets/index.ts +7 -0
- package/src/mintMarkets/mintMarketConstructor.ts +5 -0
- package/src/st-crvUSD.ts +244 -0
- package/src/utils.ts +497 -0
- package/test/fetch.test.ts +152 -0
- package/test/general.test.ts +216 -0
- package/test/leverageBorrowMore.test.ts +245 -0
- package/test/leverageCreateLoan.test.ts +236 -0
- package/test/leverageRepay.test.ts +240 -0
- package/test/readme.test.ts +475 -0
- package/test/selfLiquidate.test.ts +57 -0
- package/test/selfLiquidateCrvUSD.test.ts +54 -0
- package/test/st_crvUSD.test.ts +68 -0
- package/test/swap.test.ts +62 -0
- package/test/swapCrvUSD.test.ts +56 -0
- package/test/vault.test.ts +112 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +72 -0
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
import axios from "axios";
|
|
2
|
+
import { ethers } from "ethers";
|
|
3
|
+
import memoize from "memoizee";
|
|
4
|
+
import BigNumber from 'bignumber.js';
|
|
5
|
+
import { llamalend } from "./llamalend.js";
|
|
6
|
+
import {
|
|
7
|
+
IExtendedPoolDataFromApi,
|
|
8
|
+
INetworkName,
|
|
9
|
+
IPoolFactory,
|
|
10
|
+
I1inchSwapData,
|
|
11
|
+
IDict,
|
|
12
|
+
IMarketData,
|
|
13
|
+
IQuoteOdos,
|
|
14
|
+
} from "./interfaces";
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
const _getPoolsFromApi = memoize(
|
|
18
|
+
async (network: INetworkName, poolFactory: IPoolFactory ): Promise<IExtendedPoolDataFromApi> => {
|
|
19
|
+
const url = `https://api.curve.fi/api/getPools/${network}/${poolFactory}`;
|
|
20
|
+
const response = await axios.get(url, { validateStatus: () => true });
|
|
21
|
+
return response.data.data ?? { poolData: [], tvl: 0, tvlAll: 0 };
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
promise: true,
|
|
25
|
+
maxAge: 5 * 60 * 1000, // 5m
|
|
26
|
+
}
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
const _getAllPoolsFromApi = async (network: INetworkName): Promise<IExtendedPoolDataFromApi[]> => {
|
|
30
|
+
return await Promise.all([
|
|
31
|
+
_getPoolsFromApi(network, "main"),
|
|
32
|
+
_getPoolsFromApi(network, "crypto"),
|
|
33
|
+
_getPoolsFromApi(network, "factory"),
|
|
34
|
+
_getPoolsFromApi(network, "factory-crvusd"),
|
|
35
|
+
_getPoolsFromApi(network, "factory-crypto"),
|
|
36
|
+
_getPoolsFromApi(network, "factory-twocrypto"),
|
|
37
|
+
_getPoolsFromApi(network, "factory-tricrypto"),
|
|
38
|
+
_getPoolsFromApi(network, "factory-stable-ng"),
|
|
39
|
+
]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export const _getUsdPricesFromApi = async (): Promise<IDict<number>> => {
|
|
43
|
+
const network = llamalend.constants.NETWORK_NAME;
|
|
44
|
+
const allTypesExtendedPoolData = await _getAllPoolsFromApi(network);
|
|
45
|
+
const priceDict: IDict<Record<string, number>[]> = {};
|
|
46
|
+
const priceDictByMaxTvl: IDict<number> = {};
|
|
47
|
+
|
|
48
|
+
for (const extendedPoolData of allTypesExtendedPoolData) {
|
|
49
|
+
for (const pool of extendedPoolData.poolData) {
|
|
50
|
+
const lpTokenAddress = pool.lpTokenAddress ?? pool.address;
|
|
51
|
+
const totalSupply = pool.totalSupply / (10 ** 18);
|
|
52
|
+
if(lpTokenAddress.toLowerCase() in priceDict) {
|
|
53
|
+
priceDict[lpTokenAddress.toLowerCase()].push({
|
|
54
|
+
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
|
|
55
|
+
tvl: pool.usdTotal,
|
|
56
|
+
})
|
|
57
|
+
} else {
|
|
58
|
+
priceDict[lpTokenAddress.toLowerCase()] = []
|
|
59
|
+
priceDict[lpTokenAddress.toLowerCase()].push({
|
|
60
|
+
price: pool.usdTotal && totalSupply ? pool.usdTotal / totalSupply : 0,
|
|
61
|
+
tvl: pool.usdTotal,
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
for (const coin of pool.coins) {
|
|
66
|
+
if (typeof coin.usdPrice === "number") {
|
|
67
|
+
if(coin.address.toLowerCase() in priceDict) {
|
|
68
|
+
priceDict[coin.address.toLowerCase()].push({
|
|
69
|
+
price: coin.usdPrice,
|
|
70
|
+
tvl: pool.usdTotal,
|
|
71
|
+
})
|
|
72
|
+
} else {
|
|
73
|
+
priceDict[coin.address.toLowerCase()] = []
|
|
74
|
+
priceDict[coin.address.toLowerCase()].push({
|
|
75
|
+
price: coin.usdPrice,
|
|
76
|
+
tvl: pool.usdTotal,
|
|
77
|
+
})
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
for (const coin of pool.gaugeRewards ?? []) {
|
|
83
|
+
if (typeof coin.tokenPrice === "number") {
|
|
84
|
+
if(coin.tokenAddress.toLowerCase() in priceDict) {
|
|
85
|
+
priceDict[coin.tokenAddress.toLowerCase()].push({
|
|
86
|
+
price: coin.tokenPrice,
|
|
87
|
+
tvl: pool.usdTotal,
|
|
88
|
+
});
|
|
89
|
+
} else {
|
|
90
|
+
priceDict[coin.tokenAddress.toLowerCase()] = []
|
|
91
|
+
priceDict[coin.tokenAddress.toLowerCase()].push({
|
|
92
|
+
price: coin.tokenPrice,
|
|
93
|
+
tvl: pool.usdTotal,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
for(const address in priceDict) {
|
|
102
|
+
if(priceDict[address].length > 0) {
|
|
103
|
+
const maxTvlItem = priceDict[address].reduce((prev, current) => {
|
|
104
|
+
if (+current.tvl > +prev.tvl) {
|
|
105
|
+
return current;
|
|
106
|
+
} else {
|
|
107
|
+
return prev;
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
priceDictByMaxTvl[address] = maxTvlItem.price
|
|
111
|
+
} else {
|
|
112
|
+
priceDictByMaxTvl[address] = 0
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return priceDictByMaxTvl
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export const _getUserCollateral = memoize(
|
|
121
|
+
async (network: INetworkName, controller: string, user: string): Promise<Record<string, any>> => {
|
|
122
|
+
const url = `https://prices.curve.fi/v1/lending/collateral_events/${network}/${controller}/${user}`;
|
|
123
|
+
const response = await axios.get(url, { validateStatus: () => true });
|
|
124
|
+
return {
|
|
125
|
+
total_deposit_precise: response.data.total_deposit_precise,
|
|
126
|
+
total_deposit_from_user: response.data.total_deposit_from_user,
|
|
127
|
+
total_deposit_usd_value: response.data.total_deposit_usd_value,
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
{
|
|
131
|
+
promise: true,
|
|
132
|
+
maxAge: 60 * 1000, // 1m
|
|
133
|
+
}
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
export const _getUserCollateralCrvUsd = memoize(
|
|
137
|
+
async (network: INetworkName, controller: string, user: string): Promise<string> => {
|
|
138
|
+
const url = `https://prices.curve.fi/v1/crvusd/collateral_events/${network}/${controller}/${user}`;
|
|
139
|
+
const response = await axios.get(url, { validateStatus: () => true });
|
|
140
|
+
return response.data.total_deposit;
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
promise: true,
|
|
144
|
+
maxAge: 60 * 1000, // 1m
|
|
145
|
+
}
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
export const _getMarketsData = memoize(
|
|
149
|
+
async (network: INetworkName): Promise<IMarketData> => {
|
|
150
|
+
const url = `https://api.curve.fi/api/getLendingVaults/${network}/oneway`;
|
|
151
|
+
const response = await axios.get(
|
|
152
|
+
url,
|
|
153
|
+
{
|
|
154
|
+
headers: {"accept": "application/json"},
|
|
155
|
+
validateStatus: () => true,
|
|
156
|
+
});
|
|
157
|
+
if (response.status !== 200) {
|
|
158
|
+
throw Error(`Fetch error: ${response.status} ${response.statusText}`);
|
|
159
|
+
}
|
|
160
|
+
return response.data.data;
|
|
161
|
+
},
|
|
162
|
+
{
|
|
163
|
+
promise: true,
|
|
164
|
+
maxAge: 10 * 1000, // 10s
|
|
165
|
+
}
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
// --- ODOS ---
|
|
169
|
+
|
|
170
|
+
export const _getQuoteOdos = async (fromToken: string, toToken: string, _amount: bigint, blacklist: string, pathVizImage: boolean, slippage = 0.5): Promise<IQuoteOdos> => {
|
|
171
|
+
if (_amount === BigInt(0)) return { outAmounts: ["0.0"], pathId: '', pathVizImage: '', priceImpact: 0, slippage };
|
|
172
|
+
|
|
173
|
+
if (ethers.getAddress(fromToken) == "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") fromToken = "0x0000000000000000000000000000000000000000";
|
|
174
|
+
if (ethers.getAddress(toToken) == "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE") toToken = "0x0000000000000000000000000000000000000000";
|
|
175
|
+
|
|
176
|
+
const url = `https://prices.curve.fi/odos/quote?chain_id=${llamalend.chainId}&from_address=${ethers.getAddress(fromToken)}` +
|
|
177
|
+
`&to_address=${ethers.getAddress(toToken)}&amount=${_amount.toString()}&slippage=${slippage}&pathVizImage=${pathVizImage}` +
|
|
178
|
+
`&caller_address=${ethers.getAddress(llamalend.constants.ALIASES.leverage_zap)}&blacklist=${ethers.getAddress(blacklist)}`;
|
|
179
|
+
|
|
180
|
+
const response = await axios.get(
|
|
181
|
+
url,
|
|
182
|
+
{
|
|
183
|
+
headers: {"accept": "application/json"},
|
|
184
|
+
validateStatus: () => true,
|
|
185
|
+
});
|
|
186
|
+
if (response.status !== 200) {
|
|
187
|
+
throw Error(`Odos quote error - ${response.status} ${response.statusText}`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return { ...response.data, slippage };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
export const _getExpectedOdos = async (fromToken: string, toToken: string, _amount: bigint, blacklist: string) => {
|
|
194
|
+
return (await _getQuoteOdos(fromToken, toToken, _amount, blacklist, false)).outAmounts[0]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export const _assembleTxOdos = memoize(
|
|
198
|
+
async (pathId: string): Promise<string> => {
|
|
199
|
+
const url = `https://prices.curve.fi/odos/assemble?user=${ethers.getAddress(llamalend.constants.ALIASES.leverage_zap)}&path_id=${pathId}`;
|
|
200
|
+
|
|
201
|
+
const response = await axios.get(
|
|
202
|
+
url,
|
|
203
|
+
{
|
|
204
|
+
headers: {'Content-Type': 'application/json'},
|
|
205
|
+
validateStatus: () => true,
|
|
206
|
+
});
|
|
207
|
+
if (response.status !== 200) {
|
|
208
|
+
throw Error(`Odos assemble error - ${response.status} ${response.statusText}`);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return response.data['transaction']['data'];
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
promise: true,
|
|
215
|
+
maxAge: 10 * 1000, // 10s
|
|
216
|
+
}
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
export const _getSpotPriceOdos = memoize(
|
|
220
|
+
async (fromToken: string, toToken: string): Promise<string | undefined> => {
|
|
221
|
+
fromToken = ethers.getAddress(fromToken);
|
|
222
|
+
toToken = ethers.getAddress(toToken);
|
|
223
|
+
const url = `https://prices.curve.fi/odos/prices?chain_id=${llamalend.chainId}&tokens=${fromToken},${toToken}`;
|
|
224
|
+
const response = await axios.get(
|
|
225
|
+
url,
|
|
226
|
+
{
|
|
227
|
+
headers: {"accept": "application/json"},
|
|
228
|
+
validateStatus: () => true,
|
|
229
|
+
});
|
|
230
|
+
if (response.status !== 200) {
|
|
231
|
+
throw Error(`Odos spot prices error - ${response.status} ${response.statusText}`);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const pricesFromOdos = response.data.tokenPrices;
|
|
235
|
+
const pricesFromApi: IDict<string> = {};
|
|
236
|
+
for (const coin of [fromToken, toToken]) {
|
|
237
|
+
if (pricesFromOdos[coin] !== 0) continue;
|
|
238
|
+
const _pricesFromApi = await _getUsdPricesFromApi();
|
|
239
|
+
pricesFromApi[coin] = String(_pricesFromApi[coin] || 0);
|
|
240
|
+
}
|
|
241
|
+
const prices = { ...pricesFromOdos, ...pricesFromApi };
|
|
242
|
+
if (BigNumber(prices[fromToken]).eq(0) || BigNumber(prices[toToken]).eq( 0)) return undefined;
|
|
243
|
+
|
|
244
|
+
return (new BigNumber(prices[toToken])).div(prices[fromToken]).toString()
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
promise: true,
|
|
248
|
+
maxAge: 10 * 1000, // 10s
|
|
249
|
+
}
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
// --- 1INCH ---
|
|
253
|
+
|
|
254
|
+
export const _getExpected1inch = memoize(
|
|
255
|
+
async (fromToken: string, toToken: string, _amount: bigint): Promise<string> => {
|
|
256
|
+
if (_amount === BigInt(0)) return "0.0";
|
|
257
|
+
const url = `https://prices.curve.fi/1inch/swap/v6.0/${llamalend.chainId}/quote?src=${fromToken}&dst=${toToken}&amount=${_amount}&excludedProtocols=${llamalend.constants.EXCLUDED_PROTOCOLS_1INCH}&includeTokensInfo=true&includeProtocols=true`;
|
|
258
|
+
const response = await axios.get(
|
|
259
|
+
url,
|
|
260
|
+
{
|
|
261
|
+
headers: {"accept": "application/json"},
|
|
262
|
+
validateStatus: () => true,
|
|
263
|
+
});
|
|
264
|
+
if (response.status !== 200) {
|
|
265
|
+
throw Error(`1inch error: ${response.status} ${response.statusText}`);
|
|
266
|
+
}
|
|
267
|
+
return response.data.dstAmount;
|
|
268
|
+
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
promise: true,
|
|
272
|
+
maxAge: 10 * 1000, // 10s
|
|
273
|
+
}
|
|
274
|
+
)
|
|
275
|
+
|
|
276
|
+
export const _getSwapData1inch = memoize(
|
|
277
|
+
async (fromToken: string, toToken: string, _amount: bigint, slippage: number): Promise<I1inchSwapData> => {
|
|
278
|
+
if (_amount === BigInt(0)) throw Error("Amount must be > 0");
|
|
279
|
+
const url = `https://prices.curve.fi/1inch/swap/v6.0/${llamalend.chainId}/swap?src=${fromToken}&dst=${toToken}&amount=${_amount}&from_=${llamalend.constants.ALIASES.leverage_zap}&slippage=${slippage}&excludedProtocols=${llamalend.constants.EXCLUDED_PROTOCOLS_1INCH}&includeTokensInfo=true&includeProtocols=true&disableEstimate=true`;
|
|
280
|
+
const response = await axios.get(
|
|
281
|
+
url,
|
|
282
|
+
{
|
|
283
|
+
headers: {"accept": "application/json"},
|
|
284
|
+
validateStatus: () => true,
|
|
285
|
+
});
|
|
286
|
+
if (response.status !== 200) {
|
|
287
|
+
throw Error(`1inch error: ${response.status} ${response.statusText}`);
|
|
288
|
+
}
|
|
289
|
+
return response.data;
|
|
290
|
+
|
|
291
|
+
},
|
|
292
|
+
{
|
|
293
|
+
promise: true,
|
|
294
|
+
maxAge: 10 * 1000, // 10s
|
|
295
|
+
}
|
|
296
|
+
)
|
|
297
|
+
|
|
298
|
+
export const _getSpotPrice1inch = memoize(
|
|
299
|
+
async (fromToken: string, toToken: string): Promise<string | undefined> => {
|
|
300
|
+
const url = `https://prices.curve.fi/1inch/price/v1.1/${llamalend.chainId}?tokens=${fromToken},${toToken}¤cy=USD`;
|
|
301
|
+
const response = await axios.get(
|
|
302
|
+
url,
|
|
303
|
+
{
|
|
304
|
+
headers: {"accept": "application/json"},
|
|
305
|
+
validateStatus: () => true,
|
|
306
|
+
});
|
|
307
|
+
if (response.status !== 200) {
|
|
308
|
+
throw Error(`1inch error: ${response.status} ${response.statusText}`);
|
|
309
|
+
}
|
|
310
|
+
const pricesFromApi: IDict<string> = {};
|
|
311
|
+
for (const coin in response.data) {
|
|
312
|
+
if (response.data[coin] !== "0") continue;
|
|
313
|
+
const _pricesFromApi = await _getUsdPricesFromApi();
|
|
314
|
+
pricesFromApi[coin] = String(_pricesFromApi[coin] || 0);
|
|
315
|
+
}
|
|
316
|
+
const prices = { ...response.data, ...pricesFromApi };
|
|
317
|
+
if (prices[fromToken] === '0' || prices[toToken] === '0') return undefined;
|
|
318
|
+
|
|
319
|
+
return (new BigNumber(prices[toToken])).div(prices[fromToken]).toString()
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
promise: true,
|
|
323
|
+
maxAge: 10 * 1000, // 10s
|
|
324
|
+
}
|
|
325
|
+
)
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { ethers, Networkish } from "ethers";
|
|
2
|
+
import { LendMarketTemplate, getLendMarket } from "./lendMarkets/index.js";
|
|
3
|
+
import { MintMarketTemplate, getMintMarket} from "./mintMarkets/index.js";
|
|
4
|
+
import { llamalend as _llamalend} from "./llamalend.js";
|
|
5
|
+
import {
|
|
6
|
+
getBalances,
|
|
7
|
+
getAllowance,
|
|
8
|
+
hasAllowance,
|
|
9
|
+
ensureAllowanceEstimateGas,
|
|
10
|
+
ensureAllowance,
|
|
11
|
+
getUsdRate,
|
|
12
|
+
getGasPriceFromL2,
|
|
13
|
+
getGasInfoForL2,
|
|
14
|
+
getGasPriceFromL1,
|
|
15
|
+
totalSupply,
|
|
16
|
+
getLsdApy,
|
|
17
|
+
} from "./utils.js";
|
|
18
|
+
import {
|
|
19
|
+
convertToAssets,
|
|
20
|
+
convertToShares,
|
|
21
|
+
userBalances,
|
|
22
|
+
totalSupplyAndCrvUSDLocked,
|
|
23
|
+
maxDeposit,
|
|
24
|
+
previewDeposit,
|
|
25
|
+
depositIsApproved,
|
|
26
|
+
depositAllowance,
|
|
27
|
+
depositApproveEstimateGas,
|
|
28
|
+
depositApprove,
|
|
29
|
+
depositEstimateGas,
|
|
30
|
+
deposit,
|
|
31
|
+
maxMint,
|
|
32
|
+
previewMint,
|
|
33
|
+
mintIsApproved,
|
|
34
|
+
mintAllowance,
|
|
35
|
+
mintApproveEstimateGas,
|
|
36
|
+
mintApprove,
|
|
37
|
+
mintEstimateGas,
|
|
38
|
+
mint,
|
|
39
|
+
maxWithdraw,
|
|
40
|
+
previewWithdraw,
|
|
41
|
+
withdrawEstimateGas,
|
|
42
|
+
withdraw,
|
|
43
|
+
maxRedeem,
|
|
44
|
+
previewRedeem,
|
|
45
|
+
redeemEstimateGas,
|
|
46
|
+
redeem,
|
|
47
|
+
} from "./st-crvUSD.js";
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async function init (
|
|
51
|
+
providerType: 'JsonRpc' | 'Web3' | 'Infura' | 'Alchemy',
|
|
52
|
+
providerSettings: { url?: string, privateKey?: string, batchMaxCount? : number } | { externalProvider: ethers.Eip1193Provider } | { network?: Networkish, apiKey?: string },
|
|
53
|
+
options: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number, chainId?: number } = {}
|
|
54
|
+
): Promise<void> {
|
|
55
|
+
await _llamalend.init(providerType, providerSettings, options);
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
this.signerAddress = _llamalend.signerAddress;
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
this.chainId = _llamalend.chainId;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function setCustomFeeData (customFeeData: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number }): void {
|
|
63
|
+
_llamalend.setCustomFeeData(customFeeData);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const llamalend = {
|
|
67
|
+
init,
|
|
68
|
+
chainId: 0,
|
|
69
|
+
signerAddress: '',
|
|
70
|
+
LendMarketTemplate,
|
|
71
|
+
getLendMarket,
|
|
72
|
+
MintMarketTemplate,
|
|
73
|
+
getMintMarket,
|
|
74
|
+
totalSupply,
|
|
75
|
+
getLsdApy,
|
|
76
|
+
setCustomFeeData,
|
|
77
|
+
getBalances,
|
|
78
|
+
getAllowance,
|
|
79
|
+
hasAllowance,
|
|
80
|
+
ensureAllowance,
|
|
81
|
+
getUsdRate,
|
|
82
|
+
getGasPriceFromL1,
|
|
83
|
+
getGasPriceFromL2,
|
|
84
|
+
getGasInfoForL2,
|
|
85
|
+
fetchStats: _llamalend.fetchStats,
|
|
86
|
+
getLlammaList: _llamalend.getMintMarketList,
|
|
87
|
+
lendMarkets: {
|
|
88
|
+
fetchMarkets: _llamalend.fetchLendMarkets,
|
|
89
|
+
getMarketList: _llamalend.getLendMarketList,
|
|
90
|
+
},
|
|
91
|
+
estimateGas: {
|
|
92
|
+
ensureAllowance: ensureAllowanceEstimateGas,
|
|
93
|
+
},
|
|
94
|
+
st_crvUSD: {
|
|
95
|
+
convertToAssets,
|
|
96
|
+
convertToShares,
|
|
97
|
+
userBalances,
|
|
98
|
+
totalSupplyAndCrvUSDLocked,
|
|
99
|
+
maxDeposit,
|
|
100
|
+
previewDeposit,
|
|
101
|
+
depositIsApproved,
|
|
102
|
+
depositAllowance,
|
|
103
|
+
depositApprove,
|
|
104
|
+
deposit,
|
|
105
|
+
maxMint,
|
|
106
|
+
previewMint,
|
|
107
|
+
mintIsApproved,
|
|
108
|
+
mintAllowance,
|
|
109
|
+
mintApprove,
|
|
110
|
+
mint,
|
|
111
|
+
maxWithdraw,
|
|
112
|
+
previewWithdraw,
|
|
113
|
+
withdraw,
|
|
114
|
+
maxRedeem,
|
|
115
|
+
previewRedeem,
|
|
116
|
+
redeem,
|
|
117
|
+
estimateGas: {
|
|
118
|
+
depositApprove: depositApproveEstimateGas,
|
|
119
|
+
deposit: depositEstimateGas,
|
|
120
|
+
mintApprove: mintApproveEstimateGas,
|
|
121
|
+
mint: mintEstimateGas,
|
|
122
|
+
withdraw: withdrawEstimateGas,
|
|
123
|
+
redeem: redeemEstimateGas,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export default llamalend;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { Contract, ethers } from "ethers";
|
|
2
|
+
import { Contract as MulticallContract, Provider as MulticallProvider } from "@curvefi/ethcall";
|
|
3
|
+
|
|
4
|
+
export interface IDict<T> {
|
|
5
|
+
[index: string]: T,
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type INetworkName = "ethereum" | "optimism" | "bsc" | "xdai" | "polygon" | "x-layer" | "fantom" | "fraxtal" | "zksync" | "moonbeam" | "kava" | "mantle" | "base" | "arbitrum" | "celo" | "avalanche" | "aurora";
|
|
9
|
+
export type IChainId = 1 | 10 | 56 | 100 | 137 | 196 | 250 | 252 | 324 | 1284 | 2222 | 5000 | 8453 | 42161 | 42220 | 43114 | 1313161554;
|
|
10
|
+
export type IPoolFactory = "main" | "crypto" | "factory" | "factory-crvusd" | "factory-crypto" | "factory-twocrypto" | "factory-tricrypto" | "factory-stable-ng";
|
|
11
|
+
export interface ICurveContract {
|
|
12
|
+
contract: Contract,
|
|
13
|
+
multicallContract: MulticallContract,
|
|
14
|
+
abi: any,
|
|
15
|
+
address: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type TAmount = number | string
|
|
19
|
+
export type TGas = number | number[]
|
|
20
|
+
|
|
21
|
+
export interface ILlamma {
|
|
22
|
+
amm_address: string,
|
|
23
|
+
controller_address: string,
|
|
24
|
+
monetary_policy_address: string,
|
|
25
|
+
collateral_address: string,
|
|
26
|
+
leverage_zap: string,
|
|
27
|
+
deleverage_zap: string,
|
|
28
|
+
health_calculator_zap?: string,
|
|
29
|
+
collateral_symbol: string,
|
|
30
|
+
collateral_decimals: number,
|
|
31
|
+
min_bands: number,
|
|
32
|
+
max_bands: number,
|
|
33
|
+
default_bands: number,
|
|
34
|
+
A: number,
|
|
35
|
+
monetary_policy_abi: any
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface ICoin {
|
|
39
|
+
address: string,
|
|
40
|
+
name: string,
|
|
41
|
+
symbol: string,
|
|
42
|
+
decimals: number,
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface IOneWayMarket {
|
|
46
|
+
name: string,
|
|
47
|
+
addresses: {
|
|
48
|
+
amm: string,
|
|
49
|
+
controller: string,
|
|
50
|
+
borrowed_token: string,
|
|
51
|
+
collateral_token: string,
|
|
52
|
+
monetary_policy: string,
|
|
53
|
+
vault: string,
|
|
54
|
+
gauge: string,
|
|
55
|
+
},
|
|
56
|
+
borrowed_token: ICoin,
|
|
57
|
+
collateral_token: ICoin,
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ILlamalend {
|
|
61
|
+
provider: ethers.BrowserProvider | ethers.JsonRpcProvider,
|
|
62
|
+
multicallProvider: MulticallProvider,
|
|
63
|
+
signer: ethers.Signer | null,
|
|
64
|
+
signerAddress: string,
|
|
65
|
+
contracts: { [index: string]: { contract: Contract, multicallContract: MulticallContract } },
|
|
66
|
+
feeData: { gasPrice?: number, maxFeePerGas?: number, maxPriorityFeePerGas?: number },
|
|
67
|
+
constantOptions: { gasLimit: number },
|
|
68
|
+
options: { gasPrice?: number | bigint, maxFeePerGas?: number | bigint, maxPriorityFeePerGas?: number | bigint },
|
|
69
|
+
constants: {
|
|
70
|
+
ONE_WAY_MARKETS: IDict<IOneWayMarket>,
|
|
71
|
+
DECIMALS: IDict<number>;
|
|
72
|
+
NETWORK_NAME: INetworkName;
|
|
73
|
+
ALIASES: Record<string, string>;
|
|
74
|
+
COINS: Record<string, string>;
|
|
75
|
+
ZERO_ADDRESS: string,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ICoinFromPoolDataApi {
|
|
80
|
+
address: string,
|
|
81
|
+
symbol: string,
|
|
82
|
+
decimals: string,
|
|
83
|
+
usdPrice: number | string,
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface IReward {
|
|
87
|
+
gaugeAddress: string,
|
|
88
|
+
tokenAddress: string,
|
|
89
|
+
tokenPrice?: number,
|
|
90
|
+
name?: string,
|
|
91
|
+
symbol: string,
|
|
92
|
+
decimals?: number,
|
|
93
|
+
apy: number
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface IPoolDataFromApi {
|
|
97
|
+
id: string,
|
|
98
|
+
name: string,
|
|
99
|
+
symbol: string,
|
|
100
|
+
assetTypeName: string,
|
|
101
|
+
address: string,
|
|
102
|
+
lpTokenAddress?: string,
|
|
103
|
+
gaugeAddress?: string,
|
|
104
|
+
implementation: string,
|
|
105
|
+
implementationAddress: string,
|
|
106
|
+
coins: ICoinFromPoolDataApi[],
|
|
107
|
+
gaugeRewards?: IReward[],
|
|
108
|
+
usdTotal: number,
|
|
109
|
+
totalSupply: number,
|
|
110
|
+
amplificationCoefficient: string,
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface IExtendedPoolDataFromApi {
|
|
114
|
+
poolData: IPoolDataFromApi[],
|
|
115
|
+
tvl?: number,
|
|
116
|
+
tvlAll: number,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface IReward {
|
|
120
|
+
gaugeAddress: string,
|
|
121
|
+
tokenAddress: string,
|
|
122
|
+
symbol: string,
|
|
123
|
+
apy: number
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export type T1inchRouteStep = {
|
|
127
|
+
name: string,
|
|
128
|
+
part: number,
|
|
129
|
+
fromTokenAddress: string,
|
|
130
|
+
toTokenAddress: string,
|
|
131
|
+
}[]
|
|
132
|
+
|
|
133
|
+
export interface I1inchRoute {
|
|
134
|
+
part: number,
|
|
135
|
+
hops: T1inchRouteStep[],
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export interface I1inchSwapData {
|
|
139
|
+
tx: { data: string },
|
|
140
|
+
dstAmount: string,
|
|
141
|
+
protocols: I1inchRoute[],
|
|
142
|
+
slippage: number,
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
interface Rates {
|
|
146
|
+
borrowApr: number;
|
|
147
|
+
borrowApy: number;
|
|
148
|
+
borrowApyPcent: number;
|
|
149
|
+
lendApr: number;
|
|
150
|
+
lendApy: number;
|
|
151
|
+
lendApyPcent: number;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
interface AssetDetail {
|
|
155
|
+
symbol: string;
|
|
156
|
+
decimals: number;
|
|
157
|
+
address: string;
|
|
158
|
+
blockchainId: string;
|
|
159
|
+
usdPrice: number;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
interface Assets {
|
|
163
|
+
borrowed: AssetDetail;
|
|
164
|
+
collateral: AssetDetail;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
interface VaultShares {
|
|
168
|
+
pricePerShare: number;
|
|
169
|
+
totalShares: number;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
interface Total {
|
|
173
|
+
total: number;
|
|
174
|
+
usdTotal: number;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
interface LendingVaultUrls {
|
|
178
|
+
deposit: string;
|
|
179
|
+
withdraw: string;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface AmmBalances {
|
|
183
|
+
ammBalanceBorrowed: number;
|
|
184
|
+
ammBalanceBorrowedUsd: number;
|
|
185
|
+
ammBalanceCollateral: number;
|
|
186
|
+
ammBalanceCollateralUsd: number;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface IMarketDataAPI {
|
|
190
|
+
id: string;
|
|
191
|
+
name: string;
|
|
192
|
+
address: string;
|
|
193
|
+
controllerAddress: string;
|
|
194
|
+
ammAddress: string;
|
|
195
|
+
monetaryPolicyAddress: string;
|
|
196
|
+
rates: Rates;
|
|
197
|
+
gaugeAddress: string;
|
|
198
|
+
gaugeRewards: any[];
|
|
199
|
+
assets: Assets;
|
|
200
|
+
vaultShares: VaultShares;
|
|
201
|
+
totalSupplied: Total;
|
|
202
|
+
borrowed: Total;
|
|
203
|
+
availableToBorrow: Total;
|
|
204
|
+
lendingVaultUrls: LendingVaultUrls;
|
|
205
|
+
usdTotal: number;
|
|
206
|
+
ammBalances: AmmBalances;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface IMarketData {
|
|
210
|
+
lendingVaultData: IMarketDataAPI[]
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export interface IQuoteOdos {
|
|
214
|
+
outAmounts: string[],
|
|
215
|
+
priceImpact: number,
|
|
216
|
+
pathId: string | null,
|
|
217
|
+
pathVizImage: string,
|
|
218
|
+
slippage: number,
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
export interface ILlamma {
|
|
223
|
+
amm_address: string,
|
|
224
|
+
controller_address: string,
|
|
225
|
+
monetary_policy_address: string,
|
|
226
|
+
collateral_address: string,
|
|
227
|
+
leverage_zap: string,
|
|
228
|
+
deleverage_zap: string,
|
|
229
|
+
health_calculator_zap?: string,
|
|
230
|
+
collateral_symbol: string,
|
|
231
|
+
collateral_decimals: number,
|
|
232
|
+
min_bands: number,
|
|
233
|
+
max_bands: number,
|
|
234
|
+
default_bands: number,
|
|
235
|
+
A: number,
|
|
236
|
+
monetary_policy_abi: any
|
|
237
|
+
}
|