@0dotxyz/p0-ts-sdk 2.3.0-alpha.0 → 2.3.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -44720,6 +44720,52 @@ function deserializeTitanWireInstruction(ix) {
44720
44720
  data: Buffer.from(ix.d)
44721
44721
  });
44722
44722
  }
44723
+ var EXPONENT_CORE_PROGRAM_ID = new web3_js.PublicKey(
44724
+ "XP1BRLn8eCYSygrd8er5P4GKdzqKbC3DLoSsS5UYVZy"
44725
+ );
44726
+ new web3_js.PublicKey(
44727
+ "XPC1MM4dYACDfykNuXYZ5una2DsMDWL24CrYubCvarC"
44728
+ );
44729
+ new web3_js.PublicKey(
44730
+ "ExponentnaRg3CQbW6dqQNZKXp7gtZ9DGMp1cwC4HAS7"
44731
+ );
44732
+ var EXPONENT_EVENT_AUTHORITY_SEED = "__event_authority";
44733
+ function deriveExponentEventAuthority() {
44734
+ return web3_js.PublicKey.findProgramAddressSync(
44735
+ [Buffer.from(EXPONENT_EVENT_AUTHORITY_SEED)],
44736
+ EXPONENT_CORE_PROGRAM_ID
44737
+ )[0];
44738
+ }
44739
+ var MERGE_DISCRIMINATOR = Buffer.from([5]);
44740
+ function makeExponentMergeIx(accounts, amountNative) {
44741
+ const tokenProgram = accounts.tokenProgram ?? TOKEN_PROGRAM_ID;
44742
+ const eventAuthority = deriveExponentEventAuthority();
44743
+ const data = Buffer.alloc(MERGE_DISCRIMINATOR.length + 8);
44744
+ MERGE_DISCRIMINATOR.copy(data, 0);
44745
+ data.writeBigUInt64LE(amountNative, MERGE_DISCRIMINATOR.length);
44746
+ const keys = [
44747
+ { pubkey: accounts.owner, isSigner: true, isWritable: true },
44748
+ { pubkey: accounts.authority, isSigner: false, isWritable: true },
44749
+ { pubkey: accounts.vault, isSigner: false, isWritable: true },
44750
+ { pubkey: accounts.sySrcDstAta, isSigner: false, isWritable: true },
44751
+ { pubkey: accounts.escrowSy, isSigner: false, isWritable: true },
44752
+ { pubkey: accounts.ytSrcAta, isSigner: false, isWritable: true },
44753
+ { pubkey: accounts.ptSrcAta, isSigner: false, isWritable: true },
44754
+ { pubkey: accounts.mintYt, isSigner: false, isWritable: true },
44755
+ { pubkey: accounts.mintPt, isSigner: false, isWritable: true },
44756
+ { pubkey: tokenProgram, isSigner: false, isWritable: false },
44757
+ { pubkey: accounts.syProgram, isSigner: false, isWritable: false },
44758
+ { pubkey: accounts.addressLookupTable, isSigner: false, isWritable: false },
44759
+ { pubkey: accounts.yieldPosition, isSigner: false, isWritable: true },
44760
+ { pubkey: eventAuthority, isSigner: false, isWritable: false },
44761
+ { pubkey: EXPONENT_CORE_PROGRAM_ID, isSigner: false, isWritable: false }
44762
+ ];
44763
+ return new web3_js.TransactionInstruction({
44764
+ keys,
44765
+ programId: EXPONENT_CORE_PROGRAM_ID,
44766
+ data
44767
+ });
44768
+ }
44723
44769
 
44724
44770
  // src/vendor/klend/utils/klend/interest-rate.utils.ts
