@0dotxyz/p0-ts-sdk 2.6.1 → 2.6.2

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
@@ -62703,11 +62703,11 @@ function deriveExponentClmmEventAuthority() {
62703
62703
  EXPONENT_CLMM_PROGRAM_ID
62704
62704
  )[0];
62705
62705
  }
62706
- var MERGE_DISCRIMINATOR = Buffer.from([5]);
62706
+ var MERGE_DISCRIMINATOR = buffer.Buffer.from([5]);
62707
62707
  function makeExponentMergeIx(accounts, amountNative) {
62708
62708
  const tokenProgram = accounts.tokenProgram ?? TOKEN_PROGRAM_ID;
62709
62709
  const eventAuthority = deriveExponentEventAuthority();
62710
- const data = Buffer.alloc(MERGE_DISCRIMINATOR.length + 8);
62710
+ const data = buffer.Buffer.alloc(MERGE_DISCRIMINATOR.length + 8);
62711
62711
  MERGE_DISCRIMINATOR.copy(data, 0);
62712
62712
  data.writeBigUInt64LE(amountNative, MERGE_DISCRIMINATOR.length);
62713
62713
  const keys = [
@@ -62736,14 +62736,14 @@ function makeExponentMergeIx(accounts, amountNative) {
62736
62736
  data
62737
62737
  });
62738
62738
  }
