@acta-markets/ts-sdk 0.0.1-beta

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 (263) hide show
  1. package/README.md +308 -0
  2. package/dist/actaClient.d.ts +46 -0
  3. package/dist/actaClient.js +99 -0
  4. package/dist/chain/client.d.ts +65 -0
  5. package/dist/chain/client.js +82 -0
  6. package/dist/chain/fetch.d.ts +204 -0
  7. package/dist/chain/fetch.js +392 -0
  8. package/dist/chain/fetch.test.d.ts +1 -0
  9. package/dist/chain/fetch.test.js +158 -0
  10. package/dist/chain/flows/index.d.ts +1 -0
  11. package/dist/chain/flows/index.js +1 -0
  12. package/dist/chain/flows/openPosition.d.ts +48 -0
  13. package/dist/chain/flows/openPosition.js +78 -0
  14. package/dist/chain/flows/openPosition.test.d.ts +1 -0
  15. package/dist/chain/flows/openPosition.test.js +43 -0
  16. package/dist/chain/helpers.d.ts +16 -0
  17. package/dist/chain/helpers.js +60 -0
  18. package/dist/chain/index.d.ts +9 -0
  19. package/dist/chain/index.js +9 -0
  20. package/dist/chain/instructions.admin.d.ts +44 -0
  21. package/dist/chain/instructions.admin.js +113 -0
  22. package/dist/chain/instructions.d.ts +5 -0
  23. package/dist/chain/instructions.js +5 -0
  24. package/dist/chain/instructions.maker.d.ts +34 -0
  25. package/dist/chain/instructions.maker.js +86 -0
  26. package/dist/chain/instructions.market.d.ts +39 -0
  27. package/dist/chain/instructions.market.js +107 -0
  28. package/dist/chain/instructions.oracle.d.ts +38 -0
  29. package/dist/chain/instructions.oracle.js +63 -0
  30. package/dist/chain/instructions.position.d.ts +82 -0
  31. package/dist/chain/instructions.position.js +240 -0
  32. package/dist/chain/instructions.resolve.test.d.ts +1 -0
  33. package/dist/chain/instructions.resolve.test.js +32 -0
  34. package/dist/chain/instructions.shared.d.ts +23 -0
  35. package/dist/chain/instructions.shared.js +55 -0
  36. package/dist/chain/orders.d.ts +40 -0
  37. package/dist/chain/orders.js +117 -0
  38. package/dist/chain/orders.test.d.ts +1 -0
  39. package/dist/chain/orders.test.js +67 -0
  40. package/dist/chain/signers.d.ts +9 -0
  41. package/dist/chain/signers.js +19 -0
  42. package/dist/chain/signers.test.d.ts +1 -0
  43. package/dist/chain/signers.test.js +13 -0
  44. package/dist/chain/token.d.ts +59 -0
  45. package/dist/chain/token.js +136 -0
  46. package/dist/chain/token.test.d.ts +1 -0
  47. package/dist/chain/token.test.js +69 -0
  48. package/dist/chain/tx.d.ts +72 -0
  49. package/dist/chain/tx.js +97 -0
  50. package/dist/cjs/actaClient.js +103 -0
  51. package/dist/cjs/chain/client.js +119 -0
  52. package/dist/cjs/chain/fetch.js +431 -0
  53. package/dist/cjs/chain/fetch.test.js +160 -0
  54. package/dist/cjs/chain/flows/index.js +17 -0
  55. package/dist/cjs/chain/flows/openPosition.js +81 -0
  56. package/dist/cjs/chain/flows/openPosition.test.js +45 -0
  57. package/dist/cjs/chain/helpers.js +67 -0
  58. package/dist/cjs/chain/index.js +48 -0
  59. package/dist/cjs/chain/instructions.admin.js +119 -0
  60. package/dist/cjs/chain/instructions.js +21 -0
  61. package/dist/cjs/chain/instructions.maker.js +92 -0
  62. package/dist/cjs/chain/instructions.market.js +112 -0
  63. package/dist/cjs/chain/instructions.oracle.js +70 -0
  64. package/dist/cjs/chain/instructions.position.js +247 -0
  65. package/dist/cjs/chain/instructions.resolve.test.js +34 -0
  66. package/dist/cjs/chain/instructions.shared.js +64 -0
  67. package/dist/cjs/chain/orders.js +126 -0
  68. package/dist/cjs/chain/orders.test.js +69 -0
  69. package/dist/cjs/chain/signers.js +22 -0
  70. package/dist/cjs/chain/signers.test.js +15 -0
  71. package/dist/cjs/chain/token.js +147 -0
  72. package/dist/cjs/chain/token.test.js +71 -0
  73. package/dist/cjs/chain/tx.js +103 -0
  74. package/dist/cjs/constants.js +6 -0
  75. package/dist/cjs/constants.test.js +31 -0
  76. package/dist/cjs/events.js +75 -0
  77. package/dist/cjs/events.test.js +384 -0
  78. package/dist/cjs/generated/accounts/config.js +79 -0
  79. package/dist/cjs/generated/accounts/index.js +28 -0
  80. package/dist/cjs/generated/accounts/maker.js +71 -0
  81. package/dist/cjs/generated/accounts/market.js +93 -0
  82. package/dist/cjs/generated/accounts/oracle.js +85 -0
  83. package/dist/cjs/generated/accounts/position.js +89 -0
  84. package/dist/cjs/generated/errors/actaContract.js +154 -0
  85. package/dist/cjs/generated/errors/index.js +24 -0
  86. package/dist/cjs/generated/index.js +28 -0
  87. package/dist/cjs/generated/instructions/changeOracleSource.js +81 -0
  88. package/dist/cjs/generated/instructions/closeMarket.js +83 -0
  89. package/dist/cjs/generated/instructions/closeOracle.js +79 -0
  90. package/dist/cjs/generated/instructions/createMarket.js +116 -0
  91. package/dist/cjs/generated/instructions/createOracle.js +99 -0
  92. package/dist/cjs/generated/instructions/depositFundsToPosition.js +101 -0
  93. package/dist/cjs/generated/instructions/depositPremium.js +96 -0
  94. package/dist/cjs/generated/instructions/finalizeMarket.js +91 -0
  95. package/dist/cjs/generated/instructions/index.js +43 -0
  96. package/dist/cjs/generated/instructions/initializeConfig.js +108 -0
  97. package/dist/cjs/generated/instructions/liquidatePosition.js +107 -0
  98. package/dist/cjs/generated/instructions/openPosition.js +154 -0
  99. package/dist/cjs/generated/instructions/registerMaker.js +90 -0
  100. package/dist/cjs/generated/instructions/setOracleConfig.js +80 -0
  101. package/dist/cjs/generated/instructions/settlePosition.js +104 -0
  102. package/dist/cjs/generated/instructions/topupFeeFund.js +96 -0
  103. package/dist/cjs/generated/instructions/updateConfig.js +86 -0
  104. package/dist/cjs/generated/instructions/updateMakerQuoteSigning.js +79 -0
  105. package/dist/cjs/generated/instructions/updateMarketOracles.js +96 -0
  106. package/dist/cjs/generated/instructions/updateOraclePrice.js +89 -0
  107. package/dist/cjs/generated/instructions/withdrawFromFeeFund.js +96 -0
  108. package/dist/cjs/generated/instructions/withdrawPremium.js +96 -0
  109. package/dist/cjs/generated/programs/actaContract.js +108 -0
  110. package/dist/cjs/generated/programs/index.js +24 -0
  111. package/dist/cjs/generated/shared/index.js +94 -0
  112. package/dist/cjs/generated/types/actaEvent.js +342 -0
  113. package/dist/cjs/generated/types/eventKind.js +45 -0
  114. package/dist/cjs/generated/types/index.js +27 -0
  115. package/dist/cjs/generated/types/positionStatus.js +31 -0
  116. package/dist/cjs/generated/types/positionType.js +28 -0
  117. package/dist/cjs/idl/acta_contract.json +2329 -0
  118. package/dist/cjs/idl/hash.js +4 -0
  119. package/dist/cjs/index.js +84 -0
  120. package/dist/cjs/nonce.js +93 -0
  121. package/dist/cjs/package.json +3 -0
  122. package/dist/cjs/types/index.js +18 -0
  123. package/dist/cjs/types/orderId.js +40 -0
  124. package/dist/cjs/types/orders.js +10 -0
  125. package/dist/cjs/ws/apy.js +106 -0
  126. package/dist/cjs/ws/apy.test.js +29 -0
  127. package/dist/cjs/ws/auth.js +104 -0
  128. package/dist/cjs/ws/client.handshake.integration.test.js +69 -0
  129. package/dist/cjs/ws/client.js +861 -0
  130. package/dist/cjs/ws/client.test.js +230 -0
  131. package/dist/cjs/ws/discovery.js +48 -0
  132. package/dist/cjs/ws/flows.js +101 -0
  133. package/dist/cjs/ws/flows.test.js +85 -0
  134. package/dist/cjs/ws/index.js +23 -0
  135. package/dist/cjs/ws/sponsoredTx.js +95 -0
  136. package/dist/cjs/ws/types.js +14 -0
  137. package/dist/cjs/ws/wirePolicy.js +34 -0
  138. package/dist/constants.d.ts +2 -0
  139. package/dist/constants.js +3 -0
  140. package/dist/constants.test.d.ts +1 -0
  141. package/dist/constants.test.js +29 -0
  142. package/dist/events.d.ts +15 -0
  143. package/dist/events.js +64 -0
  144. package/dist/events.test.d.ts +1 -0
  145. package/dist/events.test.js +382 -0
  146. package/dist/generated/accounts/config.d.ts +47 -0
  147. package/dist/generated/accounts/config.js +68 -0
  148. package/dist/generated/accounts/index.d.ts +12 -0
  149. package/dist/generated/accounts/index.js +12 -0
  150. package/dist/generated/accounts/maker.d.ts +39 -0
  151. package/dist/generated/accounts/maker.js +60 -0
  152. package/dist/generated/accounts/market.d.ts +61 -0
  153. package/dist/generated/accounts/market.js +82 -0
  154. package/dist/generated/accounts/oracle.d.ts +53 -0
  155. package/dist/generated/accounts/oracle.js +74 -0
  156. package/dist/generated/accounts/position.d.ts +57 -0
  157. package/dist/generated/accounts/position.js +78 -0
  158. package/dist/generated/errors/actaContract.d.ts +103 -0
  159. package/dist/generated/errors/actaContract.js +149 -0
  160. package/dist/generated/errors/index.d.ts +8 -0
  161. package/dist/generated/errors/index.js +8 -0
  162. package/dist/generated/index.d.ts +12 -0
  163. package/dist/generated/index.js +12 -0
  164. package/dist/generated/instructions/changeOracleSource.d.ts +50 -0
  165. package/dist/generated/instructions/changeOracleSource.js +72 -0
  166. package/dist/generated/instructions/closeMarket.d.ts +58 -0
  167. package/dist/generated/instructions/closeMarket.js +74 -0
  168. package/dist/generated/instructions/closeOracle.d.ts +48 -0
  169. package/dist/generated/instructions/closeOracle.js +70 -0
  170. package/dist/generated/instructions/createMarket.d.ts +90 -0
  171. package/dist/generated/instructions/createMarket.js +107 -0
  172. package/dist/generated/instructions/createOracle.d.ts +71 -0
  173. package/dist/generated/instructions/createOracle.js +90 -0
  174. package/dist/generated/instructions/depositFundsToPosition.d.ts +73 -0
  175. package/dist/generated/instructions/depositFundsToPosition.js +92 -0
  176. package/dist/generated/instructions/depositPremium.d.ts +62 -0
  177. package/dist/generated/instructions/depositPremium.js +87 -0
  178. package/dist/generated/instructions/finalizeMarket.d.ts +63 -0
  179. package/dist/generated/instructions/finalizeMarket.js +82 -0
  180. package/dist/generated/instructions/index.d.ts +27 -0
  181. package/dist/generated/instructions/index.js +27 -0
  182. package/dist/generated/instructions/initializeConfig.d.ts +75 -0
  183. package/dist/generated/instructions/initializeConfig.js +99 -0
  184. package/dist/generated/instructions/liquidatePosition.d.ts +78 -0
  185. package/dist/generated/instructions/liquidatePosition.js +98 -0
  186. package/dist/generated/instructions/openPosition.d.ts +130 -0
  187. package/dist/generated/instructions/openPosition.js +145 -0
  188. package/dist/generated/instructions/registerMaker.d.ts +57 -0
  189. package/dist/generated/instructions/registerMaker.js +81 -0
  190. package/dist/generated/instructions/setOracleConfig.d.ts +53 -0
  191. package/dist/generated/instructions/setOracleConfig.js +71 -0
  192. package/dist/generated/instructions/settlePosition.d.ts +78 -0
  193. package/dist/generated/instructions/settlePosition.js +95 -0
  194. package/dist/generated/instructions/topupFeeFund.d.ts +67 -0
  195. package/dist/generated/instructions/topupFeeFund.js +87 -0
  196. package/dist/generated/instructions/updateConfig.d.ts +58 -0
  197. package/dist/generated/instructions/updateConfig.js +77 -0
  198. package/dist/generated/instructions/updateMakerQuoteSigning.d.ts +47 -0
  199. package/dist/generated/instructions/updateMakerQuoteSigning.js +70 -0
  200. package/dist/generated/instructions/updateMarketOracles.d.ts +65 -0
  201. package/dist/generated/instructions/updateMarketOracles.js +87 -0
  202. package/dist/generated/instructions/updateOraclePrice.d.ts +55 -0
  203. package/dist/generated/instructions/updateOraclePrice.js +80 -0
  204. package/dist/generated/instructions/withdrawFromFeeFund.d.ts +62 -0
  205. package/dist/generated/instructions/withdrawFromFeeFund.js +87 -0
  206. package/dist/generated/instructions/withdrawPremium.d.ts +62 -0
  207. package/dist/generated/instructions/withdrawPremium.js +87 -0
  208. package/dist/generated/programs/actaContract.d.ts +83 -0
  209. package/dist/generated/programs/actaContract.js +104 -0
  210. package/dist/generated/programs/index.d.ts +8 -0
  211. package/dist/generated/programs/index.js +8 -0
  212. package/dist/generated/shared/index.d.ts +49 -0
  213. package/dist/generated/shared/index.js +86 -0
  214. package/dist/generated/types/actaEvent.d.ts +249 -0
  215. package/dist/generated/types/actaEvent.js +335 -0
  216. package/dist/generated/types/eventKind.d.ts +33 -0
  217. package/dist/generated/types/eventKind.js +39 -0
  218. package/dist/generated/types/index.d.ts +11 -0
  219. package/dist/generated/types/index.js +11 -0
  220. package/dist/generated/types/positionStatus.d.ts +19 -0
  221. package/dist/generated/types/positionStatus.js +25 -0
  222. package/dist/generated/types/positionType.d.ts +16 -0
  223. package/dist/generated/types/positionType.js +22 -0
  224. package/dist/idl/acta_contract.json +2329 -0
  225. package/dist/idl/hash.d.ts +1 -0
  226. package/dist/idl/hash.js +1 -0
  227. package/dist/index.d.ts +109 -0
  228. package/dist/index.js +34 -0
  229. package/dist/nonce.d.ts +29 -0
  230. package/dist/nonce.js +89 -0
  231. package/dist/types/index.d.ts +2 -0
  232. package/dist/types/index.js +2 -0
  233. package/dist/types/orderId.d.ts +13 -0
  234. package/dist/types/orderId.js +35 -0
  235. package/dist/types/orders.d.ts +9 -0
  236. package/dist/types/orders.js +9 -0
  237. package/dist/ws/apy.d.ts +79 -0
  238. package/dist/ws/apy.js +98 -0
  239. package/dist/ws/apy.test.d.ts +1 -0
  240. package/dist/ws/apy.test.js +27 -0
  241. package/dist/ws/auth.d.ts +67 -0
  242. package/dist/ws/auth.js +98 -0
  243. package/dist/ws/client.d.ts +263 -0
  244. package/dist/ws/client.handshake.integration.test.d.ts +1 -0
  245. package/dist/ws/client.handshake.integration.test.js +64 -0
  246. package/dist/ws/client.js +857 -0
  247. package/dist/ws/client.test.d.ts +1 -0
  248. package/dist/ws/client.test.js +228 -0
  249. package/dist/ws/discovery.d.ts +13 -0
  250. package/dist/ws/discovery.js +44 -0
  251. package/dist/ws/flows.d.ts +44 -0
  252. package/dist/ws/flows.js +96 -0
  253. package/dist/ws/flows.test.d.ts +1 -0
  254. package/dist/ws/flows.test.js +83 -0
  255. package/dist/ws/index.d.ts +7 -0
  256. package/dist/ws/index.js +7 -0
  257. package/dist/ws/sponsoredTx.d.ts +39 -0
  258. package/dist/ws/sponsoredTx.js +92 -0
  259. package/dist/ws/types.d.ts +900 -0
  260. package/dist/ws/types.js +13 -0
  261. package/dist/ws/wirePolicy.d.ts +12 -0
  262. package/dist/ws/wirePolicy.js +30 -0
  263. package/package.json +87 -0
