@cubee_ee/sdk 0.1.0

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 (179) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +110 -0
  3. package/dist/clients/AdminClient.d.ts +61 -0
  4. package/dist/clients/AdminClient.d.ts.map +1 -0
  5. package/dist/clients/AdminClient.js +196 -0
  6. package/dist/clients/AdminClient.js.map +1 -0
  7. package/dist/clients/CubeBackendClient.d.ts +67 -0
  8. package/dist/clients/CubeBackendClient.d.ts.map +1 -0
  9. package/dist/clients/CubeBackendClient.js +126 -0
  10. package/dist/clients/CubeBackendClient.js.map +1 -0
  11. package/dist/clients/CubicPoolClient.d.ts +73 -0
  12. package/dist/clients/CubicPoolClient.d.ts.map +1 -0
  13. package/dist/clients/CubicPoolClient.js +425 -0
  14. package/dist/clients/CubicPoolClient.js.map +1 -0
  15. package/dist/clients/PoolFactoryClient.d.ts +45 -0
  16. package/dist/clients/PoolFactoryClient.d.ts.map +1 -0
  17. package/dist/clients/PoolFactoryClient.js +83 -0
  18. package/dist/clients/PoolFactoryClient.js.map +1 -0
  19. package/dist/clients/RpcClient.d.ts +28 -0
  20. package/dist/clients/RpcClient.d.ts.map +1 -0
  21. package/dist/clients/RpcClient.js +58 -0
  22. package/dist/clients/RpcClient.js.map +1 -0
  23. package/dist/clients/SingleTokenDepositClient.d.ts +45 -0
  24. package/dist/clients/SingleTokenDepositClient.d.ts.map +1 -0
  25. package/dist/clients/SingleTokenDepositClient.js +63 -0
  26. package/dist/clients/SingleTokenDepositClient.js.map +1 -0
  27. package/dist/clients/index.d.ts +8 -0
  28. package/dist/clients/index.d.ts.map +1 -0
  29. package/dist/clients/index.js +24 -0
  30. package/dist/clients/index.js.map +1 -0
  31. package/dist/clients/tx-builders.d.ts +32 -0
  32. package/dist/clients/tx-builders.d.ts.map +1 -0
  33. package/dist/clients/tx-builders.js +398 -0
  34. package/dist/clients/tx-builders.js.map +1 -0
  35. package/dist/config/index.d.ts +60 -0
  36. package/dist/config/index.d.ts.map +1 -0
  37. package/dist/config/index.js +59 -0
  38. package/dist/config/index.js.map +1 -0
  39. package/dist/config/networks.d.ts +10 -0
  40. package/dist/config/networks.d.ts.map +1 -0
  41. package/dist/config/networks.js +31 -0
  42. package/dist/config/networks.js.map +1 -0
  43. package/dist/config/tokens.d.ts +19 -0
  44. package/dist/config/tokens.d.ts.map +1 -0
  45. package/dist/config/tokens.js +43 -0
  46. package/dist/config/tokens.js.map +1 -0
  47. package/dist/idl/cubic_pool.json +2497 -0
  48. package/dist/idl/index.d.ts +975 -0
  49. package/dist/idl/index.d.ts.map +1 -0
  50. package/dist/idl/index.js +18 -0
  51. package/dist/idl/index.js.map +1 -0
  52. package/dist/idl/protocol_admin.json +1816 -0
  53. package/dist/idl/single_token_liquidity.json +745 -0
  54. package/dist/index.d.ts +48 -0
  55. package/dist/index.d.ts.map +1 -0
  56. package/dist/index.js +136 -0
  57. package/dist/index.js.map +1 -0
  58. package/dist/math/cubicMath.d.ts +40 -0
  59. package/dist/math/cubicMath.d.ts.map +1 -0
  60. package/dist/math/cubicMath.js +75 -0
  61. package/dist/math/cubicMath.js.map +1 -0
  62. package/dist/math/fixedPoint.d.ts +14 -0
  63. package/dist/math/fixedPoint.d.ts.map +1 -0
  64. package/dist/math/fixedPoint.js +46 -0
  65. package/dist/math/fixedPoint.js.map +1 -0
  66. package/dist/math/index.d.ts +7 -0
  67. package/dist/math/index.d.ts.map +1 -0
  68. package/dist/math/index.js +23 -0
  69. package/dist/math/index.js.map +1 -0
  70. package/dist/math/logExp.d.ts +15 -0
  71. package/dist/math/logExp.d.ts.map +1 -0
  72. package/dist/math/logExp.js +91 -0
  73. package/dist/math/logExp.js.map +1 -0
  74. package/dist/math/singleToken.d.ts +53 -0
  75. package/dist/math/singleToken.d.ts.map +1 -0
  76. package/dist/math/singleToken.js +185 -0
  77. package/dist/math/singleToken.js.map +1 -0
  78. package/dist/math/slippage.d.ts +24 -0
  79. package/dist/math/slippage.d.ts.map +1 -0
  80. package/dist/math/slippage.js +54 -0
  81. package/dist/math/slippage.js.map +1 -0
  82. package/dist/math/weightedMath.d.ts +18 -0
  83. package/dist/math/weightedMath.d.ts.map +1 -0
  84. package/dist/math/weightedMath.js +45 -0
  85. package/dist/math/weightedMath.js.map +1 -0
  86. package/dist/parsers/borsh.d.ts +24 -0
  87. package/dist/parsers/borsh.d.ts.map +1 -0
  88. package/dist/parsers/borsh.js +80 -0
  89. package/dist/parsers/borsh.js.map +1 -0
  90. package/dist/parsers/events.d.ts +3 -0
  91. package/dist/parsers/events.d.ts.map +1 -0
  92. package/dist/parsers/events.js +172 -0
  93. package/dist/parsers/events.js.map +1 -0
  94. package/dist/parsers/index.d.ts +5 -0
  95. package/dist/parsers/index.d.ts.map +1 -0
  96. package/dist/parsers/index.js +21 -0
  97. package/dist/parsers/index.js.map +1 -0
  98. package/dist/parsers/mintAccount.d.ts +22 -0
  99. package/dist/parsers/mintAccount.d.ts.map +1 -0
  100. package/dist/parsers/mintAccount.js +22 -0
  101. package/dist/parsers/mintAccount.js.map +1 -0
  102. package/dist/parsers/poolAccount.d.ts +32 -0
  103. package/dist/parsers/poolAccount.d.ts.map +1 -0
  104. package/dist/parsers/poolAccount.js +88 -0
  105. package/dist/parsers/poolAccount.js.map +1 -0
  106. package/dist/types/events.d.ts +82 -0
  107. package/dist/types/events.d.ts.map +1 -0
  108. package/dist/types/events.js +3 -0
  109. package/dist/types/events.js.map +1 -0
  110. package/dist/types/index.d.ts +5 -0
  111. package/dist/types/index.d.ts.map +1 -0
  112. package/dist/types/index.js +21 -0
  113. package/dist/types/index.js.map +1 -0
  114. package/dist/types/pool.d.ts +66 -0
  115. package/dist/types/pool.d.ts.map +1 -0
  116. package/dist/types/pool.js +3 -0
  117. package/dist/types/pool.js.map +1 -0
  118. package/dist/types/result.d.ts +23 -0
  119. package/dist/types/result.d.ts.map +1 -0
  120. package/dist/types/result.js +11 -0
  121. package/dist/types/result.js.map +1 -0
  122. package/dist/types/tx.d.ts +80 -0
  123. package/dist/types/tx.d.ts.map +1 -0
  124. package/dist/types/tx.js +3 -0
  125. package/dist/types/tx.js.map +1 -0
  126. package/dist/utils/errors.d.ts +8 -0
  127. package/dist/utils/errors.d.ts.map +1 -0
  128. package/dist/utils/errors.js +83 -0
  129. package/dist/utils/errors.js.map +1 -0
  130. package/dist/utils/index.d.ts +4 -0
  131. package/dist/utils/index.d.ts.map +1 -0
  132. package/dist/utils/index.js +20 -0
  133. package/dist/utils/index.js.map +1 -0
  134. package/dist/utils/pda.d.ts +8 -0
  135. package/dist/utils/pda.d.ts.map +1 -0
  136. package/dist/utils/pda.js +27 -0
  137. package/dist/utils/pda.js.map +1 -0
  138. package/dist/utils/retry.d.ts +22 -0
  139. package/dist/utils/retry.d.ts.map +1 -0
  140. package/dist/utils/retry.js +62 -0
  141. package/dist/utils/retry.js.map +1 -0
  142. package/package.json +54 -0
  143. package/src/clients/AdminClient.ts +254 -0
  144. package/src/clients/CubeBackendClient.ts +193 -0
  145. package/src/clients/CubicPoolClient.ts +492 -0
  146. package/src/clients/PoolFactoryClient.ts +102 -0
  147. package/src/clients/RpcClient.ts +78 -0
  148. package/src/clients/SingleTokenDepositClient.ts +91 -0
  149. package/src/clients/index.ts +7 -0
  150. package/src/clients/tx-builders.ts +538 -0
  151. package/src/config/index.ts +82 -0
  152. package/src/config/networks.ts +49 -0
  153. package/src/config/tokens.ts +52 -0
  154. package/src/idl/cubic_pool.json +2497 -0
  155. package/src/idl/index.ts +13 -0
  156. package/src/idl/protocol_admin.json +1816 -0
  157. package/src/idl/single_token_liquidity.json +745 -0
  158. package/src/index.ts +154 -0
  159. package/src/math/cubicMath.ts +89 -0
  160. package/src/math/fixedPoint.ts +39 -0
  161. package/src/math/index.ts +6 -0
  162. package/src/math/logExp.ts +82 -0
  163. package/src/math/singleToken.ts +250 -0
  164. package/src/math/slippage.ts +49 -0
  165. package/src/math/weightedMath.ts +48 -0
  166. package/src/parsers/borsh.ts +80 -0
  167. package/src/parsers/events.ts +172 -0
  168. package/src/parsers/index.ts +4 -0
  169. package/src/parsers/mintAccount.ts +37 -0
  170. package/src/parsers/poolAccount.ts +113 -0
  171. package/src/types/events.ts +100 -0
  172. package/src/types/index.ts +4 -0
  173. package/src/types/pool.ts +64 -0
  174. package/src/types/result.ts +45 -0
  175. package/src/types/tx.ts +87 -0
  176. package/src/utils/errors.ts +78 -0
  177. package/src/utils/index.ts +3 -0
  178. package/src/utils/pda.ts +58 -0
  179. package/src/utils/retry.ts +85 -0
