@cubee_ee/sdk 0.1.8 → 0.2.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.
Files changed (50) hide show
  1. package/README.md +50 -0
  2. package/dist/clients/CubicPoolClient.d.ts.map +1 -1
  3. package/dist/clients/CubicPoolClient.js +12 -6
  4. package/dist/clients/CubicPoolClient.js.map +1 -1
  5. package/dist/clients/index.d.ts +1 -0
  6. package/dist/clients/index.d.ts.map +1 -1
  7. package/dist/clients/index.js +1 -0
  8. package/dist/clients/index.js.map +1 -1
  9. package/dist/clients/tx-builders.d.ts +44 -0
  10. package/dist/clients/tx-builders.d.ts.map +1 -1
  11. package/dist/clients/tx-builders.js +59 -0
  12. package/dist/clients/tx-builders.js.map +1 -1
  13. package/dist/clients/versioned.d.ts +34 -0
  14. package/dist/clients/versioned.d.ts.map +1 -0
  15. package/dist/clients/versioned.js +55 -0
  16. package/dist/clients/versioned.js.map +1 -0
  17. package/dist/config/networks.js +2 -2
  18. package/dist/config/networks.js.map +1 -1
  19. package/dist/idl/index.d.ts +138 -14
  20. package/dist/idl/index.d.ts.map +1 -1
  21. package/dist/idl/protocol_admin.json +513 -2
  22. package/dist/index.d.ts +3 -1
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +7 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/parsers/poolAccount.d.ts +42 -11
  27. package/dist/parsers/poolAccount.d.ts.map +1 -1
  28. package/dist/parsers/poolAccount.js +96 -37
  29. package/dist/parsers/poolAccount.js.map +1 -1
  30. package/dist/types/pool.d.ts +6 -0
  31. package/dist/types/pool.d.ts.map +1 -1
  32. package/dist/types/result.d.ts +1 -1
  33. package/dist/types/result.d.ts.map +1 -1
  34. package/dist/types/result.js.map +1 -1
  35. package/dist/utils/errors.d.ts +8 -1
  36. package/dist/utils/errors.d.ts.map +1 -1
  37. package/dist/utils/errors.js +57 -17
  38. package/dist/utils/errors.js.map +1 -1
  39. package/package.json +1 -1
  40. package/src/clients/CubicPoolClient.ts +20 -6
  41. package/src/clients/index.ts +1 -0
  42. package/src/clients/tx-builders.ts +100 -0
  43. package/src/clients/versioned.ts +80 -0
  44. package/src/config/networks.ts +8 -8
  45. package/src/idl/protocol_admin.json +513 -2
  46. package/src/index.ts +5 -0
  47. package/src/parsers/poolAccount.ts +151 -48
  48. package/src/types/pool.ts +6 -0
  49. package/src/types/result.ts +1 -0
  50. package/src/utils/errors.ts +60 -17
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  AccountMeta,
3
+ AddressLookupTableProgram,
3
4
  ComputeBudgetProgram,
4
5
  PublicKey,
5
6
  SystemProgram,
@@ -34,6 +35,7 @@ const CUBIC_POOL_DISC = {
34
35
  addLiquidity: computeDiscriminator("add_liquidity"),
35
36
  removeLiquidity: computeDiscriminator("remove_liquidity"),
36
37
  initializeCubicPool: computeDiscriminator("initialize_cubic_pool"),
38
+ initializePoolAlt: computeDiscriminator("initialize_pool_alt"),
37
39
  };
38
40
 
39
41
  const STLD_DISC = {
@@ -62,6 +64,7 @@ function computeDiscriminator(ixName: string): Buffer {
62
64
  add_liquidity: "b59d59438fb63448",
63
65
  remove_liquidity: "5055d14818ceb16c",
64
66
  initialize_cubic_pool: "d79474cf79686f83",
67
+ initialize_pool_alt: "fb874202f4490c90",
65
68
  deposit_single_token: "a688a62fc7c056a9",
66
69
  initialize_config: "d07f1501c2bec446",
67
70
  };
@@ -504,6 +507,103 @@ export function buildDeployPoolTx(cfg: CubeConfig, params: DeployPoolParams): Bu
504
507
  };
