@carrot-protocol/clend-rpc 0.0.1-mrgn-fork1-dev-bc97fd7 → 0.0.3-data-fixes-dev-f69223b
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/addresses.js.map +1 -0
- package/dist/events.js.map +1 -0
- package/dist/{src/idl → idl}/clend.d.ts +8 -1
- package/dist/{src/idl → idl}/clend.js +8 -1
- package/dist/idl/clend.js.map +1 -0
- package/dist/index.js.map +1 -0
- package/dist/{src/instructions.d.ts → instructions.d.ts} +1 -0
- package/dist/{src/instructions.js → instructions.js} +10 -0
- package/dist/instructions.js.map +1 -0
- package/dist/jupUtils.js.map +1 -0
- package/dist/{src/jupiterUtils.js → jupiterUtils.js} +2 -2
- package/dist/jupiterUtils.js.map +1 -0
- package/dist/logger.js.map +1 -0
- package/dist/{src/math.d.ts → math.d.ts} +35 -6
- package/dist/{src/math.js → math.js} +112 -13
- package/dist/math.js.map +1 -0
- package/dist/mockJupiterUtils.js.map +1 -0
- package/dist/{src/rpc.d.ts → rpc.d.ts} +35 -0
- package/dist/{src/rpc.js → rpc.js} +246 -138
- package/dist/rpc.js.map +1 -0
- package/dist/{src/state.js → state.js} +8 -12
- package/dist/state.js.map +1 -0
- package/dist/utils.js.map +1 -0
- package/package.json +1 -1
- package/dist/src/addresses.js.map +0 -1
- package/dist/src/events.js.map +0 -1
- package/dist/src/idl/clend.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/instructions.js.map +0 -1
- package/dist/src/jupUtils.js.map +0 -1
- package/dist/src/jupiterUtils.js.map +0 -1
- package/dist/src/logger.js.map +0 -1
- package/dist/src/math.js.map +0 -1
- package/dist/src/mockJupiterUtils.js.map +0 -1
- package/dist/src/rpc.js.map +0 -1
- package/dist/src/state.js.map +0 -1
- package/dist/src/utils.js.map +0 -1
- /package/dist/{src/addresses.d.ts → addresses.d.ts} +0 -0
- /package/dist/{src/addresses.js → addresses.js} +0 -0
- /package/dist/{src/events.d.ts → events.d.ts} +0 -0
- /package/dist/{src/events.js → events.js} +0 -0
- /package/dist/{src/index.d.ts → index.d.ts} +0 -0
- /package/dist/{src/index.js → index.js} +0 -0
- /package/dist/{src/jupUtils.d.ts → jupUtils.d.ts} +0 -0
- /package/dist/{src/jupUtils.js → jupUtils.js} +0 -0
- /package/dist/{src/jupiterUtils.d.ts → jupiterUtils.d.ts} +0 -0
- /package/dist/{src/logger.d.ts → logger.d.ts} +0 -0
- /package/dist/{src/logger.js → logger.js} +0 -0
- /package/dist/{src/mockJupiterUtils.d.ts → mockJupiterUtils.d.ts} +0 -0
- /package/dist/{src/mockJupiterUtils.js → mockJupiterUtils.js} +0 -0
- /package/dist/{src/state.d.ts → state.d.ts} +0 -0
- /package/dist/{src/utils.d.ts → utils.d.ts} +0 -0
- /package/dist/{src/utils.js → utils.js} +0 -0
|
@@ -199,9 +199,9 @@ class ClendClient {
|
|
|
199
199
|
emissionsRemaining: (0, utils_1.wrappedI80F48toBigNumber)(data.emissionsRemaining),
|
|
200
200
|
emissionsMint: new anchor_1.web3.PublicKey(data.emissionsMint),
|
|
201
201
|
collectedProgramFeesOutstanding: Number((0, utils_1.wrappedI80F48toBigNumber)(data.collectedProgramFeesOutstanding)),
|
|
202
|
-
assetAmount,
|
|
202
|
+
assetAmount: new anchor_1.BN(assetAmount.toFixed(0)),
|
|
203
203
|
assetAmountUi,
|
|
204
|
-
liabilityAmount,
|
|
204
|
+
liabilityAmount: new anchor_1.BN(liabilityAmount.toFixed(0)),
|
|
205
205
|
liabilityAmountUi,
|
|
206
206
|
utilizationRate,
|
|
207
207
|
baseInterestRate,
|
|
@@ -292,6 +292,14 @@ class ClendClient {
|
|
|
292
292
|
const txSig = await this.send([ix], [clendAccount]);
|
|
293
293
|
return { clendAccount: clendAccount.publicKey, txSig };
|
|
294
294
|
}
|
|
295
|
+
async closeClendAccount(clendAccount) {
|
|
296
|
+
const clendAccountData = await this.getClendAccount(clendAccount);
|
|
297
|
+
if (clendAccountData === undefined) {
|
|
298
|
+
throw new Error(`clend account not found: ${clendAccount.toString()}`);
|
|
299
|
+
}
|
|
300
|
+
const ix = await this.instructions.closeClendAccount(clendAccount, this.address());
|
|
301
|
+
return this.send([ix]);
|
|
302
|
+
}
|
|
295
303
|
// Lending Operations
|
|
296
304
|
async deposit(clendGroup, clendAccount, mint, amount) {
|
|
297
305
|
const bank = (0, addresses_1.getBankPda)(clendGroup, mint);
|
|
@@ -400,43 +408,19 @@ class ClendClient {
|
|
|
400
408
|
return this.send([ix]);
|
|
401
409
|
}
|
|
402
410
|
async depositLeverage(clendGroup, clendAccount, collateralMint, debtMint, depositAmount, targetLeverage, slippageBps) {
|
|
411
|
+
const params = await this.getDepositLeverageParams(clendGroup, collateralMint, debtMint, depositAmount, targetLeverage);
|
|
412
|
+
const { ixns, luts, additionalSigners } = await this.getDepositLeverageIxns(clendGroup, this.address(), clendAccount, collateralMint, debtMint, params.debtBankData.mintDecimals, params.collateralBankData.mintDecimals, params.borrowAmount, depositAmount, slippageBps, 0);
|
|
413
|
+
return this.send(ixns, additionalSigners, luts);
|
|
414
|
+
}
|
|
415
|
+
async getDepositLeverageIxns(clendGroup, user, clendAccount, collateralMint, debtMint, debtMintDecimals, collateralMintDecimals, borrowAmount, depositAmount, slippageBps, additionalIxnCount) {
|
|
403
416
|
// Get token programs
|
|
404
417
|
const collateralTokenProgram = (0, utils_1.getTokenProgramForMint)(collateralMint);
|
|
405
418
|
const debtTokenProgram = (0, utils_1.getTokenProgramForMint)(debtMint);
|
|
406
|
-
// Get decimals
|
|
407
|
-
const collateralDecimals = (0, utils_1.getTokenDecimalsForMint)(collateralMint);
|
|
408
|
-
const debtDecimals = (0, utils_1.getTokenDecimalsForMint)(debtMint);
|
|
409
|
-
// Get bank addrs
|
|
410
|
-
const collateralBank = (0, addresses_1.getBankPda)(clendGroup, collateralMint);
|
|
411
419
|
const debtBank = (0, addresses_1.getBankPda)(clendGroup, debtMint);
|
|
412
|
-
|
|
413
|
-
const collateralBankData = await this.getBank(collateralBank);
|
|
414
|
-
const debtBankData = await this.getBank(debtBank);
|
|
415
|
-
// Derive token accounts
|
|
416
|
-
const userCollateralAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralMint, this.address(), true, collateralTokenProgram);
|
|
417
|
-
const userDebtAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtMint, this.address(), true, debtTokenProgram);
|
|
418
|
-
// Calculate borrow amount based on target leverage
|
|
419
|
-
const depositAmountUI = (0, math_1.amountToUi)(depositAmount, collateralDecimals);
|
|
420
|
-
// Get current prices from Jupiter
|
|
421
|
-
const collateralPrice = await this.getJupiterPrice(collateralMint);
|
|
422
|
-
const debtPrice = await this.getJupiterPrice(debtMint);
|
|
423
|
-
// Calculate collateral value in debt token terms
|
|
424
|
-
const { borrowAmountUi } = (0, math_1.computeLoopingAmounts)(depositAmountUI, targetLeverage, collateralPrice, debtPrice, collateralBankData.config.assetWeightMaint, debtBankData.config.liabilityWeightMaint);
|
|
425
|
-
const borrowAmount = (0, math_1.uiToAmount)(borrowAmountUi, debtDecimals);
|
|
426
|
-
utils_1.logger.info("depositLeverage params calculated", {
|
|
427
|
-
borrowAmount: borrowAmount.toString(),
|
|
428
|
-
borrowAmountUi,
|
|
429
|
-
depositAmount: depositAmount.toString(),
|
|
430
|
-
depositAmountUI,
|
|
431
|
-
collateralPrice,
|
|
432
|
-
debtPrice,
|
|
433
|
-
collateralDecimals,
|
|
434
|
-
debtDecimals,
|
|
435
|
-
targetLeverage,
|
|
436
|
-
});
|
|
420
|
+
const collateralBank = (0, addresses_1.getBankPda)(clendGroup, collateralMint);
|
|
437
421
|
// Get Jupiter quote for swapping debt token to collateral token
|
|
438
422
|
const swapMode = api_1.SwapMode.ExactIn;
|
|
439
|
-
const swapQuote = await this.getJupiterQuote(debtMint, collateralMint,
|
|
423
|
+
const swapQuote = await this.getJupiterQuote(debtMint, collateralMint, debtMintDecimals, collateralMintDecimals, new decimal_js_1.default(borrowAmount.toString()), slippageBps, swapMode);
|
|
440
424
|
utils_1.logger.debug("depositLeverage swapQuote", {
|
|
441
425
|
inAmount: swapQuote.quoteResponse.inAmount,
|
|
442
426
|
outAmount: swapQuote.quoteResponse.outAmount,
|
|
@@ -449,7 +433,7 @@ class ClendClient {
|
|
|
449
433
|
utils_1.logger.info("Creating init clend account ix");
|
|
450
434
|
// create clend account
|
|
451
435
|
const clendAccountKp = anchor_1.web3.Keypair.generate();
|
|
452
|
-
const ix = await this.instructions.initializeClendAccount(clendGroup,
|
|
436
|
+
const ix = await this.instructions.initializeClendAccount(clendGroup, user, clendAccountKp);
|
|
453
437
|
initClendAccountIx = ix;
|
|
454
438
|
additionalSigners.push(clendAccountKp);
|
|
455
439
|
clendAccount = clendAccountKp.publicKey;
|
|
@@ -484,13 +468,15 @@ class ClendClient {
|
|
|
484
468
|
inputMint: debtMint,
|
|
485
469
|
outputMint: collateralMint,
|
|
486
470
|
};
|
|
487
|
-
const swapIxs = await this.getJupiterSwap(
|
|
471
|
+
const swapIxs = await this.getJupiterSwap(user, swapInputs, swapQuote);
|
|
488
472
|
// Calculate additional collateral from swap
|
|
489
473
|
const additionalCollateralAmount = new anchor_1.BN(swapQuote.quoteResponse.outAmount);
|
|
490
474
|
const totalCollateralAmount = depositAmount.add(additionalCollateralAmount);
|
|
475
|
+
const userDebtAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtMint, user, true, debtTokenProgram);
|
|
476
|
+
const userCollateralAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralMint, user, true, collateralTokenProgram);
|
|
491
477
|
// Create transaction instructions in correct order
|
|
492
|
-
const borrowIx = await this.instructions.borrow(clendGroup, clendAccount,
|
|
493
|
-
const depositIx = await this.instructions.deposit(clendGroup, clendAccount,
|
|
478
|
+
const borrowIx = await this.instructions.borrow(clendGroup, clendAccount, user, debtBank, userDebtAta, debtTokenProgram, borrowAmount, remainingAccounts);
|
|
479
|
+
const depositIx = await this.instructions.deposit(clendGroup, clendAccount, user, collateralBank, userCollateralAta, collateralTokenProgram, totalCollateralAmount, remainingAccounts);
|
|
494
480
|
// add lending and swap instructions
|
|
495
481
|
const ixnsWithoutFlashLoan = [
|
|
496
482
|
borrowIx,
|
|
@@ -509,26 +495,145 @@ class ClendClient {
|
|
|
509
495
|
const endIndex = new anchor_1.BN(cuIxns +
|
|
510
496
|
ixnsWithoutFlashLoan.length +
|
|
511
497
|
addInitClendAccountIncrement +
|
|
512
|
-
startFlashLoanIncrement
|
|
513
|
-
|
|
498
|
+
startFlashLoanIncrement +
|
|
499
|
+
additionalIxnCount);
|
|
500
|
+
const { beginFlashLoanIx, endFlashLoanIx } = await this.instructions.createFlashLoanInstructions(clendAccount, user, endIndex, remainingAccounts);
|
|
514
501
|
const ixns = [
|
|
515
502
|
...initClendAccountIxns,
|
|
516
503
|
beginFlashLoanIx,
|
|
517
504
|
...ixnsWithoutFlashLoan,
|
|
518
505
|
endFlashLoanIx,
|
|
519
506
|
];
|
|
520
|
-
|
|
521
|
-
|
|
507
|
+
return { ixns, luts: swapIxs.lookupTables, additionalSigners };
|
|
508
|
+
}
|
|
509
|
+
async getDepositLeverageParams(clendGroup, collateralMint, debtMint, depositAmount, targetLeverage) {
|
|
510
|
+
// Get decimals
|
|
511
|
+
const collateralDecimals = (0, utils_1.getTokenDecimalsForMint)(collateralMint);
|
|
512
|
+
const debtDecimals = (0, utils_1.getTokenDecimalsForMint)(debtMint);
|
|
513
|
+
// Get bank addrs
|
|
514
|
+
const debtBank = (0, addresses_1.getBankPda)(clendGroup, debtMint);
|
|
515
|
+
const collateralBank = (0, addresses_1.getBankPda)(clendGroup, collateralMint);
|
|
516
|
+
// Get bank data
|
|
517
|
+
const debtBankData = await this.getBank(debtBank);
|
|
518
|
+
const collateralBankData = await this.getBank(collateralBank);
|
|
519
|
+
// Calculate borrow amount based on target leverage
|
|
520
|
+
const depositAmountUI = (0, math_1.amountToUi)(depositAmount, collateralDecimals);
|
|
521
|
+
// Get current prices from Jupiter
|
|
522
|
+
const collateralPrice = await this.getJupiterPrice(collateralMint);
|
|
523
|
+
const debtPrice = await this.getJupiterPrice(debtMint);
|
|
524
|
+
// Calculate collateral value in debt token terms
|
|
525
|
+
const { borrowAmountUi, totalDepositAmountUi } = (0, math_1.computeLoopingAmounts)(depositAmountUI, targetLeverage, collateralPrice, debtPrice, 1, 1);
|
|
526
|
+
const borrowAmount = (0, math_1.uiToAmount)(borrowAmountUi, debtDecimals);
|
|
527
|
+
// get required borrow data
|
|
528
|
+
const borrowApy = debtBankData.borrowApy;
|
|
529
|
+
const originationFeeRate = debtBankData.config.interestRateConfig.protocolOriginationFee;
|
|
530
|
+
// adjust borrow amount for borrowing costs
|
|
531
|
+
const borrowAmountAfterBorrowingCosts = (0, math_1.adjustBorrowForImmediateCosts)(borrowAmount, originationFeeRate, borrowApy, 0);
|
|
532
|
+
//// 3. Adjust for Slippage (Borrow MORE)
|
|
533
|
+
//// Apply slippage adjustment to the amount already adjusted for costs
|
|
534
|
+
//const finalBorrowAmount = adjustBorrowForSlippage(
|
|
535
|
+
// borrowAmountAfterBorrowingCosts, // Input the cost-adjusted amount
|
|
536
|
+
// slippageBps,
|
|
537
|
+
// debtDecimals,
|
|
538
|
+
//);
|
|
539
|
+
const finalBorrowAmount = borrowAmountAfterBorrowingCosts;
|
|
540
|
+
const finalBorrowAmountUi = (0, math_1.amountToUi)(finalBorrowAmount, debtDecimals);
|
|
541
|
+
utils_1.logger.info("depositLeverage params calculated", {
|
|
542
|
+
borrowAmount: borrowAmount.toString(),
|
|
543
|
+
borrowAmountAfterBorrowingCosts: borrowAmountAfterBorrowingCosts.toString(),
|
|
544
|
+
finalBorrowAmount: finalBorrowAmount.toString(),
|
|
545
|
+
finalBorrowAmountUi,
|
|
546
|
+
borrowAmountUi,
|
|
547
|
+
depositAmount: depositAmount.toString(),
|
|
548
|
+
depositAmountUI,
|
|
549
|
+
collateralPrice,
|
|
550
|
+
debtPrice,
|
|
551
|
+
collateralDecimals,
|
|
552
|
+
debtDecimals,
|
|
553
|
+
targetLeverage,
|
|
554
|
+
expectedTotalDepositAmountUi: totalDepositAmountUi,
|
|
555
|
+
});
|
|
556
|
+
return {
|
|
557
|
+
borrowAmount,
|
|
558
|
+
totalDepositAmountUi,
|
|
559
|
+
debtBankData,
|
|
560
|
+
collateralBankData,
|
|
561
|
+
};
|
|
522
562
|
}
|
|
523
563
|
async withdrawLeverage(clendGroup, clendAccount, collateralMint, debtMint, withdrawAmount, withdrawAll, slippageBps) {
|
|
564
|
+
const params = await this.getWithdrawLeverageParams(clendGroup, clendAccount, collateralMint, debtMint, withdrawAmount, withdrawAll);
|
|
565
|
+
const { ixns, luts } = await this.getWithdrawLeverageIxns(clendGroup, clendAccount, collateralMint, debtMint, params.collateralBankData.mintDecimals, params.debtBankData.mintDecimals, withdrawAll, params.debtToRepay, params.collateralToWithdraw, slippageBps, 0);
|
|
566
|
+
// Send transaction
|
|
567
|
+
return this.send(ixns, [], luts);
|
|
568
|
+
}
|
|
569
|
+
async getWithdrawLeverageIxns(clendGroup, clendAccount, collateralMint, debtMint, collateralDecimals, debtDecimals, withdrawAll, debtToRepay, collateralToWithdraw, slippageBps, additionalIxnCount) {
|
|
570
|
+
const debtBank = (0, addresses_1.getBankPda)(clendGroup, debtMint);
|
|
571
|
+
const collateralBank = (0, addresses_1.getBankPda)(clendGroup, collateralMint);
|
|
572
|
+
// Get remaining accounts for flash loan
|
|
573
|
+
const clendAccountData = await this.getClendAccount(clendAccount);
|
|
574
|
+
if (!clendAccountData) {
|
|
575
|
+
throw new Error(`Clend account not found: ${clendAccount.toString()}`);
|
|
576
|
+
}
|
|
577
|
+
let activeBanks = (0, utils_1.getClendAccountActiveBanks)(clendAccountData);
|
|
578
|
+
// If withdrawAll is true, remove both banks from activeBanks
|
|
579
|
+
if (withdrawAll) {
|
|
580
|
+
activeBanks = activeBanks.filter((b) => !b.equals(collateralBank) && !b.equals(debtBank));
|
|
581
|
+
}
|
|
582
|
+
const activeBankData = [];
|
|
583
|
+
for (const bank of activeBanks) {
|
|
584
|
+
const bankData = await this.getBank(bank);
|
|
585
|
+
activeBankData.push(bankData);
|
|
586
|
+
}
|
|
587
|
+
const remainingAccounts = (0, utils_1.getClendAccountRemainingAccounts)(activeBankData);
|
|
588
|
+
// Get Jupiter quote for swapping collateral to debt token (ExactOut mode)
|
|
589
|
+
const swapMode = api_1.SwapMode.ExactOut;
|
|
590
|
+
const swapQuote = await this.getJupiterQuote(collateralMint, debtMint, collateralDecimals, debtDecimals, new decimal_js_1.default(debtToRepay.toString()), slippageBps, swapMode);
|
|
591
|
+
utils_1.logger.debug("withdrawLeverage swapQuote", {
|
|
592
|
+
swapQuote,
|
|
593
|
+
});
|
|
594
|
+
// Calculate how much collateral we need to swap for debt repayment
|
|
595
|
+
const collateralToSwap = new anchor_1.BN(swapQuote.quoteResponse.inAmount);
|
|
596
|
+
utils_1.logger.debug("withdrawLeverage collateralToSwap", {
|
|
597
|
+
collateralToSwap: collateralToSwap.toString(),
|
|
598
|
+
});
|
|
599
|
+
// Get swap instructions
|
|
600
|
+
const swapInputs = {
|
|
601
|
+
inputAmountLamports: new decimal_js_1.default(collateralToSwap.toString()),
|
|
602
|
+
inputMint: collateralMint,
|
|
603
|
+
outputMint: debtMint,
|
|
604
|
+
};
|
|
605
|
+
const swapIxs = await this.getJupiterSwap(clendAccountData.authority, swapInputs, swapQuote);
|
|
606
|
+
const collateralTokenProgram = (0, utils_1.getTokenProgramForMint)(collateralMint);
|
|
607
|
+
const debtTokenProgram = (0, utils_1.getTokenProgramForMint)(debtMint);
|
|
608
|
+
const userCollateralAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralMint, clendAccountData.authority, true, collateralTokenProgram);
|
|
609
|
+
const userDebtAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtMint, clendAccountData.authority, true, debtTokenProgram);
|
|
610
|
+
// Create withdraw instruction
|
|
611
|
+
const withdrawIx = await this.instructions.withdraw(clendGroup, clendAccount, clendAccountData.authority, collateralBank, userCollateralAta, collateralTokenProgram, collateralToWithdraw, withdrawAll, remainingAccounts);
|
|
612
|
+
// Create repay instruction
|
|
613
|
+
const repayIx = await this.instructions.repay(clendGroup, clendAccount, clendAccountData.authority, debtBank, userDebtAta, debtTokenProgram, debtToRepay, withdrawAll, remainingAccounts);
|
|
614
|
+
// Assemble all instructions without flash loan
|
|
615
|
+
const ixnsWithoutFlashLoan = [
|
|
616
|
+
withdrawIx,
|
|
617
|
+
...swapIxs.swapIxs,
|
|
618
|
+
repayIx,
|
|
619
|
+
];
|
|
620
|
+
// Create flash loan instructions
|
|
621
|
+
const cuIxns = 2;
|
|
622
|
+
const endIndex = new anchor_1.BN(cuIxns + ixnsWithoutFlashLoan.length + 1 + additionalIxnCount);
|
|
623
|
+
const { beginFlashLoanIx, endFlashLoanIx } = await this.instructions.createFlashLoanInstructions(clendAccount, clendAccountData.authority, endIndex, remainingAccounts);
|
|
624
|
+
// Assemble all instructions in the correct order
|
|
625
|
+
const instructions = [
|
|
626
|
+
beginFlashLoanIx,
|
|
627
|
+
...ixnsWithoutFlashLoan,
|
|
628
|
+
endFlashLoanIx,
|
|
629
|
+
];
|
|
630
|
+
return { ixns: instructions, luts: swapIxs.lookupTables };
|
|
631
|
+
}
|
|
632
|
+
async getWithdrawLeverageParams(clendGroup, clendAccount, collateralMint, debtMint, withdrawAmount, withdrawAll) {
|
|
524
633
|
utils_1.logger.info("withdrawLeverage params", {
|
|
525
634
|
withdrawAmount: withdrawAmount.toString(),
|
|
526
635
|
withdrawAll,
|
|
527
|
-
slippageBps,
|
|
528
636
|
});
|
|
529
|
-
// Get token programs
|
|
530
|
-
const collateralTokenProgram = (0, utils_1.getTokenProgramForMint)(collateralMint);
|
|
531
|
-
const debtTokenProgram = (0, utils_1.getTokenProgramForMint)(debtMint);
|
|
532
637
|
// Get bank data
|
|
533
638
|
const collateralBank = (0, addresses_1.getBankPda)(clendGroup, collateralMint);
|
|
534
639
|
const debtBank = (0, addresses_1.getBankPda)(clendGroup, debtMint);
|
|
@@ -537,9 +642,6 @@ class ClendClient {
|
|
|
537
642
|
// Get decimals
|
|
538
643
|
const collateralDecimals = collateralBankData.mintDecimals;
|
|
539
644
|
const debtDecimals = debtBankData.mintDecimals;
|
|
540
|
-
// Derive token accounts
|
|
541
|
-
const userCollateralAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralMint, this.address(), true, collateralTokenProgram);
|
|
542
|
-
const userDebtAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtMint, this.address(), true, debtTokenProgram);
|
|
543
645
|
// Get account data
|
|
544
646
|
const clendAccountData = await this.getClendAccount(clendAccount);
|
|
545
647
|
if (!clendAccountData) {
|
|
@@ -599,7 +701,10 @@ class ClendClient {
|
|
|
599
701
|
debtMaintWeight,
|
|
600
702
|
});
|
|
601
703
|
// Calculate leverage-preserving withdrawal
|
|
602
|
-
const { debtToRepayUi, newCollateralUi, newDebtUi, currentLeverage, newLeverage, } = (0, math_2.computeWithdrawLeverageAmounts)(collateralAmountUI, debtAmountUI, withdrawAmountUI, collateralPrice, debtPrice,
|
|
704
|
+
const { debtToRepayUi, newCollateralUi, newDebtUi, currentLeverage, newLeverage, } = (0, math_2.computeWithdrawLeverageAmounts)(collateralAmountUI, debtAmountUI, withdrawAmountUI, collateralPrice, debtPrice,
|
|
705
|
+
//collateralMaintWeight,
|
|
706
|
+
//debtMaintWeight,
|
|
707
|
+
1, 1);
|
|
603
708
|
utils_1.logger.debug("withdrawLeverage params calculated", {
|
|
604
709
|
debtToRepayUi,
|
|
605
710
|
newCollateralUi,
|
|
@@ -617,71 +722,25 @@ class ClendClient {
|
|
|
617
722
|
collateralToWithdraw: collateralToWithdraw.toString(),
|
|
618
723
|
debtToRepay: debtToRepay.toString(),
|
|
619
724
|
});
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
}
|
|
626
|
-
const activeBankData = [];
|
|
627
|
-
for (const bank of activeBanks) {
|
|
628
|
-
const bankData = await this.getBank(bank);
|
|
629
|
-
activeBankData.push(bankData);
|
|
630
|
-
}
|
|
631
|
-
const remainingAccounts = (0, utils_1.getClendAccountRemainingAccounts)(activeBankData);
|
|
632
|
-
// Get Jupiter quote for swapping collateral to debt token (ExactOut mode)
|
|
633
|
-
const swapMode = api_1.SwapMode.ExactOut;
|
|
634
|
-
const swapQuote = await this.getJupiterQuote(collateralMint, debtMint, collateralDecimals, debtDecimals, new decimal_js_1.default(debtToRepay.toString()), slippageBps, swapMode);
|
|
635
|
-
utils_1.logger.debug("withdrawLeverage swapQuote", {
|
|
636
|
-
swapQuote,
|
|
637
|
-
});
|
|
638
|
-
// Calculate how much collateral we need to swap for debt repayment
|
|
639
|
-
const collateralToSwap = new anchor_1.BN(swapQuote.quoteResponse.inAmount);
|
|
640
|
-
utils_1.logger.debug("withdrawLeverage collateralToSwap", {
|
|
641
|
-
collateralToSwap: collateralToSwap.toString(),
|
|
642
|
-
});
|
|
643
|
-
// Get swap instructions
|
|
644
|
-
const swapInputs = {
|
|
645
|
-
inputAmountLamports: new decimal_js_1.default(collateralToSwap.toString()),
|
|
646
|
-
inputMint: collateralMint,
|
|
647
|
-
outputMint: debtMint,
|
|
725
|
+
return {
|
|
726
|
+
debtToRepay,
|
|
727
|
+
collateralToWithdraw,
|
|
728
|
+
collateralBankData,
|
|
729
|
+
debtBankData,
|
|
648
730
|
};
|
|
649
|
-
const swapIxs = await this.getJupiterSwap(this.address(), swapInputs, swapQuote);
|
|
650
|
-
// Create withdraw instruction
|
|
651
|
-
const withdrawIx = await this.instructions.withdraw(clendGroup, clendAccount, this.address(), collateralBank, userCollateralAta, collateralTokenProgram, collateralToWithdraw, withdrawAll, remainingAccounts);
|
|
652
|
-
// Create repay instruction
|
|
653
|
-
const repayIx = await this.instructions.repay(clendGroup, clendAccount, this.address(), debtBank, userDebtAta, debtTokenProgram, debtToRepay, withdrawAll, remainingAccounts);
|
|
654
|
-
// Assemble all instructions without flash loan
|
|
655
|
-
const ixnsWithoutFlashLoan = [
|
|
656
|
-
withdrawIx,
|
|
657
|
-
...swapIxs.swapIxs,
|
|
658
|
-
repayIx,
|
|
659
|
-
];
|
|
660
|
-
// Create flash loan instructions
|
|
661
|
-
const cuIxns = 2;
|
|
662
|
-
const endIndex = new anchor_1.BN(cuIxns + ixnsWithoutFlashLoan.length + 1);
|
|
663
|
-
const { beginFlashLoanIx, endFlashLoanIx } = await this.instructions.createFlashLoanInstructions(clendAccount, this.address(), endIndex, remainingAccounts);
|
|
664
|
-
// Assemble all instructions in the correct order
|
|
665
|
-
const instructions = [
|
|
666
|
-
beginFlashLoanIx,
|
|
667
|
-
...ixnsWithoutFlashLoan,
|
|
668
|
-
endFlashLoanIx,
|
|
669
|
-
];
|
|
670
|
-
// Send transaction
|
|
671
|
-
return this.send(instructions, [], swapIxs.lookupTables);
|
|
672
731
|
}
|
|
673
732
|
async adjustLeverage(clendGroup, clendAccount, collateralMint, debtMint, targetLeverage, slippageBps) {
|
|
674
|
-
|
|
675
|
-
const
|
|
676
|
-
|
|
733
|
+
const params = await this.getAdjustLeverageParams(clendGroup, clendAccount, collateralMint, debtMint, targetLeverage);
|
|
734
|
+
const { ixns, luts } = await this.getAdjustLeverageIxns(clendGroup, clendAccount, this.address(), params.debtBankData, params.collateralBankData, params.isIncrease, params.debtDeltaUi, slippageBps, 0);
|
|
735
|
+
// Send transaction
|
|
736
|
+
return this.send(ixns, [], luts);
|
|
737
|
+
}
|
|
738
|
+
async getAdjustLeverageParams(clendGroup, clendAccount, collateralMint, debtMint, targetLeverage) {
|
|
677
739
|
// Get bank data
|
|
678
740
|
const collateralBank = (0, addresses_1.getBankPda)(clendGroup, collateralMint);
|
|
679
741
|
const debtBank = (0, addresses_1.getBankPda)(clendGroup, debtMint);
|
|
680
742
|
const collateralBankData = await this.getBank(collateralBank);
|
|
681
743
|
const debtBankData = await this.getBank(debtBank);
|
|
682
|
-
// Derive token accounts
|
|
683
|
-
const userCollateralAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralMint, this.address(), true, collateralTokenProgram);
|
|
684
|
-
const userDebtAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtMint, this.address(), true, debtTokenProgram);
|
|
685
744
|
// Get account data to determine current positions
|
|
686
745
|
const clendAccountData = await this.getClendAccount(clendAccount);
|
|
687
746
|
if (!clendAccountData) {
|
|
@@ -701,9 +760,6 @@ class ClendClient {
|
|
|
701
760
|
if (!debtBalance || !collateralBalance) {
|
|
702
761
|
throw new Error("Could not find debt or collateral position in account");
|
|
703
762
|
}
|
|
704
|
-
// Calculate current leverage
|
|
705
|
-
const collateralDecimals = collateralBankData.mintDecimals;
|
|
706
|
-
const debtDecimals = debtBankData.mintDecimals;
|
|
707
763
|
// Get current prices from Jupiter
|
|
708
764
|
const collateralPrice = await this.getJupiterPrice(collateralMint);
|
|
709
765
|
const debtPrice = await this.getJupiterPrice(debtMint);
|
|
@@ -713,8 +769,15 @@ class ClendClient {
|
|
|
713
769
|
const collateralWeight = collateralBankData.config.assetWeightMaint;
|
|
714
770
|
const debtWeight = debtBankData.config.liabilityWeightMaint;
|
|
715
771
|
// Calculate current leverage and values
|
|
716
|
-
const currentLeverage = (0, math_1.calculateWeightedLeverage)(collateralAmountUI, collateralPrice,
|
|
717
|
-
|
|
772
|
+
const currentLeverage = (0, math_1.calculateWeightedLeverage)(collateralAmountUI, collateralPrice,
|
|
773
|
+
//collateralWeight,
|
|
774
|
+
1, debtAmountUI, debtPrice,
|
|
775
|
+
//debtWeight,
|
|
776
|
+
1);
|
|
777
|
+
const { collateralDelta, debtDelta, isIncrease } = (0, math_1.computeAdjustLeverageAmounts)(collateralAmountUI, debtAmountUI, collateralPrice, debtPrice, currentLeverage, targetLeverage,
|
|
778
|
+
//collateralWeight,
|
|
779
|
+
//debtWeight,
|
|
780
|
+
1, 1);
|
|
718
781
|
utils_1.logger.info("adjustLeverage params calculated", {
|
|
719
782
|
collateralDelta,
|
|
720
783
|
debtDelta,
|
|
@@ -728,14 +791,31 @@ class ClendClient {
|
|
|
728
791
|
collateralWeight,
|
|
729
792
|
debtWeight,
|
|
730
793
|
});
|
|
794
|
+
return {
|
|
795
|
+
collateralDeltaUi: collateralDelta,
|
|
796
|
+
debtDeltaUi: debtDelta,
|
|
797
|
+
isIncrease,
|
|
798
|
+
collateralBankData,
|
|
799
|
+
debtBankData,
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
async getAdjustLeverageIxns(clendGroup, clendAccount, user, debtBankData, collateralBankData, isIncrease, debtDeltaUi, slippageBps, additionalIxnCount) {
|
|
803
|
+
const clendAccountData = await this.getClendAccount(clendAccount);
|
|
804
|
+
if (!clendAccountData) {
|
|
805
|
+
throw new Error(`Clend account not found: ${clendAccount.toString()}`);
|
|
806
|
+
}
|
|
807
|
+
const debtTokenProgram = (0, utils_1.getTokenProgramForMint)(debtBankData.mint);
|
|
808
|
+
const collateralTokenProgram = (0, utils_1.getTokenProgramForMint)(collateralBankData.mint);
|
|
809
|
+
const userDebtAta = (0, spl_token_1.getAssociatedTokenAddressSync)(debtBankData.mint, user, true, debtTokenProgram);
|
|
810
|
+
const userCollateralAta = (0, spl_token_1.getAssociatedTokenAddressSync)(collateralBankData.mint, user, true, collateralTokenProgram);
|
|
731
811
|
// Get remaining accounts for flash loan
|
|
732
812
|
let activeBanks = (0, utils_1.getClendAccountActiveBanks)(clendAccountData);
|
|
733
813
|
// Add both banks to active banks if not already present
|
|
734
|
-
if (!activeBanks.some((bank) => bank.equals(
|
|
735
|
-
activeBanks.push(
|
|
814
|
+
if (!activeBanks.some((bank) => bank.equals(debtBankData.key))) {
|
|
815
|
+
activeBanks.push(debtBankData.key);
|
|
736
816
|
}
|
|
737
|
-
if (!activeBanks.some((bank) => bank.equals(
|
|
738
|
-
activeBanks.push(
|
|
817
|
+
if (!activeBanks.some((bank) => bank.equals(collateralBankData.key))) {
|
|
818
|
+
activeBanks.push(collateralBankData.key);
|
|
739
819
|
}
|
|
740
820
|
const activeBankData = [];
|
|
741
821
|
for (const bank of activeBanks) {
|
|
@@ -748,16 +828,13 @@ class ClendClient {
|
|
|
748
828
|
let swapLookupTables = [];
|
|
749
829
|
if (isIncrease) {
|
|
750
830
|
utils_1.logger.info("increasing leverage");
|
|
751
|
-
|
|
752
|
-
const additionalDebtAmountUI = debtDelta * debtPrice;
|
|
753
|
-
const additionalDebtAmount = (0, math_1.uiToAmount)(additionalDebtAmountUI, debtDecimals);
|
|
831
|
+
const additionalDebtAmount = (0, math_1.uiToAmount)(debtDeltaUi, debtBankData.mintDecimals);
|
|
754
832
|
utils_1.logger.debug("additionalDebtAmount calculated", {
|
|
755
|
-
additionalDebtAmountUI,
|
|
756
833
|
additionalDebtAmount: additionalDebtAmount.toString(),
|
|
757
834
|
});
|
|
758
835
|
// Get Jupiter quote for swapping debt token to collateral token
|
|
759
836
|
const swapMode = api_1.SwapMode.ExactIn;
|
|
760
|
-
const swapQuote = await this.getJupiterQuote(
|
|
837
|
+
const swapQuote = await this.getJupiterQuote(debtBankData.mint, collateralBankData.mint, debtBankData.mintDecimals, collateralBankData.mintDecimals, new decimal_js_1.default(additionalDebtAmount.toString()), slippageBps, swapMode);
|
|
761
838
|
utils_1.logger.debug("swapQuote calculated", {
|
|
762
839
|
inAmount: swapQuote.quoteResponse.inAmount,
|
|
763
840
|
outAmount: swapQuote.quoteResponse.outAmount,
|
|
@@ -766,29 +843,31 @@ class ClendClient {
|
|
|
766
843
|
// Get swap instructions
|
|
767
844
|
const swapInputs = {
|
|
768
845
|
inputAmountLamports: new decimal_js_1.default(additionalDebtAmount.toString()),
|
|
769
|
-
inputMint:
|
|
770
|
-
outputMint:
|
|
846
|
+
inputMint: debtBankData.mint,
|
|
847
|
+
outputMint: collateralBankData.mint,
|
|
771
848
|
};
|
|
772
|
-
const swapIxs = await this.getJupiterSwap(
|
|
849
|
+
const swapIxs = await this.getJupiterSwap(user, swapInputs, swapQuote);
|
|
773
850
|
// Borrow more debt
|
|
774
|
-
const borrowIx = await this.instructions.borrow(clendGroup, clendAccount,
|
|
851
|
+
const borrowIx = await this.instructions.borrow(clendGroup, clendAccount, user, debtBankData.key, userDebtAta, debtTokenProgram, additionalDebtAmount, remainingAccounts);
|
|
775
852
|
// Calculate additional collateral from swap
|
|
776
853
|
const additionalCollateralAmount = new anchor_1.BN(swapQuote.quoteResponse.outAmount);
|
|
777
854
|
utils_1.logger.debug("additionalCollateralAmount calculated", {
|
|
778
855
|
additionalCollateralAmount: additionalCollateralAmount.toString(),
|
|
779
856
|
});
|
|
780
857
|
// Deposit additional collateral
|
|
781
|
-
const depositIx = await this.instructions.deposit(clendGroup, clendAccount,
|
|
858
|
+
const depositIx = await this.instructions.deposit(clendGroup, clendAccount, user, collateralBankData.key, userCollateralAta, collateralTokenProgram, additionalCollateralAmount, remainingAccounts);
|
|
782
859
|
instructions.push(borrowIx, ...swapIxs.swapIxs, depositIx);
|
|
783
860
|
swapLookupTables = swapIxs.lookupTables;
|
|
784
861
|
}
|
|
785
862
|
else {
|
|
786
|
-
utils_1.logger.info("decreasing leverage");
|
|
787
863
|
// Calculate amounts for decreasing leverage
|
|
788
|
-
const debtToReduceAmount = (0, math_1.uiToAmount)(
|
|
864
|
+
const debtToReduceAmount = (0, math_1.uiToAmount)(debtDeltaUi, debtBankData.mintDecimals);
|
|
865
|
+
utils_1.logger.info("decreasing leverage", {
|
|
866
|
+
debtToReduceAmount: debtToReduceAmount.toString(),
|
|
867
|
+
});
|
|
789
868
|
// Get Jupiter quote for swapping collateral token to debt token with ExactOut
|
|
790
869
|
const swapMode = api_1.SwapMode.ExactOut;
|
|
791
|
-
const swapQuote = await this.getJupiterQuote(
|
|
870
|
+
const swapQuote = await this.getJupiterQuote(collateralBankData.mint, debtBankData.mint, collateralBankData.mintDecimals, debtBankData.mintDecimals, new decimal_js_1.default(debtToReduceAmount.toString()), slippageBps, swapMode);
|
|
792
871
|
utils_1.logger.debug("swapQuote calculated", {
|
|
793
872
|
inAmount: swapQuote.quoteResponse.inAmount,
|
|
794
873
|
outAmount: swapQuote.quoteResponse.outAmount,
|
|
@@ -803,32 +882,31 @@ class ClendClient {
|
|
|
803
882
|
// Get swap instructions
|
|
804
883
|
const swapInputs = {
|
|
805
884
|
inputAmountLamports: new decimal_js_1.default(collateralToSwap.toString()),
|
|
806
|
-
inputMint:
|
|
807
|
-
outputMint:
|
|
885
|
+
inputMint: collateralBankData.mint,
|
|
886
|
+
outputMint: debtBankData.mint,
|
|
808
887
|
};
|
|
809
|
-
const swapIxs = await this.getJupiterSwap(
|
|
888
|
+
const swapIxs = await this.getJupiterSwap(user, swapInputs, swapQuote);
|
|
810
889
|
// Withdraw collateral
|
|
811
|
-
const withdrawIx = await this.instructions.withdraw(clendGroup, clendAccount,
|
|
890
|
+
const withdrawIx = await this.instructions.withdraw(clendGroup, clendAccount, user, collateralBankData.key, userCollateralAta, collateralTokenProgram, collateralToSwap, false, remainingAccounts);
|
|
812
891
|
// Repay debt
|
|
813
|
-
const repayAmount = (0, math_1.uiToAmount)(
|
|
892
|
+
const repayAmount = (0, math_1.uiToAmount)(debtDeltaUi, debtBankData.mintDecimals);
|
|
814
893
|
utils_1.logger.debug("repayAmount calculated", {
|
|
815
894
|
repayAmount: repayAmount.toString(),
|
|
816
895
|
});
|
|
817
|
-
const repayIx = await this.instructions.repay(clendGroup, clendAccount,
|
|
896
|
+
const repayIx = await this.instructions.repay(clendGroup, clendAccount, user, debtBankData.key, userDebtAta, debtTokenProgram, repayAmount, false, remainingAccounts);
|
|
818
897
|
instructions.push(withdrawIx, ...swapIxs.swapIxs, repayIx);
|
|
819
898
|
swapLookupTables = swapIxs.lookupTables;
|
|
820
899
|
}
|
|
821
900
|
const cuIxns = 2;
|
|
822
|
-
const endIndex = new anchor_1.BN(cuIxns + instructions.length + 1);
|
|
823
|
-
const { beginFlashLoanIx, endFlashLoanIx } = await this.instructions.createFlashLoanInstructions(clendAccount,
|
|
901
|
+
const endIndex = new anchor_1.BN(cuIxns + instructions.length + 1 + additionalIxnCount);
|
|
902
|
+
const { beginFlashLoanIx, endFlashLoanIx } = await this.instructions.createFlashLoanInstructions(clendAccount, user, endIndex, remainingAccounts);
|
|
824
903
|
// Create a new array with the flash loan instructions wrapping the existing instructions
|
|
825
904
|
const finalInstructions = [
|
|
826
905
|
beginFlashLoanIx,
|
|
827
906
|
...instructions,
|
|
828
907
|
endFlashLoanIx,
|
|
829
908
|
];
|
|
830
|
-
|
|
831
|
-
return this.send(finalInstructions, [], swapLookupTables);
|
|
909
|
+
return { ixns: finalInstructions, luts: swapLookupTables };
|
|
832
910
|
}
|
|
833
911
|
async getEvents(txSigs) {
|
|
834
912
|
const events = [];
|
|
@@ -856,6 +934,36 @@ class ClendClient {
|
|
|
856
934
|
}
|
|
857
935
|
return events;
|
|
858
936
|
}
|
|
937
|
+
async getAccountsForAuthority(group, authority) {
|
|
938
|
+
const accounts = await this.instructions.program.account.clendAccount.all([
|
|
939
|
+
{
|
|
940
|
+
memcmp: {
|
|
941
|
+
bytes: group.toBase58(),
|
|
942
|
+
offset: 8, // clendGroup is the first field in the account, so only offset is the discriminant
|
|
943
|
+
},
|
|
944
|
+
},
|
|
945
|
+
{
|
|
946
|
+
memcmp: {
|
|
947
|
+
bytes: authority.toBase58(),
|
|
948
|
+
offset: 8 + 32, // authority is the second field in the account after the authority, so offset by the discriminant and a pubkey
|
|
949
|
+
},
|
|
950
|
+
},
|
|
951
|
+
]);
|
|
952
|
+
const addrs = accounts.map((account) => account.publicKey);
|
|
953
|
+
return addrs;
|
|
954
|
+
}
|
|
955
|
+
async getAccountsForGroup(group) {
|
|
956
|
+
const accounts = await this.instructions.program.account.clendAccount.all([
|
|
957
|
+
{
|
|
958
|
+
memcmp: {
|
|
959
|
+
bytes: group.toBase58(),
|
|
960
|
+
offset: 8, // clendGroup is the first field in the account, so only offset is the discriminant
|
|
961
|
+
},
|
|
962
|
+
},
|
|
963
|
+
]);
|
|
964
|
+
const addrs = accounts.map((account) => account.publicKey);
|
|
965
|
+
return addrs;
|
|
966
|
+
}
|
|
859
967
|
}
|
|
860
968
|
exports.ClendClient = ClendClient;
|
|
861
969
|
//# sourceMappingURL=rpc.js.map
|