@kamino-finance/klend-sdk 5.0.3 → 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.
Files changed (111) hide show
  1. package/dist/classes/fraction.d.ts.map +1 -1
  2. package/dist/classes/fraction.js.map +1 -1
  3. package/dist/classes/manager.d.ts.map +1 -1
  4. package/dist/classes/manager.js +1 -14
  5. package/dist/classes/manager.js.map +1 -1
  6. package/dist/classes/market.d.ts +0 -4
  7. package/dist/classes/market.d.ts.map +1 -1
  8. package/dist/classes/market.js +0 -15
  9. package/dist/classes/market.js.map +1 -1
  10. package/dist/classes/obligation.d.ts.map +1 -1
  11. package/dist/classes/obligation.js +1 -2
  12. package/dist/classes/obligation.js.map +1 -1
  13. package/dist/classes/reserve.d.ts +0 -1
  14. package/dist/classes/reserve.d.ts.map +1 -1
  15. package/dist/classes/reserve.js +27 -22
  16. package/dist/classes/reserve.js.map +1 -1
  17. package/dist/classes/utils.d.ts +0 -3
  18. package/dist/classes/utils.d.ts.map +1 -1
  19. package/dist/classes/utils.js +0 -30
  20. package/dist/classes/utils.js.map +1 -1
  21. package/dist/client_kamino_manager.js +1 -1
  22. package/dist/client_kamino_manager.js.map +1 -1
  23. package/dist/idl.json +2 -27
  24. package/dist/idl_codegen/accounts/LendingMarket.d.ts +0 -6
  25. package/dist/idl_codegen/accounts/LendingMarket.d.ts.map +1 -1
  26. package/dist/idl_codegen/accounts/LendingMarket.js +1 -8
  27. package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -1
  28. package/dist/idl_codegen/accounts/Obligation.d.ts +12 -3
  29. package/dist/idl_codegen/accounts/Obligation.d.ts.map +1 -1
  30. package/dist/idl_codegen/accounts/Obligation.js +4 -1
  31. package/dist/idl_codegen/accounts/Obligation.js.map +1 -1
  32. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts +0 -20
  33. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts.map +1 -1
  34. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js +1 -33
  35. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js.map +1 -1
  36. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +0 -13
  37. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts.map +1 -1
  38. package/dist/idl_codegen/types/UpdateLendingMarketMode.js +1 -25
  39. package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -1
  40. package/dist/idl_codegen/types/index.d.ts +4 -4
  41. package/dist/idl_codegen/types/index.d.ts.map +1 -1
  42. package/dist/idl_codegen/types/index.js.map +1 -1
  43. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  44. package/dist/lending_operations/repay_with_collateral_operations.js +6 -0
  45. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  46. package/dist/leverage/calcs.d.ts +28 -1
  47. package/dist/leverage/calcs.d.ts.map +1 -1
  48. package/dist/leverage/calcs.js +204 -8
  49. package/dist/leverage/calcs.js.map +1 -1
  50. package/dist/leverage/index.d.ts +1 -0
  51. package/dist/leverage/index.d.ts.map +1 -1
  52. package/dist/leverage/index.js +1 -0
  53. package/dist/leverage/index.js.map +1 -1
  54. package/dist/leverage/operations.d.ts +14 -241
  55. package/dist/leverage/operations.d.ts.map +1 -1
  56. package/dist/leverage/operations.js +508 -776
  57. package/dist/leverage/operations.js.map +1 -1
  58. package/dist/leverage/types.d.ts +173 -0
  59. package/dist/leverage/types.d.ts.map +1 -0
  60. package/dist/leverage/types.js +3 -0
  61. package/dist/leverage/types.js.map +1 -0
  62. package/dist/leverage/utils.d.ts +5 -5
  63. package/dist/leverage/utils.d.ts.map +1 -1
  64. package/dist/leverage/utils.js +68 -33
  65. package/dist/leverage/utils.js.map +1 -1
  66. package/dist/utils/ObligationType.d.ts.map +1 -1
  67. package/dist/utils/ObligationType.js.map +1 -1
  68. package/dist/utils/constants.d.ts +1 -0
  69. package/dist/utils/constants.d.ts.map +1 -1
  70. package/dist/utils/constants.js +2 -1
  71. package/dist/utils/constants.js.map +1 -1
  72. package/dist/utils/fuzz.d.ts +3 -0
  73. package/dist/utils/fuzz.d.ts.map +1 -0
  74. package/dist/utils/fuzz.js +11 -0
  75. package/dist/utils/fuzz.js.map +1 -0
  76. package/dist/utils/index.d.ts +1 -0
  77. package/dist/utils/index.d.ts.map +1 -1
  78. package/dist/utils/index.js +1 -0
  79. package/dist/utils/index.js.map +1 -1
  80. package/dist/utils/managerTypes.d.ts.map +1 -1
  81. package/dist/utils/managerTypes.js.map +1 -1
  82. package/dist/utils/pubkey.d.ts.map +1 -1
  83. package/dist/utils/pubkey.js.map +1 -1
  84. package/dist/utils/rpc.d.ts.map +1 -1
  85. package/dist/utils/rpc.js.map +1 -1
  86. package/package.json +1 -1
  87. package/src/classes/fraction.ts +0 -5
  88. package/src/classes/manager.ts +1 -14
  89. package/src/classes/market.ts +1 -18
  90. package/src/classes/obligation.ts +1 -2
  91. package/src/classes/reserve.ts +39 -55
  92. package/src/classes/utils.ts +0 -30
  93. package/src/client_kamino_manager.ts +1 -1
  94. package/src/idl_codegen/accounts/LendingMarket.ts +1 -12
  95. package/src/idl_codegen/accounts/Obligation.ts +12 -3
  96. package/src/idl_codegen/types/UpdateLendingMarketConfigValue.ts +0 -43
  97. package/src/idl_codegen/types/UpdateLendingMarketMode.ts +0 -30
  98. package/src/idl_codegen/types/index.ts +0 -4
  99. package/src/lending_operations/repay_with_collateral_operations.ts +2 -0
  100. package/src/leverage/calcs.ts +315 -8
  101. package/src/leverage/index.ts +1 -0
  102. package/src/leverage/operations.ts +1079 -1331
  103. package/src/leverage/types.ts +211 -0
  104. package/src/leverage/utils.ts +103 -64
  105. package/src/utils/ObligationType.ts +0 -6
  106. package/src/utils/constants.ts +2 -0
  107. package/src/utils/fuzz.ts +5 -0
  108. package/src/utils/index.ts +1 -0
  109. package/src/utils/managerTypes.ts +0 -1
  110. package/src/utils/pubkey.ts +0 -2
  111. package/src/utils/rpc.ts +0 -1