505
508
  }
506
509
 
510
+ // ============================================================
511
+ // initialize_pool_alt — per-pool Address Lookup Table
512
+ // ============================================================
513
+
514
+ export interface InitializePoolAltParams {
515
+ /** Pool PDA. */
516
+ pool: PublicKey;
517
+ /** `CubicPoolConfig` account this pool is pinned to. Read by the
518
+ * program to gate the alternative-authority path (`config.protocol_admin`). */
519
+ config: PublicKey;
520
+ /** Authority. Must equal either `pool.pool_admin` (per-pool owner
521
+ * path) or `config.protocol_admin` (Treasury PDA via the
522
+ * protocol-admin CPI wrapper). The ALT address is derived from
523
+ * `[authority, recent_slot]`. */
524
+ authority: PublicKey;
525
+ /** Rent payer. Decoupled from `authority` so the wrapper path can
526
+ * pay from a regular wallet while Treasury PDA acts as authority.
527
+ * In the pool-admin path pass the same key for both. */
528
+ payer: PublicKey;
529
+ /**
530
+ * Recent slot. Used by the upstream ALT program to derive the table
531
+ * address as `[authority, recent_slot]`. Must be a slot the runtime
532
+ * still has in slot-hashes — caller should pass
533
+ * `await connection.getSlot('finalized')`.
534
+ */
535
+ recentSlot: BN;
536
+ }
537
+
538
+ /**
539
+ * Re-derives the Address Lookup Table address from `(authority, recent_slot)`
540
+ * the same way the upstream ALT program does.
541
+ *
542
+ * `@solana/web3.js` v1 doesn't expose `deriveLookupTableAddress` as a static,
543
+ * so we inline the seed derivation.
544
+ */
545
+ export function deriveAltAddress(authority: PublicKey, recentSlot: BN): PublicKey {
546
+ const slotBuf = recentSlot.toArrayLike(Buffer, "le", 8);
547
+ const [addr] = PublicKey.findProgramAddressSync(
548
+ [authority.toBuffer(), slotBuf],
549
+ AddressLookupTableProgram.programId,
550
+ );
551
+ return addr;
552
+ }
553
+
554
+ /**
555
+ * Build the on-chain `initialize_pool_alt` instruction. Performs three
556
+ * upstream CPIs inside the program: create + extend + freeze. After
557
+ * this ix lands, the ALT is immutable and the pool's `lookup_table`
558
+ * field points at it.
559
+ *
560
+ * Caller must NOT use the ALT in the same slot (warmup) — wait at
561
+ * least one slot before sending any v0 tx that references it.
562
+ */
563
+ export function buildInitializePoolAltIx(
564
+ cfg: CubeConfig,
565
+ params: InitializePoolAltParams,
566
+ ): TransactionInstruction {
567
+ const altAddr = deriveAltAddress(params.authority, params.recentSlot);
568
+
569
+ const data = Buffer.concat([
570
+ CUBIC_POOL_DISC.initializePoolAlt,
571
+ encodeU64(params.recentSlot),
572
+ ]);
573
+
574
+ // Account order MUST match cubic-pool's InitializePoolAlt context:
575
+ // pool, config, authority, payer, lookup_table, system_program, alt_program.
576
+ const keys: AccountMeta[] = [
577
+ { pubkey: params.pool, isSigner: false, isWritable: true },
578
+ { pubkey: params.config, isSigner: false, isWritable: false },
579
+ { pubkey: params.authority, isSigner: true, isWritable: false },
580
+ { pubkey: params.payer, isSigner: true, isWritable: true },
581
+ { pubkey: altAddr, isSigner: false, isWritable: true },
582
+ { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
583
+ { pubkey: AddressLookupTableProgram.programId, isSigner: false, isWritable: false },
584
+ ];
585
+
586
+ return new TransactionInstruction({
587
+ programId: cfg.programs.cubicPool,
588
+ keys,
589
+ data,
590
+ });
591
+ }
592
+
593
+ export function buildInitializePoolAltTx(
594
+ cfg: CubeConfig,
595
+ params: InitializePoolAltParams,
596
+ ): BuiltTx & { lookupTable: PublicKey } {
597
+ return {
598
+ instructions: [
599
+ ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }),
600
+ buildInitializePoolAltIx(cfg, params),
601
+ ],
602
+ suggestedCuLimit: 200_000,
603
+ lookupTable: deriveAltAddress(params.authority, params.recentSlot),
604
+ };
605
+ }
606
+
507
607
  // ============================================================