62739
- Buffer.from([17]);
62740
- Buffer.from([4]);
62741
- Buffer.from([39]);
62742
- Buffer.from([7]);
62743
- var CLMM_TRADE_PT_DISCRIMINATOR = Buffer.from([3]);
62739
+ buffer.Buffer.from([17]);
62740
+ buffer.Buffer.from([4]);
62741
+ buffer.Buffer.from([39]);
62742
+ buffer.Buffer.from([7]);
62743
+ var CLMM_TRADE_PT_DISCRIMINATOR = buffer.Buffer.from([3]);
62744
62744
  function encodeOptionU64(value) {
62745
- if (value === null) return Buffer.from([0]);
62746
- const buf = Buffer.alloc(9);
62745
+ if (value === null) return buffer.Buffer.from([0]);
62746
+ const buf = buffer.Buffer.alloc(9);
62747
62747
  buf.writeUInt8(1, 0);
62748
62748
  buf.writeBigUInt64LE(value, 1);
62749
62749
  return buf;
@@ -62762,14 +62762,14 @@ function exponentClmmBuyPtArgs({
62762
62762
  function makeExponentClmmTradePtIx(accounts, args) {
62763
62763
  const tokenProgram = accounts.tokenProgram ?? TOKEN_PROGRAM_ID;
62764
62764
  const eventAuthority = deriveExponentClmmEventAuthority();
62765
- const head = Buffer.alloc(CLMM_TRADE_PT_DISCRIMINATOR.length + 9);
62765
+ const head = buffer.Buffer.alloc(CLMM_TRADE_PT_DISCRIMINATOR.length + 9);
62766
62766
  CLMM_TRADE_PT_DISCRIMINATOR.copy(head, 0);
62767
62767
  head.writeBigUInt64LE(args.amountIn, CLMM_TRADE_PT_DISCRIMINATOR.length);
62768
62768
  head.writeUInt8(args.swapDirection, CLMM_TRADE_PT_DISCRIMINATOR.length + 8);
62769
- const data = Buffer.concat([
62769
+ const data = buffer.Buffer.concat([
62770
62770
  head,
62771
62771
  encodeOptionU64(args.amountOutConstraint),
62772
- Buffer.from([0])
62772
+ buffer.Buffer.from([0])
62773
62773
  ]);
62774
62774
  const keys = [
62775
62775
  { pubkey: accounts.trader, isSigner: true, isWritable: true },
@@ -75298,7 +75298,6 @@ async function tryBridgedCollateralSwap(params, bridgeOpts) {
75298
75298
  }
75299
75299
  var DEFAULT_ROLL_SLIPPAGE_BPS = 50;
75300
75300
  var TRADE_PT_EVENT_AMOUNT_OUT_OFFSET = 138;
75301
- var MERGE_EVENT_AMOUNT_SY_OUT_OFFSET = 296;
75302
75301
  async function makeRollPtTx(params) {
75303
75302
  const {
75304
75303
  program,
@@ -75417,6 +75416,7 @@ async function buildRollPtFlashloanTx({
75417
75416
  const { withdrawBank, tokenProgram: withdrawTokenProgram, totalPositionAmount, withdrawAmount } = withdrawOpts;
75418
75417
  const { depositBank, tokenProgram: depositTokenProgram } = depositOpts;
75419
75418
  const authority = marginfiAccount.authority;
75419
+ const simulateTx = params.simulateTx ?? defaultRollQuoteSimulator(connection);
75420
75420
  if (withdrawAmount !== void 0 && withdrawAmount <= 0) {
75421
75421
  throw new Error("withdrawAmount must be greater than 0");
75422
75422
  }
@@ -75470,24 +75470,13 @@ async function buildRollPtFlashloanTx({
75470
75470
  clmm.addressLookupTable
75471
75471
  ];
75472
75472
  }
75473
- const redeemIxs = [...setupIxs, ...cuRequestIxs, ...withdrawIxs.instructions, mergeIx];
75474
- const mergeReturn = await simulateRollReturn({
75475
- program,
75476
- marginfiAccount,
75477
- bankMap,
75478
- connection,
75479
- blockhash,
75480
- luts,
75481
- ixs: redeemIxs,
75482
- programId: EXPONENT_CORE_PROGRAM_ID,
75483
- label: "merge"
75484
- });
75485
- const syExact = readReturnU64(mergeReturn, MERGE_EVENT_AMOUNT_SY_OUT_OFFSET, "merge amount_sy_out");
75473
+ const syExact = merge.computeRedeemedAmountNative(withdrawNative);
75486
75474
  if (syExact <= 0n) {
75487
75475
  throw new Error("roll-pt: merge would redeem 0 SY (empty/invalid matured vault state)");
75488
75476
  }
75489
75477
  const exactPtOut = await quoteClmmTradeOut({
75490
75478
  connection,
75479
+ simulateTx,
75491
75480
  clmm,
75492
75481
  amountInSyNative: syExact,
75493
75482
  payer: authority
@@ -75550,62 +75539,40 @@ async function buildRollPtFlashloanTx({
75550
75539
  };
75551
75540
  return { flashloanTx, swapQuote, withdrawIxs, depositIxs };
75552
75541
  }
75553
- async function simulateRollReturn({
75554
- program,
75555
- marginfiAccount,
75556
- bankMap,
75557
- connection,
75558
- blockhash,
75559
- luts,
75560
- ixs,
75561
- programId,
75562
- label
75563
- }) {
75564
- const quoteTx = await makeFlashLoanTx({
75565
- program,
75566
- marginfiAccount,
75567
- bankMap,
75568
- addressLookupTableAccounts: luts,
75569
- blockhash,
75570
- ixs,
75571
- isSync: false
75572
- });
75573
- const sim = await connection.simulateTransaction(quoteTx, {
75574
- sigVerify: false,
75575
- replaceRecentBlockhash: true
75576
- });
75577
- const logs = sim.value.logs ?? [];
75578
- if (process.env.ROLL_DEBUG) {
75579
- console.error(
75580
- `[roll ${label} sim] err:`,
75581
- JSON.stringify(sim.value.err),
75582
- "returnData:",
75583
- JSON.stringify(sim.value.returnData),
75584
- "logsLen:",
75585
- logs.length,
75586
- "truncated:",
75587
- logs.some((l) => l.includes("truncated"))
75588
- );
75589
- }
75590
- const prefix = `Program return: ${programId.toBase58()} `;
75591
- const line = [...logs].reverse().find((l) => l.startsWith(prefix));
75592
- if (!line) {
75593
- throw new Error(
75594
- `roll-pt: could not read ${label} return data from quote simulation (err=${JSON.stringify(
75595
- sim.value.err
75596
- )})`
75597
- );
75598
- }
75599
- return Buffer.from(line.slice(prefix.length), "base64");
75542
+ function defaultRollQuoteSimulator(connection) {
75543
+ return async (tx) => {
75544
+ const sim = await connection.simulateTransaction(tx, {
75545
+ sigVerify: false,
75546
+ replaceRecentBlockhash: true
75547
+ });
75548
+ return {
75549
+ err: sim.value.err,
75550
+ logs: sim.value.logs,
75551
+ returnData: sim.value.returnData
75552
+ };
75553
+ };
75600
75554
  }
75601
- function readReturnU64(data, offset, what) {
75602
- if (data.length < offset + 8) {
75603
- throw new Error(`roll-pt: ${what} return data too short (${data.length} bytes)`);
75555
+ function tokenBalanceDelta(sim, mint, owner) {
75556
+ if (!sim.preTokenBalances && !sim.postTokenBalances) return null;
75557
+ const sum = (list) => (list ?? []).filter((b) => b.mint === mint && b.owner === owner).reduce((acc, b) => acc + BigInt(b.amount), 0n);
75558
+ return sum(sim.postTokenBalances) - sum(sim.preTokenBalances);
75559
+ }
75560
+ function readTradePtOut(data, amountIn) {
75561
+ if (data.length === 16) {
75562
+ const a = data.readBigUInt64LE(0);
75563
+ const b = data.readBigUInt64LE(8);
75564
+ if (a === amountIn) return b;
75565
+ if (b === amountIn) return a;
75566
+ return null;
75567
+ }
75568
+ if (data.length >= TRADE_PT_EVENT_AMOUNT_OUT_OFFSET + 8) {
75569
+ return data.readBigUInt64LE(TRADE_PT_EVENT_AMOUNT_OUT_OFFSET);
75604
75570
  }
75605
- return data.readBigUInt64LE(offset);
75571
+ return null;
75606
75572
  }
75607
75573
  async function quoteClmmTradeOut({
75608
75574
  connection,
75575
+ simulateTx,
75609
75576
  clmm,
75610
75577
  amountInSyNative,
75611
75578
  payer
@@ -75646,21 +75613,20 @@ async function quoteClmmTradeOut({
75646
75613
  recentBlockhash: blockhash,
75647
75614
  instructions: [createPtAta, quoteIx]
75648
75615
  }).compileToV0Message([clmm.addressLookupTable]);
75649
- const sim = await connection.simulateTransaction(new web3_js.VersionedTransaction(message), {
75650
- sigVerify: false,
75651
- replaceRecentBlockhash: true
75652
- });
75653
- const rd = sim.value.returnData;
75616
+ const sim = await simulateTx(new web3_js.VersionedTransaction(message));
75654
75617
  if (process.env.ROLL_DEBUG) {
75655
- console.error("[roll trade quote] err:", JSON.stringify(sim.value.err), "returnData?", !!rd);
75618
+ console.error("[roll trade quote] err:", JSON.stringify(sim.err), "returnData?", !!sim.returnData);
75656
75619
  }
75657
- if (!rd?.data || rd.programId !== EXPONENT_CLMM_PROGRAM_ID.toBase58()) {
75658
- throw new Error(
75659
- `roll-pt: CLMM trade quote produced no return data (err=${JSON.stringify(sim.value.err)})`
75660
- );
75620
+ const delta = tokenBalanceDelta(sim, clmm.pt.mint.toBase58(), trader.toBase58());
75621
+ if (delta !== null && delta > 0n) return delta;
75622
+ const rd = sim.returnData;
75623
+ if (rd?.data && rd.programId === EXPONENT_CLMM_PROGRAM_ID.toBase58()) {
75624
+ const out = readTradePtOut(buffer.Buffer.from(rd.data[0], rd.data[1]), amountInSyNative);
75625
+ if (out !== null && out > 0n) return out;
75661
75626
  }
75662
- const data = Buffer.from(rd.data[0], rd.data[1]);
75663
- return readReturnU64(data, TRADE_PT_EVENT_AMOUNT_OUT_OFFSET, "trade_pt amount_out");
75627
+ throw new Error(
75628
+ `roll-pt: CLMM trade quote produced no readable output (err=${JSON.stringify(sim.err)})`
75629
+ );
75664
75630
  }
75665
75631
  var MAX_BALANCES = 16;
75666
75632
  var DEFAULT_MAX_TRANSFER_POSITIONS = 5;
@@ -79821,7 +79787,7 @@ async function computeStakedBankMultipliers(stakedBanks, connection) {
79821
79787
  }
79822
79788
  const stakeLamports = poolStakeInfo.lamports;
79823
79789
  const supplyBuffer = lstMintInfo.data.slice(36, 44);
79824
- const lstMintSupply = Number(Buffer.from(supplyBuffer).readBigUInt64LE(0));
79790
+ const lstMintSupply = Number(buffer.Buffer.from(supplyBuffer).readBigUInt64LE(0));
79825
79791
  if (lstMintSupply === 0) {
79826
79792
  multiplierByBank.set(bankAddr, new BigNumber3__default.default(1));
79827
79793
  continue;