@@ -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
+ };
@@ -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 const getTokenToKtokenSwapper = async (
29
- connection: Connection,
28
+ export async function getTokenToKtokenSwapper<QuoteResponse>(
30
29
  kaminoMarket: KaminoMarket,
31
30
  kamino: Kamino,
32
31
  depositor: PublicKey,
33
- swapper: SwapIxnsProvider,
32
+ slippagePct: Decimal,
33
+ swapper: SwapQuoteIxsProvider<QuoteResponse>,
34
34
  priceAinB: PriceAinBProvider,
35
35
  includeAtaIxns: boolean = true
36
- ): Promise<SwapIxnsProvider> => {
36
+ ): Promise<SwapQuoteIxsProvider<QuoteResponse>> {
37
37
  return async (
38
- amountInLamports: number,
39
- amountInMint: PublicKey,
40
- amountOutMint: PublicKey,
41
- slippage: number,
42
- amountDebtAtaBalance?: Decimal
43
- ) => {
44
- const slippageBps = new Decimal(slippage).mul('100');
45
- const mintInDecimals = kaminoMarket.getReserveByMint(amountInMint)!.state.liquidity.mintDecimals.toNumber();
46
- const amountIn = lamportsToNumberDecimal(amountInLamports, mintInDecimals);
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
- connection,
51
+ kaminoMarket.getConnection(),
54
52
  kamino,
55
53
  depositor,
56
- amountInMint,
57
- amountOutMint,
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
- return [ixWithLookup.instructions, ixWithLookup.lookupTablesAddresses];
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: SwapIxnsProvider,
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 const getKtokenToTokenSwapper = async (
130
+ export async function getKtokenToTokenSwapper<QuoteResponse>(
122
131
  kaminoMarket: KaminoMarket,
123
132
  kamino: Kamino,
124
133
  depositor: PublicKey,
125
- swapper: SwapIxnsProvider
126
- ): Promise<SwapIxnsProvider> => {
127
- return async (amountInLamports: number, amountInMint: PublicKey, amountOutMint: PublicKey, slippage: number) => {
128
- const amountInDecimals = kaminoMarket.getReserveByMint(amountInMint)!.state.liquidity.mintDecimals.toNumber();
129
- const amountToWithdraw = lamportsToNumberDecimal(amountInLamports, amountInDecimals);
130
- const kaminoStrategy = await kamino.getStrategyByKTokenMint(amountInMint);
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', amountInMint.toString(), ' for ', amountOutMint.toString(), 'token');
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 (amountOutMint.equals(kaminoStrategy?.strategy.tokenAMint!)) {
143
- const [swapIxs, swapLookupTables] = await swapper(
144
- estimatedBOut.toNumber(),
145
- kaminoStrategy?.strategy.tokenBMint!,
146
- kaminoStrategy?.strategy.tokenAMint!,
147
- slippage
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 [[...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swapIxs], swapLookupTables];
151
- } else if (amountOutMint.equals(kaminoStrategy?.strategy.tokenBMint!)) {
152
- const [swapIxs, swapLookupTables] = await swapper(
153
- estimatedAOut.toNumber(),
154
- kaminoStrategy?.strategy.tokenAMint!,
155
- kaminoStrategy?.strategy.tokenBMint!,
156
- slippage
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 [[...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swapIxs], swapLookupTables];
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(swapper: SwapIxnsProvider): KaminoSwapperIxBuilder {
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
- slippageBps: Decimal,
263
+ _slippage: Decimal,
235
264
  _allKeys: PublicKey[]
236
265
  ): Promise<[TransactionInstruction[], PublicKey[]]> => {
237
266
  if (input.tokenBToSwapAmount.lt(0)) {
238
- return await swapper(
239
- input.tokenBToSwapAmount.abs().toNumber(),
240
- tokenBMint,
241
- tokenAMint,
242
- slippageBps.toNumber() / 100
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
- return await swapper(
246
- input.tokenAToSwapAmount.abs().toNumber(),
247
- tokenAMint,
248
- tokenBMint,
249
- slippageBps.toNumber() / 100
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
  }
@@ -24,7 +24,6 @@ export class VanillaObligation {
24
24
  constructor(programId: PublicKey) {
25
25
  this.programId = programId;
26
26
  }
27
-
28
27
  toArgs() {
29
28
  const initObligationArgs: InitObligationArgsModel = {
30
29
  tag: VanillaObligation.tag,
@@ -35,7 +34,6 @@ export class VanillaObligation {
35
34
 
36
35
  return initObligationArgs;
37
36
  }
38
-
39
37
  toPda(market: PublicKey, user: PublicKey) {
40
38
  return getObligationPdaWithArgs(market, user, this.toArgs(), this.programId);
41
39
  }
@@ -63,7 +61,6 @@ export class MultiplyObligation {
63
61
 
64
62
  return initObligationArgs;
65
63
  }
66
-
67
64
  toPda(market: PublicKey, user: PublicKey) {
68
65
  return getObligationPdaWithArgs(market, user, this.toArgs(), this.programId);
69
66
  }
@@ -91,7 +88,6 @@ export class LeverageObligation {
91
88
 
92
89
  return initObligationArgs;
93
90
  }
94
-
95
91
  toPda(market: PublicKey, user: PublicKey) {
96
92
  return getObligationPdaWithArgs(market, user, this.toArgs(), this.programId);
97
93
  }
@@ -106,7 +102,6 @@ export class LendingObligation {
106
102
  this.token = token;
107
103
  this.programId = programId;
108
104
  }
109
-
110
105
  toArgs() {
111
106
  const initObligationArgs: InitObligationArgsModel = {
112
107
  tag: LendingObligation.tag,
@@ -117,7 +112,6 @@ export class LendingObligation {
117
112
 
118
113
  return initObligationArgs;
119
114
  }
120
-
121
115
  toPda(market: PublicKey, user: PublicKey) {
122
116
  return getObligationPdaWithArgs(market, user, this.toArgs(), this.programId);
123
117
  }
@@ -73,6 +73,8 @@ export const MIN_AUTODELEVERAGE_BONUS_BPS = 50;
73
73
  */
74
74
  export const WRAPPED_SOL_MINT = new PublicKey('So11111111111111111111111111111111111111112');
75
75
 
76
+ export const SOL_DECIMALS = 9;
77
+
76
78
  /**
77
79
  * USDC Mint
78
80
  */
@@ -0,0 +1,5 @@
1
+ import Decimal from 'decimal.js';
2
+
3
+ export function fuzzyEqual(a: Decimal.Value, b: Decimal.Value, epsilon = 0.0001) {
4
+ return new Decimal(a).sub(b).abs().lte(epsilon);
5
+ }
@@ -12,3 +12,4 @@ export * from './oracle';
12
12
  export * from './lookupTable';
13
13
  export * from './sendTransactionsUtils';
14
14
  export * from './managerTypes';
15
+ export * from './fuzz';
@@ -49,7 +49,6 @@ export interface AssetConfig {
49
49
  assetReserveConfigParams: AssetReserveConfigParams;
50
50
 
51
51
  setAssetConfigParams(assetReserveConfigParams: AssetReserveConfigParams): void;
52
-
53
52
  getReserveConfig(): ReserveConfig;
54
53
  }
55
54
 
@@ -239,7 +239,6 @@ export class PubkeyHashMap<K extends PublicKey, V> implements Map<K, V> {
239
239
  export class HashablePublicKey extends PublicKey implements IEquality<HashablePublicKey> {
240
240
  // We only use the last 32 bits of the public key for hashing
241
241
  static MASK = new BN(1).shln(32).subn(1);
242
-
243
242
  constructor(value: PublicKey | string | Buffer | Uint8Array | Array<number>) {
244
243
  super(value);
245
244
  }
@@ -258,6 +257,5 @@ export class HashablePublicKey extends PublicKey implements IEquality<HashablePu
258
257
 
259
258
  interface IEquality<T extends IEquality<T>> {
260
259
  equals(other: T): boolean;
261
-
262
260
  hashCode(): number;
263
261
  }
package/src/utils/rpc.ts CHANGED
@@ -10,7 +10,6 @@ import {
10
10
  import { Buffer } from 'buffer';
11
11
  import axios from 'axios';
12
12
  import { init, decompress } from '@bokuweb/zstd-wasm';
13
-
14
13
  (async () => {
15
14
  await init();
16
15
  })();