@@ -0,0 +1,102 @@
1
+ import { ComputeBudgetProgram, Keypair, PublicKey, TransactionInstruction } from "@solana/web3.js";
2
+ import BN from "bn.js";
3
+ import { CubeConfig } from "../config";
4
+ import { SdkResult, err, ok } from "../types/result";
5
+ import { BuiltTx, DeployPoolParams } from "../types/tx";
6
+ import {
7
+ buildDeployPoolTx,
8
+ buildInitializeCubicPoolIx,
9
+ buildInitializeConfigIx,
10
+ } from "./tx-builders";
11
+ import { derivePoolPda } from "../utils/pda";
12
+
13
+ export interface PoolFactoryClientParams {
14
+ config: CubeConfig;
15
+ }
16
+
17
+ export interface InitializeConfigParams {
18
+ payer: PublicKey;
19
+ defaultProtocolFeeRate: number;
20
+ }
21
+
22
+ /**
23
+ * Builds transactions for protocol governance / pool deployment. Consumed
24
+ * by the deploy-pool UI flow and by the backend's admin tools.
25
+ */
26
+ export class PoolFactoryClient {
27
+ readonly config: CubeConfig;
28
+
29
+ constructor(params: PoolFactoryClientParams) {
30
+ this.config = params.config;
31
+ }
32
+
33
+ /**
34
+ * Deploy a new pool atop an existing `CubicPoolConfig`. Caller is
35
+ * responsible for funding `params.payer` with enough SOL to cover rent
36
+ * for the pool account and the BPT mint PDA.
37
+ *
38
+ * Returns the `pool` and `bptMint` pubkeys it WILL create (derived PDAs)
39
+ * plus the signable tx.
40
+ */
41
+ buildDeployPoolTx(params: DeployPoolParams): SdkResult<BuiltTx & { pool: PublicKey; bptMint: PublicKey }> {
42
+ if (params.tokens.length < 2 || params.tokens.length > 10) {
43
+ return err("invalid_input", "tokens.length must be in [2, 10]");
44
+ }
45
+ if (params.tokens.length !== params.weightsBps.length) {
46
+ return err("invalid_input", "tokens/weightsBps length mismatch");
47
+ }
48
+ if (params.tokens.length !== params.virtualBalances.length) {
49
+ return err("invalid_input", "tokens/virtualBalances length mismatch");
50
+ }
51
+ const sum = params.weightsBps.reduce((a, b) => a + b, 0);
52
+ if (sum !== 10_000) return err("invalid_input", `weights must sum to 10000 (got ${sum})`);
53
+ for (const vb of params.virtualBalances) {
54
+ if (vb.isZero()) return err("invalid_input", "virtualBalances must all be > 0");
55
+ }
56
+
57
+ const [pool] = derivePoolPda(this.config.programs.cubicPool, params.configKey, params.poolId);
58
+ const [bptMint] = PublicKey.findProgramAddressSync(
59
+ [Buffer.from("bpt_mint"), pool.toBuffer()],
60
+ this.config.programs.cubicPool
61
+ );
62
+ try {
63
+ const tx = buildDeployPoolTx(this.config, params);
64
+ return ok({ ...tx, pool, bptMint });
65
+ } catch (e) {
66
+ return err("tx_build_failed", "Failed to build deploy-pool tx", e);
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Initialize a new CubicPoolConfig. The `config` signer must be freshly
72
+ * generated and paired with its keypair when signing the outer tx.
73
+ * Returns the tx + the config keypair the caller should sign with.
74
+ */
75
+ buildInitializeConfigTx(params: InitializeConfigParams): SdkResult<
76
+ BuiltTx & { configKeypair: Keypair }
77
+ > {
78
+ try {
79
+ const configKeypair = Keypair.generate();
80
+ const ix = buildInitializeConfigIx(this.config, {
81
+ config: configKeypair.publicKey,
82
+ payer: params.payer,
83
+ defaultProtocolFeeRate: params.defaultProtocolFeeRate,
84
+ });
85
+ return ok({
86
+ instructions: [ComputeBudgetProgram.setComputeUnitLimit({ units: 200_000 }), ix],
87
+ suggestedCuLimit: 200_000,
88
+ configKeypair,
89
+ });
90
+ } catch (e) {
91
+ return err("tx_build_failed", "Failed to build initialize_config tx", e);
92
+ }
93
+ }
94
+
95
+ /**
96
+ * Low-level: return the raw `initialize_cubic_pool` ix without a CU
97
+ * budget or wrapping. Useful for combining with other ixs in the same tx.
98
+ */
99
+ initializeCubicPoolIx(params: DeployPoolParams): TransactionInstruction {
100
+ return buildInitializeCubicPoolIx(this.config, params);
101
+ }
102
+ }
@@ -0,0 +1,78 @@
1
+ import { Commitment, Connection, PublicKey } from "@solana/web3.js";
2
+ import { SdkResult } from "../types/result";
3
+ import { safeCall, RetryOptions } from "../utils/retry";
4
+
5
+ export interface RpcClientParams {
6
+ endpoint: string;
7
+ apiKey?: string;
8
+ commitment?: Commitment;
9
+ /** Optional headers (e.g. for Helius/Triton auth). */
10
+ headers?: Record<string, string>;
11
+ }
12
+
13
+ /**
14
+ * Thin wrapper around `@solana/web3.js` Connection with built-in retry +
15
+ * human-readable error mapping. Every public method returns a SdkResult<T>.
16
+ */
17
+ export class RpcClient {
18
+ readonly connection: Connection;
19
+ private readonly defaultCommitment: Commitment;
20
+
21
+ constructor(params: RpcClientParams) {
22
+ const endpoint = params.apiKey
23
+ ? appendApiKey(params.endpoint, params.apiKey)
24
+ : params.endpoint;
25
+ this.defaultCommitment = params.commitment ?? "confirmed";
26
+ this.connection = new Connection(endpoint, {
27
+ commitment: this.defaultCommitment,
28
+ httpHeaders: params.headers,
29
+ });
30
+ }
31
+
32
+ call<T>(
33
+ fn: (conn: Connection) => Promise<T>,
34
+ retry: RetryOptions = {}
35
+ ): Promise<SdkResult<T>> {
36
+ return safeCall(() => fn(this.connection), retry);
37
+ }
38
+
39
+ getAccountInfo(
40
+ pk: PublicKey,
41
+ retry: RetryOptions = {}
42
+ ): Promise<SdkResult<{ data: Buffer; owner: PublicKey; lamports: number } | null>> {
43
+ return safeCall(async () => {
44
+ const info = await this.connection.getAccountInfo(pk, this.defaultCommitment);
45
+ if (!info) return null;
46
+ return {
47
+ data: info.data,
48
+ owner: info.owner,
49
+ lamports: info.lamports,
50
+ };
51
+ }, retry);
52
+ }
53
+
54
+ getMultipleAccountsInfo(
55
+ pks: PublicKey[],
56
+ retry: RetryOptions = {}
57
+ ): Promise<SdkResult<(Buffer | null)[]>> {
58
+ return safeCall(async () => {
59
+ const infos = await this.connection.getMultipleAccountsInfo(pks, this.defaultCommitment);
60
+ return infos.map((i) => (i ? i.data : null));
61
+ }, retry);
62
+ }
63
+
64
+ getSlot(retry: RetryOptions = {}): Promise<SdkResult<number>> {
65
+ return safeCall(() => this.connection.getSlot(this.defaultCommitment), retry);
66
+ }
67
+ }
68
+
69
+ function appendApiKey(endpoint: string, apiKey: string): string {
70
+ try {
71
+ const u = new URL(endpoint);
72
+ u.searchParams.set("api-key", apiKey);
73
+ return u.toString();
74
+ } catch {
75
+ const sep = endpoint.includes("?") ? "&" : "?";
76
+ return `${endpoint}${sep}api-key=${encodeURIComponent(apiKey)}`;
77
+ }
78
+ }
@@ -0,0 +1,91 @@
1
+ import { Commitment, PublicKey } from "@solana/web3.js";
2
+ import BN from "bn.js";
3
+ import { CubeConfig } from "../config";
4
+ import { PoolInfo } from "../types/pool";
5
+ import { SdkResult, err, ok } from "../types/result";
6
+ import {
7
+ BuiltTx,
8
+ SingleTokenDepositParams,
9
+ SingleTokenDepositQuote,
10
+ } from "../types/tx";
11
+ import { deriveHelperPda } from "../utils/pda";
12
+ import { buildSingleTokenDepositTx } from "./tx-builders";
13
+ import { RpcClient } from "./RpcClient";
14
+ import { CubicPoolClient } from "./CubicPoolClient";
15
+
16
+ export interface SingleTokenDepositClientParams {
17
+ config: CubeConfig;
18
+ poolAddress: PublicKey;
19
+ /** Optional — reuses an existing one if provided. */
20
+ rpc?: RpcClient | { endpoint: string; apiKey?: string; commitment?: Commitment };
21
+ /**
22
+ * Optional parent pool client — lets the deposit client reuse an already
23
+ * synced PoolInfo instead of making a fresh RPC round-trip.
24
+ */
25
+ poolClient?: CubicPoolClient;
26
+ }
27
+
28
+ /**
29
+ * Convenience class that wraps the single-token deposit flow end-to-end:
30
+ * quoting, transaction building, and helper-PDA derivation.
31
+ *
32
+ * Most consumers don't need to instantiate this directly — they can call
33
+ * `CubicPoolClient.singleTokenDeposit.*` which proxies here.
34
+ */
35
+ export class SingleTokenDepositClient {
36
+ readonly config: CubeConfig;
37
+ readonly poolAddress: PublicKey;
38
+ private readonly poolClient: CubicPoolClient;
39
+
40
+ constructor(params: SingleTokenDepositClientParams) {
41
+ this.config = params.config;
42
+ this.poolAddress = params.poolAddress;
43
+ this.poolClient =
44
+ params.poolClient ??
45
+ new CubicPoolClient({
46
+ config: params.config,
47
+ poolAddress: params.poolAddress,
48
+ rpc:
49
+ params.rpc ??
50
+ {
51
+ endpoint: params.config.defaults.rpcEndpoint,
52
+ commitment: params.config.defaults.rpcCommitment,
53
+ },
54
+ });
55
+ }
56
+
57
+ /** Derived helper PDA for this pool. */
58
+ helperPda(): PublicKey {
59
+ return deriveHelperPda(this.config.programs.singleTokenLiquidity, this.poolAddress)[0];
60
+ }
61
+
62
+ /** Ensure pool state is loaded; populates the underlying CubicPoolClient cache. */
63
+ async sync(): Promise<SdkResult<PoolInfo>> {
64
+ return this.poolClient.sync();
65
+ }
66
+
67
+ /** Off-chain estimate of the deposit outcome. */
68
+ quote(
69
+ tokenInIndex: number,
70
+ amountIn: BN,
71
+ slippageHundredthsBps?: number
72
+ ): SdkResult<SingleTokenDepositQuote> {
73
+ return this.poolClient.quoteSingleTokenDeposit(tokenInIndex, amountIn, slippageHundredthsBps);
74
+ }
75
+
76
+ /** Build a ready-to-sign transaction. Includes idempotent helper-ATA creates. */
77
+ buildTx(params: SingleTokenDepositParams): SdkResult<BuiltTx> {
78
+ const pool = this.poolClient.getCached();
79
+ if (!pool) return err("invalid_input", "Call sync() first to populate pool state");
80
+ if (params.tokenInIndex < 0 || params.tokenInIndex >= pool.tokenCount) {
81
+ return err("invalid_input", "Invalid tokenInIndex");
82
+ }
83
+ if (params.amountIn.lten(0)) return err("invalid_input", "amountIn must be > 0");
84
+ try {
85
+ const tx = buildSingleTokenDepositTx(this.config, pool, params);
86
+ return ok(tx);
87
+ } catch (e) {
88
+ return err("tx_build_failed", "Failed to build single-token deposit tx", e);
89
+ }
90
+ }
91
+ }
@@ -0,0 +1,7 @@
1
+ export * from "./RpcClient";
2
+ export * from "./CubeBackendClient";
3
+ export * from "./CubicPoolClient";
4
+ export * from "./tx-builders";
5
+ export * from "./SingleTokenDepositClient";
6
+ export * from "./PoolFactoryClient";
7
+ export * from "./AdminClient";