44725
44771
  function getKaminoTotalSupply(reserve) {
@@ -50349,6 +50395,249 @@ async function buildSwapDebtFlashloanTx({
50349
50395
  repayIxs
50350
50396
  };
50351
50397
  }
50398
+ async function makeRollPtTx(params) {
50399
+ const {
50400
+ program,
50401
+ marginfiAccount,
50402
+ connection,
50403
+ bankMap,
50404
+ oraclePrices,
50405
+ withdrawOpts,
50406
+ depositOpts,
50407
+ mergeAccounts,
50408
+ underlying,
50409
+ assetShareValueMultiplierByBank,
50410
+ addressLookupTableAccounts,
50411
+ crossbarUrl
50412
+ } = params;
50413
+ const blockhash = (await connection.getLatestBlockhash("confirmed")).blockhash;
50414
+ const setupIxs = await makeSetupIx({
50415
+ connection,
50416
+ authority: marginfiAccount.authority,
50417
+ tokens: [
50418
+ { mint: withdrawOpts.withdrawBank.mint, tokenProgram: withdrawOpts.tokenProgram },
50419
+ { mint: underlying.mint, tokenProgram: underlying.tokenProgram ?? TOKEN_PROGRAM_ID },
50420
+ { mint: mergeAccounts.mintYt, tokenProgram: TOKEN_PROGRAM_ID },
50421
+ { mint: depositOpts.depositBank.mint, tokenProgram: depositOpts.tokenProgram }
50422
+ ]
50423
+ });
50424
+ const { flashloanTx, setupInstructions, swapQuote, withdrawIxs, depositIxs } = await buildRollPtFlashloanTx({
50425
+ ...params,
50426
+ blockhash
50427
+ });
50428
+ const engineSetupInstructions = setupInstructions.filter((ix) => {
50429
+ if (ix.programId.equals(web3_js.ComputeBudgetProgram.programId)) return false;
50430
+ if (ix.programId.equals(ASSOCIATED_TOKEN_PROGRAM_ID)) {
50431
+ const mintKey = ix.keys[3]?.pubkey;
50432
+ if (mintKey?.equals(withdrawOpts.withdrawBank.mint) || mintKey?.equals(underlying.mint) || mintKey?.equals(mergeAccounts.mintYt) || mintKey?.equals(depositOpts.depositBank.mint)) {
50433
+ return false;
50434
+ }
50435
+ }
50436
+ return true;
50437
+ });
50438
+ setupIxs.push(...engineSetupInstructions);
50439
+ const { instructions: updateFeedIxs, luts: feedLuts } = await makeSmartCrankSwbFeedIx({
50440
+ marginfiAccount,
50441
+ bankMap,
50442
+ oraclePrices,
50443
+ assetShareValueMultiplierByBank,
50444
+ instructions: [...withdrawIxs.instructions, ...depositIxs.instructions],
50445
+ program,
50446
+ connection,
50447
+ crossbarUrl
50448
+ });
50449
+ const additionalTxs = [];
50450
+ if (setupIxs.length > 0) {
50451
+ const txs = splitInstructionsToFitTransactions([], setupIxs, {
50452
+ blockhash,
50453
+ payerKey: marginfiAccount.authority,
50454
+ luts: addressLookupTableAccounts ?? []
50455
+ });
50456
+ additionalTxs.push(
50457
+ ...txs.map(
50458
+ (tx) => addTransactionMetadata(tx, {
50459
+ type: "CREATE_ATA" /* CREATE_ATA */,
50460
+ addressLookupTables: addressLookupTableAccounts
50461
+ })
50462
+ )
50463
+ );
50464
+ }
50465
+ if (updateFeedIxs.length > 0) {
50466
+ const message = new web3_js.TransactionMessage({
50467
+ payerKey: marginfiAccount.authority,
50468
+ recentBlockhash: blockhash,
50469
+ instructions: updateFeedIxs
50470
+ }).compileToV0Message(feedLuts);
50471
+ additionalTxs.push(
50472
+ addTransactionMetadata(new web3_js.VersionedTransaction(message), {
50473
+ addressLookupTables: feedLuts,
50474
+ type: "CRANK" /* CRANK */
50475
+ })
50476
+ );
50477
+ }
50478
+ const transactions = [...additionalTxs, flashloanTx];
50479
+ return {
50480
+ transactions,
50481
+ actionTxIndex: transactions.length - 1,
50482
+ quoteResponse: swapQuote
50483
+ };
50484
+ }
50485
+ async function buildRollPtFlashloanTx({
50486
+ program,
50487
+ marginfiAccount,
50488
+ bankMap,
50489
+ withdrawOpts,
50490
+ depositOpts,
50491
+ mergeAccounts,
50492
+ underlying,
50493
+ redeemedAmountNative,
50494
+ swapOpts,
50495
+ bankMetadataMap,
50496
+ addressLookupTableAccounts,
50497
+ connection,
50498
+ overrideInferAccounts,
50499
+ blockhash,
50500
+ swapEngineRunner
50501
+ }) {
50502
+ const { withdrawBank, tokenProgram: withdrawTokenProgram, totalPositionAmount } = withdrawOpts;
50503
+ const { depositBank, tokenProgram: depositTokenProgram } = depositOpts;
50504
+ const authority = marginfiAccount.authority;
50505
+ if (withdrawOpts.withdrawAmount !== void 0 && withdrawOpts.withdrawAmount <= 0) {
50506
+ throw new Error("withdrawAmount must be greater than 0");
50507
+ }
50508
+ const actualWithdrawAmount = Math.min(
50509
+ withdrawOpts.withdrawAmount ?? totalPositionAmount,
50510
+ totalPositionAmount
50511
+ );
50512
+ const isFullWithdraw = isWholePosition(
50513
+ { amount: totalPositionAmount, isLending: true },
50514
+ actualWithdrawAmount,
50515
+ withdrawBank.mintDecimals
50516
+ );
50517
+ const cuRequestIxs = [
50518
+ web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 12e5 }),
50519
+ web3_js.ComputeBudgetProgram.setComputeUnitPrice({ microLamports: 1 })
50520
+ ];
50521
+ const withdrawIxs = await makeWithdrawIx3({
50522
+ program,
50523
+ bank: withdrawBank,
50524
+ bankMap,
50525
+ tokenProgram: withdrawTokenProgram,
50526
+ amount: actualWithdrawAmount,
50527
+ marginfiAccount,
50528
+ authority,
50529
+ withdrawAll: isFullWithdraw,
50530
+ isSync: false,
50531
+ opts: { createAtas: false, wrapAndUnwrapSol: false, overrideInferAccounts }
50532
+ });
50533
+ const mergeIx = makeExponentMergeIx(
50534
+ mergeAccounts,
50535
+ BigInt(uiToNative(actualWithdrawAmount, withdrawBank.mintDecimals).toString())
50536
+ );
50537
+ const depositIxs = await makeDepositIx3({
50538
+ program,
50539
+ bank: depositBank,
50540
+ tokenProgram: depositTokenProgram,
50541
+ amount: 0,
50542
+ accountAddress: marginfiAccount.address,
50543
+ authority,
50544
+ group: marginfiAccount.group,
50545
+ opts: { wrapAndUnwrapSol: false, overrideInferAccounts }
50546
+ });
50547
+ const destinationTokenAccount = getAssociatedTokenAddressSync(
50548
+ depositBank.mint,
50549
+ authority,
50550
+ true,
50551
+ depositTokenProgram.equals(TOKEN_2022_PROGRAM_ID) ? TOKEN_2022_PROGRAM_ID : void 0
50552
+ );
50553
+ const swapConstraints = await computeFlashloanSwapConstraints({
50554
+ program,
50555
+ marginfiAccount,
50556
+ bankMap,
50557
+ bankMetadataMap,
50558
+ addressLookupTableAccounts: addressLookupTableAccounts ?? [],
50559
+ primaryIx: { type: "withdraw", bank: withdrawBank, tokenProgram: withdrawTokenProgram },
50560
+ secondaryIx: { type: "deposit", bank: depositBank, tokenProgram: depositTokenProgram },
50561
+ overrideInferAccounts
50562
+ });
50563
+ const runEngine = swapEngineRunner ?? runSwapEngine;
50564
+ const engineResult = await runEngine({
50565
+ inputMint: underlying.mint.toBase58(),
50566
+ outputMint: depositBank.mint.toBase58(),
50567
+ amountNative: Number(redeemedAmountNative),
50568
+ inputDecimals: underlying.decimals,
50569
+ outputDecimals: depositBank.mintDecimals,
50570
+ ...swapEngineQuoteFieldsFromOpts(swapOpts),
50571
+ taker: authority,
50572
+ destinationTokenAccount,
50573
+ connection,
50574
+ footprint: {
50575
+ instructions: [
50576
+ ...cuRequestIxs,
50577
+ ...withdrawIxs.instructions,
50578
+ mergeIx,
50579
+ ...depositIxs.instructions
50580
+ ],
50581
+ luts: addressLookupTableAccounts ?? [],
50582
+ payer: authority,
50583
+ sizeConstraint: swapConstraints.sizeConstraint,
50584
+ maxSwapTotalAccounts: swapConstraints.maxSwapTotalAccounts
50585
+ },
50586
+ providers: swapEngineProvidersFromOpts(swapOpts)
50587
+ });
50588
+ const depositIxToPatch = depositIxs.instructions.find(isDepositIx);
50589
+ if (!depositIxToPatch) {
50590
+ throw new Error("roll-pt: could not locate deposit instruction for amount patching");
50591
+ }
50592
+ patchDepositAmount(depositIxToPatch, engineResult.outputAmountNative);
50593
+ const swapInstructions = engineResult.swapInstructions;
50594
+ const setupInstructions = engineResult.setupInstructions;
50595
+ const swapLookupTables = engineResult.swapLuts;
50596
+ const swapQuote = engineResult.quoteResponse;
50597
+ const luts = [...addressLookupTableAccounts ?? [], ...swapLookupTables];
50598
+ const allNonFlIxs = [
50599
+ ...cuRequestIxs,
50600
+ ...withdrawIxs.instructions,
50601
+ mergeIx,
50602
+ ...swapInstructions,
50603
+ ...depositIxs.instructions
50604
+ ];
50605
+ if (swapInstructions.length > 0) {
50606
+ compileFlashloanPrecheck({
50607
+ allIxs: allNonFlIxs,
50608
+ payer: authority,
50609
+ luts,
50610
+ sizeConstraint: swapConstraints.sizeConstraint,
50611
+ swapIxCount: swapInstructions.length,
50612
+ swapLutCount: swapLookupTables.length
50613
+ });
50614
+ }
50615
+ const flashloanTx = await makeFlashLoanTx({
50616
+ program,
50617
+ marginfiAccount,
50618
+ bankMap,
50619
+ addressLookupTableAccounts: luts,
50620
+ blockhash,
50621
+ ixs: allNonFlIxs,
50622
+ isSync: false
50623
+ });
50624
+ const txSize = getTxSize(flashloanTx);
50625
+ const totalKeys = getTotalAccountKeys(flashloanTx);
50626
+ if (txSize > MAX_TX_SIZE || totalKeys > MAX_ACCOUNT_LOCKS) {
50627
+ throw TransactionBuildingError.swapSizeExceededPositionSwap(
50628
+ txSize,
50629
+ totalKeys,
50630
+ swapOpts.swapConfig?.provider
50631
+ );
50632
+ }
50633
+ return {
50634
+ flashloanTx,
50635
+ setupInstructions,
50636
+ swapQuote,
50637
+ withdrawIxs,
50638
+ depositIxs
50639
+ };
50640
+ }
50352
50641
 
50353
50642
  // src/services/account/services/account-simulation.service.ts
50354
50643
  async function simulateAccountHealthCacheWithFallback(params) {
@@ -56689,6 +56978,7 @@ exports.makeRefreshKaminoBanksIxs = makeRefreshKaminoBanksIxs;
56689
56978
  exports.makeRepayIx = makeRepayIx3;
56690
56979
  exports.makeRepayTx = makeRepayTx;
56691
56980
  exports.makeRepayWithCollatTx = makeRepayWithCollatTx;
56981
+ exports.makeRollPtTx = makeRollPtTx;
56692
56982
  exports.makeSetupIx = makeSetupIx;
56693
56983
  exports.makeSmartCrankSwbFeedIx = makeSmartCrankSwbFeedIx;
56694
56984
  exports.makeSwapCollateralTx = makeSwapCollateralTx;