@kamino-finance/klend-sdk 5.0.2 → 5.0.4
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/obligation.d.ts.map +1 -1
- package/dist/classes/obligation.js +1 -2
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/reserve.d.ts +0 -1
- package/dist/classes/reserve.d.ts.map +1 -1
- package/dist/classes/reserve.js +3 -7
- package/dist/classes/reserve.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +6 -0
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +28 -1
- package/dist/leverage/calcs.d.ts.map +1 -1
- package/dist/leverage/calcs.js +204 -8
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/index.d.ts +1 -0
- package/dist/leverage/index.d.ts.map +1 -1
- package/dist/leverage/index.js +1 -0
- package/dist/leverage/index.js.map +1 -1
- package/dist/leverage/operations.d.ts +14 -241
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +508 -776
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +173 -0
- package/dist/leverage/types.d.ts.map +1 -0
- package/dist/leverage/types.js +3 -0
- package/dist/leverage/types.js.map +1 -0
- package/dist/leverage/utils.d.ts +5 -5
- package/dist/leverage/utils.d.ts.map +1 -1
- package/dist/leverage/utils.js +68 -33
- package/dist/leverage/utils.js.map +1 -1
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +2 -1
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/fuzz.d.ts +3 -0
- package/dist/utils/fuzz.d.ts.map +1 -0
- package/dist/utils/fuzz.js +11 -0
- package/dist/utils/fuzz.js.map +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/obligation.ts +1 -2
- package/src/classes/reserve.ts +3 -16
- package/src/lending_operations/repay_with_collateral_operations.ts +2 -0
- package/src/leverage/calcs.ts +315 -8
- package/src/leverage/index.ts +1 -0
- package/src/leverage/operations.ts +1079 -1331
- package/src/leverage/types.ts +211 -0
- package/src/leverage/utils.ts +103 -64
- package/src/utils/constants.ts +2 -0
- package/src/utils/fuzz.ts +5 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { AddressLookupTableAccount, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
import Decimal from 'decimal.js';
|
|
3
|
+
import { KaminoMarket, KaminoObligation } from '../classes';
|
|
4
|
+
import { InstructionsWithLookupTables, Kamino, StrategyWithAddress } from '@kamino-finance/kliquidity-sdk';
|
|
5
|
+
import { ObligationType, ObligationTypeTag } from '../utils';
|
|
6
|
+
|
|
7
|
+
export type SwapQuoteProvider<QuoteResponse> = (
|
|
8
|
+
inputs: SwapInputs,
|
|
9
|
+
klendAccounts: Array<PublicKey>
|
|
10
|
+
) => Promise<SwapQuote<QuoteResponse>>;
|
|
11
|
+
|
|
12
|
+
export type SwapQuoteIxsProvider<QuoteResponse> = (
|
|
13
|
+
inputs: SwapInputs,
|
|
14
|
+
klendAccounts: Array<PublicKey>,
|
|
15
|
+
quote: SwapQuote<QuoteResponse>
|
|
16
|
+
) => Promise<SwapQuoteIxs>;
|
|
17
|
+
|
|
18
|
+
export type SwapQuote<QuoteResponse> = {
|
|
19
|
+
priceAInB: Decimal;
|
|
20
|
+
quoteResponse?: QuoteResponse;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type SwapQuoteIxs = {
|
|
24
|
+
preActionIxs: TransactionInstruction[];
|
|
25
|
+
swapIxs: TransactionInstruction[];
|
|
26
|
+
lookupTables: AddressLookupTableAccount[];
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type PriceAinBProvider = (mintA: PublicKey, mintB: PublicKey) => Promise<Decimal>;
|
|
30
|
+
|
|
31
|
+
export type IsKtokenProvider = (token: PublicKey | string) => Promise<boolean>;
|
|
32
|
+
|
|
33
|
+
export type SwapInputs = {
|
|
34
|
+
inputAmountLamports: Decimal;
|
|
35
|
+
inputMint: PublicKey;
|
|
36
|
+
outputMint: PublicKey;
|
|
37
|
+
amountDebtAtaBalance: Decimal | undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type KaminoDepositSwapOverride = (
|
|
41
|
+
kaminoMarket: KaminoMarket,
|
|
42
|
+
kamino: Kamino,
|
|
43
|
+
depositor: PublicKey,
|
|
44
|
+
amountInMint: PublicKey,
|
|
45
|
+
amountOutMint: PublicKey,
|
|
46
|
+
amountIn: Decimal,
|
|
47
|
+
slippageFactor: Decimal,
|
|
48
|
+
amountDebtAtaBalance: Decimal
|
|
49
|
+
) => Promise<InstructionsWithLookupTables>;
|
|
50
|
+
|
|
51
|
+
export type DepsoitLeverageIxsResponse<QuoteResponse> = {
|
|
52
|
+
ixs: TransactionInstruction[];
|
|
53
|
+
lookupTables: AddressLookupTableAccount[];
|
|
54
|
+
swapInputs: SwapInputs;
|
|
55
|
+
initialInputs: DepositLeverageInitialInputs<QuoteResponse>;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export type DepositLeverageInitialInputs<QuoteResponse> = {
|
|
59
|
+
calcs: DepositLeverageCalcsResult;
|
|
60
|
+
swapQuote: SwapQuote<QuoteResponse>;
|
|
61
|
+
currentSlot: number;
|
|
62
|
+
klendAccounts: Array<PublicKey>;
|
|
63
|
+
collIsKtoken: boolean;
|
|
64
|
+
obligation: KaminoObligation | ObligationType | undefined;
|
|
65
|
+
strategy: StrategyWithAddress | undefined;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export interface DepositWithLeverageSwapInputsProps<QuoteResponse> {
|
|
69
|
+
owner: PublicKey;
|
|
70
|
+
kaminoMarket: KaminoMarket;
|
|
71
|
+
debtTokenMint: PublicKey;
|
|
72
|
+
collTokenMint: PublicKey;
|
|
73
|
+
obligation: KaminoObligation | null;
|
|
74
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
75
|
+
referrer: PublicKey;
|
|
76
|
+
currentSlot: number;
|
|
77
|
+
depositAmount: Decimal;
|
|
78
|
+
priceDebtToColl: Decimal;
|
|
79
|
+
slippagePct: Decimal;
|
|
80
|
+
targetLeverage: Decimal;
|
|
81
|
+
selectedTokenMint: PublicKey;
|
|
82
|
+
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
83
|
+
kamino: Kamino | undefined;
|
|
84
|
+
scopeFeed: string | undefined;
|
|
85
|
+
quoteBufferBps: Decimal;
|
|
86
|
+
priceAinB: PriceAinBProvider;
|
|
87
|
+
isKtoken: IsKtokenProvider;
|
|
88
|
+
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export interface DepositWithLeverageProps<QuoteResponse> extends DepositWithLeverageSwapInputsProps<QuoteResponse> {
|
|
92
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type DepositLeverageCalcsResult = {
|
|
96
|
+
flashBorrowInCollToken: Decimal;
|
|
97
|
+
initDepositInSol: Decimal;
|
|
98
|
+
debtTokenToBorrow: Decimal;
|
|
99
|
+
collTokenToDeposit: Decimal;
|
|
100
|
+
swapDebtTokenIn: Decimal;
|
|
101
|
+
swapCollTokenExpectedOut: Decimal;
|
|
102
|
+
flashBorrowInDebtTokenKtokenOnly: Decimal;
|
|
103
|
+
singleSidedDepositKtokenOnly: Decimal;
|
|
104
|
+
requiredCollateralKtokenOnly: Decimal;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export type WithdrawLeverageIxsResponse<QuoteResponse> = {
|
|
108
|
+
ixs: TransactionInstruction[];
|
|
109
|
+
lookupTables: AddressLookupTableAccount[];
|
|
110
|
+
swapInputs: SwapInputs;
|
|
111
|
+
initialInputs: WithdrawLeverageInitialInputs<QuoteResponse>;
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
export type WithdrawLeverageInitialInputs<QuoteResponse> = {
|
|
115
|
+
calcs: WithdrawLeverageCalcsResult;
|
|
116
|
+
swapQuote: SwapQuote<QuoteResponse>;
|
|
117
|
+
currentSlot: number;
|
|
118
|
+
klendAccounts: Array<PublicKey>;
|
|
119
|
+
collIsKtoken: boolean;
|
|
120
|
+
obligation: KaminoObligation | ObligationType | undefined;
|
|
121
|
+
strategy: StrategyWithAddress | undefined;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export interface WithdrawWithLeverageSwapInputsProps<QuoteResponse> {
|
|
125
|
+
owner: PublicKey;
|
|
126
|
+
kaminoMarket: KaminoMarket;
|
|
127
|
+
debtTokenMint: PublicKey;
|
|
128
|
+
collTokenMint: PublicKey;
|
|
129
|
+
obligation: KaminoObligation;
|
|
130
|
+
deposited: Decimal;
|
|
131
|
+
borrowed: Decimal;
|
|
132
|
+
referrer: PublicKey;
|
|
133
|
+
currentSlot: number;
|
|
134
|
+
withdrawAmount: Decimal;
|
|
135
|
+
priceCollToDebt: Decimal;
|
|
136
|
+
slippagePct: Decimal;
|
|
137
|
+
isClosingPosition: boolean;
|
|
138
|
+
selectedTokenMint: PublicKey;
|
|
139
|
+
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
140
|
+
kamino: Kamino | undefined;
|
|
141
|
+
scopeFeed: string | undefined;
|
|
142
|
+
quoteBufferBps: Decimal;
|
|
143
|
+
isKtoken: IsKtokenProvider;
|
|
144
|
+
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface WithdrawWithLeverageProps<QuoteResponse> extends WithdrawWithLeverageSwapInputsProps<QuoteResponse> {
|
|
148
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export type WithdrawLeverageCalcsResult = {
|
|
152
|
+
withdrawAmount: Decimal;
|
|
153
|
+
repayAmount: Decimal;
|
|
154
|
+
collTokenSwapIn: Decimal;
|
|
155
|
+
depositTokenWithdrawAmount: Decimal;
|
|
156
|
+
debtTokenExpectedSwapOut: Decimal;
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
export type AdjustLeverageIxsResponse<QuoteResponse> = {
|
|
160
|
+
ixs: TransactionInstruction[];
|
|
161
|
+
lookupTables: AddressLookupTableAccount[];
|
|
162
|
+
swapInputs: SwapInputs;
|
|
163
|
+
initialInputs: AdjustLeverageInitialInputs<QuoteResponse>;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export type AdjustLeverageInitialInputs<QuoteResponse> = {
|
|
167
|
+
calcs: AdjustLeverageCalcsResult;
|
|
168
|
+
swapQuote: SwapQuote<QuoteResponse>;
|
|
169
|
+
currentSlot: number;
|
|
170
|
+
klendAccounts: Array<PublicKey>;
|
|
171
|
+
isDeposit: boolean;
|
|
172
|
+
collIsKtoken: boolean;
|
|
173
|
+
obligation: KaminoObligation | ObligationType | undefined;
|
|
174
|
+
strategy: StrategyWithAddress | undefined;
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
export interface AdjustLeverageSwapInputsProps<QuoteResponse> {
|
|
178
|
+
owner: PublicKey;
|
|
179
|
+
kaminoMarket: KaminoMarket;
|
|
180
|
+
debtTokenMint: PublicKey;
|
|
181
|
+
collTokenMint: PublicKey;
|
|
182
|
+
obligation: KaminoObligation;
|
|
183
|
+
depositedLamports: Decimal;
|
|
184
|
+
borrowedLamports: Decimal;
|
|
185
|
+
referrer: PublicKey;
|
|
186
|
+
currentSlot: number;
|
|
187
|
+
targetLeverage: Decimal;
|
|
188
|
+
priceCollToDebt: Decimal;
|
|
189
|
+
priceDebtToColl: Decimal;
|
|
190
|
+
slippagePct: Decimal;
|
|
191
|
+
budgetAndPriorityFeeIxs?: TransactionInstruction[];
|
|
192
|
+
kamino: Kamino | undefined;
|
|
193
|
+
scopeFeed: string | undefined;
|
|
194
|
+
quoteBufferBps: Decimal;
|
|
195
|
+
priceAinB: PriceAinBProvider;
|
|
196
|
+
isKtoken: IsKtokenProvider;
|
|
197
|
+
quoter: SwapQuoteProvider<QuoteResponse>;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export interface AdjustLeverageProps<QuoteResponse> extends AdjustLeverageSwapInputsProps<QuoteResponse> {
|
|
201
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export type AdjustLeverageCalcsResult = {
|
|
205
|
+
adjustDepositPosition: Decimal;
|
|
206
|
+
adjustBorrowPosition: Decimal;
|
|
207
|
+
amountToFlashBorrowDebt: Decimal;
|
|
208
|
+
borrowAmount: Decimal;
|
|
209
|
+
expectedDebtTokenAtaBalance: Decimal;
|
|
210
|
+
withdrawAmountWithSlippageAndFlashLoanFee: Decimal;
|
|
211
|
+
};
|
package/src/leverage/utils.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Kamino, StrategyWithAddress } from '@kamino-finance/kliquidity-sdk';
|
|
2
2
|
import { KaminoMarket, KaminoReserve, lamportsToNumberDecimal } from '../classes';
|
|
3
3
|
import { Connection, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
4
|
-
import { PriceAinBProvider, SwapIxnsProvider } from './operations';
|
|
5
4
|
import Decimal from 'decimal.js';
|
|
6
|
-
import { getTokenAccountBalanceDecimal } from '../utils';
|
|
5
|
+
import { getLookupTableAccounts, getTokenAccountBalanceDecimal } from '../utils';
|
|
7
6
|
import { numberToLamportsDecimal } from '../classes/utils';
|
|
8
7
|
import BN from 'bn.js';
|
|
8
|
+
import { PriceAinBProvider, SwapInputs, SwapQuote, SwapQuoteIxs, SwapQuoteIxsProvider } from './types';
|
|
9
9
|
|
|
10
10
|
export interface KaminoSwapperIxBuilder {
|
|
11
11
|
(
|
|
@@ -25,48 +25,55 @@ export interface DepositAmountsForSwap {
|
|
|
25
25
|
tokenBToSwapAmount: Decimal;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
export
|
|
29
|
-
connection: Connection,
|
|
28
|
+
export async function getTokenToKtokenSwapper<QuoteResponse>(
|
|
30
29
|
kaminoMarket: KaminoMarket,
|
|
31
30
|
kamino: Kamino,
|
|
32
31
|
depositor: PublicKey,
|
|
33
|
-
|
|
32
|
+
slippagePct: Decimal,
|
|
33
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>,
|
|
34
34
|
priceAinB: PriceAinBProvider,
|
|
35
35
|
includeAtaIxns: boolean = true
|
|
36
|
-
): Promise<
|
|
36
|
+
): Promise<SwapQuoteIxsProvider<QuoteResponse>> {
|
|
37
37
|
return async (
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
console.debug('Depositing token', amountInMint.toString(), ' for ', amountOutMint.toString(), 'ktoken');
|
|
48
|
-
if (amountDebtAtaBalance === undefined) {
|
|
38
|
+
inputs: SwapInputs,
|
|
39
|
+
klendAccounts: Array<PublicKey>,
|
|
40
|
+
quote: SwapQuote<QuoteResponse>
|
|
41
|
+
): Promise<SwapQuoteIxs> => {
|
|
42
|
+
const slippageBps = new Decimal(slippagePct).mul('100');
|
|
43
|
+
const mintInDecimals = kaminoMarket.getReserveByMint(inputs.inputMint)!.state.liquidity.mintDecimals.toNumber();
|
|
44
|
+
const amountIn = lamportsToNumberDecimal(inputs.inputAmountLamports, mintInDecimals);
|
|
45
|
+
console.debug('Depositing token', inputs.inputMint.toString(), ' for ', inputs.outputMint.toString(), 'ktoken');
|
|
46
|
+
if (inputs.amountDebtAtaBalance === undefined) {
|
|
49
47
|
throw Error('Amount in debt ATA balance is undefined for leverage ktoken deposit');
|
|
50
48
|
}
|
|
51
49
|
|
|
52
50
|
const ixWithLookup = (await getKtokenDepositIxs(
|
|
53
|
-
|
|
51
|
+
kaminoMarket.getConnection(),
|
|
54
52
|
kamino,
|
|
55
53
|
depositor,
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
inputs.inputMint,
|
|
55
|
+
inputs.outputMint,
|
|
58
56
|
amountIn,
|
|
59
57
|
slippageBps,
|
|
60
|
-
amountDebtAtaBalance,
|
|
58
|
+
inputs.amountDebtAtaBalance,
|
|
61
59
|
swapper,
|
|
62
60
|
priceAinB,
|
|
63
|
-
includeAtaIxns
|
|
61
|
+
includeAtaIxns,
|
|
62
|
+
klendAccounts,
|
|
63
|
+
quote
|
|
64
64
|
))!;
|
|
65
|
-
|
|
65
|
+
|
|
66
|
+
const luts = await getLookupTableAccounts(kaminoMarket.getConnection(), ixWithLookup.lookupTablesAddresses);
|
|
67
|
+
|
|
68
|
+
return {
|
|
69
|
+
preActionIxs: [],
|
|
70
|
+
swapIxs: ixWithLookup.instructions,
|
|
71
|
+
lookupTables: luts,
|
|
72
|
+
};
|
|
66
73
|
};
|
|
67
|
-
}
|
|
74
|
+
}
|
|
68
75
|
|
|
69
|
-
export async function getKtokenDepositIxs(
|
|
76
|
+
export async function getKtokenDepositIxs<QuoteResponse>(
|
|
70
77
|
connection: Connection,
|
|
71
78
|
kamino: Kamino,
|
|
72
79
|
depositor: PublicKey,
|
|
@@ -75,9 +82,11 @@ export async function getKtokenDepositIxs(
|
|
|
75
82
|
amountToDeposit: Decimal,
|
|
76
83
|
slippageBps: Decimal,
|
|
77
84
|
amountExpectedDepositAtaBalance: Decimal,
|
|
78
|
-
swapper:
|
|
85
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>,
|
|
79
86
|
priceAinB: PriceAinBProvider,
|
|
80
|
-
includeAtaIxns: boolean = true
|
|
87
|
+
includeAtaIxns: boolean = true,
|
|
88
|
+
klendAccounts: Array<PublicKey>,
|
|
89
|
+
quote: SwapQuote<QuoteResponse>
|
|
81
90
|
) {
|
|
82
91
|
const kaminoStrategy = await kamino.getStrategyByKTokenMint(ktokenMint);
|
|
83
92
|
const tokenAMint = kaminoStrategy?.strategy.tokenAMint!;
|
|
@@ -94,7 +103,7 @@ export async function getKtokenDepositIxs(
|
|
|
94
103
|
depositor,
|
|
95
104
|
slippageBps,
|
|
96
105
|
undefined,
|
|
97
|
-
swapProviderToKaminoSwapProvider(swapper),
|
|
106
|
+
swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
|
|
98
107
|
tokensBalances,
|
|
99
108
|
priceAinBDecimal,
|
|
100
109
|
includeAtaIxns
|
|
@@ -108,7 +117,7 @@ export async function getKtokenDepositIxs(
|
|
|
108
117
|
depositor,
|
|
109
118
|
slippageBps,
|
|
110
119
|
undefined,
|
|
111
|
-
swapProviderToKaminoSwapProvider(swapper),
|
|
120
|
+
swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
|
|
112
121
|
tokensBalances,
|
|
113
122
|
priceAinBDecimal,
|
|
114
123
|
includeAtaIxns
|
|
@@ -118,18 +127,18 @@ export async function getKtokenDepositIxs(
|
|
|
118
127
|
}
|
|
119
128
|
}
|
|
120
129
|
|
|
121
|
-
export
|
|
130
|
+
export async function getKtokenToTokenSwapper<QuoteResponse>(
|
|
122
131
|
kaminoMarket: KaminoMarket,
|
|
123
132
|
kamino: Kamino,
|
|
124
133
|
depositor: PublicKey,
|
|
125
|
-
swapper:
|
|
126
|
-
): Promise<
|
|
127
|
-
return async (
|
|
128
|
-
const amountInDecimals = kaminoMarket.getReserveByMint(
|
|
129
|
-
const amountToWithdraw = lamportsToNumberDecimal(
|
|
130
|
-
const kaminoStrategy = await kamino.getStrategyByKTokenMint(
|
|
134
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>
|
|
135
|
+
): Promise<SwapQuoteIxsProvider<QuoteResponse>> {
|
|
136
|
+
return async (inputs: SwapInputs, klendAccounts: Array<PublicKey>, quote: SwapQuote<QuoteResponse>) => {
|
|
137
|
+
const amountInDecimals = kaminoMarket.getReserveByMint(inputs.inputMint)!.state.liquidity.mintDecimals.toNumber();
|
|
138
|
+
const amountToWithdraw = lamportsToNumberDecimal(inputs.inputAmountLamports, amountInDecimals);
|
|
139
|
+
const kaminoStrategy = await kamino.getStrategyByKTokenMint(inputs.inputMint);
|
|
131
140
|
|
|
132
|
-
console.log('Withdrawing ktoken',
|
|
141
|
+
console.log('Withdrawing ktoken', inputs.inputMint.toString(), ' for ', inputs.outputMint.toString(), 'token');
|
|
133
142
|
|
|
134
143
|
const ixWithdraw = (await getKtokenWithdrawIxs(kamino, depositor, kaminoStrategy!, amountToWithdraw))!;
|
|
135
144
|
|
|
@@ -139,29 +148,45 @@ export const getKtokenToTokenSwapper = async (
|
|
|
139
148
|
amountToWithdraw
|
|
140
149
|
);
|
|
141
150
|
|
|
142
|
-
if (
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
151
|
+
if (inputs.outputMint.equals(kaminoStrategy!.strategy.tokenAMint!)) {
|
|
152
|
+
const { swapIxs, lookupTables } = await swapper(
|
|
153
|
+
{
|
|
154
|
+
inputAmountLamports: estimatedBOut,
|
|
155
|
+
inputMint: kaminoStrategy!.strategy.tokenBMint!,
|
|
156
|
+
outputMint: kaminoStrategy!.strategy.tokenAMint!,
|
|
157
|
+
amountDebtAtaBalance: new Decimal(0),
|
|
158
|
+
},
|
|
159
|
+
klendAccounts,
|
|
160
|
+
quote
|
|
148
161
|
);
|
|
149
162
|
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
163
|
+
return {
|
|
164
|
+
preActionIxs: [],
|
|
165
|
+
swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swapIxs],
|
|
166
|
+
lookupTables,
|
|
167
|
+
};
|
|
168
|
+
} else if (inputs.outputMint.equals(kaminoStrategy!.strategy.tokenBMint!)) {
|
|
169
|
+
const { swapIxs, lookupTables } = await swapper(
|
|
170
|
+
{
|
|
171
|
+
inputAmountLamports: estimatedAOut,
|
|
172
|
+
inputMint: kaminoStrategy!.strategy.tokenAMint!,
|
|
173
|
+
outputMint: kaminoStrategy!.strategy.tokenBMint!,
|
|
174
|
+
amountDebtAtaBalance: new Decimal(0),
|
|
175
|
+
},
|
|
176
|
+
klendAccounts,
|
|
177
|
+
quote
|
|
157
178
|
);
|
|
158
179
|
|
|
159
|
-
return
|
|
180
|
+
return {
|
|
181
|
+
preActionIxs: [],
|
|
182
|
+
swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swapIxs],
|
|
183
|
+
lookupTables,
|
|
184
|
+
};
|
|
160
185
|
} else {
|
|
161
186
|
throw Error('Deposit token is neither A nor B in the strategy');
|
|
162
187
|
}
|
|
163
188
|
};
|
|
164
|
-
}
|
|
189
|
+
}
|
|
165
190
|
|
|
166
191
|
export async function getKtokenWithdrawIxs(
|
|
167
192
|
kamino: Kamino,
|
|
@@ -225,29 +250,43 @@ export async function getKtokenWithdrawEstimatesAndPrice(
|
|
|
225
250
|
return [estimatedAOutDecimal, estimatedBOutDecimal];
|
|
226
251
|
}
|
|
227
252
|
|
|
228
|
-
export function swapProviderToKaminoSwapProvider(
|
|
253
|
+
export function swapProviderToKaminoSwapProvider<QuoteResponse>(
|
|
254
|
+
swapper: SwapQuoteIxsProvider<QuoteResponse>,
|
|
255
|
+
klendAccounts: Array<PublicKey>,
|
|
256
|
+
swapQuote: SwapQuote<QuoteResponse>
|
|
257
|
+
): KaminoSwapperIxBuilder {
|
|
229
258
|
return async (
|
|
230
259
|
input: DepositAmountsForSwap,
|
|
231
260
|
tokenAMint: PublicKey,
|
|
232
261
|
tokenBMint: PublicKey,
|
|
233
262
|
_owner: PublicKey,
|
|
234
|
-
|
|
263
|
+
_slippage: Decimal,
|
|
235
264
|
_allKeys: PublicKey[]
|
|
236
265
|
): Promise<[TransactionInstruction[], PublicKey[]]> => {
|
|
237
266
|
if (input.tokenBToSwapAmount.lt(0)) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
267
|
+
const swapperIxs = await swapper(
|
|
268
|
+
{
|
|
269
|
+
inputAmountLamports: input.tokenBToSwapAmount.abs(),
|
|
270
|
+
inputMint: tokenBMint,
|
|
271
|
+
outputMint: tokenAMint,
|
|
272
|
+
amountDebtAtaBalance: undefined,
|
|
273
|
+
},
|
|
274
|
+
klendAccounts,
|
|
275
|
+
swapQuote
|
|
243
276
|
);
|
|
277
|
+
return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.key)];
|
|
244
278
|
} else if (input.tokenAToSwapAmount.lt(0)) {
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
279
|
+
const swapperIxs = await swapper(
|
|
280
|
+
{
|
|
281
|
+
inputAmountLamports: input.tokenAToSwapAmount.abs(),
|
|
282
|
+
inputMint: tokenAMint,
|
|
283
|
+
outputMint: tokenBMint,
|
|
284
|
+
amountDebtAtaBalance: undefined,
|
|
285
|
+
},
|
|
286
|
+
klendAccounts,
|
|
287
|
+
swapQuote
|
|
250
288
|
);
|
|
289
|
+
return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.key)];
|
|
251
290
|
} else {
|
|
252
291
|
throw Error('Nothing to swap');
|
|
253
292
|
}
|
package/src/utils/constants.ts
CHANGED
package/src/utils/index.ts
CHANGED