@@ -0,0 +1 @@
1
+ export declare const ACTA_IDL_SHA256 = "1466fbb647b4c33af315eefbb61e5e572ce14f22f16d050ac2894f8e017aaf66";
@@ -0,0 +1 @@
1
+ export const ACTA_IDL_SHA256 = "1466fbb647b4c33af315eefbb61e5e572ce14f22f16d050ac2894f8e017aaf66";
@@ -0,0 +1,109 @@
1
+ import type { Address } from "@solana/addresses";
2
+ import { ACTA_IDL_SHA256 } from "./idl/hash";
3
+ export * from "./constants";
4
+ export * from "./generated/accounts";
5
+ export * from "./generated/errors";
6
+ export * from "./generated/instructions";
7
+ export * from "./generated/types";
8
+ export * from "./actaClient";
9
+ export * from "./events";
10
+ export * from "./nonce";
11
+ export * as ws from "./ws";
12
+ export * as chain from "./chain";
13
+ export * as types from "./types";
14
+ export { ActaWsClient } from "./ws/client";
15
+ export type { ActaWsClientOptions, ActaWsClientEvents, ClientRole as ActaWsClientRole, ConnectionState as ActaWsConnectionState, ClientState as ActaWsClientState, WebSocketFactory as ActaWsWebSocketFactory, WebSocketLike as ActaWsWebSocketLike, } from "./ws/client";
16
+ export { KeypairAuthProvider, WalletAuthProvider, CustomAuthProvider, } from "./ws/auth";
17
+ export type { AuthProvider, WalletLike } from "./ws/auth";
18
+ export { ActaChainClient } from "./chain/client";
19
+ export type { ChainOptions } from "./chain/client";
20
+ export * from "./chain/tx";
21
+ export declare const ACTA_IDL: {
22
+ version: string;
23
+ name: string;
24
+ instructions: {
25
+ name: string;
26
+ accounts: {
27
+ name: string;
28
+ isMut: boolean;
29
+ isSigner: boolean;
30
+ docs: string[];
31
+ }[];
32
+ args: ({
33
+ name: string;
34
+ type: string;
35
+ } | {
36
+ name: string;
37
+ type: {
38
+ array: (string | number)[];
39
+ };
40
+ })[];
41
+ discriminant: {
42
+ type: string;
43
+ value: number;
44
+ };
45
+ }[];
46
+ accounts: {
47
+ name: string;
48
+ type: {
49
+ kind: string;
50
+ fields: ({
51
+ name: string;
52
+ type: string;
53
+ } | {
54
+ name: string;
55
+ type: {
56
+ array: (string | number)[];
57
+ };
58
+ })[];
59
+ };
60
+ }[];
61
+ types: ({
62
+ name: string;
63
+ type: {
64
+ kind: string;
65
+ variants: {
66
+ name: string;
67
+ }[];
68
+ };
69
+ } | {
70
+ name: string;
71
+ type: {
72
+ kind: string;
73
+ variants: {
74
+ name: string;
75
+ fields: ({
76
+ name: string;
77
+ type: string;
78
+ } | {
79
+ name: string;
80
+ type: {
81
+ array: (string | number)[];
82
+ };
83
+ })[];
84
+ }[];
85
+ };
86
+ })[];
87
+ metadata: {
88
+ origin: string;
89
+ address: string;
90
+ acta: {
91
+ events: {
92
+ version: number;
93
+ format: string;
94
+ types: never[];
95
+ fields: {};
96
+ };
97
+ };
98
+ };
99
+ errors: {
100
+ code: number;
101
+ name: string;
102
+ msg: string;
103
+ }[];
104
+ };
105
+ export declare function getActaProgramId(): Address<string>;
106
+ export declare function setActaProgramId(programId: Address<string>): void;
107
+ export declare function clearActaProgramId(): void;
108
+ export declare const ACTA_PROGRAM_ID: Address<string>;
109
+ export { ACTA_IDL_SHA256 };
package/dist/index.js ADDED
@@ -0,0 +1,34 @@
1
+ import idl from "./idl/acta_contract.json";
2
+ import { ACTA_IDL_SHA256 } from "./idl/hash";
3
+ import { ACTA_CONTRACT_PROGRAM_ADDRESS } from "./generated/programs";
4
+ export * from "./constants";
5
+ export * from "./generated/accounts";
6
+ export * from "./generated/errors";
7
+ export * from "./generated/instructions";
8
+ export * from "./generated/types";
9
+ export * from "./actaClient";
10
+ export * from "./events";
11
+ export * from "./nonce";
12
+ // New SDK surface (namespaced).
13
+ export * as ws from "./ws";
14
+ export * as chain from "./chain";
15
+ export * as types from "./types";
16
+ export { ActaWsClient } from "./ws/client";
17
+ export { KeypairAuthProvider, WalletAuthProvider, CustomAuthProvider, } from "./ws/auth";
18
+ export { ActaChainClient } from "./chain/client";
19
+ export * from "./chain/tx";
20
+ export const ACTA_IDL = idl;
21
+ // --- Program ID override (useful for tests/devnet deployments) ---
22
+ let PROGRAM_ID_OVERRIDE = null;
23
+ export function getActaProgramId() {
24
+ return (PROGRAM_ID_OVERRIDE ?? ACTA_CONTRACT_PROGRAM_ADDRESS);
25
+ }
26
+ export function setActaProgramId(programId) {
27
+ PROGRAM_ID_OVERRIDE = programId;
28
+ }
29
+ export function clearActaProgramId() {
30
+ PROGRAM_ID_OVERRIDE = null;
31
+ }
32
+ // Default program id exports.
33
+ export const ACTA_PROGRAM_ID = ACTA_CONTRACT_PROGRAM_ADDRESS;
34
+ export { ACTA_IDL_SHA256 };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Nonce helpers for maker quotes.
3
+ *
4
+ * IMPORTANT:
5
+ * - If you don't pass a seed, the SDK will try to use `crypto.getRandomValues`.
6
+ * - If no crypto source is available, you MUST provide a 32-byte seed.
7
+ */
8
+ /**
9
+ * Xoshiro256++ nonce generator (fast, deterministic).
10
+ * Seed can be passed explicitly (recommended for reproducibility),
11
+ * or generated internally if crypto.getRandomValues is available.
12
+ */
13
+ export declare class NonceGenerator {
14
+ private s0;
15
+ private s1;
16
+ private s2;
17
+ private s3;
18
+ private seed;
19
+ constructor(seed?: Uint8Array);
20
+ /** Return a copy of the seed used to initialize this generator. */
21
+ getSeed(): Uint8Array;
22
+ /** Return next raw u64 (as bigint). */
23
+ nextU64(): bigint;
24
+ /**
25
+ * Return a nonce safe for JSON (`number`).
26
+ * Uses lower 53 bits to avoid precision loss.
27
+ */
28
+ nextNonce(): number;
29
+ }
package/dist/nonce.js ADDED
@@ -0,0 +1,89 @@
1
+ /**
2
+ * Nonce helpers for maker quotes.
3
+ *
4
+ * IMPORTANT:
5
+ * - If you don't pass a seed, the SDK will try to use `crypto.getRandomValues`.
6
+ * - If no crypto source is available, you MUST provide a 32-byte seed.
7
+ */
8
+ const MASK_64 = (1n << 64n) - 1n;
9
+ const MAX_SAFE_U64 = (1n << 53n) - 1n;
10
+ const SEED_SIZE = 32;
11
+ function u64(x) {
12
+ return x & MASK_64;
13
+ }
14
+ function rotl(x, k) {
15
+ return u64((x << k) | (x >> (64n - k)));
16
+ }
17
+ function readU64LE(bytes, offset) {
18
+ let out = 0n;
19
+ for (let i = 7; i >= 0; i--) {
20
+ out = (out << 8n) | BigInt(bytes[offset + i]);
21
+ }
22
+ return out;
23
+ }
24
+ function ensureNonZeroState(state) {
25
+ if ((state[0] | state[1] | state[2] | state[3]) === 0n) {
26
+ throw new Error("nonce seed must not be all zeros");
27
+ }
28
+ }
29
+ function makeSeed() {
30
+ if (globalThis.crypto?.getRandomValues) {
31
+ const seed = new Uint8Array(SEED_SIZE);
32
+ globalThis.crypto.getRandomValues(seed);
33
+ return seed;
34
+ }
35
+ throw new Error("no crypto.getRandomValues available; provide a 32-byte seed explicitly");
36
+ }
37
+ /**
38
+ * Xoshiro256++ nonce generator (fast, deterministic).
39
+ * Seed can be passed explicitly (recommended for reproducibility),
40
+ * or generated internally if crypto.getRandomValues is available.
41
+ */
42
+ export class NonceGenerator {
43
+ s0;
44
+ s1;
45
+ s2;
46
+ s3;
47
+ seed;
48
+ constructor(seed) {
49
+ const resolvedSeed = seed ? new Uint8Array(seed) : makeSeed();
50
+ if (resolvedSeed.length !== SEED_SIZE) {
51
+ throw new Error(`nonce seed must be 32 bytes, got ${resolvedSeed.length}`);
52
+ }
53
+ const state = [
54
+ readU64LE(resolvedSeed, 0),
55
+ readU64LE(resolvedSeed, 8),
56
+ readU64LE(resolvedSeed, 16),
57
+ readU64LE(resolvedSeed, 24),
58
+ ];
59
+ ensureNonZeroState(state);
60
+ this.s0 = u64(state[0]);
61
+ this.s1 = u64(state[1]);
62
+ this.s2 = u64(state[2]);
63
+ this.s3 = u64(state[3]);
64
+ this.seed = resolvedSeed;
65
+ }
66
+ /** Return a copy of the seed used to initialize this generator. */
67
+ getSeed() {
68
+ return new Uint8Array(this.seed);
69
+ }
70
+ /** Return next raw u64 (as bigint). */
71
+ nextU64() {
72
+ const result = u64(rotl(u64(this.s0 + this.s3), 23n) + this.s0);
73
+ const t = u64(this.s1 << 17n);
74
+ this.s2 ^= this.s0;
75
+ this.s3 ^= this.s1;
76
+ this.s1 ^= this.s2;
77
+ this.s0 ^= this.s3;
78
+ this.s2 ^= t;
79
+ this.s3 = rotl(this.s3, 45n);
80
+ return result;
81
+ }
82
+ /**
83
+ * Return a nonce safe for JSON (`number`).
84
+ * Uses lower 53 bits to avoid precision loss.
85
+ */
86
+ nextNonce() {
87
+ return Number(this.nextU64() & MAX_SAFE_U64);
88
+ }
89
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./orders";
2
+ export * from "./orderId";
@@ -0,0 +1,2 @@
1
+ export * from "./orders";
2
+ export * from "./orderId";
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Canonical OrderId representation across the SDK.
3
+ *
4
+ * Contract expects `orderId` as **32 bytes**: `sha256(order_preimage_182)`.
5
+ *
6
+ * - In TypeScript we represent it as `Uint8Array` of length 32.
7
+ * - For JSON/Wire formats we typically use **hex** (64 chars) when needed.
8
+ */
9
+ export type OrderId32 = Uint8Array;
10
+ export type OrderIdHex32 = string;
11
+ export declare function assertOrderId32(orderId: Uint8Array): asserts orderId is OrderId32;
12
+ export declare function orderIdToHex(orderId: Uint8Array): OrderIdHex32;
13
+ export declare function orderIdFromHex(hex: string): OrderId32;
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Canonical OrderId representation across the SDK.
3
+ *
4
+ * Contract expects `orderId` as **32 bytes**: `sha256(order_preimage_182)`.
5
+ *
6
+ * - In TypeScript we represent it as `Uint8Array` of length 32.
7
+ * - For JSON/Wire formats we typically use **hex** (64 chars) when needed.
8
+ */
9
+ export function assertOrderId32(orderId) {
10
+ if (!(orderId instanceof Uint8Array)) {
11
+ throw new Error("orderId must be a Uint8Array");
12
+ }
13
+ if (orderId.length !== 32) {
14
+ throw new Error(`orderId must be 32 bytes, got ${orderId.length}`);
15
+ }
16
+ }
17
+ export function orderIdToHex(orderId) {
18
+ assertOrderId32(orderId);
19
+ return Buffer.from(orderId).toString("hex");
20
+ }
21
+ export function orderIdFromHex(hex) {
22
+ if (typeof hex !== "string") {
23
+ throw new Error("orderId hex must be a string");
24
+ }
25
+ const cleaned = hex.startsWith("0x") ? hex.slice(2) : hex;
26
+ if (cleaned.length !== 64) {
27
+ throw new Error(`orderId hex must be 64 hex chars (32 bytes), got ${cleaned.length}`);
28
+ }
29
+ if (!/^[0-9a-fA-F]+$/.test(cleaned)) {
30
+ throw new Error("orderId hex contains non-hex characters");
31
+ }
32
+ const bytes = Uint8Array.from(Buffer.from(cleaned, "hex"));
33
+ assertOrderId32(bytes);
34
+ return bytes;
35
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * SDK-level order-related *types*.
3
+ *
4
+ * These types are shared across on-chain flows and any off-chain tooling that needs
5
+ * to construct or reason about order preimages / orderIds.
6
+ *
7
+ * Implementation lives in `src/chain/orders.ts`.
8
+ */
9
+ export type { OrderPreimageArgs, OrderSignatureParams, SignerLike, } from "../chain/orders";
@@ -0,0 +1,9 @@
1
+ /**
2
+ * SDK-level order-related *types*.
3
+ *
4
+ * These types are shared across on-chain flows and any off-chain tooling that needs
5
+ * to construct or reason about order preimages / orderIds.
6
+ *
7
+ * Implementation lives in `src/chain/orders.ts`.
8
+ */
9
+ export {};
@@ -0,0 +1,79 @@
1
+ /**
2
+ * APY / annualized premium yield helpers for WS-only frontends.
3
+ *
4
+ * Goal: given a quote premium and an option term, compute:
5
+ * - "how much premium will I receive?" (notional)
6
+ * - "what is that as APR/APY?" (annualized)
7
+ *
8
+ * This is a display metric (premium / collateral), NOT an expected return model.
9
+ */
10
+ export declare const PRICE_SCALE = 1000000000;
11
+ export declare const SECONDS_PER_YEAR: number;
12
+ export type PositionTypeWire = "covered_call" | "cash_secured_put";
13
+ /** Annualize a term yield over `secondsToExpiry`. */
14
+ export declare function annualizeYield(args: {
15
+ /** Term yield in [0..] */
16
+ termYield: number;
17
+ secondsToExpiry: number;
18
+ }): {
19
+ apr: number;
20
+ apy: number;
21
+ };
22
+ /** term_yield = premium_notional / collateral_notional */
23
+ export declare function termYieldFromNotionals(args: {
24
+ premiumNotional: number;
25
+ collateralNotional: number;
26
+ }): number;
27
+ /**
28
+ * Compute premium/collateral notionals and APR/APY from *human unit* amounts.
29
+ *
30
+ * Example (your case):
31
+ * - covered_call
32
+ * - underlyingAmount = 10 SOL
33
+ * - spotPrice = 150 USD/SOL => collateralNotional = 1500 USD
34
+ * - premiumPerUnderlying = 1.5 USD/SOL => premiumNotional = 15 USD
35
+ * - 7 days => ~52% APR (simple)
36
+ */
37
+ export declare function computeApyFromAmounts(args: {
38
+ positionType: PositionTypeWire;
39
+ /** underlying amount in human units (e.g. 10 SOL) */
40
+ underlyingAmount: number;
41
+ /** premium per 1 underlying in quote units (e.g. USD), gross */
42
+ premiumPerUnderlying: number;
43
+ /** for puts: strike price per 1 underlying (quote units) */
44
+ strikePrice: number;
45
+ /** for calls: current spot price per 1 underlying (quote units) */
46
+ spotPrice?: number;
47
+ /** seconds to option expiry (market expiry, not RFQ TTL) */
48
+ secondsToExpiry: number;
49
+ }): {
50
+ premiumNotional: number;
51
+ collateralNotional: number;
52
+ termYield: number;
53
+ apr: number;
54
+ apy: number;
55
+ };
56
+ /** Convert a 1e9-scaled u64 price into a float price. */
57
+ export declare function scaledPriceToFloat(price1e9: bigint | number): number;
58
+ /**
59
+ * Convenience wrapper for WS-native numeric fields (1e9-scale).
60
+ *
61
+ * - `grossPremiumPerUnit1e9` is WS quote `price`
62
+ * - `strike1e9` is RFQ/quote strike
63
+ * - `spotPrice1e9` must be provided for covered_call (from your own spot feed)
64
+ * - `secondsToExpiry` should be (market.expiry_ts - now)
65
+ */
66
+ export declare function computeApyFromScaledPrices(args: {
67
+ positionType: PositionTypeWire;
68
+ underlyingAmount: number;
69
+ grossPremiumPerUnit1e9: bigint | number;
70
+ strike1e9: bigint | number;
71
+ spotPrice1e9?: bigint | number;
72
+ secondsToExpiry: number;
73
+ }): {
74
+ premiumNotional: number;
75
+ collateralNotional: number;
76
+ termYield: number;
77
+ apr: number;
78
+ apy: number;
79
+ };
package/dist/ws/apy.js ADDED
@@ -0,0 +1,98 @@
1
+ /**
2
+ * APY / annualized premium yield helpers for WS-only frontends.
3
+ *
4
+ * Goal: given a quote premium and an option term, compute:
5
+ * - "how much premium will I receive?" (notional)
6
+ * - "what is that as APR/APY?" (annualized)
7
+ *
8
+ * This is a display metric (premium / collateral), NOT an expected return model.
9
+ */
10
+ export const PRICE_SCALE = 1_000_000_000;
11
+ export const SECONDS_PER_YEAR = 365 * 24 * 60 * 60;
12
+ /** Annualize a term yield over `secondsToExpiry`. */
13
+ export function annualizeYield(args) {
14
+ const y = args.termYield;
15
+ const dt = args.secondsToExpiry;
16
+ if (!Number.isFinite(y) || y < 0)
17
+ throw new Error("invalid termYield");
18
+ if (!Number.isFinite(dt) || dt <= 0)
19
+ return { apr: 0, apy: 0 };
20
+ const yearOverDt = SECONDS_PER_YEAR / dt;
21
+ const apr = y * yearOverDt;
22
+ const apy = Math.pow(1 + y, yearOverDt) - 1;
23
+ return { apr, apy };
24
+ }
25
+ /** term_yield = premium_notional / collateral_notional */
26
+ export function termYieldFromNotionals(args) {
27
+ const p = args.premiumNotional;
28
+ const c = args.collateralNotional;
29
+ if (!Number.isFinite(p) || p < 0)
30
+ throw new Error("invalid premiumNotional");
31
+ if (!Number.isFinite(c) || c <= 0)
32
+ throw new Error("invalid collateralNotional");
33
+ return p / c;
34
+ }
35
+ /**
36
+ * Compute premium/collateral notionals and APR/APY from *human unit* amounts.
37
+ *
38
+ * Example (your case):
39
+ * - covered_call
40
+ * - underlyingAmount = 10 SOL
41
+ * - spotPrice = 150 USD/SOL => collateralNotional = 1500 USD
42
+ * - premiumPerUnderlying = 1.5 USD/SOL => premiumNotional = 15 USD
43
+ * - 7 days => ~52% APR (simple)
44
+ */
45
+ export function computeApyFromAmounts(args) {
46
+ const qty = args.underlyingAmount;
47
+ if (!Number.isFinite(qty) || qty <= 0)
48
+ throw new Error("invalid underlyingAmount");
49
+ const premPU = args.premiumPerUnderlying;
50
+ if (!Number.isFinite(premPU) || premPU < 0)
51
+ throw new Error("invalid premiumPerUnderlying");
52
+ const premiumNotional = qty * premPU;
53
+ let collateralPerUnderlying;
54
+ if (args.positionType === "cash_secured_put") {
55
+ collateralPerUnderlying = args.strikePrice;
56
+ }
57
+ else {
58
+ collateralPerUnderlying = args.spotPrice ?? NaN;
59
+ }
60
+ if (!Number.isFinite(collateralPerUnderlying) || collateralPerUnderlying <= 0) {
61
+ throw new Error(args.positionType === "covered_call"
62
+ ? "spotPrice is required for covered_call yield"
63
+ : "invalid strikePrice");
64
+ }
65
+ const collateralNotional = qty * collateralPerUnderlying;
66
+ const termYield = termYieldFromNotionals({ premiumNotional, collateralNotional });
67
+ const { apr, apy } = annualizeYield({ termYield, secondsToExpiry: args.secondsToExpiry });
68
+ return { premiumNotional, collateralNotional, termYield, apr, apy };
69
+ }
70
+ function toNum(v) {
71
+ // Note: bigint->number can lose precision for huge values; acceptable for UI display.
72
+ return typeof v === "bigint" ? Number(v) : v;
73
+ }
74
+ /** Convert a 1e9-scaled u64 price into a float price. */
75
+ export function scaledPriceToFloat(price1e9) {
76
+ const n = toNum(price1e9);
77
+ if (!Number.isFinite(n))
78
+ throw new Error("invalid price1e9");
79
+ return n / PRICE_SCALE;
80
+ }
81
+ /**
82
+ * Convenience wrapper for WS-native numeric fields (1e9-scale).
83
+ *
84
+ * - `grossPremiumPerUnit1e9` is WS quote `price`
85
+ * - `strike1e9` is RFQ/quote strike
86
+ * - `spotPrice1e9` must be provided for covered_call (from your own spot feed)
87
+ * - `secondsToExpiry` should be (market.expiry_ts - now)
88
+ */
89
+ export function computeApyFromScaledPrices(args) {
90
+ return computeApyFromAmounts({
91
+ positionType: args.positionType,
92
+ underlyingAmount: args.underlyingAmount,
93
+ premiumPerUnderlying: scaledPriceToFloat(args.grossPremiumPerUnit1e9),
94
+ strikePrice: scaledPriceToFloat(args.strike1e9),
95
+ spotPrice: args.spotPrice1e9 == null ? undefined : scaledPriceToFloat(args.spotPrice1e9),
96
+ secondsToExpiry: args.secondsToExpiry,
97
+ });
98
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,27 @@
1
+ import { computeApyFromAmounts, computeApyFromScaledPrices } from "./apy";
2
+ describe("ws apy helpers", () => {
3
+ it("matches the intuitive 10 SOL @ $150, $15 premium, 7 days => ~52% APR", () => {
4
+ const res = computeApyFromAmounts({
5
+ positionType: "covered_call",
6
+ underlyingAmount: 10,
7
+ spotPrice: 150,
8
+ strikePrice: 150, // not used for calls
9
+ premiumPerUnderlying: 1.5,
10
+ secondsToExpiry: 7 * 24 * 60 * 60,
11
+ });
12
+ expect(res.premiumNotional).toBeCloseTo(15, 10);
13
+ expect(res.collateralNotional).toBeCloseTo(1500, 10);
14
+ expect(res.termYield).toBeCloseTo(0.01, 10);
15
+ expect(res.apr).toBeCloseTo(0.01 * (365 / 7), 3);
16
+ });
17
+ it("scaled wrapper works (1e9 scale)", () => {
18
+ const res = computeApyFromScaledPrices({
19
+ positionType: "cash_secured_put",
20
+ underlyingAmount: 10,
21
+ grossPremiumPerUnit1e9: 1_500_000_000, // 1.5
22
+ strike1e9: 150_000_000_000, // 150
23
+ secondsToExpiry: 7 * 24 * 60 * 60,
24
+ });
25
+ expect(res.premiumNotional).toBeCloseTo(15, 10);
26
+ });
27
+ });
@@ -0,0 +1,67 @@
1
+ /**
2
+ * WebSocket auth providers.
3
+ *
4
+ * RFQ WS auth handshake:
5
+ * - Server sends `AuthRequest { challenge: <human-readable text> }`
6
+ * - Client signs UTF-8 bytes of that text and responds:
7
+ * `AuthChallenge { challenge, signature: base58(ed25519(utf8(challenge))), pubkey }`
8
+ *
9
+ * Source of truth (server):
10
+ * - rust-backend/rfq-server/src/server/ws.rs
11
+ * - rust-backend/rfq-server/src/session/handler.rs
12
+ */
13
+ export interface AuthProvider {
14
+ /** Base58-encoded public key (address). */
15
+ getPublicKey(): Promise<string>;
16
+ /** Base58-encoded ed25519 signature over UTF-8 bytes of the challenge text. */
17
+ signChallenge(challenge: string): Promise<string>;
18
+ }
19
+ /**
20
+ * Auth provider backed by a `CryptoKeyPair`.
21
+ *
22
+ */
23
+ export declare class KeypairAuthProvider implements AuthProvider {
24
+ private readonly address;
25
+ private readonly privateKey;
26
+ private constructor();
27
+ /** Create from a Solana CLI-style 64-byte secret key array (private + public). */
28
+ static fromSecretKeyBytes(secretKey64: Uint8Array): Promise<KeypairAuthProvider>;
29
+ /** Create from a 32-byte private key. */
30
+ static fromPrivateKeyBytes(privateKey32: Uint8Array): Promise<KeypairAuthProvider>;
31
+ /** Create from Solana CLI keypair JSON (array of numbers). */
32
+ static fromJson(json: number[] | string): Promise<KeypairAuthProvider>;
33
+ getPublicKey(): Promise<string>;
34
+ signChallenge(challenge: string): Promise<string>;
35
+ }
36
+ /**
37
+ * Wallet-like provider (browser wallets, custom signers, etc.)
38
+ *
39
+ * We intentionally do NOT depend on @solana/web3.js types. Consumers can adapt their wallet
40
+ * implementation to this minimal interface.
41
+ */
42
+ export declare class WalletAuthProvider implements AuthProvider {
43
+ private readonly wallet;
44
+ constructor(wallet: WalletLike);
45
+ getPublicKey(): Promise<string>;
46
+ signChallenge(challenge: string): Promise<string>;
47
+ }
48
+ export type WalletLike = {
49
+ /** Preferred: base58 address string. */
50
+ publicKeyBase58?: string | null;
51
+ /** Optional: object exposing `toBase58()` (no web3.js import needed). */
52
+ publicKey?: {
53
+ toBase58(): string;
54
+ } | null;
55
+ /** Returns raw 64-byte signature. */
56
+ signMessage(message: Uint8Array): Promise<Uint8Array>;
57
+ };
58
+ /**
59
+ * Custom auth provider for fully custom integrations (hardware wallets, remote signers, etc.).
60
+ */
61
+ export declare class CustomAuthProvider implements AuthProvider {
62
+ private readonly getPublicKeyFn;
63
+ private readonly signChallengeFn;
64
+ constructor(getPublicKey: () => Promise<string>, signChallenge: (challenge: string) => Promise<string>);
65
+ getPublicKey(): Promise<string>;
66
+ signChallenge(challenge: string): Promise<string>;
67
+ }