@kamino-finance/klend-sdk 7.1.1 → 7.1.3
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/@codegen/unstaking_pool/accounts/PoolState.d.ts +53 -0
- package/dist/@codegen/unstaking_pool/accounts/PoolState.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/accounts/PoolState.js +167 -0
- package/dist/@codegen/unstaking_pool/accounts/PoolState.js.map +1 -0
- package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.d.ts +41 -0
- package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.js +143 -0
- package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.js.map +1 -0
- package/dist/@codegen/unstaking_pool/accounts/index.d.ts +5 -0
- package/dist/@codegen/unstaking_pool/accounts/index.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/accounts/index.js +8 -0
- package/dist/@codegen/unstaking_pool/accounts/index.js.map +1 -0
- package/dist/@codegen/unstaking_pool/errors/anchor.d.ts +435 -0
- package/dist/@codegen/unstaking_pool/errors/anchor.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/errors/anchor.js +767 -0
- package/dist/@codegen/unstaking_pool/errors/anchor.js.map +1 -0
- package/dist/@codegen/unstaking_pool/errors/custom.d.ts +259 -0
- package/dist/@codegen/unstaking_pool/errors/custom.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/errors/custom.js +458 -0
- package/dist/@codegen/unstaking_pool/errors/custom.js.map +1 -0
- package/dist/@codegen/unstaking_pool/errors/index.d.ts +6 -0
- package/dist/@codegen/unstaking_pool/errors/index.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/errors/index.js +86 -0
- package/dist/@codegen/unstaking_pool/errors/index.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/burn.d.ts +21 -0
- package/dist/@codegen/unstaking_pool/instructions/burn.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/burn.js +67 -0
- package/dist/@codegen/unstaking_pool/instructions/burn.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.d.ts +8 -0
- package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.js +16 -0
- package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/collect.d.ts +16 -0
- package/dist/@codegen/unstaking_pool/instructions/collect.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/collect.js +24 -0
- package/dist/@codegen/unstaking_pool/instructions/collect.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/index.d.ts +17 -0
- package/dist/@codegen/unstaking_pool/instructions/index.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/index.js +20 -0
- package/dist/@codegen/unstaking_pool/instructions/index.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/initializePool.d.ts +14 -0
- package/dist/@codegen/unstaking_pool/instructions/initializePool.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/initializePool.js +22 -0
- package/dist/@codegen/unstaking_pool/instructions/initializePool.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/mint.d.ts +24 -0
- package/dist/@codegen/unstaking_pool/instructions/mint.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/mint.js +74 -0
- package/dist/@codegen/unstaking_pool/instructions/mint.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/splitTicket.d.ts +15 -0
- package/dist/@codegen/unstaking_pool/instructions/splitTicket.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/splitTicket.js +62 -0
- package/dist/@codegen/unstaking_pool/instructions/splitTicket.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/updateAdmin.d.ts +7 -0
- package/dist/@codegen/unstaking_pool/instructions/updateAdmin.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/updateAdmin.js +19 -0
- package/dist/@codegen/unstaking_pool/instructions/updateAdmin.js.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.d.ts +13 -0
- package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.js +60 -0
- package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.js.map +1 -0
- package/dist/@codegen/unstaking_pool/programId.d.ts +4 -0
- package/dist/@codegen/unstaking_pool/programId.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/programId.js +9 -0
- package/dist/@codegen/unstaking_pool/programId.js.map +1 -0
- package/dist/@codegen/unstaking_pool/types/PoolConfigField.d.ts +45 -0
- package/dist/@codegen/unstaking_pool/types/PoolConfigField.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/types/PoolConfigField.js +132 -0
- package/dist/@codegen/unstaking_pool/types/PoolConfigField.js.map +1 -0
- package/dist/@codegen/unstaking_pool/types/index.d.ts +5 -0
- package/dist/@codegen/unstaking_pool/types/index.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/types/index.js +39 -0
- package/dist/@codegen/unstaking_pool/types/index.js.map +1 -0
- package/dist/@codegen/unstaking_pool/utils/borshAddress.d.ts +4 -0
- package/dist/@codegen/unstaking_pool/utils/borshAddress.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/utils/borshAddress.js +30 -0
- package/dist/@codegen/unstaking_pool/utils/borshAddress.js.map +1 -0
- package/dist/@codegen/unstaking_pool/utils/index.d.ts +2 -0
- package/dist/@codegen/unstaking_pool/utils/index.d.ts.map +1 -0
- package/dist/@codegen/unstaking_pool/utils/index.js +18 -0
- package/dist/@codegen/unstaking_pool/utils/index.js.map +1 -0
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +5 -5
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/index.d.ts +5 -0
- package/dist/classes/index.d.ts.map +1 -1
- package/dist/classes/index.js +5 -0
- package/dist/classes/index.js.map +1 -1
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +2 -3
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/stakePool.d.ts +8 -0
- package/dist/classes/stakePool.d.ts.map +1 -0
- package/dist/classes/stakePool.js +18 -0
- package/dist/classes/stakePool.js.map +1 -0
- package/dist/classes/standardStakePool.d.ts +76 -0
- package/dist/classes/standardStakePool.d.ts.map +1 -0
- package/dist/classes/standardStakePool.js +400 -0
- package/dist/classes/standardStakePool.js.map +1 -0
- package/dist/classes/unstakingPool.d.ts +115 -0
- package/dist/classes/unstakingPool.d.ts.map +1 -0
- package/dist/classes/unstakingPool.js +372 -0
- package/dist/classes/unstakingPool.js.map +1 -0
- package/dist/classes/unstakingPoolTypes.d.ts +10 -0
- package/dist/classes/unstakingPoolTypes.d.ts.map +1 -0
- package/dist/classes/unstakingPoolTypes.js +3 -0
- package/dist/classes/unstakingPoolTypes.js.map +1 -0
- package/dist/classes/vault.d.ts +0 -9
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +6 -41
- package/dist/classes/vault.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 +0 -2
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +0 -1
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +2 -27
- package/dist/leverage/calcs.d.ts.map +1 -1
- package/dist/leverage/calcs.js +6 -136
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/operations.d.ts +8 -9
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +72 -228
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +0 -19
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/leverage/utils.d.ts +2 -19
- package/dist/leverage/utils.d.ts.map +1 -1
- package/dist/leverage/utils.js +0 -164
- package/dist/leverage/utils.js.map +1 -1
- package/dist/lib.d.ts +1 -0
- package/dist/lib.d.ts.map +1 -1
- package/dist/lib.js +3 -1
- package/dist/lib.js.map +1 -1
- package/dist/utils/lookupTable.d.ts +9 -0
- package/dist/utils/lookupTable.d.ts.map +1 -1
- package/dist/utils/lookupTable.js +37 -0
- package/dist/utils/lookupTable.js.map +1 -1
- package/package.json +4 -2
- package/src/@codegen/unstaking_pool/accounts/PoolState.ts +188 -0
- package/src/@codegen/unstaking_pool/accounts/UnstakeTicket.ts +156 -0
- package/src/@codegen/unstaking_pool/accounts/index.ts +4 -0
- package/src/@codegen/unstaking_pool/errors/anchor.ts +773 -0
- package/src/@codegen/unstaking_pool/errors/custom.ts +477 -0
- package/src/@codegen/unstaking_pool/errors/index.ts +68 -0
- package/src/@codegen/unstaking_pool/instructions/burn.ts +70 -0
- package/src/@codegen/unstaking_pool/instructions/changeTicketAuthority.ts +37 -0
- package/src/@codegen/unstaking_pool/instructions/collect.ts +53 -0
- package/src/@codegen/unstaking_pool/instructions/index.ts +19 -0
- package/src/@codegen/unstaking_pool/instructions/initializePool.ts +49 -0
- package/src/@codegen/unstaking_pool/instructions/mint.ts +80 -0
- package/src/@codegen/unstaking_pool/instructions/splitTicket.ts +59 -0
- package/src/@codegen/unstaking_pool/instructions/updateAdmin.ts +39 -0
- package/src/@codegen/unstaking_pool/instructions/updatePoolConfig.ts +58 -0
- package/src/@codegen/unstaking_pool/programId.ts +9 -0
- package/src/@codegen/unstaking_pool/types/PoolConfigField.ts +121 -0
- package/src/@codegen/unstaking_pool/types/index.ts +12 -0
- package/src/@codegen/unstaking_pool/utils/borshAddress.ts +43 -0
- package/src/@codegen/unstaking_pool/utils/index.ts +1 -0
- package/src/classes/action.ts +10 -5
- package/src/classes/index.ts +5 -0
- package/src/classes/manager.ts +3 -4
- package/src/classes/stakePool.ts +21 -0
- package/src/classes/standardStakePool.ts +487 -0
- package/src/classes/unstakingPool.ts +504 -0
- package/src/classes/unstakingPoolTypes.ts +12 -0
- package/src/classes/vault.ts +43 -56
- package/src/idl/unstaking_pool.json +662 -0
- package/src/lending_operations/repay_with_collateral_operations.ts +0 -2
- package/src/lending_operations/swap_collateral_operations.ts +0 -1
- package/src/leverage/calcs.ts +2 -201
- package/src/leverage/operations.ts +45 -377
- package/src/leverage/types.ts +0 -20
- package/src/leverage/utils.ts +3 -320
- package/src/lib.ts +1 -0
- package/src/utils/lookupTable.ts +48 -0
package/src/leverage/calcs.ts
CHANGED
|
@@ -1,14 +1,7 @@
|
|
|
1
1
|
import { Address, Slot } from '@solana/kit';
|
|
2
2
|
import Decimal from 'decimal.js';
|
|
3
|
-
import { collToLamportsDecimal, Kamino, StrategyWithAddress, TokenAmounts } from '@kamino-finance/kliquidity-sdk';
|
|
4
3
|
import { KaminoMarket, KaminoObligation, KaminoReserve, toJson } from '../classes';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
AdjustLeverageCalcsResult,
|
|
8
|
-
DepositLeverageCalcsResult,
|
|
9
|
-
PriceAinBProvider,
|
|
10
|
-
WithdrawLeverageCalcsResult,
|
|
11
|
-
} from './types';
|
|
4
|
+
import { AdjustLeverageCalcsResult, DepositLeverageCalcsResult, WithdrawLeverageCalcsResult } from './types';
|
|
12
5
|
import { fuzzyEqual } from '../utils';
|
|
13
6
|
|
|
14
7
|
const closingPositionDiffTolerance = 0.0001;
|
|
@@ -404,73 +397,6 @@ export const estimateDepositMode = ({
|
|
|
404
397
|
};
|
|
405
398
|
};
|
|
406
399
|
|
|
407
|
-
/**
|
|
408
|
-
* Given an amount of ktokens, returns the estimated amount of token A and token B that need to be deposited
|
|
409
|
-
* The amount of A and B may result in less ktokens being minted, the actual amount of ktokens minted is returned as well
|
|
410
|
-
* @param kamino
|
|
411
|
-
* @param strategy
|
|
412
|
-
* @param mintAmount - desired amount of ktokens to mint
|
|
413
|
-
* @param strategyHoldings - optional strategy holdings, if not provided will be fetched from the blockchain
|
|
414
|
-
* @returns [tokenA, tokenB, actualMintAmount]
|
|
415
|
-
*/
|
|
416
|
-
export async function simulateMintKToken(
|
|
417
|
-
kamino: Kamino,
|
|
418
|
-
strategy: StrategyWithAddress,
|
|
419
|
-
mintAmount: Decimal,
|
|
420
|
-
strategyHoldings?: TokenAmounts
|
|
421
|
-
): Promise<[Decimal, Decimal, Decimal]> {
|
|
422
|
-
let holdings = strategyHoldings;
|
|
423
|
-
if (!holdings) {
|
|
424
|
-
holdings = await kamino.getStrategyTokensHoldings(strategy, 'DEPOSIT');
|
|
425
|
-
}
|
|
426
|
-
const sharesIssuedDecimal = new Decimal(strategy.strategy.sharesIssued.toString()).div(
|
|
427
|
-
10 ** strategy.strategy.sharesMintDecimals.toNumber()
|
|
428
|
-
);
|
|
429
|
-
|
|
430
|
-
// Add 1 because the sdk doesn't round up where the SC will
|
|
431
|
-
const strategyA = holdings.a.div(10 ** strategy.strategy.tokenAMintDecimals.toNumber());
|
|
432
|
-
const strategyB = holdings.b.div(10 ** strategy.strategy.tokenBMintDecimals.toNumber());
|
|
433
|
-
const aPerShare = strategyA.div(sharesIssuedDecimal);
|
|
434
|
-
const bPerShare = strategyB.div(sharesIssuedDecimal);
|
|
435
|
-
|
|
436
|
-
const requiredA = aPerShare.mul(mintAmount);
|
|
437
|
-
const requiredB = bPerShare.mul(mintAmount);
|
|
438
|
-
const pxAInB = strategyB.div(strategyA);
|
|
439
|
-
|
|
440
|
-
console.info(
|
|
441
|
-
`Estimating kToken mint of ${mintAmount} ktokens on strategy ${strategy.address.toString()} requires: estimated A: ${requiredA}, estimated B: ${requiredB}. Current pool state:\n${toJson(
|
|
442
|
-
{ ...holdings, sharesIssued: sharesIssuedDecimal, poolPxAInB: pxAInB }
|
|
443
|
-
)}`
|
|
444
|
-
);
|
|
445
|
-
|
|
446
|
-
// If we deposited with this exact ratio - how many ktokens do we actually get from the program?
|
|
447
|
-
const RustDecimal = Decimal.clone({ precision: 18, rounding: Decimal.ROUND_FLOOR });
|
|
448
|
-
|
|
449
|
-
const usA = new RustDecimal(holdings.a);
|
|
450
|
-
const usB = new RustDecimal(holdings.b);
|
|
451
|
-
const uA = new RustDecimal(requiredA.mul(10 ** strategy.strategy.tokenAMintDecimals.toNumber()).ceil());
|
|
452
|
-
const uB = new RustDecimal(requiredB.mul(10 ** strategy.strategy.tokenBMintDecimals.toNumber()).ceil());
|
|
453
|
-
|
|
454
|
-
const ratio = usA.div(usB);
|
|
455
|
-
const depositableB = uA.div(ratio).floor();
|
|
456
|
-
let actualA, actualB;
|
|
457
|
-
if (depositableB.lte(uB)) {
|
|
458
|
-
actualA = depositableB.mul(ratio).floor();
|
|
459
|
-
actualB = uB;
|
|
460
|
-
} else {
|
|
461
|
-
actualA = uB.mul(ratio).floor();
|
|
462
|
-
actualB = actualA.div(ratio).floor();
|
|
463
|
-
}
|
|
464
|
-
const actualMintFromA = actualA.mul(strategy.strategy.sharesIssued.toString()).div(holdings.a).floor();
|
|
465
|
-
const actualMintFromB = actualB.mul(strategy.strategy.sharesIssued.toString()).div(holdings.b).floor();
|
|
466
|
-
const actualMint = Decimal.min(actualMintFromA, actualMintFromB).div(
|
|
467
|
-
10 ** strategy.strategy.sharesMintDecimals.toNumber()
|
|
468
|
-
);
|
|
469
|
-
console.log(`Actual deposit amounts: A: ${actualA}, B: ${actualB}, kTokens to mint: ${actualMint}`);
|
|
470
|
-
|
|
471
|
-
return [requiredA, requiredB, actualMint];
|
|
472
|
-
}
|
|
473
|
-
|
|
474
400
|
export const depositLeverageCalcs = (props: {
|
|
475
401
|
depositAmount: Decimal;
|
|
476
402
|
depositTokenIsCollToken: boolean;
|
|
@@ -509,9 +435,6 @@ export const depositLeverageCalcs = (props: {
|
|
|
509
435
|
collTokenToDeposit: finalColl,
|
|
510
436
|
swapDebtTokenIn: debt,
|
|
511
437
|
swapCollTokenExpectedOut: finalColl.sub(depositAmount),
|
|
512
|
-
flashBorrowInDebtTokenKtokenOnly: new Decimal(0),
|
|
513
|
-
singleSidedDepositKtokenOnly: new Decimal(0),
|
|
514
|
-
requiredCollateralKtokenOnly: new Decimal(0),
|
|
515
438
|
};
|
|
516
439
|
} else {
|
|
517
440
|
const y = targetLeverage.mul(priceDebtToColl);
|
|
@@ -527,113 +450,6 @@ export const depositLeverageCalcs = (props: {
|
|
|
527
450
|
collTokenToDeposit: finalColl,
|
|
528
451
|
swapDebtTokenIn: debt.add(depositAmount),
|
|
529
452
|
swapCollTokenExpectedOut: finalColl,
|
|
530
|
-
flashBorrowInDebtTokenKtokenOnly: new Decimal(0),
|
|
531
|
-
singleSidedDepositKtokenOnly: new Decimal(0),
|
|
532
|
-
requiredCollateralKtokenOnly: new Decimal(0),
|
|
533
|
-
};
|
|
534
|
-
}
|
|
535
|
-
};
|
|
536
|
-
|
|
537
|
-
export const depositLeverageKtokenCalcs = async (props: {
|
|
538
|
-
kamino: Kamino;
|
|
539
|
-
strategy: StrategyWithAddress;
|
|
540
|
-
debtTokenMint: Address;
|
|
541
|
-
depositAmount: Decimal;
|
|
542
|
-
depositTokenIsCollToken: boolean;
|
|
543
|
-
depositTokenIsSol: boolean;
|
|
544
|
-
priceDebtToColl: Decimal;
|
|
545
|
-
targetLeverage: Decimal;
|
|
546
|
-
slippagePct: Decimal;
|
|
547
|
-
flashLoanFee: Decimal;
|
|
548
|
-
priceAinB: PriceAinBProvider;
|
|
549
|
-
strategyHoldings?: TokenAmounts;
|
|
550
|
-
}): Promise<DepositLeverageCalcsResult> => {
|
|
551
|
-
const {
|
|
552
|
-
kamino,
|
|
553
|
-
strategy,
|
|
554
|
-
debtTokenMint,
|
|
555
|
-
depositAmount,
|
|
556
|
-
depositTokenIsCollToken,
|
|
557
|
-
depositTokenIsSol,
|
|
558
|
-
priceDebtToColl,
|
|
559
|
-
targetLeverage,
|
|
560
|
-
slippagePct,
|
|
561
|
-
flashLoanFee,
|
|
562
|
-
priceAinB,
|
|
563
|
-
strategyHoldings,
|
|
564
|
-
} = props;
|
|
565
|
-
const initDepositInSol = depositTokenIsSol ? depositAmount : new Decimal(0);
|
|
566
|
-
const slippage = slippagePct.div('100');
|
|
567
|
-
|
|
568
|
-
let flashBorrowInDebtToken: Decimal;
|
|
569
|
-
let collTokenToDeposit: Decimal;
|
|
570
|
-
let debtTokenToBorrow: Decimal;
|
|
571
|
-
|
|
572
|
-
if (depositTokenIsCollToken) {
|
|
573
|
-
const x = slippage.add('1').div(priceDebtToColl);
|
|
574
|
-
const y = flashLoanFee.add('1').mul(priceDebtToColl);
|
|
575
|
-
const z = targetLeverage.mul(y).div(targetLeverage.sub(1));
|
|
576
|
-
flashBorrowInDebtToken = depositAmount.div(z.minus(new Decimal(1).div(x)));
|
|
577
|
-
collTokenToDeposit = depositAmount.add(flashBorrowInDebtToken.div(x));
|
|
578
|
-
debtTokenToBorrow = flashBorrowInDebtToken.mul(new Decimal(1).add(flashLoanFee));
|
|
579
|
-
|
|
580
|
-
return {
|
|
581
|
-
flashBorrowInCollToken: new Decimal(0),
|
|
582
|
-
initDepositInSol,
|
|
583
|
-
collTokenToDeposit,
|
|
584
|
-
debtTokenToBorrow,
|
|
585
|
-
swapDebtTokenIn: new Decimal(0),
|
|
586
|
-
swapCollTokenExpectedOut: new Decimal(0),
|
|
587
|
-
flashBorrowInDebtTokenKtokenOnly: flashBorrowInDebtToken,
|
|
588
|
-
requiredCollateralKtokenOnly: collTokenToDeposit.sub(depositAmount), // Assuming netValue is requiredCollateral, adjust as needed
|
|
589
|
-
singleSidedDepositKtokenOnly: flashBorrowInDebtToken,
|
|
590
|
-
};
|
|
591
|
-
} else {
|
|
592
|
-
const y = targetLeverage.mul(priceDebtToColl);
|
|
593
|
-
// although we will only swap ~half of the debt token, we account for the slippage on the entire amount as we are working backwards from the minimum collateral and do not know the exact swap proportion in advance
|
|
594
|
-
// This also allows for some variation in the pool ratios between calculation + submitting the tx
|
|
595
|
-
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
596
|
-
// Calculate the amount of collateral tokens we will deposit in order to achieve the desired leverage after swapping a portion of the debt token and flash loan fees
|
|
597
|
-
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
598
|
-
// Calculate how many A and B tokens we will need to actually mint the desired amount of ktoken collateral
|
|
599
|
-
// The actual amount of ktokens received may be less than the finalColl due to smart proportional contract logic
|
|
600
|
-
// So we use the actualColl as the amount we will deposit
|
|
601
|
-
const [estimatedA, estimatedB, actualColl] = await simulateMintKToken(
|
|
602
|
-
kamino!,
|
|
603
|
-
strategy,
|
|
604
|
-
finalColl,
|
|
605
|
-
strategyHoldings
|
|
606
|
-
);
|
|
607
|
-
const { tokenAMint, tokenBMint } = strategy.strategy;
|
|
608
|
-
const pxAinB = await priceAinB(tokenAMint, tokenBMint);
|
|
609
|
-
const isTokenADeposit = tokenAMint === debtTokenMint;
|
|
610
|
-
// Calculate the amount we need to flash borrow by combining value of A and B into the debt token
|
|
611
|
-
const singleSidedDepositAmount = isTokenADeposit
|
|
612
|
-
? estimatedA.add(estimatedB.div(pxAinB))
|
|
613
|
-
: estimatedB.add(estimatedA.mul(pxAinB));
|
|
614
|
-
|
|
615
|
-
// Add slippage to the entire amount, add flash loan fee to part we will flash borrow
|
|
616
|
-
flashBorrowInDebtToken = singleSidedDepositAmount
|
|
617
|
-
.div(new Decimal('1').sub(slippage))
|
|
618
|
-
.sub(depositAmount)
|
|
619
|
-
.div(new Decimal('1').sub(flashLoanFee));
|
|
620
|
-
// Deposit the min ktoken amount we calculated at the beginning
|
|
621
|
-
// Any slippage will be left in the user's wallet as ktokens
|
|
622
|
-
collTokenToDeposit = actualColl;
|
|
623
|
-
debtTokenToBorrow = flashBorrowInDebtToken.div(new Decimal('1').sub(flashLoanFee));
|
|
624
|
-
// Add slippage to ensure we try to swap/deposit as much as possible after flash loan fees
|
|
625
|
-
const singleSidedDeposit = singleSidedDepositAmount.div(new Decimal('1').sub(slippage));
|
|
626
|
-
|
|
627
|
-
return {
|
|
628
|
-
flashBorrowInCollToken: new Decimal(0),
|
|
629
|
-
initDepositInSol,
|
|
630
|
-
collTokenToDeposit,
|
|
631
|
-
debtTokenToBorrow,
|
|
632
|
-
swapDebtTokenIn: new Decimal(0),
|
|
633
|
-
swapCollTokenExpectedOut: new Decimal(0),
|
|
634
|
-
flashBorrowInDebtTokenKtokenOnly: flashBorrowInDebtToken,
|
|
635
|
-
singleSidedDepositKtokenOnly: singleSidedDeposit,
|
|
636
|
-
requiredCollateralKtokenOnly: collTokenToDeposit, // Assuming collTokenToDeposit is requiredCollateral, adjust as needed
|
|
637
453
|
};
|
|
638
454
|
}
|
|
639
455
|
};
|
|
@@ -710,17 +526,13 @@ export function withdrawLeverageCalcs(
|
|
|
710
526
|
}
|
|
711
527
|
|
|
712
528
|
export async function adjustDepositLeverageCalcs(
|
|
713
|
-
market: KaminoMarket,
|
|
714
|
-
owner: Address,
|
|
715
529
|
debtReserve: KaminoReserve,
|
|
716
530
|
adjustDepositPosition: Decimal,
|
|
717
531
|
adjustBorrowPosition: Decimal,
|
|
718
532
|
priceDebtToColl: Decimal,
|
|
719
533
|
flashLoanFee: Decimal,
|
|
720
|
-
slippagePct: Decimal
|
|
721
|
-
collIsKtoken: boolean
|
|
534
|
+
slippagePct: Decimal
|
|
722
535
|
): Promise<AdjustLeverageCalcsResult> {
|
|
723
|
-
// used if coll is Ktoken and we borrow debt token instead
|
|
724
536
|
const amountToFlashBorrowDebt = adjustDepositPosition
|
|
725
537
|
.div(priceDebtToColl)
|
|
726
538
|
.mul(new Decimal(new Decimal(1).add(slippagePct.div(100))))
|
|
@@ -731,20 +543,11 @@ export async function adjustDepositLeverageCalcs(
|
|
|
731
543
|
.mul(new Decimal(new Decimal(1).add(slippagePct.div(100))))
|
|
732
544
|
.div(priceDebtToColl);
|
|
733
545
|
|
|
734
|
-
const expectedDebtTokenAtaBalance = await getExpectedTokenBalanceAfterBorrow(
|
|
735
|
-
market.getRpc(),
|
|
736
|
-
debtReserve.getLiquidityMint(),
|
|
737
|
-
owner,
|
|
738
|
-
collToLamportsDecimal(!collIsKtoken ? borrowAmount : amountToFlashBorrowDebt, debtReserve!.stats.decimals).floor(),
|
|
739
|
-
debtReserve!.state.liquidity.mintDecimals.toNumber()
|
|
740
|
-
);
|
|
741
|
-
|
|
742
546
|
return {
|
|
743
547
|
adjustDepositPosition,
|
|
744
548
|
adjustBorrowPosition,
|
|
745
549
|
amountToFlashBorrowDebt,
|
|
746
550
|
borrowAmount,
|
|
747
|
-
expectedDebtTokenAtaBalance,
|
|
748
551
|
withdrawAmountWithSlippageAndFlashLoanFee: new Decimal(0),
|
|
749
552
|
};
|
|
750
553
|
}
|
|
@@ -755,7 +558,6 @@ export function adjustWithdrawLeverageCalcs(
|
|
|
755
558
|
flashLoanFee: Decimal,
|
|
756
559
|
slippagePct: Decimal
|
|
757
560
|
): AdjustLeverageCalcsResult {
|
|
758
|
-
// used if coll is Ktoken and we borrow debt token instead
|
|
759
561
|
const withdrawAmountWithSlippageAndFlashLoanFee = Decimal.abs(adjustDepositPosition)
|
|
760
562
|
.mul(new Decimal(1).plus(flashLoanFee))
|
|
761
563
|
.mul(new Decimal(1).add(slippagePct.div(100)));
|
|
@@ -765,7 +567,6 @@ export function adjustWithdrawLeverageCalcs(
|
|
|
765
567
|
adjustBorrowPosition,
|
|
766
568
|
amountToFlashBorrowDebt: new Decimal(0),
|
|
767
569
|
borrowAmount: new Decimal(0),
|
|
768
|
-
expectedDebtTokenAtaBalance: new Decimal(0),
|
|
769
570
|
withdrawAmountWithSlippageAndFlashLoanFee,
|
|
770
571
|
};
|
|
771
572
|
}
|