@kamino-finance/klend-sdk 5.10.35-beta.0 → 5.10.35-beta.1
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/dist/classes/action.d.ts +26 -24
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +270 -501
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/manager.js +1 -1
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/obligation.d.ts +1 -1
- package/dist/classes/obligation.d.ts.map +1 -1
- package/dist/classes/obligation.js +1 -1
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/vault.js +6 -6
- package/dist/classes/vault.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.js +9 -5
- package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +7 -3
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +27 -7
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts +0 -5
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +2 -4
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +8 -8
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +24 -27
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +1 -4
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/utils/lookupTable.d.ts +27 -0
- package/dist/utils/lookupTable.d.ts.map +1 -1
- package/dist/utils/lookupTable.js +58 -0
- package/dist/utils/lookupTable.js.map +1 -1
- package/dist/utils/seeds.d.ts +9 -3
- package/dist/utils/seeds.d.ts.map +1 -1
- package/dist/utils/seeds.js +11 -5
- package/dist/utils/seeds.js.map +1 -1
- package/dist/utils/userMetadata.js +6 -6
- package/dist/utils/userMetadata.js.map +1 -1
- package/package.json +1 -4
- package/src/classes/action.ts +423 -662
- package/src/classes/manager.ts +1 -1
- package/src/classes/obligation.ts +1 -1
- package/src/classes/vault.ts +1 -1
- package/src/client.ts +3 -7
- package/src/lending_operations/repay_with_collateral_calcs.ts +14 -5
- package/src/lending_operations/repay_with_collateral_operations.ts +30 -14
- package/src/lending_operations/swap_collateral_operations.ts +0 -11
- package/src/leverage/operations.ts +11 -38
- package/src/leverage/types.ts +1 -4
- package/src/utils/lookupTable.ts +62 -0
- package/src/utils/seeds.ts +12 -7
- package/src/utils/userMetadata.ts +14 -14
- package/dist/classes/lut_utils.d.ts +0 -29
- package/dist/classes/lut_utils.d.ts.map +0 -1
- package/dist/classes/lut_utils.js +0 -62
- package/dist/classes/lut_utils.js.map +0 -1
- package/src/classes/lut_utils.ts +0 -63
package/src/classes/manager.ts
CHANGED
|
@@ -266,7 +266,7 @@ export class KaminoObligation {
|
|
|
266
266
|
/**
|
|
267
267
|
* @returns total borrow power of the obligation, relative to max LTV of each asset's reserve
|
|
268
268
|
*/
|
|
269
|
-
|
|
269
|
+
getMaxAllowedBorrowValue(): Decimal {
|
|
270
270
|
return new Fraction(this.state.allowedBorrowValueSf).toDecimal();
|
|
271
271
|
}
|
|
272
272
|
|
package/src/classes/vault.ts
CHANGED
|
@@ -82,7 +82,7 @@ import {
|
|
|
82
82
|
import { batchFetch, collToLamportsDecimal, ZERO } from '@kamino-finance/kliquidity-sdk';
|
|
83
83
|
import { FullBPSDecimal } from '@kamino-finance/kliquidity-sdk/dist/utils/CreationParameters';
|
|
84
84
|
import { FarmState } from '@kamino-finance/farms-sdk/dist';
|
|
85
|
-
import { getAccountsInLUT, initLookupTableIx } from '
|
|
85
|
+
import { getAccountsInLUT, initLookupTableIx } from '../utils/lookupTable';
|
|
86
86
|
import {
|
|
87
87
|
getFarmStakeIxs,
|
|
88
88
|
getFarmUnstakeAndWithdrawIxs,
|
package/src/client.ts
CHANGED
|
@@ -308,8 +308,7 @@ async function deposit(connection: Connection, wallet: Keypair, token: string, d
|
|
|
308
308
|
depositAmount,
|
|
309
309
|
kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
|
|
310
310
|
wallet.publicKey,
|
|
311
|
-
new VanillaObligation(STAGING_LENDING_MARKET)
|
|
312
|
-
true
|
|
311
|
+
new VanillaObligation(STAGING_LENDING_MARKET)
|
|
313
312
|
);
|
|
314
313
|
console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
|
|
315
314
|
|
|
@@ -329,8 +328,7 @@ async function withdraw(connection: Connection, wallet: Keypair, token: string,
|
|
|
329
328
|
depositAmount,
|
|
330
329
|
kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
|
|
331
330
|
wallet.publicKey,
|
|
332
|
-
new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET))
|
|
333
|
-
true
|
|
331
|
+
new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET))
|
|
334
332
|
);
|
|
335
333
|
console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
|
|
336
334
|
|
|
@@ -350,8 +348,7 @@ async function borrow(connection: Connection, wallet: Keypair, token: string, bo
|
|
|
350
348
|
borrowAmount,
|
|
351
349
|
kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
|
|
352
350
|
wallet.publicKey,
|
|
353
|
-
new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET))
|
|
354
|
-
true
|
|
351
|
+
new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET))
|
|
355
352
|
);
|
|
356
353
|
console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
|
|
357
354
|
|
|
@@ -372,7 +369,6 @@ async function repay(connection: Connection, wallet: Keypair, token: string, bor
|
|
|
372
369
|
kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
|
|
373
370
|
wallet.publicKey,
|
|
374
371
|
new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET)),
|
|
375
|
-
true,
|
|
376
372
|
await connection.getSlot()
|
|
377
373
|
);
|
|
378
374
|
console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
|
|
@@ -2,6 +2,7 @@ import Decimal from 'decimal.js';
|
|
|
2
2
|
import { KaminoMarket, KaminoObligation, KaminoReserve, numberToLamportsDecimal } from '../classes';
|
|
3
3
|
import { PublicKey } from '@solana/web3.js';
|
|
4
4
|
import { lamportsToDecimal } from '../classes/utils';
|
|
5
|
+
import { MaxWithdrawLtvCheck, getMaxWithdrawLtvCheck } from './repay_with_collateral_operations';
|
|
5
6
|
|
|
6
7
|
export function calcRepayAmountWithSlippage(
|
|
7
8
|
kaminoMarket: KaminoMarket,
|
|
@@ -102,6 +103,7 @@ export function calcMaxWithdrawCollateral(
|
|
|
102
103
|
.filter((p) => !p.reserveAddress.equals(borrow.reserveAddress))
|
|
103
104
|
.reduce((acc, b) => acc.add(b.marketValueRefreshed), new Decimal('0'));
|
|
104
105
|
}
|
|
106
|
+
const maxWithdrawLtvCheck = getMaxWithdrawLtvCheck(obligation);
|
|
105
107
|
|
|
106
108
|
let remainingDepositsValueWithLtv = new Decimal('0');
|
|
107
109
|
if (obligation.getDeposits().length > 1) {
|
|
@@ -109,8 +111,13 @@ export function calcMaxWithdrawCollateral(
|
|
|
109
111
|
.getDeposits()
|
|
110
112
|
.filter((p) => !p.reserveAddress.equals(deposit.reserveAddress))
|
|
111
113
|
.reduce((acc, d) => {
|
|
112
|
-
const { maxLtv } = obligation.getLtvForReserve(
|
|
113
|
-
|
|
114
|
+
const { maxLtv, liquidationLtv } = obligation.getLtvForReserve(
|
|
115
|
+
market,
|
|
116
|
+
market.getReserveByAddress(d.reserveAddress)!
|
|
117
|
+
);
|
|
118
|
+
const maxWithdrawLtv =
|
|
119
|
+
maxWithdrawLtvCheck === MaxWithdrawLtvCheck.LIQUIDATION_THRESHOLD ? liquidationLtv : maxLtv;
|
|
120
|
+
return acc.add(d.marketValueRefreshed.mul(maxWithdrawLtv));
|
|
114
121
|
}, new Decimal('0'));
|
|
115
122
|
}
|
|
116
123
|
|
|
@@ -123,16 +130,18 @@ export function calcMaxWithdrawCollateral(
|
|
|
123
130
|
repayingAllDebt: repayAmountLamports.gte(borrow.amount),
|
|
124
131
|
};
|
|
125
132
|
} else {
|
|
126
|
-
const { maxLtv: collMaxLtv } = obligation.getLtvForReserve(
|
|
133
|
+
const { maxLtv: collMaxLtv, liquidationLtv: collLiquidationLtv } = obligation.getLtvForReserve(
|
|
127
134
|
market,
|
|
128
135
|
market.getReserveByAddress(depositReserve.address)!
|
|
129
136
|
);
|
|
137
|
+
const maxWithdrawLtv =
|
|
138
|
+
maxWithdrawLtvCheck === MaxWithdrawLtvCheck.LIQUIDATION_THRESHOLD ? collLiquidationLtv : collMaxLtv;
|
|
130
139
|
const numerator = deposit.marketValueRefreshed
|
|
131
|
-
.mul(
|
|
140
|
+
.mul(maxWithdrawLtv)
|
|
132
141
|
.add(remainingDepositsValueWithLtv)
|
|
133
142
|
.sub(remainingBorrowsValue);
|
|
134
143
|
|
|
135
|
-
const denominator = depositReserve.getOracleMarketPrice().mul(
|
|
144
|
+
const denominator = depositReserve.getOracleMarketPrice().mul(maxWithdrawLtv);
|
|
136
145
|
const maxCollWithdrawAmount = numerator.div(denominator);
|
|
137
146
|
const withdrawableCollLamports = maxCollWithdrawAmount.mul(depositReserve.getMintFactor()).floor();
|
|
138
147
|
|
|
@@ -52,10 +52,14 @@ interface RepayWithCollSwapInputsProps<QuoteResponse> {
|
|
|
52
52
|
isClosingPosition: boolean;
|
|
53
53
|
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
54
54
|
scopeRefresh?: ScopeRefresh;
|
|
55
|
-
useV2Ixs: boolean;
|
|
56
55
|
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
57
56
|
}
|
|
58
57
|
|
|
58
|
+
export enum MaxWithdrawLtvCheck {
|
|
59
|
+
MAX_LTV,
|
|
60
|
+
LIQUIDATION_THRESHOLD,
|
|
61
|
+
}
|
|
62
|
+
|
|
59
63
|
export async function getRepayWithCollSwapInputs<QuoteResponse>({
|
|
60
64
|
collTokenMint,
|
|
61
65
|
currentSlot,
|
|
@@ -68,7 +72,6 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
|
|
|
68
72
|
isClosingPosition,
|
|
69
73
|
budgetAndPriorityFeeIxs,
|
|
70
74
|
scopeRefresh,
|
|
71
|
-
useV2Ixs,
|
|
72
75
|
}: RepayWithCollSwapInputsProps<QuoteResponse>): Promise<{
|
|
73
76
|
swapInputs: SwapInputs;
|
|
74
77
|
initialInputs: RepayWithCollInitialInputs<QuoteResponse>;
|
|
@@ -134,8 +137,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
|
|
|
134
137
|
},
|
|
135
138
|
isClosingPosition,
|
|
136
139
|
repayAmountLamports,
|
|
137
|
-
inputAmountLamports
|
|
138
|
-
useV2Ixs
|
|
140
|
+
inputAmountLamports
|
|
139
141
|
);
|
|
140
142
|
const uniqueKlendAccounts = uniqueAccounts(klendIxs);
|
|
141
143
|
|
|
@@ -192,7 +194,6 @@ export async function getRepayWithCollIxs<QuoteResponse>({
|
|
|
192
194
|
swapper,
|
|
193
195
|
referrer,
|
|
194
196
|
scopeRefresh,
|
|
195
|
-
useV2Ixs,
|
|
196
197
|
logger = console.log,
|
|
197
198
|
}: RepayWithCollIxsProps<QuoteResponse>): Promise<RepayWithCollIxsResponse<QuoteResponse>> {
|
|
198
199
|
const { swapInputs, initialInputs } = await getRepayWithCollSwapInputs({
|
|
@@ -207,13 +208,21 @@ export async function getRepayWithCollIxs<QuoteResponse>({
|
|
|
207
208
|
isClosingPosition,
|
|
208
209
|
budgetAndPriorityFeeIxs,
|
|
209
210
|
scopeRefresh,
|
|
210
|
-
useV2Ixs,
|
|
211
211
|
});
|
|
212
212
|
const { debtRepayAmountLamports, flashRepayAmountLamports, maxCollateralWithdrawLamports, swapQuote } = initialInputs;
|
|
213
213
|
const { inputAmountLamports: collSwapInLamports } = swapInputs;
|
|
214
214
|
|
|
215
|
-
const collReserve = kaminoMarket.getReserveByMint(collTokenMint)
|
|
216
|
-
|
|
215
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
216
|
+
|
|
217
|
+
if (!collReserve) {
|
|
218
|
+
throw new Error(`Collateral reserve with mint ${collTokenMint} not found in market ${kaminoMarket.getAddress()}`);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
222
|
+
|
|
223
|
+
if (!debtReserve) {
|
|
224
|
+
throw new Error(`Debt reserve with mint ${debtTokenMint} not found in market ${kaminoMarket.getAddress()}`);
|
|
225
|
+
}
|
|
217
226
|
|
|
218
227
|
// the client should use these values to prevent this input, but the tx may succeed, so we don't want to fail
|
|
219
228
|
// there is also a chance that the tx will consume debt token from the user's ata which they would not expect
|
|
@@ -248,8 +257,7 @@ export async function getRepayWithCollIxs<QuoteResponse>({
|
|
|
248
257
|
swapResponse,
|
|
249
258
|
isClosingPosition,
|
|
250
259
|
debtRepayAmountLamports,
|
|
251
|
-
swapInputs.inputAmountLamports
|
|
252
|
-
useV2Ixs
|
|
260
|
+
swapInputs.inputAmountLamports
|
|
253
261
|
);
|
|
254
262
|
|
|
255
263
|
return {
|
|
@@ -272,8 +280,7 @@ async function buildRepayWithCollateralIxs(
|
|
|
272
280
|
swapQuoteIxs: SwapIxs,
|
|
273
281
|
isClosingPosition: boolean,
|
|
274
282
|
debtRepayAmountLamports: Decimal,
|
|
275
|
-
collWithdrawLamports: Decimal
|
|
276
|
-
useV2Ixs: boolean
|
|
283
|
+
collWithdrawLamports: Decimal
|
|
277
284
|
): Promise<TransactionInstruction[]> {
|
|
278
285
|
// 1. Create atas & budget txns
|
|
279
286
|
const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(1_400_000);
|
|
@@ -303,8 +310,12 @@ async function buildRepayWithCollateralIxs(
|
|
|
303
310
|
|
|
304
311
|
const requestElevationGroup = !isClosingPosition && obligation.state.elevationGroup !== 0;
|
|
305
312
|
|
|
313
|
+
const maxWithdrawLtvCheck = getMaxWithdrawLtvCheck(obligation);
|
|
314
|
+
|
|
306
315
|
// 3. Repay using the flash borrowed funds & withdraw collateral to swap and pay the flash loan
|
|
307
|
-
const repayAndWithdrawAction = await
|
|
316
|
+
const repayAndWithdrawAction = await (maxWithdrawLtvCheck === MaxWithdrawLtvCheck.MAX_LTV
|
|
317
|
+
? KaminoAction.buildRepayAndWithdrawTxns
|
|
318
|
+
: KaminoAction.buildRepayAndWithdrawV2Txns)(
|
|
308
319
|
market,
|
|
309
320
|
isClosingPosition ? U64_MAX : debtRepayAmountLamports.toString(),
|
|
310
321
|
debtReserve.getLiquidityMint(),
|
|
@@ -313,7 +324,6 @@ async function buildRepayWithCollateralIxs(
|
|
|
313
324
|
obligation.state.owner,
|
|
314
325
|
currentSlot,
|
|
315
326
|
obligation,
|
|
316
|
-
useV2Ixs,
|
|
317
327
|
0,
|
|
318
328
|
false,
|
|
319
329
|
requestElevationGroup,
|
|
@@ -337,3 +347,9 @@ async function buildRepayWithCollateralIxs(
|
|
|
337
347
|
flashRepayIxn,
|
|
338
348
|
];
|
|
339
349
|
}
|
|
350
|
+
|
|
351
|
+
export const getMaxWithdrawLtvCheck = (obligation: KaminoObligation) => {
|
|
352
|
+
return obligation.refreshedStats.userTotalBorrowBorrowFactorAdjusted.gte(obligation.refreshedStats.borrowLimit)
|
|
353
|
+
? MaxWithdrawLtvCheck.LIQUIDATION_THRESHOLD
|
|
354
|
+
: MaxWithdrawLtvCheck.MAX_LTV;
|
|
355
|
+
};
|
|
@@ -62,7 +62,6 @@ export interface SwapCollIxnsInputs<QuoteResponse> {
|
|
|
62
62
|
currentSlot: number;
|
|
63
63
|
budgetAndPriorityFeeIxns?: TransactionInstruction[];
|
|
64
64
|
scopeRefresh?: ScopeRefresh;
|
|
65
|
-
useV2Ixs: boolean;
|
|
66
65
|
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
67
66
|
swapper: SwapIxsProvider<QuoteResponse>;
|
|
68
67
|
logger?: (msg: string, ...extra: any[]) => void;
|
|
@@ -82,11 +81,6 @@ export interface SwapCollIxnsOutputs<QuoteResponse> {
|
|
|
82
81
|
*/
|
|
83
82
|
lookupTables: AddressLookupTableAccount[];
|
|
84
83
|
|
|
85
|
-
/**
|
|
86
|
-
* Whether the swap is using V2 instructions.
|
|
87
|
-
*/
|
|
88
|
-
useV2Ixs: boolean;
|
|
89
|
-
|
|
90
84
|
/**
|
|
91
85
|
* Informational-only details of the token amounts/fees/rates that were used during construction of `ixs`.
|
|
92
86
|
*/
|
|
@@ -173,7 +167,6 @@ export async function getSwapCollIxns<QuoteResponse>(
|
|
|
173
167
|
return {
|
|
174
168
|
ixs: listIxns(klendIxns, externalSwapIxns.ixns),
|
|
175
169
|
lookupTables: externalSwapIxns.luts,
|
|
176
|
-
useV2Ixs: context.useV2Ixs,
|
|
177
170
|
simulationDetails: {
|
|
178
171
|
flashLoan: {
|
|
179
172
|
targetCollFlashBorrowedAmount: klendIxns.simulationDetails.targetCollFlashBorrowedAmount,
|
|
@@ -204,7 +197,6 @@ type SwapCollContext<QuoteResponse> = {
|
|
|
204
197
|
swapper: SwapIxsProvider<QuoteResponse>;
|
|
205
198
|
referrer: PublicKey;
|
|
206
199
|
currentSlot: number;
|
|
207
|
-
useV2Ixs: boolean;
|
|
208
200
|
scopeRefresh: ScopeRefresh | undefined;
|
|
209
201
|
logger: (msg: string, ...extra: any[]) => void;
|
|
210
202
|
};
|
|
@@ -237,7 +229,6 @@ function extractArgsAndContext<QuoteResponse>(
|
|
|
237
229
|
referrer: inputs.referrer,
|
|
238
230
|
scopeRefresh: inputs.scopeRefresh,
|
|
239
231
|
currentSlot: inputs.currentSlot,
|
|
240
|
-
useV2Ixs: inputs.useV2Ixs,
|
|
241
232
|
},
|
|
242
233
|
];
|
|
243
234
|
}
|
|
@@ -379,7 +370,6 @@ async function getDepositTargetCollIxns(
|
|
|
379
370
|
context.targetCollReserve.getLiquidityMint(),
|
|
380
371
|
context.obligation.state.owner,
|
|
381
372
|
context.obligation,
|
|
382
|
-
context.useV2Ixs,
|
|
383
373
|
0, // no extra compute budget
|
|
384
374
|
false, // we do not need ATA ixns here (we construct and close them ourselves)
|
|
385
375
|
removesElevationGroup, // we may need to (temporarily) remove the elevation group; the same or a different one will be set on withdraw, if requested
|
|
@@ -429,7 +419,6 @@ async function getWithdrawSourceCollIxns(
|
|
|
429
419
|
context.sourceCollReserve.getLiquidityMint(),
|
|
430
420
|
context.obligation.state.owner,
|
|
431
421
|
context.obligation,
|
|
432
|
-
context.useV2Ixs,
|
|
433
422
|
0, // no extra compute budget
|
|
434
423
|
false, // we do not need ATA ixns here (we construct and close them ourselves)
|
|
435
424
|
requestedElevationGroup !== undefined, // the `elevationGroupIdToRequestAfterWithdraw()` has already decided on this
|
|
@@ -53,7 +53,7 @@ import {
|
|
|
53
53
|
DepositLeverageInitialInputs,
|
|
54
54
|
DepositWithLeverageProps,
|
|
55
55
|
DepositWithLeverageSwapInputsProps,
|
|
56
|
-
|
|
56
|
+
DepsoitLeverageIxsResponse,
|
|
57
57
|
PriceAinBProvider,
|
|
58
58
|
SwapInputs,
|
|
59
59
|
SwapIxs,
|
|
@@ -86,7 +86,6 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
|
86
86
|
priceAinB,
|
|
87
87
|
isKtoken,
|
|
88
88
|
quoter,
|
|
89
|
-
useV2Ixs,
|
|
90
89
|
elevationGroupOverride,
|
|
91
90
|
}: DepositWithLeverageSwapInputsProps<QuoteResponse>): Promise<{
|
|
92
91
|
swapInputs: SwapInputs;
|
|
@@ -152,7 +151,6 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
|
152
151
|
},
|
|
153
152
|
strategy,
|
|
154
153
|
collIsKtoken,
|
|
155
|
-
useV2Ixs,
|
|
156
154
|
elevationGroupOverride
|
|
157
155
|
);
|
|
158
156
|
|
|
@@ -317,8 +315,7 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
|
|
|
317
315
|
quoter,
|
|
318
316
|
swapper,
|
|
319
317
|
elevationGroupOverride,
|
|
320
|
-
|
|
321
|
-
}: DepositWithLeverageProps<QuoteResponse>): Promise<DepositLeverageIxsResponse<QuoteResponse>> {
|
|
318
|
+
}: DepositWithLeverageProps<QuoteResponse>): Promise<DepsoitLeverageIxsResponse<QuoteResponse>> {
|
|
322
319
|
const { swapInputs, initialInputs } = await getDepositWithLeverageSwapInputs({
|
|
323
320
|
owner,
|
|
324
321
|
kaminoMarket,
|
|
@@ -340,7 +337,6 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
|
|
|
340
337
|
priceAinB,
|
|
341
338
|
isKtoken,
|
|
342
339
|
quoter,
|
|
343
|
-
useV2Ixs,
|
|
344
340
|
});
|
|
345
341
|
|
|
346
342
|
let depositSwapper: SwapIxsProvider<QuoteResponse>;
|
|
@@ -396,7 +392,6 @@ export async function getDepositWithLeverageIxns<QuoteResponse>({
|
|
|
396
392
|
},
|
|
397
393
|
initialInputs.strategy,
|
|
398
394
|
initialInputs.collIsKtoken,
|
|
399
|
-
useV2Ixs,
|
|
400
395
|
elevationGroupOverride
|
|
401
396
|
);
|
|
402
397
|
|
|
@@ -423,7 +418,6 @@ async function buildDepositWithLeverageIxns(
|
|
|
423
418
|
swapQuoteIxs: SwapIxs,
|
|
424
419
|
strategy: StrategyWithAddress | undefined,
|
|
425
420
|
collIsKtoken: boolean,
|
|
426
|
-
useV2Ixs: boolean,
|
|
427
421
|
elevationGroupOverride?: number
|
|
428
422
|
): Promise<TransactionInstruction[]> {
|
|
429
423
|
const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(3000000);
|
|
@@ -536,7 +530,6 @@ async function buildDepositWithLeverageIxns(
|
|
|
536
530
|
debtTokenMint,
|
|
537
531
|
owner,
|
|
538
532
|
obligation!,
|
|
539
|
-
useV2Ixs,
|
|
540
533
|
0,
|
|
541
534
|
false,
|
|
542
535
|
elevationGroupOverride === 0 ? false : true, // emode
|
|
@@ -599,7 +592,6 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
599
592
|
quoteBufferBps,
|
|
600
593
|
isKtoken,
|
|
601
594
|
quoter,
|
|
602
|
-
useV2Ixs,
|
|
603
595
|
}: WithdrawWithLeverageSwapInputsProps<QuoteResponse>): Promise<{
|
|
604
596
|
swapInputs: SwapInputs;
|
|
605
597
|
initialInputs: WithdrawLeverageInitialInputs<QuoteResponse>;
|
|
@@ -650,8 +642,7 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
650
642
|
lookupTables: [],
|
|
651
643
|
},
|
|
652
644
|
strategy,
|
|
653
|
-
collIsKtoken
|
|
654
|
-
useV2Ixs
|
|
645
|
+
collIsKtoken
|
|
655
646
|
);
|
|
656
647
|
|
|
657
648
|
const uniqueKlendAccounts = uniqueAccounts(klendIxs);
|
|
@@ -734,7 +725,6 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
734
725
|
isKtoken,
|
|
735
726
|
quoter,
|
|
736
727
|
swapper,
|
|
737
|
-
useV2Ixs,
|
|
738
728
|
}: WithdrawWithLeverageProps<QuoteResponse>): Promise<WithdrawLeverageIxsResponse<QuoteResponse>> {
|
|
739
729
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
740
730
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
@@ -762,7 +752,6 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
762
752
|
quoteBufferBps,
|
|
763
753
|
isKtoken,
|
|
764
754
|
quoter,
|
|
765
|
-
useV2Ixs,
|
|
766
755
|
});
|
|
767
756
|
|
|
768
757
|
let withdrawSwapper: SwapIxsProvider<QuoteResponse>;
|
|
@@ -813,8 +802,7 @@ export async function getWithdrawWithLeverageIxns<QuoteResponse>({
|
|
|
813
802
|
lookupTables,
|
|
814
803
|
},
|
|
815
804
|
initialInputs.strategy,
|
|
816
|
-
initialInputs.collIsKtoken
|
|
817
|
-
useV2Ixs
|
|
805
|
+
initialInputs.collIsKtoken
|
|
818
806
|
);
|
|
819
807
|
|
|
820
808
|
// Send ixns and lookup tables
|
|
@@ -841,8 +829,7 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
841
829
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
842
830
|
swapQuoteIxs: SwapIxs,
|
|
843
831
|
strategy: StrategyWithAddress | undefined,
|
|
844
|
-
collIsKtoken: boolean
|
|
845
|
-
useV2Ixs: boolean
|
|
832
|
+
collIsKtoken: boolean
|
|
846
833
|
): Promise<TransactionInstruction[]> {
|
|
847
834
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
848
835
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
@@ -954,7 +941,6 @@ export async function buildWithdrawWithLeverageIxns(
|
|
|
954
941
|
owner,
|
|
955
942
|
currentSlot,
|
|
956
943
|
obligation,
|
|
957
|
-
useV2Ixs,
|
|
958
944
|
0,
|
|
959
945
|
false,
|
|
960
946
|
false, // to be checked and created in a setup tx in the UI (won't be the case for withdraw anyway as this would be created in deposit)
|
|
@@ -1000,7 +986,6 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1000
986
|
quoteBufferBps,
|
|
1001
987
|
isKtoken,
|
|
1002
988
|
quoter,
|
|
1003
|
-
useV2Ixs,
|
|
1004
989
|
}: AdjustLeverageSwapInputsProps<QuoteResponse>): Promise<{
|
|
1005
990
|
swapInputs: SwapInputs;
|
|
1006
991
|
initialInputs: AdjustLeverageInitialInputs<QuoteResponse>;
|
|
@@ -1066,8 +1051,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1066
1051
|
swapIxs: [],
|
|
1067
1052
|
lookupTables: [],
|
|
1068
1053
|
},
|
|
1069
|
-
budgetAndPriorityFeeIxs
|
|
1070
|
-
useV2Ixs
|
|
1054
|
+
budgetAndPriorityFeeIxs
|
|
1071
1055
|
);
|
|
1072
1056
|
|
|
1073
1057
|
const uniqueKlendAccounts = uniqueAccounts(klendIxs);
|
|
@@ -1170,8 +1154,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1170
1154
|
swapIxs: [],
|
|
1171
1155
|
lookupTables: [],
|
|
1172
1156
|
},
|
|
1173
|
-
budgetAndPriorityFeeIxs
|
|
1174
|
-
useV2Ixs
|
|
1157
|
+
budgetAndPriorityFeeIxs
|
|
1175
1158
|
);
|
|
1176
1159
|
|
|
1177
1160
|
const uniqueKlendAccounts = uniqueAccounts(klendIxs);
|
|
@@ -1257,7 +1240,6 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1257
1240
|
isKtoken,
|
|
1258
1241
|
quoter,
|
|
1259
1242
|
swapper,
|
|
1260
|
-
useV2Ixs,
|
|
1261
1243
|
}: AdjustLeverageProps<QuoteResponse>): Promise<AdjustLeverageIxsResponse<QuoteResponse>> {
|
|
1262
1244
|
const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
|
|
1263
1245
|
owner,
|
|
@@ -1280,7 +1262,6 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1280
1262
|
priceAinB,
|
|
1281
1263
|
isKtoken,
|
|
1282
1264
|
quoter,
|
|
1283
|
-
useV2Ixs,
|
|
1284
1265
|
});
|
|
1285
1266
|
|
|
1286
1267
|
// leverage increased so we need to deposit and borrow more
|
|
@@ -1328,8 +1309,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1328
1309
|
swapIxs,
|
|
1329
1310
|
lookupTables,
|
|
1330
1311
|
},
|
|
1331
|
-
budgetAndPriorityFeeIxs
|
|
1332
|
-
useV2Ixs
|
|
1312
|
+
budgetAndPriorityFeeIxs
|
|
1333
1313
|
);
|
|
1334
1314
|
return {
|
|
1335
1315
|
ixs,
|
|
@@ -1375,8 +1355,7 @@ export async function getAdjustLeverageIxns<QuoteResponse>({
|
|
|
1375
1355
|
swapIxs,
|
|
1376
1356
|
lookupTables,
|
|
1377
1357
|
},
|
|
1378
|
-
budgetAndPriorityFeeIxs
|
|
1379
|
-
useV2Ixs
|
|
1358
|
+
budgetAndPriorityFeeIxs
|
|
1380
1359
|
);
|
|
1381
1360
|
|
|
1382
1361
|
return {
|
|
@@ -1404,8 +1383,7 @@ async function buildIncreaseLeverageIxns(
|
|
|
1404
1383
|
scopeFeed: string | undefined,
|
|
1405
1384
|
collIsKtoken: boolean,
|
|
1406
1385
|
swapQuoteIxs: SwapIxs,
|
|
1407
|
-
budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined
|
|
1408
|
-
useV2Ixs: boolean
|
|
1386
|
+
budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined
|
|
1409
1387
|
): Promise<TransactionInstruction[]> {
|
|
1410
1388
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1411
1389
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
@@ -1497,7 +1475,6 @@ async function buildIncreaseLeverageIxns(
|
|
|
1497
1475
|
collTokenMint,
|
|
1498
1476
|
owner,
|
|
1499
1477
|
obligation,
|
|
1500
|
-
useV2Ixs,
|
|
1501
1478
|
0,
|
|
1502
1479
|
false,
|
|
1503
1480
|
false,
|
|
@@ -1515,7 +1492,6 @@ async function buildIncreaseLeverageIxns(
|
|
|
1515
1492
|
debtTokenMint,
|
|
1516
1493
|
owner,
|
|
1517
1494
|
obligation,
|
|
1518
|
-
useV2Ixs,
|
|
1519
1495
|
0,
|
|
1520
1496
|
false,
|
|
1521
1497
|
false,
|
|
@@ -1575,8 +1551,7 @@ async function buildDecreaseLeverageIxns(
|
|
|
1575
1551
|
scopeFeed: string | undefined,
|
|
1576
1552
|
collIsKtoken: boolean,
|
|
1577
1553
|
swapQuoteIxs: SwapIxs,
|
|
1578
|
-
budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined
|
|
1579
|
-
useV2Ixs: boolean
|
|
1554
|
+
budgetAndPriorityFeeIxns: TransactionInstruction[] | undefined
|
|
1580
1555
|
): Promise<TransactionInstruction[]> {
|
|
1581
1556
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1582
1557
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
@@ -1676,7 +1651,6 @@ async function buildDecreaseLeverageIxns(
|
|
|
1676
1651
|
debtTokenMint,
|
|
1677
1652
|
owner,
|
|
1678
1653
|
obligation,
|
|
1679
|
-
useV2Ixs,
|
|
1680
1654
|
currentSlot,
|
|
1681
1655
|
undefined,
|
|
1682
1656
|
0,
|
|
@@ -1695,7 +1669,6 @@ async function buildDecreaseLeverageIxns(
|
|
|
1695
1669
|
collTokenMint,
|
|
1696
1670
|
owner,
|
|
1697
1671
|
obligation,
|
|
1698
|
-
useV2Ixs,
|
|
1699
1672
|
0,
|
|
1700
1673
|
false,
|
|
1701
1674
|
false,
|
package/src/leverage/types.ts
CHANGED
|
@@ -49,7 +49,7 @@ export type KaminoDepositSwapOverride = (
|
|
|
49
49
|
amountDebtAtaBalance: Decimal
|
|
50
50
|
) => Promise<InstructionsWithLookupTables>;
|
|
51
51
|
|
|
52
|
-
export type
|
|
52
|
+
export type DepsoitLeverageIxsResponse<QuoteResponse> = {
|
|
53
53
|
ixs: TransactionInstruction[];
|
|
54
54
|
lookupTables: AddressLookupTableAccount[];
|
|
55
55
|
swapInputs: SwapInputs;
|
|
@@ -90,7 +90,6 @@ export interface DepositWithLeverageSwapInputsProps<QuoteResponse> {
|
|
|
90
90
|
// currently only used to disable requesting elevation group when this value is 0
|
|
91
91
|
// to be implemented properly in the future
|
|
92
92
|
elevationGroupOverride?: number;
|
|
93
|
-
useV2Ixs: boolean;
|
|
94
93
|
}
|
|
95
94
|
|
|
96
95
|
export interface DepositWithLeverageProps<QuoteResponse> extends DepositWithLeverageSwapInputsProps<QuoteResponse> {
|
|
@@ -147,7 +146,6 @@ export interface WithdrawWithLeverageSwapInputsProps<QuoteResponse> {
|
|
|
147
146
|
quoteBufferBps: Decimal;
|
|
148
147
|
isKtoken: IsKtokenProvider;
|
|
149
148
|
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
150
|
-
useV2Ixs: boolean;
|
|
151
149
|
}
|
|
152
150
|
|
|
153
151
|
export interface WithdrawWithLeverageProps<QuoteResponse> extends WithdrawWithLeverageSwapInputsProps<QuoteResponse> {
|
|
@@ -201,7 +199,6 @@ export interface AdjustLeverageSwapInputsProps<QuoteResponse> {
|
|
|
201
199
|
priceAinB: PriceAinBProvider;
|
|
202
200
|
isKtoken: IsKtokenProvider;
|
|
203
201
|
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
204
|
-
useV2Ixs: boolean;
|
|
205
202
|
}
|
|
206
203
|
|
|
207
204
|
export interface AdjustLeverageProps<QuoteResponse> extends AdjustLeverageSwapInputsProps<QuoteResponse> {
|
package/src/utils/lookupTable.ts
CHANGED
|
@@ -53,3 +53,65 @@ export const extendLookupTableIxs = (
|
|
|
53
53
|
|
|
54
54
|
return extendLookupIxs;
|
|
55
55
|
};
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* This method retuns an instruction that creates a lookup table, alongside the pubkey of the lookup table
|
|
59
|
+
* @param payer - the owner of the lookup table
|
|
60
|
+
* @param slot - the current slot
|
|
61
|
+
* @returns - the instruction to create the lookup table and its address
|
|
62
|
+
*/
|
|
63
|
+
export function initLookupTableIx(payer: PublicKey, slot: number): [TransactionInstruction, PublicKey] {
|
|
64
|
+
const [ixn, address] = AddressLookupTableProgram.createLookupTable({
|
|
65
|
+
authority: payer,
|
|
66
|
+
payer,
|
|
67
|
+
recentSlot: slot,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return [ixn, address];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* This method retuns an instruction that deactivates a lookup table, which is needed to close it
|
|
75
|
+
* @param payer - the owner of the lookup table
|
|
76
|
+
* @param lookupTable - the lookup table to deactivate
|
|
77
|
+
* @returns - the instruction to deactivate the lookup table
|
|
78
|
+
*/
|
|
79
|
+
export function deactivateLookupTableIx(payer: PublicKey, lookupTable: PublicKey): TransactionInstruction {
|
|
80
|
+
const ixn = AddressLookupTableProgram.deactivateLookupTable({
|
|
81
|
+
authority: payer,
|
|
82
|
+
lookupTable: lookupTable,
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return ixn;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* This method returns an instruction that closes a lookup table. That lookup table needs to be disabled at least 500 blocks before closing it.
|
|
90
|
+
* @param payer - the owner of the lookup table
|
|
91
|
+
* @param lookupTable - the lookup table to close
|
|
92
|
+
* @returns - the instruction to close the lookup table
|
|
93
|
+
*/
|
|
94
|
+
/// this require the LUT to be deactivated at least 500 blocks before
|
|
95
|
+
export function closeLookupTableIx(payer: PublicKey, lookupTable: PublicKey): TransactionInstruction {
|
|
96
|
+
const ixn = AddressLookupTableProgram.closeLookupTable({
|
|
97
|
+
authority: payer,
|
|
98
|
+
recipient: payer,
|
|
99
|
+
lookupTable: lookupTable,
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return ixn;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Returns the accounts in a lookup table
|
|
107
|
+
* @param lookupTable - lookup table to get the accounts from
|
|
108
|
+
* @returns - an array of accounts in the lookup table
|
|
109
|
+
*/
|
|
110
|
+
export async function getAccountsInLUT(connection: Connection, lookupTable: PublicKey): Promise<PublicKey[]> {
|
|
111
|
+
const lutState = await connection.getAddressLookupTable(lookupTable);
|
|
112
|
+
if (!lutState || !lutState.value) {
|
|
113
|
+
throw new Error(`Lookup table ${lookupTable} not found`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return lutState.value.state.addresses;
|
|
117
|
+
}
|
package/src/utils/seeds.ts
CHANGED
|
@@ -38,11 +38,10 @@ export const BASE_SEED_REFERRER_STATE = 'ref_state';
|
|
|
38
38
|
* Short url seed
|
|
39
39
|
*/
|
|
40
40
|
export const BASE_SEED_SHORT_URL = 'short_url';
|
|
41
|
-
|
|
42
41
|
/**
|
|
43
|
-
*
|
|
42
|
+
* Farm user state seed
|
|
44
43
|
*/
|
|
45
|
-
export const
|
|
44
|
+
export const BASE_SEED_USER_STATE = 'user';
|
|
46
45
|
|
|
47
46
|
/**
|
|
48
47
|
* Encapsulates all the PDAs for a given reserve
|
|
@@ -188,9 +187,15 @@ export function shortUrlPda(shortUrl: string, programId: PublicKey = PROGRAM_ID)
|
|
|
188
187
|
return PublicKey.findProgramAddressSync([Buffer.from(BASE_SEED_SHORT_URL), Buffer.from(shortUrl)], programId);
|
|
189
188
|
}
|
|
190
189
|
|
|
191
|
-
|
|
190
|
+
/**
|
|
191
|
+
* Returns the PDA for the obligation farm state
|
|
192
|
+
* @param farm
|
|
193
|
+
* @param obligation
|
|
194
|
+
* @returns pda
|
|
195
|
+
*/
|
|
196
|
+
export function obligationFarmStatePda(farm: PublicKey, obligation: PublicKey) {
|
|
192
197
|
return PublicKey.findProgramAddressSync(
|
|
193
|
-
[Buffer.from(
|
|
194
|
-
|
|
195
|
-
);
|
|
198
|
+
[Buffer.from(BASE_SEED_USER_STATE), farm.toBytes(), obligation.toBytes()],
|
|
199
|
+
farmsId
|
|
200
|
+
)[0];
|
|
196
201
|
}
|