@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,216 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import llamalend from "../src/index.js";
|
|
3
|
+
import { getOneWayMarket, OneWayMarketTemplate } from "../src/markets/index.js";
|
|
4
|
+
import { BN } from "../src/utils.js";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const ONE_WAY_MARKETS = ['one-way-market-0'];
|
|
8
|
+
|
|
9
|
+
const generalTest = (id: string) => {
|
|
10
|
+
describe(`${id} oneWayMarket general test`, function () {
|
|
11
|
+
let oneWayMarket: OneWayMarketTemplate;
|
|
12
|
+
|
|
13
|
+
before(async function () {
|
|
14
|
+
oneWayMarket = getOneWayMarket(id);
|
|
15
|
+
const maxDeposit = Number(await oneWayMarket.vault.maxDeposit()) * 0.99;
|
|
16
|
+
await oneWayMarket.vault.deposit(maxDeposit);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('Create loan', async function () {
|
|
20
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
21
|
+
const initialState = await oneWayMarket.userState();
|
|
22
|
+
|
|
23
|
+
assert.equal(Number(initialState.collateral), 0);
|
|
24
|
+
assert.equal(Number(initialState.borrowed), 0);
|
|
25
|
+
assert.equal(Number(initialState.debt), 0);
|
|
26
|
+
assert.isAbove(Number(initialBalances.collateral), 0);
|
|
27
|
+
|
|
28
|
+
const collateralAmount = 0.5;
|
|
29
|
+
const N = 5;
|
|
30
|
+
const maxRecv = await oneWayMarket.createLoanMaxRecv(collateralAmount, N);
|
|
31
|
+
const debtAmount = (Number(maxRecv) / 2).toFixed(18);
|
|
32
|
+
const createLoanPrices = await oneWayMarket.createLoanPrices(collateralAmount, debtAmount, N);
|
|
33
|
+
const createLoanFullHealth = await oneWayMarket.createLoanHealth(collateralAmount, debtAmount, N);
|
|
34
|
+
const createLoanHealth = await oneWayMarket.createLoanHealth(collateralAmount, debtAmount, N, false);
|
|
35
|
+
|
|
36
|
+
await oneWayMarket.createLoan(collateralAmount, debtAmount, N);
|
|
37
|
+
|
|
38
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
39
|
+
const state = await oneWayMarket.userState();
|
|
40
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
41
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
42
|
+
const health = await oneWayMarket.userHealth(false);
|
|
43
|
+
|
|
44
|
+
assert.approximately(Number(createLoanPrices[0]), Number(userPrices[0]), 1e-2, 'price 0');
|
|
45
|
+
assert.approximately(Number(createLoanPrices[1]), Number(userPrices[1]), 1e-2, 'price 1');
|
|
46
|
+
assert.approximately(Number(createLoanFullHealth), Number(fullHealth), 0.1, 'full health');
|
|
47
|
+
assert.approximately(Number(createLoanHealth), Number(health), 1e-4, 'health');
|
|
48
|
+
assert.equal(Number(balances.collateral), Number(initialBalances.collateral) - Number(collateralAmount), 'wallet collateral');
|
|
49
|
+
assert.approximately(Number(balances.borrowed), BN(initialBalances.borrowed).plus(Number(debtAmount)).toNumber(), 1e-12, 'wallet borrowed');
|
|
50
|
+
assert.equal(Number(state.collateral), Number(collateralAmount), 'state collateral');
|
|
51
|
+
assert.equal(Number(state.debt), Number(debtAmount), 'state debt');
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('Borrow more', async function () {
|
|
55
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
56
|
+
const initialState = await oneWayMarket.userState();
|
|
57
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
58
|
+
|
|
59
|
+
assert.isTrue(loanExists);
|
|
60
|
+
assert.isAbove(Number(initialBalances.collateral), 0);
|
|
61
|
+
|
|
62
|
+
const collateralAmount = 0.5;
|
|
63
|
+
const maxRecv = await oneWayMarket.borrowMoreMaxRecv(collateralAmount);
|
|
64
|
+
const debtAmount = (Number(maxRecv) / 2).toFixed(18);
|
|
65
|
+
const borrowMorePrices = await oneWayMarket.borrowMorePrices(collateralAmount, debtAmount);
|
|
66
|
+
const borrowMoreFullHealth = await oneWayMarket.borrowMoreHealth(collateralAmount, debtAmount);
|
|
67
|
+
const borrowMoreHealth = await oneWayMarket.borrowMoreHealth(collateralAmount, debtAmount, false);
|
|
68
|
+
|
|
69
|
+
await oneWayMarket.borrowMore(collateralAmount, debtAmount);
|
|
70
|
+
|
|
71
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
72
|
+
const state = await oneWayMarket.userState();
|
|
73
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
74
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
75
|
+
const health = await oneWayMarket.userHealth(false);
|
|
76
|
+
|
|
77
|
+
assert.approximately(Number(borrowMorePrices[0]), Number(userPrices[0]), 1e-2, 'price 0');
|
|
78
|
+
assert.approximately(Number(borrowMorePrices[1]), Number(userPrices[1]), 1e-2, 'price 1');
|
|
79
|
+
assert.approximately(Number(borrowMoreFullHealth), Number(fullHealth), 1e-2, 'full health');
|
|
80
|
+
assert.approximately(Number(borrowMoreHealth), Number(health), 1e-4, 'health');
|
|
81
|
+
assert.equal(Number(balances.collateral), Number(initialBalances.collateral) - Number(collateralAmount), 'wallet collateral');
|
|
82
|
+
assert.equal(balances.borrowed, BN(initialBalances.borrowed).plus(BN(debtAmount)).toString(), 'wallet borrowed');
|
|
83
|
+
assert.equal(Number(state.collateral), Number(initialState.collateral) + Number(collateralAmount), 'state collateral');
|
|
84
|
+
assert.approximately(Number(state.debt), Number(initialState.debt) + Number(debtAmount), 1e-4, 'state debt');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('Add collateral', async function () {
|
|
88
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
89
|
+
const initialState = await oneWayMarket.userState();
|
|
90
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
91
|
+
|
|
92
|
+
assert.isTrue(loanExists);
|
|
93
|
+
assert.isAbove(Number(initialBalances.collateral), 0);
|
|
94
|
+
|
|
95
|
+
const collateralAmount = 1;
|
|
96
|
+
const addCollateralPrices = await oneWayMarket.addCollateralPrices(collateralAmount);
|
|
97
|
+
const addCollateralFullHealth = await oneWayMarket.addCollateralHealth(collateralAmount);
|
|
98
|
+
const addCollateralHealth = await oneWayMarket.addCollateralHealth(collateralAmount, false);
|
|
99
|
+
|
|
100
|
+
await oneWayMarket.addCollateral(collateralAmount);
|
|
101
|
+
|
|
102
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
103
|
+
const state = await oneWayMarket.userState();
|
|
104
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
105
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
106
|
+
const health = await oneWayMarket.userHealth(false);
|
|
107
|
+
|
|
108
|
+
assert.approximately(Number(addCollateralPrices[0]), Number(userPrices[0]), 1e-2, 'price 0');
|
|
109
|
+
assert.approximately(Number(addCollateralPrices[1]), Number(userPrices[1]), 1e-2, 'price 1');
|
|
110
|
+
assert.approximately(Number(addCollateralFullHealth), Number(fullHealth), 1e-2, 'full health');
|
|
111
|
+
assert.approximately(Number(addCollateralHealth), Number(health), 1e-4, 'health');
|
|
112
|
+
assert.equal(Number(balances.collateral), Number(initialBalances.collateral) - Number(collateralAmount), 'wallet collateral');
|
|
113
|
+
assert.equal(Number(balances.borrowed), Number(initialBalances.borrowed), 'wallet borrowed');
|
|
114
|
+
assert.equal(Number(state.collateral), Number(initialState.collateral) + Number(collateralAmount), 'state collateral');
|
|
115
|
+
assert.approximately(Number(initialState.debt), Number(state.debt), 1e-4, 'state debt');
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('Remove collateral', async function () {
|
|
119
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
120
|
+
const initialState = await oneWayMarket.userState();
|
|
121
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
122
|
+
|
|
123
|
+
assert.isTrue(loanExists);
|
|
124
|
+
assert.isAbove(Number(initialState.collateral), 0);
|
|
125
|
+
|
|
126
|
+
const maxRemovable = await oneWayMarket.maxRemovable();
|
|
127
|
+
const collateralAmount = (Number(maxRemovable) / 2).toFixed(oneWayMarket.collateral_token.decimals);
|
|
128
|
+
const removeCollateralPrices = await oneWayMarket.removeCollateralPrices(collateralAmount);
|
|
129
|
+
const removeCollateralFullHealth = await oneWayMarket.removeCollateralHealth(collateralAmount);
|
|
130
|
+
const removeCollateralHealth = await oneWayMarket.removeCollateralHealth(collateralAmount, false);
|
|
131
|
+
|
|
132
|
+
await oneWayMarket.removeCollateral(collateralAmount);
|
|
133
|
+
|
|
134
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
135
|
+
const state = await oneWayMarket.userState();
|
|
136
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
137
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
138
|
+
const health = await oneWayMarket.userHealth(false);
|
|
139
|
+
|
|
140
|
+
assert.approximately(Number(removeCollateralPrices[0]), Number(userPrices[0]), 1e-2, 'price 0');
|
|
141
|
+
assert.approximately(Number(removeCollateralPrices[1]), Number(userPrices[1]), 1e-2, 'price 1');
|
|
142
|
+
assert.approximately(Number(removeCollateralFullHealth), Number(fullHealth), 1e-2, 'full health');
|
|
143
|
+
assert.approximately(Number(removeCollateralHealth), Number(health), 1e-4, 'health');
|
|
144
|
+
assert.equal(Number(balances.collateral), Number(initialBalances.collateral) + Number(collateralAmount), 'wallet collateral');
|
|
145
|
+
assert.equal(Number(balances.borrowed), Number(initialBalances.borrowed), 'wallet borrowed');
|
|
146
|
+
assert.equal(state.collateral, BN(initialState.collateral).minus(BN(collateralAmount)).toString(), 'state collateral');
|
|
147
|
+
assert.approximately(Number(initialState.debt), Number(state.debt), 1e-4, 'state debt');
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it('Partial repay', async function () {
|
|
151
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
152
|
+
const initialState = await oneWayMarket.userState();
|
|
153
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
154
|
+
const debtAmount = (Number(initialState.debt) / 4).toFixed(18);
|
|
155
|
+
|
|
156
|
+
assert.isTrue(loanExists);
|
|
157
|
+
assert.isAtLeast(Number(initialBalances.borrowed), Number(debtAmount));
|
|
158
|
+
|
|
159
|
+
const repayPrices = await oneWayMarket.repayPrices(debtAmount);
|
|
160
|
+
const repayFullHealth = await oneWayMarket.repayHealth(debtAmount);
|
|
161
|
+
const repayHealth = await oneWayMarket.repayHealth(debtAmount, false);
|
|
162
|
+
|
|
163
|
+
await oneWayMarket.repay(debtAmount);
|
|
164
|
+
|
|
165
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
166
|
+
const state = await oneWayMarket.userState();
|
|
167
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
168
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
169
|
+
const health = await oneWayMarket.userHealth(false);
|
|
170
|
+
|
|
171
|
+
assert.approximately(Number(repayPrices[0]), Number(userPrices[0]), 1e-2, 'price 0');
|
|
172
|
+
assert.approximately(Number(repayPrices[1]), Number(userPrices[1]), 1e-2, 'price 1');
|
|
173
|
+
assert.approximately(Number(repayFullHealth), Number(fullHealth), 1e-2, 'full health');
|
|
174
|
+
assert.approximately(Number(repayHealth), Number(health), 1e-4, 'health');
|
|
175
|
+
assert.equal(Number(balances.collateral), Number(initialBalances.collateral), 'wallet collateral');
|
|
176
|
+
assert.equal(balances.borrowed, BN(initialBalances.borrowed).minus(BN(debtAmount)).toString(), 'wallet borrowed');
|
|
177
|
+
assert.equal(Number(state.collateral), Number(initialState.collateral), 'state collateral');
|
|
178
|
+
assert.equal(Number(state.borrowed), Number(initialState.borrowed), 'state borrowed');
|
|
179
|
+
assert.approximately(Number(state.debt), Number(initialState.debt) - Number(debtAmount), 1e-4, 'state debt');
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
it('Full repay', async function () {
|
|
183
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
184
|
+
const initialState = await oneWayMarket.userState();
|
|
185
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
186
|
+
|
|
187
|
+
assert.isTrue(loanExists);
|
|
188
|
+
assert.isAtLeast(Number(initialBalances.borrowed), Number(initialState.debt));
|
|
189
|
+
|
|
190
|
+
await oneWayMarket.fullRepay();
|
|
191
|
+
|
|
192
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
193
|
+
const state = await oneWayMarket.userState();
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
assert.approximately(Number(balances.collateral), Number(initialBalances.collateral) + Number(initialState.collateral), 10**(-oneWayMarket.collateral_token.decimals), 'wallet collateral');
|
|
197
|
+
assert.approximately(Number(balances.borrowed), Number(initialBalances.borrowed) - Number(initialState.debt), 1e-3, 'wallet borrowed');
|
|
198
|
+
assert.equal(Number(state.collateral), 0, 'state collateral');
|
|
199
|
+
assert.equal(Number(state.borrowed), 0, 'state borrowed');
|
|
200
|
+
assert.equal(Number(state.debt), 0, 'state debt');
|
|
201
|
+
});
|
|
202
|
+
})
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
describe('General test', async function () {
|
|
206
|
+
this.timeout(120000);
|
|
207
|
+
|
|
208
|
+
before(async function () {
|
|
209
|
+
await llamalend.init('JsonRpc', {},{ gasPrice: 0 });
|
|
210
|
+
await llamalend.oneWayfactory.fetchMarkets();
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
for (const oneWayMarketId of ONE_WAY_MARKETS) {
|
|
214
|
+
generalTest(oneWayMarketId);
|
|
215
|
+
}
|
|
216
|
+
})
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { assert } from "chai";
|
|
2
|
+
import llamalend from "../src/index.js";
|
|
3
|
+
import { getOneWayMarket, OneWayMarketTemplate } from "../src/markets/index.js";
|
|
4
|
+
import { BN } from "../src/utils.js";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
const ONE_WAY_MARKETS = ['one-way-market-9'];
|
|
8
|
+
|
|
9
|
+
const generalTest = (id: string) => {
|
|
10
|
+
describe(`${id} leverage borrowMore test`, function () {
|
|
11
|
+
let oneWayMarket: OneWayMarketTemplate;
|
|
12
|
+
|
|
13
|
+
before(async function () {
|
|
14
|
+
oneWayMarket = getOneWayMarket(id);
|
|
15
|
+
if (Number(await oneWayMarket.vault.totalLiquidity()) === 0) {
|
|
16
|
+
const maxDeposit = Number(await oneWayMarket.vault.maxDeposit()) * 0.7;
|
|
17
|
+
await oneWayMarket.vault.deposit(maxDeposit);
|
|
18
|
+
}
|
|
19
|
+
if (!(await oneWayMarket.userLoanExists())) {
|
|
20
|
+
const collateralAmount = 0.02;
|
|
21
|
+
const borrowedAmount = 1000;
|
|
22
|
+
const N = 10;
|
|
23
|
+
const { maxDebt } = await oneWayMarket.leverage.createLoanMaxRecv(collateralAmount, borrowedAmount, N);
|
|
24
|
+
const debtAmount = (Number(maxDebt) / 2).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
25
|
+
|
|
26
|
+
await oneWayMarket.leverage.createLoanExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
27
|
+
await oneWayMarket.leverage.createLoan(collateralAmount, borrowedAmount, debtAmount, N, 1);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('Leverage borrowMore collateral only, debt too high', async function () {
|
|
32
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
33
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
34
|
+
|
|
35
|
+
assert.isTrue(loanExists, "loanExists");
|
|
36
|
+
assert.isAbove(Number(initialBalances.collateral), 0, "collateral > 0");
|
|
37
|
+
assert.isAbove(Number(initialBalances.borrowed), 0, "borrowed > 0");
|
|
38
|
+
|
|
39
|
+
const collateralAmount = 0.02;
|
|
40
|
+
const borrowedAmount = 0;
|
|
41
|
+
const { maxDebt } = await oneWayMarket.leverage.borrowMoreMaxRecv(collateralAmount, borrowedAmount);
|
|
42
|
+
const debtAmount = (Number(maxDebt) * 1.004).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await oneWayMarket.leverage.borrowMoreExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
46
|
+
await oneWayMarket.leverage.borrowMore(collateralAmount, borrowedAmount, debtAmount,1);
|
|
47
|
+
throw Error("Did not revert");
|
|
48
|
+
} catch (e) {
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
assert.notEqual(e.message, "Did not revert", e.message);
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
assert.isTrue(e.message.startsWith('execution reverted: "Debt too high"'), e.message);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('Leverage borrowMore borrowed only, debt too high', async function () {
|
|
57
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
58
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
59
|
+
|
|
60
|
+
assert.isTrue(loanExists, "loanExists");
|
|
61
|
+
assert.isAbove(Number(initialBalances.collateral), 0, "collateral > 0");
|
|
62
|
+
assert.isAbove(Number(initialBalances.borrowed), 0, "borrowed > 0");
|
|
63
|
+
|
|
64
|
+
const collateralAmount = 0;
|
|
65
|
+
const borrowedAmount = 1000;
|
|
66
|
+
const { maxDebt } = await oneWayMarket.leverage.borrowMoreMaxRecv(collateralAmount, borrowedAmount);
|
|
67
|
+
const debtAmount = (Number(maxDebt) * 1.004).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await oneWayMarket.leverage.borrowMoreExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
71
|
+
await oneWayMarket.leverage.borrowMore(collateralAmount, borrowedAmount, debtAmount,1);
|
|
72
|
+
throw Error("Did not revert");
|
|
73
|
+
} catch (e) {
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
assert.notEqual(e.message, "Did not revert", e.message);
|
|
76
|
+
// @ts-ignore
|
|
77
|
+
assert.isTrue(e.message.startsWith('execution reverted: "Debt too high"'), e.message);
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('Leverage borrowMore, debt too high', async function () {
|
|
82
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
83
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
84
|
+
|
|
85
|
+
assert.isTrue(loanExists, "loanExists");
|
|
86
|
+
assert.isAbove(Number(initialBalances.collateral), 0, "collateral > 0");
|
|
87
|
+
assert.isAbove(Number(initialBalances.borrowed), 0, "borrowed > 0");
|
|
88
|
+
|
|
89
|
+
const collateralAmount = 0.02;
|
|
90
|
+
const borrowedAmount = 1000;
|
|
91
|
+
const { maxDebt } = await oneWayMarket.leverage.borrowMoreMaxRecv(collateralAmount, borrowedAmount);
|
|
92
|
+
const debtAmount = (Number(maxDebt) * 1.004).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
93
|
+
|
|
94
|
+
try {
|
|
95
|
+
await oneWayMarket.leverage.borrowMoreExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
96
|
+
await oneWayMarket.leverage.borrowMore(collateralAmount, borrowedAmount, debtAmount,1);
|
|
97
|
+
throw Error("Did not revert");
|
|
98
|
+
} catch (e) {
|
|
99
|
+
// @ts-ignore
|
|
100
|
+
assert.notEqual(e.message, "Did not revert", e.message);
|
|
101
|
+
// @ts-ignore
|
|
102
|
+
assert.isTrue(e.message.startsWith('execution reverted: "Debt too high"'), e.message);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('Leverage borrowMore collateral only', async function () {
|
|
107
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
108
|
+
const initialState = await oneWayMarket.userState();
|
|
109
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
110
|
+
|
|
111
|
+
assert.isTrue(loanExists, "loanExists");
|
|
112
|
+
assert.isAbove(Number(initialBalances.collateral), 0, "collateral > 0");
|
|
113
|
+
assert.isAbove(Number(initialBalances.borrowed), 0, "borrowed > 0");
|
|
114
|
+
|
|
115
|
+
const collateralAmount = 0.02;
|
|
116
|
+
const borrowedAmount = 0;
|
|
117
|
+
const { maxDebt } = await oneWayMarket.leverage.borrowMoreMaxRecv(collateralAmount, borrowedAmount);
|
|
118
|
+
const debtAmount = (Number(maxDebt) / 2).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
119
|
+
const { totalCollateral } = await oneWayMarket.leverage.borrowMoreExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
120
|
+
const borrowMoreBands = await oneWayMarket.leverage.borrowMoreBands(collateralAmount, borrowedAmount, debtAmount);
|
|
121
|
+
const borrowMorePrices = await oneWayMarket.leverage.borrowMorePrices(collateralAmount, borrowedAmount, debtAmount);
|
|
122
|
+
const borrowMoreFullHealth = await oneWayMarket.leverage.borrowMoreHealth(collateralAmount, borrowedAmount, debtAmount);
|
|
123
|
+
const borrowMoreHealth = await oneWayMarket.leverage.borrowMoreHealth(collateralAmount, borrowedAmount, debtAmount, false);
|
|
124
|
+
|
|
125
|
+
await oneWayMarket.leverage.borrowMore(collateralAmount, borrowedAmount, debtAmount,1);
|
|
126
|
+
|
|
127
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
128
|
+
const state = await oneWayMarket.userState();
|
|
129
|
+
const userBands = await oneWayMarket.userBands();
|
|
130
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
131
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
132
|
+
const health = await oneWayMarket.userHealth(false);
|
|
133
|
+
|
|
134
|
+
assert.equal(Number(borrowMoreBands[0]), Number(userBands[0]), 'band 0');
|
|
135
|
+
assert.equal(Number(borrowMoreBands[1]), Number(userBands[1]), 'band 1');
|
|
136
|
+
assert.approximately(Number(borrowMorePrices[0]), Number(userPrices[0]), 1, 'price 0');
|
|
137
|
+
assert.approximately(Number(borrowMorePrices[1]), Number(userPrices[1]), 1, 'price 1');
|
|
138
|
+
assert.approximately(Number(borrowMoreFullHealth), Number(fullHealth), 1, 'full health');
|
|
139
|
+
assert.approximately(Number(borrowMoreHealth), Number(health), 0.3, 'health');
|
|
140
|
+
assert.equal(Number(balances.collateral), BN(initialBalances.collateral).minus(collateralAmount).toNumber(), 'wallet collateral');
|
|
141
|
+
assert.equal(Number(balances.borrowed), Number(initialBalances.borrowed) - borrowedAmount, 'wallet borrowed');
|
|
142
|
+
const collateralDiff = Number(state.collateral) - Number(initialState.collateral);
|
|
143
|
+
assert.isAtMost(Math.abs(collateralDiff - Number(totalCollateral)) / Number(totalCollateral), 0.01, 'state collateral');
|
|
144
|
+
const debtDiff = Number(state.debt) - Number(initialState.debt);
|
|
145
|
+
assert.isAtMost(Math.abs(debtDiff - Number(debtAmount)) / Number(debtAmount), 2e-6, 'state debt');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('Leverage borrowMore borrowed only', async function () {
|
|
149
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
150
|
+
const initialState = await oneWayMarket.userState();
|
|
151
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
152
|
+
|
|
153
|
+
assert.isTrue(loanExists, "loanExists");
|
|
154
|
+
assert.isAbove(Number(initialBalances.collateral), 0, "collateral > 0");
|
|
155
|
+
assert.isAbove(Number(initialBalances.borrowed), 0, "borrowed > 0");
|
|
156
|
+
|
|
157
|
+
const collateralAmount = 0;
|
|
158
|
+
const borrowedAmount = 1000;
|
|
159
|
+
const { maxDebt } = await oneWayMarket.leverage.borrowMoreMaxRecv(collateralAmount, borrowedAmount);
|
|
160
|
+
const debtAmount = (Number(maxDebt) / 2).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
161
|
+
const { totalCollateral } = await oneWayMarket.leverage.borrowMoreExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
162
|
+
const borrowMoreBands = await oneWayMarket.leverage.borrowMoreBands(collateralAmount, borrowedAmount, debtAmount);
|
|
163
|
+
const borrowMorePrices = await oneWayMarket.leverage.borrowMorePrices(collateralAmount, borrowedAmount, debtAmount);
|
|
164
|
+
const borrowMoreFullHealth = await oneWayMarket.leverage.borrowMoreHealth(collateralAmount, borrowedAmount, debtAmount);
|
|
165
|
+
const borrowMoreHealth = await oneWayMarket.leverage.borrowMoreHealth(collateralAmount, borrowedAmount, debtAmount, false);
|
|
166
|
+
|
|
167
|
+
await oneWayMarket.leverage.borrowMore(collateralAmount, borrowedAmount, debtAmount,1);
|
|
168
|
+
|
|
169
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
170
|
+
const state = await oneWayMarket.userState();
|
|
171
|
+
const userBands = await oneWayMarket.userBands();
|
|
172
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
173
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
174
|
+
const health = await oneWayMarket.userHealth(false);
|
|
175
|
+
|
|
176
|
+
assert.equal(Number(borrowMoreBands[0]), Number(userBands[0]), 'band 0');
|
|
177
|
+
assert.equal(Number(borrowMoreBands[1]), Number(userBands[1]), 'band 1');
|
|
178
|
+
assert.approximately(Number(borrowMorePrices[0]), Number(userPrices[0]), 1, 'price 0');
|
|
179
|
+
assert.approximately(Number(borrowMorePrices[1]), Number(userPrices[1]), 1, 'price 1');
|
|
180
|
+
assert.approximately(Number(borrowMoreFullHealth), Number(fullHealth), 1, 'full health');
|
|
181
|
+
assert.approximately(Number(borrowMoreHealth), Number(health), 0.3, 'health');
|
|
182
|
+
assert.equal(Number(balances.collateral), BN(initialBalances.collateral).minus(collateralAmount).toNumber(), 'wallet collateral');
|
|
183
|
+
assert.equal(Number(balances.borrowed), Number(initialBalances.borrowed) - borrowedAmount, 'wallet borrowed');
|
|
184
|
+
const collateralDiff = Number(state.collateral) - Number(initialState.collateral);
|
|
185
|
+
assert.isAtMost(Math.abs(collateralDiff - Number(totalCollateral)) / Number(totalCollateral), 0.01, 'state collateral');
|
|
186
|
+
const debtDiff = Number(state.debt) - Number(initialState.debt);
|
|
187
|
+
assert.isAtMost(Math.abs(debtDiff - Number(debtAmount)) / Number(debtAmount), 2e-6, 'state debt');
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('Leverage borrowMore', async function () {
|
|
191
|
+
const initialBalances = await oneWayMarket.wallet.balances();
|
|
192
|
+
const initialState = await oneWayMarket.userState();
|
|
193
|
+
const loanExists = await oneWayMarket.userLoanExists();
|
|
194
|
+
|
|
195
|
+
assert.isTrue(loanExists, "loanExists");
|
|
196
|
+
assert.isAbove(Number(initialBalances.collateral), 0, "collateral > 0");
|
|
197
|
+
assert.isAbove(Number(initialBalances.borrowed), 0, "borrowed > 0");
|
|
198
|
+
|
|
199
|
+
const collateralAmount = 0.02;
|
|
200
|
+
const borrowedAmount = 1000;
|
|
201
|
+
const { maxDebt } = await oneWayMarket.leverage.borrowMoreMaxRecv(collateralAmount, borrowedAmount);
|
|
202
|
+
const debtAmount = (Number(maxDebt) * 0.999).toFixed(oneWayMarket.borrowed_token.decimals);
|
|
203
|
+
const { totalCollateral } = await oneWayMarket.leverage.borrowMoreExpectedCollateral(collateralAmount, borrowedAmount, debtAmount, 1);
|
|
204
|
+
const borrowMoreBands = await oneWayMarket.leverage.borrowMoreBands(collateralAmount, borrowedAmount, debtAmount);
|
|
205
|
+
const borrowMorePrices = await oneWayMarket.leverage.borrowMorePrices(collateralAmount, borrowedAmount, debtAmount);
|
|
206
|
+
const borrowMoreFullHealth = await oneWayMarket.leverage.borrowMoreHealth(collateralAmount, borrowedAmount, debtAmount);
|
|
207
|
+
const borrowMoreHealth = await oneWayMarket.leverage.borrowMoreHealth(collateralAmount, borrowedAmount, debtAmount, false);
|
|
208
|
+
|
|
209
|
+
await oneWayMarket.leverage.borrowMore(collateralAmount, borrowedAmount, debtAmount,1);
|
|
210
|
+
|
|
211
|
+
const balances = await oneWayMarket.wallet.balances();
|
|
212
|
+
const state = await oneWayMarket.userState();
|
|
213
|
+
const userBands = await oneWayMarket.userBands();
|
|
214
|
+
const userPrices = await oneWayMarket.userPrices();
|
|
215
|
+
const fullHealth = await oneWayMarket.userHealth();
|
|
216
|
+
const health = await oneWayMarket.userHealth(false);
|
|
217
|
+
|
|
218
|
+
assert.equal(Number(borrowMoreBands[0]), Number(userBands[0]), 'band 0');
|
|
219
|
+
assert.equal(Number(borrowMoreBands[1]), Number(userBands[1]), 'band 1');
|
|
220
|
+
assert.approximately(Number(borrowMorePrices[0]), Number(userPrices[0]), 1, 'price 0');
|
|
221
|
+
assert.approximately(Number(borrowMorePrices[1]), Number(userPrices[1]), 1, 'price 1');
|
|
222
|
+
assert.approximately(Number(borrowMoreFullHealth), Number(fullHealth), 1, 'full health');
|
|
223
|
+
assert.approximately(Number(borrowMoreHealth), Number(health), 0.3, 'health');
|
|
224
|
+
assert.equal(Number(balances.collateral), BN(initialBalances.collateral).minus(collateralAmount).toNumber(), 'wallet collateral');
|
|
225
|
+
assert.equal(Number(balances.borrowed), Number(initialBalances.borrowed) - borrowedAmount, 'wallet borrowed');
|
|
226
|
+
const collateralDiff = Number(state.collateral) - Number(initialState.collateral);
|
|
227
|
+
assert.isAtMost(Math.abs(collateralDiff - Number(totalCollateral)) / Number(totalCollateral), 0.01, 'state collateral');
|
|
228
|
+
const debtDiff = Number(state.debt) - Number(initialState.debt);
|
|
229
|
+
assert.isAtMost(Math.abs(debtDiff - Number(debtAmount)) / Number(debtAmount), 2e-6, 'state debt');
|
|
230
|
+
});
|
|
231
|
+
})
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
describe('Leverage borrowMore test', async function () {
|
|
235
|
+
this.timeout(180000);
|
|
236
|
+
|
|
237
|
+
before(async function () {
|
|
238
|
+
await llamalend.init('JsonRpc', {},{ gasPrice: 0 });
|
|
239
|
+
await llamalend.oneWayfactory.fetchMarkets();
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
for (const oneWayMarketId of ONE_WAY_MARKETS) {
|
|
243
|
+
generalTest(oneWayMarketId);
|
|
244
|
+
}
|
|
245
|
+
})
|