508
608
  // Borsh encoding helpers (subset used above)
509
609
  // ============================================================
@@ -0,0 +1,80 @@
1
+ import {
2
+ AddressLookupTableAccount,
3
+ Connection,
4
+ PublicKey,
5
+ TransactionInstruction,
6
+ TransactionMessage,
7
+ VersionedTransaction,
8
+ } from "@solana/web3.js";
9
+ import { PoolInfo } from "../types/pool";
10
+ import { BuiltTx } from "../types/tx";
11
+ import { SdkResult, err, ok } from "../types/result";
12
+
13
+ /**
14
+ * Compile a set of instructions into a `VersionedTransaction` (v0),
15
+ * optionally using a per-pool Address Lookup Table to shrink the wire
16
+ * size below the 1232-byte legacy ceiling.
17
+ *
18
+ * ALT inclusion rules:
19
+ * - If `lookupTable` is `PublicKey.default` (or undefined) → no ALT
20
+ * is referenced; the tx is v0 but uses only static account keys.
21
+ * - Otherwise → fetch the ALT via `connection.getAddressLookupTable`
22
+ * and include it. If the fetch fails, the function returns an
23
+ * `alt_fetch_failed` error rather than silently falling back.
24
+ *
25
+ * Wallets supporting the standard `signTransaction(Transaction |
26
+ * VersionedTransaction)` overload (Phantom, Solflare, Backpack, Glow,
27
+ * the wallet-adapter set since 2023) sign these without changes.
28
+ */
29
+ export async function buildVersionedTx(
30
+ conn: Connection,
31
+ payer: PublicKey,
32
+ instructions: TransactionInstruction[],
33
+ lookupTable: PublicKey | undefined,
34
+ ): Promise<SdkResult<{ tx: VersionedTransaction; alts: AddressLookupTableAccount[] }>> {
35
+ const alts: AddressLookupTableAccount[] = [];
36
+
37
+ if (lookupTable && !lookupTable.equals(PublicKey.default)) {
38
+ try {
39
+ const res = await conn.getAddressLookupTable(lookupTable);
40
+ if (!res.value) {
41
+ return err(
42
+ "alt_fetch_failed",
43
+ `Lookup table ${lookupTable.toBase58()} not found on-chain. ` +
44
+ "The pool advertises an ALT but the account is missing — " +
45
+ "the ALT may have been closed or the RPC is stale.",
46
+ );
47
+ }
48
+ alts.push(res.value);
49
+ } catch (e) {
50
+ return err(
51
+ "alt_fetch_failed",
52
+ `Failed to fetch lookup table ${lookupTable.toBase58()}`,
53
+ e,
54
+ );
55
+ }
56
+ }
57
+
58
+ const { blockhash } = await conn.getLatestBlockhash("confirmed");
59
+ const msg = new TransactionMessage({
60
+ payerKey: payer,
61
+ recentBlockhash: blockhash,
62
+ instructions,
63
+ }).compileToV0Message(alts);
64
+
65
+ return ok({ tx: new VersionedTransaction(msg), alts });
66
+ }
67
+
68
+ /**
69
+ * Convenience: take a SDK-built `BuiltTx` and a pool snapshot, produce
70
+ * a signed-ready `VersionedTransaction`. Picks up `pool.lookupTable`
71
+ * automatically.
72
+ */
73
+ export async function compileBuiltTx(
74
+ conn: Connection,
75
+ payer: PublicKey,
76
+ built: BuiltTx,
77
+ pool: Pick<PoolInfo, "lookupTable">,
78
+ ): Promise<SdkResult<{ tx: VersionedTransaction; alts: AddressLookupTableAccount[] }>> {
79
+ return buildVersionedTx(conn, payer, built.instructions, pool.lookupTable);
80
+ }
@@ -16,41 +16,41 @@ export const NETWORK_PROGRAMS: Record<Network, NetworkPrograms> = {
16
16
  // disabled in the frontend. If/when stld ships on mainnet, replace this
17
17
  // with the real mainnet program ID.
18
18
  singleTokenLiquidity: new PublicKey(
19
- "7BpdUH1tzTSXLuQNo6YpjJ8Eagw8AkrS6cnkxiJdCFS2"
19
+ "7BpdUH1tzTSXLuQNo6YpjJ8Eagw8AkrS6cnkxiJdCFS2",
20
20
  ),
21
21
  protocolAdmin: new PublicKey(
22
- "3jiojHZbjJQ7QLMGSTjFwxVEmx4NtuRy34nLAmsJME81"
22
+ "3jiojHZbjJQ7QLMGSTjFwxVEmx4NtuRy34nLAmsJME81",
23
23
  ),
24
24
  },
