@hadron-fi/sdk 0.3.1 → 0.3.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/index.mjs CHANGED
@@ -166,6 +166,12 @@ function fromQ32(q32) {
166
166
  function pctToQ32(pct) {
167
167
  return toQ32(pct);
168
168
  }
169
+ function spreadBpsToQ32(bps) {
170
+ return toQ32(1 - bps / 1e4);
171
+ }
172
+ function spreadQ32ToBps(q32) {
173
+ return (1 - fromQ32(q32)) * 1e4;
174
+ }
169
175
 
170
176
  // src/accounts/index.ts
171
177
  import { PublicKey as PublicKey3 } from "@solana/web3.js";
@@ -275,9 +281,9 @@ function decodeMidpriceOracle(data) {
275
281
  const authority = new PublicKey3(buf.subarray(0, 32));
276
282
  const sequence = buf.readBigUInt64LE(32);
277
283
  const midpriceQ32 = buf.readBigUInt64LE(40);
278
- const baseSpreadQ32 = buf.readBigUInt64LE(48);
284
+ const spreadFactorQ32 = buf.readBigUInt64LE(48);
279
285
  const lastUpdateSlot = buf.length >= MIDPRICE_ORACLE_SIZE ? buf.readBigUInt64LE(56) : 0n;
280
- return { authority, sequence, midpriceQ32, baseSpreadQ32, lastUpdateSlot };
286
+ return { authority, sequence, midpriceQ32, spreadFactorQ32, lastUpdateSlot };
281
287
  }
282
288
  function decodeCurveMeta(data) {
283
289
  const buf = Buffer.from(data);
@@ -593,10 +599,11 @@ function buildWithdraw(user, configPda, mintX, mintY, tokenProgramX, tokenProgra
593
599
  // src/instructions/swap.ts
594
600
  import {
595
601
  SYSVAR_CLOCK_PUBKEY,
602
+ SYSVAR_INSTRUCTIONS_PUBKEY,
596
603
  TransactionInstruction as TransactionInstruction4
597
604
  } from "@solana/web3.js";
598
605
  import { getAssociatedTokenAddressSync as getAssociatedTokenAddressSync4 } from "@solana/spl-token";
599
- function buildSwapExactIn(user, poolAddresses, mintX, mintY, tokenProgramX, tokenProgramY, params, programId = HADRON_PROGRAM_ID) {
606
+ function buildSwapExactIn(user, poolAddresses, mintX, mintY, tokenProgramX, tokenProgramY, params, programId = HADRON_PROGRAM_ID, spreadConfigInitialized = false) {
600
607
  const data = Buffer.alloc(1 + 1 + 8 + 8 + 8);
601
608
  let offset = 0;
602
609
  data.writeUInt8(Discriminator.SwapExactIn, offset);
@@ -621,39 +628,50 @@ function buildSwapExactIn(user, poolAddresses, mintX, mintY, tokenProgramX, toke
621
628
  false,
622
629
  inputMintProgram
623
630
  );
631
+ const keys = [
632
+ { pubkey: tokenProgramX, isSigner: false, isWritable: false },
633
+ { pubkey: tokenProgramY, isSigner: false, isWritable: false },
634
+ { pubkey: poolAddresses.config, isSigner: false, isWritable: false },
635
+ {
636
+ pubkey: poolAddresses.midpriceOracle,
637
+ isSigner: false,
638
+ isWritable: false
639
+ },
640
+ { pubkey: poolAddresses.curveMeta, isSigner: false, isWritable: false },
641
+ {
642
+ pubkey: poolAddresses.curvePrefabs,
643
+ isSigner: false,
644
+ isWritable: true
645
+ },
646
+ { pubkey: poolAddresses.config, isSigner: false, isWritable: false },
647
+ // authority = pool address PDA
648
+ { pubkey: user, isSigner: true, isWritable: false },
649
+ { pubkey: userSource, isSigner: false, isWritable: true },
650
+ { pubkey: vaultSource, isSigner: false, isWritable: true },
651
+ { pubkey: vaultDest, isSigner: false, isWritable: true },
652
+ { pubkey: userDest, isSigner: false, isWritable: true },
653
+ { pubkey: feeConfigPda, isSigner: false, isWritable: false },
654
+ { pubkey: feeRecipientAta, isSigner: false, isWritable: true },
655
+ { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
656
+ {
657
+ pubkey: poolAddresses.curveUpdates,
658
+ isSigner: false,
659
+ isWritable: true
660
+ }
661
+ ];
662
+ if (spreadConfigInitialized) {
663
+ const [spreadConfigPda] = getSpreadConfigAddress(
664
+ poolAddresses.config,
665
+ programId
666
+ );
667
+ keys.push(
668
+ { pubkey: spreadConfigPda, isSigner: false, isWritable: false },
669
+ { pubkey: SYSVAR_INSTRUCTIONS_PUBKEY, isSigner: false, isWritable: false }
670
+ );
671
+ }
624
672
  return new TransactionInstruction4({
625
673
  programId,
626
- keys: [
627
- { pubkey: tokenProgramX, isSigner: false, isWritable: false },
628
- { pubkey: tokenProgramY, isSigner: false, isWritable: false },
629
- { pubkey: poolAddresses.config, isSigner: false, isWritable: false },
630
- {
631
- pubkey: poolAddresses.midpriceOracle,
632
- isSigner: false,
633
- isWritable: false
634
- },
635
- { pubkey: poolAddresses.curveMeta, isSigner: false, isWritable: false },
636
- {
637
- pubkey: poolAddresses.curvePrefabs,
638
- isSigner: false,
639
- isWritable: true
640
- },
641
- { pubkey: poolAddresses.config, isSigner: false, isWritable: false },
642
- // authority = pool address PDA
643
- { pubkey: user, isSigner: true, isWritable: false },
644
- { pubkey: userSource, isSigner: false, isWritable: true },
645
- { pubkey: vaultSource, isSigner: false, isWritable: true },
646
- { pubkey: vaultDest, isSigner: false, isWritable: true },
647
- { pubkey: userDest, isSigner: false, isWritable: true },
648
- { pubkey: feeConfigPda, isSigner: false, isWritable: false },
649
- { pubkey: feeRecipientAta, isSigner: false, isWritable: true },
650
- { pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false },
651
- {
652
- pubkey: poolAddresses.curveUpdates,
653
- isSigner: false,
654
- isWritable: true
655
- }
656
- ],
674
+ keys,
657
675
  data
658
676
  });
659
677
  }
@@ -838,7 +856,7 @@ function buildUpdateBaseSpread(authority, midpriceOraclePda, params, programId =
838
856
  const data = Buffer.alloc(1 + 8 + 8);
839
857
  data.writeUInt8(Discriminator.UpdateBaseSpread, 0);
840
858
  data.writeBigUInt64LE(params.sequence ?? 0n, 1);
841
- data.writeBigUInt64LE(params.baseSpreadQ32, 9);
859
+ data.writeBigUInt64LE(params.spreadFactorQ32, 9);
842
860
  return new TransactionInstruction6({
843
861
  programId,
844
862
  keys: [
@@ -854,7 +872,7 @@ function buildUpdateMidpriceAndBaseSpread(authority, midpriceOraclePda, params,
854
872
  data.writeUInt8(Discriminator.UpdateMidpriceAndBaseSpread, 0);
855
873
  data.writeBigUInt64LE(params.sequence ?? 0n, 1);
856
874
  data.writeBigUInt64LE(params.midpriceQ32, 9);
857
- data.writeBigUInt64LE(params.baseSpreadQ32, 17);
875
+ data.writeBigUInt64LE(params.spreadFactorQ32, 17);
858
876
  return new TransactionInstruction6({
859
877
  programId,
860
878
  keys: [
@@ -1309,9 +1327,13 @@ var Hadron = class _Hadron {
1309
1327
  getMidprice() {
1310
1328
  return fromQ32(this.oracle.midpriceQ32);
1311
1329
  }
1312
- /** Get base spread as a floating-point number. */
1313
- getBaseSpread() {
1314
- return fromQ32(this.oracle.baseSpreadQ32);
1330
+ /** Get spread factor as a floating-point number (e.g. 0.9995 = 5 bps). */
1331
+ getSpreadFactor() {
1332
+ return fromQ32(this.oracle.spreadFactorQ32);
1333
+ }
1334
+ /** Get spread in basis points (e.g. 5 for a 0.9995 factor). */
1335
+ getSpreadBps() {
1336
+ return (1 - fromQ32(this.oracle.spreadFactorQ32)) * 1e4;
1315
1337
  }
1316
1338
  /** Get the currently active curve slot indices. */
1317
1339
  getActiveCurveSlots() {
@@ -1375,7 +1397,8 @@ var Hadron = class _Hadron {
1375
1397
  this.config.tokenProgramX,
1376
1398
  this.config.tokenProgramY,
1377
1399
  params,
1378
- this.programId
1400
+ this.programId,
1401
+ this.config.spreadConfigInitialized
1379
1402
  );
1380
1403
  }
1381
1404
  /** Build set curve instruction. */
@@ -2287,6 +2310,8 @@ export {
2287
2310
  getSpreadConfigAddress,
2288
2311
  isSlotInitialized,
2289
2312
  pctToQ32,
2313
+ spreadBpsToQ32,
2314
+ spreadQ32ToBps,
2290
2315
  toQ32
2291
2316
  };
2292
2317
  //# sourceMappingURL=index.mjs.map