25
25
  devnet: {
26
26
  cubicPool: new PublicKey("8iQtGj9mcUfFUGaiCpPy89swC3s8YTC8FhVZWfgeZhwu"),
27
27
  singleTokenLiquidity: new PublicKey(
28
- "7BpdUH1tzTSXLuQNo6YpjJ8Eagw8AkrS6cnkxiJdCFS2"
28
+ "7BpdUH1tzTSXLuQNo6YpjJ8Eagw8AkrS6cnkxiJdCFS2",
29
29
  ),
30
30
  protocolAdmin: new PublicKey(
31
- "3jiojHZbjJQ7QLMGSTjFwxVEmx4NtuRy34nLAmsJME81"
31
+ "3jiojHZbjJQ7QLMGSTjFwxVEmx4NtuRy34nLAmsJME81",
32
32
  ),
33
33
  },
34
34
  localnet: {
35
35
  cubicPool: new PublicKey("8iQtGj9mcUfFUGaiCpPy89swC3s8YTC8FhVZWfgeZhwu"),
36
36
  singleTokenLiquidity: new PublicKey(
37
- "7BpdUH1tzTSXLuQNo6YpjJ8Eagw8AkrS6cnkxiJdCFS2"
37
+ "7BpdUH1tzTSXLuQNo6YpjJ8Eagw8AkrS6cnkxiJdCFS2",
38
38
  ),
39
39
  protocolAdmin: new PublicKey(
40
- "3jiojHZbjJQ7QLMGSTjFwxVEmx4NtuRy34nLAmsJME81"
40
+ "3jiojHZbjJQ7QLMGSTjFwxVEmx4NtuRy34nLAmsJME81",
41
41
  ),
42
42
  },
43
43
  };
44
44
 
45
45
  export const DEFAULT_RPC_ENDPOINT: Record<Network, string> = {
46
- mainnet: "https://api.mainnet-beta.solana.com",
46
+ mainnet: "https://solana.drpc.org",
47
47
  devnet: "https://api.devnet.solana.com",
48
48
  localnet: "http://127.0.0.1:8899",
49
49
  };
50
50
 
51
51
  export const DEFAULT_RPC_ENDPOINTS: Record<Network, string[]> = {
52
52
  mainnet: [
53
- "https://api.mainnet-beta.solana.com",
53
+ "https://solana.drpc.org",
54
54
  "https://solana-rpc.publicnode.com",
55
55
  "https://solana.api.pocket.network",
56
56
  ],