@n1xyz/nord-ts 0.0.1 → 0.0.5

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 (260) hide show
  1. package/README.md +148 -65
  2. package/dist/bridge/client.d.ts +150 -0
  3. package/dist/bridge/client.js +394 -0
  4. package/dist/bridge/const.d.ts +23 -0
  5. package/dist/bridge/const.js +47 -0
  6. package/dist/bridge/index.d.ts +5 -0
  7. package/dist/bridge/index.js +23 -0
  8. package/dist/bridge/types.d.ts +118 -0
  9. package/dist/bridge/types.js +16 -0
  10. package/dist/bridge/utils.d.ts +64 -0
  11. package/dist/bridge/utils.js +131 -0
  12. package/dist/const.d.ts +2 -5
  13. package/dist/const.js +2 -6
  14. package/dist/gen/common.d.ts +6 -1
  15. package/dist/gen/common.js +19 -9
  16. package/dist/gen/nord.d.ts +76 -21
  17. package/dist/gen/nord.js +987 -423
  18. package/dist/idl/bridge.d.ts +2 -0
  19. package/dist/idl/bridge.js +703 -0
  20. package/dist/index.d.ts +5 -2
  21. package/dist/index.js +18 -2
  22. package/dist/nord/{actions.d.ts → api/actions.d.ts} +6 -6
  23. package/dist/nord/{actions.js → api/actions.js} +6 -10
  24. package/dist/nord/api/core.d.ts +49 -0
  25. package/dist/nord/api/core.js +121 -0
  26. package/dist/nord/api/market.d.ts +36 -0
  27. package/dist/nord/api/market.js +98 -0
  28. package/dist/nord/api/metrics.d.ts +67 -0
  29. package/dist/nord/api/metrics.js +132 -0
  30. package/dist/nord/api/queries.d.ts +81 -0
  31. package/dist/nord/api/queries.js +187 -0
  32. package/dist/nord/client/Nord.d.ts +335 -0
  33. package/dist/nord/client/Nord.js +532 -0
  34. package/dist/nord/client/NordUser.d.ts +320 -0
  35. package/dist/nord/client/NordUser.js +697 -0
  36. package/dist/nord/index.d.ts +9 -2
  37. package/dist/nord/index.js +30 -6
  38. package/dist/nord/models/Subscriber.d.ts +37 -0
  39. package/dist/nord/models/Subscriber.js +25 -0
  40. package/dist/nord/utils/NordError.d.ts +35 -0
  41. package/dist/nord/utils/NordError.js +46 -0
  42. package/dist/types.d.ts +143 -86
  43. package/dist/types.js +12 -1
  44. package/dist/utils.d.ts +9 -0
  45. package/dist/utils.js +20 -1
  46. package/dist/websocket/NordWebSocketClient.d.ts +71 -0
  47. package/dist/websocket/NordWebSocketClient.js +343 -0
  48. package/dist/websocket/events.d.ts +19 -0
  49. package/dist/websocket/events.js +2 -0
  50. package/dist/websocket/index.d.ts +2 -0
  51. package/dist/websocket/index.js +5 -0
  52. package/docs/assets/hierarchy.js +1 -0
  53. package/docs/assets/highlight.css +16 -16
  54. package/docs/assets/icons.js +17 -14
  55. package/docs/assets/icons.svg +1 -1
  56. package/docs/assets/main.js +5 -4
  57. package/docs/assets/navigation.js +1 -1
  58. package/docs/assets/search.js +1 -1
  59. package/docs/assets/style.css +1423 -1227
  60. package/docs/classes/Nord.html +189 -43
  61. package/docs/classes/NordError.html +24 -0
  62. package/docs/classes/NordUser.html +120 -35
  63. package/docs/classes/NordWebSocketClient.html +335 -0
  64. package/docs/classes/SolanaBridgeClient.html +86 -0
  65. package/docs/classes/Subscriber.html +10 -6
  66. package/docs/enums/FillMode.html +5 -5
  67. package/docs/enums/KeyType.html +4 -4
  68. package/docs/enums/MetricPeriod.html +9 -0
  69. package/docs/enums/PdaSeedType.html +9 -0
  70. package/docs/enums/PeakTpsPeriodUnit.html +7 -7
  71. package/docs/enums/Side.html +3 -3
  72. package/docs/enums/WebSocketMessageType.html +7 -0
  73. package/docs/functions/actionQueryRollman.html +6 -0
  74. package/docs/functions/actionsQueryRollman.html +6 -0
  75. package/docs/functions/aggregateMetrics-1.html +7 -0
  76. package/docs/functions/assert.html +1 -1
  77. package/docs/functions/bigIntToProtoU128.html +3 -3
  78. package/docs/functions/blockQueryRollman.html +6 -0
  79. package/docs/functions/blockSummaryQueryRollman.html +6 -0
  80. package/docs/functions/bridgeToBN.html +5 -0
  81. package/docs/functions/bufferToHex.html +4 -0
  82. package/docs/functions/cancelOrder.html +1 -0
  83. package/docs/functions/checkPubKeyLength.html +1 -1
  84. package/docs/functions/checkedFetch.html +4 -4
  85. package/docs/functions/createSession.html +1 -0
  86. package/docs/functions/decodeLengthDelimited.html +7 -6
  87. package/docs/functions/encodeLengthDelimited.html +4 -4
  88. package/docs/functions/fillModeToProtoFillMode.html +4 -4
  89. package/docs/functions/findMarket.html +1 -1
  90. package/docs/functions/findPda.html +6 -0
  91. package/docs/functions/findToken.html +1 -1
  92. package/docs/functions/fromBN.html +5 -0
  93. package/docs/functions/getAccount.html +6 -0
  94. package/docs/functions/getActionNonce.html +5 -0
  95. package/docs/functions/getCurrentTps.html +6 -0
  96. package/docs/functions/getInfo.html +5 -0
  97. package/docs/functions/getMedianLatency.html +6 -0
  98. package/docs/functions/getOrderbook.html +6 -0
  99. package/docs/functions/getPeakTps.html +6 -0
  100. package/docs/functions/getTimestamp.html +5 -0
  101. package/docs/functions/getTotalTransactions.html +5 -0
  102. package/docs/functions/getTrades.html +6 -0
  103. package/docs/functions/getUserAccountIds.html +6 -0
  104. package/docs/functions/hexToBuffer.html +4 -0
  105. package/docs/functions/initWebSocketClient.html +12 -0
  106. package/docs/functions/keypairFromPrivateKey.html +4 -0
  107. package/docs/functions/makeSigningFunction.html +4 -0
  108. package/docs/functions/makeWalletSignFn.html +5 -5
  109. package/docs/functions/marketsStats.html +5 -0
  110. package/docs/functions/optExpect.html +4 -4
  111. package/docs/functions/optMap.html +5 -5
  112. package/docs/functions/optUnwrap.html +2 -2
  113. package/docs/functions/panic.html +1 -1
  114. package/docs/functions/placeOrder.html +1 -0
  115. package/docs/functions/queryAction.html +6 -0
  116. package/docs/functions/queryBlock.html +6 -0
  117. package/docs/functions/queryLastNBlocks.html +5 -0
  118. package/docs/functions/queryPrometheus.html +6 -0
  119. package/docs/functions/queryRecentActions.html +6 -0
  120. package/docs/functions/queryRecentBlocks.html +6 -0
  121. package/docs/functions/revokeSession.html +1 -0
  122. package/docs/functions/shortenPublicKey.html +5 -0
  123. package/docs/functions/signAction.html +2 -2
  124. package/docs/functions/toBN.html +5 -0
  125. package/docs/functions/toScaledU128.html +5 -5
  126. package/docs/functions/toScaledU64.html +5 -5
  127. package/docs/functions/transfer.html +1 -0
  128. package/docs/functions/withdraw.html +1 -0
  129. package/docs/hierarchy.html +1 -0
  130. package/docs/index.html +39 -20
  131. package/docs/interfaces/Account.html +8 -8
  132. package/docs/interfaces/ActionInfo.html +8 -8
  133. package/docs/interfaces/ActionNonceResponse.html +3 -0
  134. package/docs/interfaces/ActionQuery.html +4 -4
  135. package/docs/interfaces/ActionResponse.html +8 -8
  136. package/docs/interfaces/ActionsExtendedInfo.html +10 -10
  137. package/docs/interfaces/ActionsQuery.html +5 -5
  138. package/docs/interfaces/ActionsResponse.html +6 -6
  139. package/docs/interfaces/AggregateMetrics.html +12 -12
  140. package/docs/interfaces/BlockFacts.html +10 -0
  141. package/docs/interfaces/BlockQuery.html +6 -6
  142. package/docs/interfaces/BlockResponse.html +6 -6
  143. package/docs/interfaces/BlockSummary.html +8 -8
  144. package/docs/interfaces/BlockSummaryResponse.html +6 -6
  145. package/docs/interfaces/DeltaEvent.html +6 -6
  146. package/docs/interfaces/DepositSplParams.html +10 -0
  147. package/docs/interfaces/Info.html +3 -3
  148. package/docs/interfaces/Market.html +8 -6
  149. package/docs/interfaces/MarketStats.html +7 -7
  150. package/docs/interfaces/MarketsStatsResponse.html +2 -2
  151. package/docs/interfaces/NordConfig.html +14 -5
  152. package/docs/interfaces/NordWebSocketClientEvents.html +4 -0
  153. package/docs/interfaces/NordWebSocketEvents.html +8 -0
  154. package/docs/interfaces/Order.html +6 -6
  155. package/docs/interfaces/OrderInfo.html +6 -6
  156. package/docs/interfaces/OrderbookEntry.html +4 -0
  157. package/docs/interfaces/OrderbookQuery.html +6 -0
  158. package/docs/interfaces/OrderbookResponse.html +6 -10
  159. package/docs/interfaces/OrderbookSubscription.html +159 -0
  160. package/docs/interfaces/PerpMarketStats.html +5 -5
  161. package/docs/interfaces/RollmanActionExtendedInfo.html +4 -4
  162. package/docs/interfaces/RollmanActionInfo.html +4 -4
  163. package/docs/interfaces/RollmanActionResponse.html +4 -4
  164. package/docs/interfaces/RollmanActionsResponse.html +2 -2
  165. package/docs/interfaces/RollmanBlockResponse.html +3 -3
  166. package/docs/interfaces/SPLTokenInfo.html +10 -0
  167. package/docs/interfaces/SolanaBridgeConfig.html +10 -0
  168. package/docs/interfaces/StateFacts.html +10 -0
  169. package/docs/interfaces/SubscriberConfig.html +3 -3
  170. package/docs/interfaces/TimestampResponse.html +3 -0
  171. package/docs/interfaces/Token.html +5 -5
  172. package/docs/interfaces/TokenInfo.html +5 -0
  173. package/docs/interfaces/Trade.html +5 -5
  174. package/docs/interfaces/TradeSubscription.html +159 -0
  175. package/docs/interfaces/Trades.html +5 -5
  176. package/docs/interfaces/TradesQuery.html +6 -0
  177. package/docs/interfaces/TradesResponse.html +7 -12
  178. package/docs/interfaces/TransferParams.html +8 -0
  179. package/docs/interfaces/UserAccountIdsQuery.html +3 -0
  180. package/docs/interfaces/UserAccountIdsResponse.html +3 -0
  181. package/docs/interfaces/WebSocketDeltaUpdate.html +9 -0
  182. package/docs/interfaces/WebSocketSubscription.html +4 -0
  183. package/docs/interfaces/WebSocketTradeUpdate.html +6 -0
  184. package/docs/interfaces/WebSocketUserUpdate.html +6 -0
  185. package/docs/interfaces/WithdrawalClaim.html +14 -0
  186. package/docs/interfaces/WithdrawalParams.html +8 -0
  187. package/docs/modules.html +1 -77
  188. package/docs/types/BigIntValue.html +2 -2
  189. package/docs/types/WebSocketMessage.html +1 -0
  190. package/docs/variables/DEBUG_KEYS.html +1 -1
  191. package/docs/variables/DEFAULT_FUNDING_AMOUNTS.html +1 -1
  192. package/docs/variables/DEV_TOKEN_INFOS.html +1 -1
  193. package/docs/variables/DEV_URL.html +1 -1
  194. package/docs/variables/MAX_BUFFER_LEN.html +1 -1
  195. package/docs/variables/SESSION_TTL.html +1 -1
  196. package/docs/variables/WEBSERVER_DEV_URL.html +1 -1
  197. package/docs/variables/ZERO_DECIMAL.html +1 -1
  198. package/docs/variables/_private.html +2 -0
  199. package/eslint.config.mjs +66 -0
  200. package/package.json +20 -23
  201. package/src/bridge/client.ts +487 -0
  202. package/src/bridge/const.ts +53 -0
  203. package/src/bridge/index.ts +7 -0
  204. package/src/bridge/types.ts +127 -0
  205. package/src/bridge/utils.ts +140 -0
  206. package/src/const.ts +4 -9
  207. package/src/gen/common.ts +27 -10
  208. package/src/gen/nord.ts +1045 -487
  209. package/src/idl/bridge.ts +702 -0
  210. package/src/index.ts +21 -2
  211. package/src/nord/{actions.ts → api/actions.ts} +12 -16
  212. package/src/nord/api/core.ts +130 -0
  213. package/src/nord/api/market.ts +125 -0
  214. package/src/nord/api/metrics.ts +154 -0
  215. package/src/nord/api/queries.ts +236 -0
  216. package/src/nord/client/Nord.ts +652 -0
  217. package/src/nord/client/NordUser.ts +1101 -0
  218. package/src/nord/index.ts +16 -2
  219. package/src/nord/models/Subscriber.ts +56 -0
  220. package/src/nord/utils/NordError.ts +72 -0
  221. package/src/types.ts +163 -92
  222. package/src/utils.ts +22 -1
  223. package/src/websocket/NordWebSocketClient.ts +432 -0
  224. package/src/websocket/events.ts +31 -0
  225. package/src/websocket/index.ts +2 -0
  226. package/tsconfig.eslint.json +12 -0
  227. package/.eslintignore +0 -1
  228. package/.eslintrc.js +0 -20
  229. package/dist/abis/ERC20_ABI.d.ts +0 -39
  230. package/dist/abis/ERC20_ABI.js +0 -313
  231. package/dist/abis/NORD_GETTERS_FACET_ABI.d.ts +0 -34
  232. package/dist/abis/NORD_GETTERS_FACET_ABI.js +0 -195
  233. package/dist/abis/NORD_RAMP_FACET_ABI.d.ts +0 -35
  234. package/dist/abis/NORD_RAMP_FACET_ABI.js +0 -144
  235. package/dist/abis/index.d.ts +0 -3
  236. package/dist/abis/index.js +0 -9
  237. package/dist/nord/Nord.d.ts +0 -76
  238. package/dist/nord/Nord.js +0 -376
  239. package/dist/nord/NordImpl.d.ts +0 -7
  240. package/dist/nord/NordImpl.js +0 -6
  241. package/dist/nord/NordUser.d.ts +0 -77
  242. package/dist/nord/NordUser.js +0 -249
  243. package/docs/functions/createWebSocketSubscription.html +0 -12
  244. package/docs/interfaces/ERC20TokenInfo.html +0 -5
  245. package/docs/interfaces/OrderbookOrder.html +0 -6
  246. package/docs/interfaces/TradeInfo.html +0 -20
  247. package/docs/interfaces/TradesQueryParams.html +0 -10
  248. package/docs/variables/DEV_CONTRACT_ADDRESS.html +0 -1
  249. package/docs/variables/ERC20_ABI.html +0 -1
  250. package/docs/variables/EVM_DEV_URL.html +0 -1
  251. package/docs/variables/FAUCET_PRIVATE_ADDRESS.html +0 -1
  252. package/docs/variables/NORD_GETTERS_FACET_ABI.html +0 -1
  253. package/docs/variables/NORD_RAMP_FACET_ABI.html +0 -1
  254. package/src/abis/ERC20_ABI.ts +0 -310
  255. package/src/abis/NORD_GETTERS_FACET_ABI.ts +0 -192
  256. package/src/abis/NORD_RAMP_FACET_ABI.ts +0 -141
  257. package/src/abis/index.ts +0 -3
  258. package/src/nord/Nord.ts +0 -504
  259. package/src/nord/NordImpl.ts +0 -8
  260. package/src/nord/NordUser.ts +0 -469
@@ -0,0 +1,394 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.SolanaBridgeClient = void 0;
37
+ const anchor = __importStar(require("@coral-xyz/anchor"));
38
+ const anchor_1 = require("@coral-xyz/anchor");
39
+ const spl_token_1 = require("@solana/spl-token");
40
+ const web3_js_1 = require("@solana/web3.js");
41
+ const types_1 = require("./types");
42
+ // Import the BRIDGE_IDL
43
+ const bridge_1 = require("../idl/bridge");
44
+ /**
45
+ * Solana Bridge Client for interacting with the bridge program
46
+ */
47
+ class SolanaBridgeClient {
48
+ /**
49
+ * Create a new Solana Bridge Client
50
+ *
51
+ * @param config Bridge configuration
52
+ * @param wallet Anchor wallet for signing transactions
53
+ */
54
+ constructor(config, wallet) {
55
+ this.config = config;
56
+ this.connection = new web3_js_1.Connection(config.rpcUrl, {
57
+ commitment: config.commitment,
58
+ });
59
+ this.programId = new web3_js_1.PublicKey(config.programId);
60
+ // Create the provider
61
+ const provider = new anchor_1.AnchorProvider(this.connection, wallet, {
62
+ commitment: config.commitment,
63
+ skipPreflight: true, // Skip simulation
64
+ });
65
+ // Set the provider globally
66
+ anchor.setProvider(provider);
67
+ // Store the provider
68
+ this.provider = provider;
69
+ this.program = new anchor.Program({ ...bridge_1.BRIDGE_IDL, address: config.programId }, provider);
70
+ }
71
+ /**
72
+ * Derive a PDA (Program Derived Address) for the given seeds
73
+ *
74
+ * @param type PDA seed type
75
+ * @param seeds Additional seeds
76
+ * @returns [PDA, bump]
77
+ */
78
+ async findPda(type, ...seeds) {
79
+ const seedBuffers = [
80
+ Buffer.from(type),
81
+ ...seeds.map((seed) => {
82
+ if (seed instanceof web3_js_1.PublicKey) {
83
+ return seed.toBuffer();
84
+ }
85
+ else if (typeof seed === "string") {
86
+ return Buffer.from(seed);
87
+ }
88
+ else if (typeof seed === "number") {
89
+ // Convert number to little-endian byte array (8 bytes for u64)
90
+ const buffer = Buffer.alloc(8);
91
+ buffer.writeBigUInt64LE(BigInt(seed), 0);
92
+ return buffer;
93
+ }
94
+ return seed;
95
+ }),
96
+ ];
97
+ return web3_js_1.PublicKey.findProgramAddressSync(seedBuffers, this.programId);
98
+ }
99
+ /**
100
+ * Find the contract storage PDA
101
+ *
102
+ * @returns [PDA, bump]
103
+ */
104
+ async findContractStoragePda() {
105
+ return this.findPda(types_1.PdaSeedType.ContractStorage);
106
+ }
107
+ /**
108
+ * Find the asset whitelisted PDA for a token mint
109
+ *
110
+ * @param mint Token mint address
111
+ * @returns [PDA, bump]
112
+ */
113
+ async findAssetWhitelistedPda(mint) {
114
+ return this.findPda(types_1.PdaSeedType.AssetWhitelisted, mint);
115
+ }
116
+ /**
117
+ * Find the deposit storage PDA for a deposit index
118
+ *
119
+ * @param depositIndex Deposit index
120
+ * @returns [PDA, bump]
121
+ */
122
+ async findDepositStoragePda(depositIndex) {
123
+ return this.findPda(types_1.PdaSeedType.DepositStorage, depositIndex);
124
+ }
125
+ /**
126
+ * Find the block storage PDA for a block ID
127
+ *
128
+ * @param blockId Block ID
129
+ * @returns [PDA, bump]
130
+ */
131
+ async findBlockStoragePda(blockId) {
132
+ return this.findPda(types_1.PdaSeedType.BlockStorage, blockId);
133
+ }
134
+ /**
135
+ * Find the withdrawal nullifier PDA
136
+ *
137
+ * @param blockId Block ID
138
+ * @param leafIndex Leaf index
139
+ * @returns [PDA, bump]
140
+ */
141
+ async findWithdrawalNullifierPda(blockId, leafIndex) {
142
+ return this.findPda(types_1.PdaSeedType.WithdrawalNullifier, blockId, leafIndex);
143
+ }
144
+ /**
145
+ * Find the authority PDA
146
+ *
147
+ * @returns [PDA, bump]
148
+ */
149
+ async findAuthorityPda() {
150
+ return this.findPda(types_1.PdaSeedType.Authority);
151
+ }
152
+ /**
153
+ * Helper method to sign and send a transaction
154
+ *
155
+ * @param transaction Transaction to sign and send
156
+ * @param signers Additional signers (beyond the provider's wallet)
157
+ * @returns Transaction signature
158
+ */
159
+ async signAndSendTransaction(transaction, signers = []) {
160
+ // Use the provider to sign and send the transaction with skipPreflight
161
+ const txSignature = await this.provider.sendAndConfirm(transaction, signers, {
162
+ skipPreflight: true,
163
+ });
164
+ return txSignature;
165
+ }
166
+ /**
167
+ * Deposit SPL tokens to the bridge
168
+ *
169
+ * @param params Deposit parameters
170
+ * @param signer Signer keypair
171
+ * @returns Transaction signature
172
+ */
173
+ async depositSpl(params) {
174
+ const [contractStorage] = await this.findContractStoragePda();
175
+ const [assetWhitelisted] = await this.findAssetWhitelistedPda(params.mint);
176
+ // Get the last deposit index from contract storage
177
+ const contractStorageAccount = await this.program.account.contractStorage.fetch(contractStorage);
178
+ const lastDepositIndex = contractStorageAccount.lastDepositIndex.toNumber();
179
+ // Find the deposit PDA for this deposit
180
+ const [deposit] = await this.findDepositStoragePda(lastDepositIndex + 1);
181
+ // Find the previous deposit PDA if it exists
182
+ let prevDeposit = undefined;
183
+ if (lastDepositIndex > 0) {
184
+ const [prevDepositPda] = await this.findDepositStoragePda(lastDepositIndex);
185
+ prevDeposit = prevDepositPda;
186
+ }
187
+ // Build the transaction
188
+ const accounts = {
189
+ depositor: this.provider.wallet.publicKey,
190
+ deposit,
191
+ assetWhitelisted,
192
+ contractStorage,
193
+ fromAccount: params.fromAccount,
194
+ toAccount: params.toAccount,
195
+ tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
196
+ systemProgram: web3_js_1.SystemProgram.programId,
197
+ };
198
+ // Create the transaction
199
+ const tx = await this.program.methods
200
+ .depositSpl(params.amount)
201
+ .accounts({ ...accounts, prevDeposit: prevDeposit })
202
+ .transaction();
203
+ // Check if toAccount is initialized and add preinstruction if needed
204
+ try {
205
+ await this.connection.getTokenAccountBalance(params.toAccount);
206
+ }
207
+ catch {
208
+ // Account doesn't exist, add instruction to create it
209
+ // Get the authority PDA which is the owner of the toAccount
210
+ const [authority] = await this.findAuthorityPda();
211
+ const createTokenAccountIx = (0, spl_token_1.createAssociatedTokenAccountInstruction)(this.provider.wallet.publicKey, // payer
212
+ params.toAccount, // associated token account address
213
+ authority, // owner of the token account
214
+ params.mint, // token mint
215
+ spl_token_1.TOKEN_2022_PROGRAM_ID);
216
+ // Add the instruction to the beginning of the transaction
217
+ tx.instructions.unshift(createTokenAccountIx);
218
+ }
219
+ // Sign and send the transaction using our helper method
220
+ return await this.signAndSendTransaction(tx);
221
+ }
222
+ /**
223
+ * Withdraw tokens from the bridge
224
+ *
225
+ * @param params Withdrawal parameters
226
+ * @param signer Signer keypair
227
+ * @returns Transaction signature
228
+ */
229
+ async withdraw(params, signer) {
230
+ const [stateUpdate] = await this.findBlockStoragePda(params.claim.blockId.toNumber());
231
+ const [withdrawalNullifier] = await this.findWithdrawalNullifierPda(params.claim.blockId.toNumber(), params.claim.leafIndex.toNumber());
232
+ const [authority] = await this.findAuthorityPda();
233
+ // Build the transaction
234
+ const tx = await this.program.methods
235
+ .withdraw(params.claim)
236
+ .accounts({
237
+ payer: signer.publicKey,
238
+ stateUpdate,
239
+ withdrawalNullifier,
240
+ fromAccount: params.fromAccount,
241
+ toAccount: params.toAccount,
242
+ authority,
243
+ tokenProgram: spl_token_1.TOKEN_2022_PROGRAM_ID,
244
+ systemProgram: web3_js_1.SystemProgram.programId,
245
+ })
246
+ .transaction();
247
+ // Sign and send the transaction using our helper method
248
+ return await this.signAndSendTransaction(tx, [signer]);
249
+ }
250
+ /**
251
+ * Whitelist an asset (token) for use with the bridge
252
+ *
253
+ * @param mint Token mint address
254
+ * @param signer Operator keypair
255
+ * @returns Transaction signature
256
+ */
257
+ async whitelistAsset(mint, signer) {
258
+ const [contractStorage] = await this.findContractStoragePda();
259
+ const [assetWhitelisted] = await this.findAssetWhitelistedPda(mint);
260
+ // Build the transaction
261
+ const tx = await this.program.methods
262
+ .whitelistAsset(mint)
263
+ .accounts({
264
+ operator: signer.publicKey,
265
+ contractStorage,
266
+ assetWhitelisted,
267
+ systemProgram: web3_js_1.SystemProgram.programId,
268
+ })
269
+ .transaction();
270
+ // Sign and send the transaction using our helper method
271
+ return await this.signAndSendTransaction(tx, [signer]);
272
+ }
273
+ /**
274
+ * Propose a new block
275
+ *
276
+ * @param facts Block facts
277
+ * @param signer Operator keypair
278
+ * @returns Transaction signature
279
+ */
280
+ async proposeBlock(facts, signer) {
281
+ const [contractStorage] = await this.findContractStoragePda();
282
+ // Get the last block ID from contract storage
283
+ const contractStorageAccount = await this.program.account.contractStorage.fetch(contractStorage);
284
+ const [block] = await this.findBlockStoragePda(contractStorageAccount.lastBlockId.toNumber() + 1);
285
+ // Find the last deposit PDA
286
+ const [lastDeposit] = await this.findDepositStoragePda(facts.nextStateFacts.lastDepositIndex.toNumber());
287
+ // Find the DA fact state
288
+ const daFactState = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(types_1.PdaSeedType.DaFactStorage), facts.daCommitment], this.programId)[0];
289
+ // Build the transaction
290
+ const tx = await this.program.methods
291
+ .proposeBlock(facts)
292
+ .accounts({
293
+ operator: signer.publicKey,
294
+ block,
295
+ lastDeposit,
296
+ daFactState,
297
+ contractStorage,
298
+ systemProgram: web3_js_1.SystemProgram.programId,
299
+ })
300
+ .transaction();
301
+ // Sign and send the transaction using our helper method
302
+ return await this.signAndSendTransaction(tx, [signer]);
303
+ }
304
+ /**
305
+ * Finalize a block
306
+ *
307
+ * @param blockId Block ID
308
+ * @param stateUpdateId State update ID
309
+ * @param signer Payer keypair
310
+ * @returns Transaction signature
311
+ */
312
+ async finalizeBlock(blockId, stateUpdateId, signer) {
313
+ const [contractStorage] = await this.findContractStoragePda();
314
+ const [block] = await this.findBlockStoragePda(blockId);
315
+ // Build the transaction
316
+ const tx = await this.program.methods
317
+ .finalizeBlock(new anchor.BN(stateUpdateId))
318
+ .accounts({
319
+ payer: signer.publicKey,
320
+ block,
321
+ contractStorage,
322
+ systemProgram: web3_js_1.SystemProgram.programId,
323
+ })
324
+ .transaction();
325
+ // Sign and send the transaction using our helper method
326
+ return await this.signAndSendTransaction(tx, [signer]);
327
+ }
328
+ /**
329
+ * Finalize a DA fact
330
+ *
331
+ * @param fact DA fact (32-byte array)
332
+ * @param signer Payer keypair
333
+ * @returns Transaction signature
334
+ */
335
+ async finalizeDaFact(fact, signer) {
336
+ const factStateStorage = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from(types_1.PdaSeedType.DaFactStorage), fact], this.programId)[0];
337
+ // Build the transaction
338
+ const tx = await this.program.methods
339
+ .finalizeDaFact(Array.from(fact))
340
+ .accounts({
341
+ payer: signer.publicKey,
342
+ factStateStorage,
343
+ systemProgram: web3_js_1.SystemProgram.programId,
344
+ })
345
+ .transaction();
346
+ // Sign and send the transaction using our helper method
347
+ return await this.signAndSendTransaction(tx, [signer]);
348
+ }
349
+ /**
350
+ * Initialize the bridge contract
351
+ *
352
+ * @param operator Operator public key
353
+ * @param initialAppStateCommitment Initial app state commitment (32-byte array)
354
+ * @param signer Payer keypair
355
+ * @returns Transaction signature
356
+ */
357
+ async initialize(operator, initialAppStateCommitment, signer) {
358
+ const [contractStorage] = await this.findContractStoragePda();
359
+ // Build the transaction
360
+ const tx = await this.program.methods
361
+ .initialize(operator, Array.from(initialAppStateCommitment))
362
+ .accounts({
363
+ payer: signer.publicKey,
364
+ program: this.programId,
365
+ contractStorage,
366
+ systemProgram: web3_js_1.SystemProgram.programId,
367
+ })
368
+ .transaction();
369
+ // Sign and send the transaction using our helper method
370
+ return await this.signAndSendTransaction(tx, [signer]);
371
+ }
372
+ /**
373
+ * Create an associated token account if it doesn't exist
374
+ *
375
+ * @param mint Token mint
376
+ * @param owner Account owner
377
+ * @param payer Transaction payer
378
+ * @returns Associated token account address
379
+ */
380
+ async createTokenAccountIfNeeded(mint, owner, payer) {
381
+ const associatedTokenAddress = await (0, spl_token_1.getAssociatedTokenAddress)(mint, owner);
382
+ try {
383
+ await this.connection.getTokenAccountBalance(associatedTokenAddress);
384
+ }
385
+ catch {
386
+ // Account doesn't exist, create it
387
+ const transaction = new web3_js_1.Transaction().add((0, spl_token_1.createAssociatedTokenAccountInstruction)(payer.publicKey, associatedTokenAddress, owner, mint));
388
+ // Sign and send the transaction using our helper method
389
+ await this.signAndSendTransaction(transaction, [payer]);
390
+ }
391
+ return associatedTokenAddress;
392
+ }
393
+ }
394
+ exports.SolanaBridgeClient = SolanaBridgeClient;
@@ -0,0 +1,23 @@
1
+ import { SPLTokenInfo } from "./types";
2
+ import { Commitment } from "@solana/web3.js";
3
+ export declare const SOLANA_MAINNET_URL = "https://api.mainnet-beta.solana.com";
4
+ export declare const SOLANA_DEVNET_URL = "https://api.devnet.solana.com";
5
+ export declare const SOLANA_TESTNET_URL = "https://api.testnet.solana.com";
6
+ export declare const SOLANA_LOCALNET_URL = "http://localhost:8899";
7
+ export declare const DEFAULT_COMMITMENT: Commitment;
8
+ export declare const DEV_URL = "http://localhost";
9
+ export declare const WEBSERVER_DEV_URL: string;
10
+ export declare const SOLANA_PROGRAM_ID = "CVDFLCAjXhVWiPXH9nTCTpCgVzmDVoiPzNJYuccr1dqB";
11
+ export declare const DEV_TOKEN_INFOS: SPLTokenInfo[];
12
+ export declare const DEFAULT_FUNDING_AMOUNTS: {
13
+ [key: string]: [string, number];
14
+ };
15
+ export declare const PDA_SEEDS: {
16
+ CONTRACT_STORAGE: string;
17
+ ASSET_WHITELISTED: string;
18
+ DEPOSIT_STORAGE: string;
19
+ BLOCK_STORAGE: string;
20
+ WITHDRAWAL_NULLIFIER: string;
21
+ AUTHORITY: string;
22
+ DA_FACT_STORAGE: string;
23
+ };
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PDA_SEEDS = exports.DEFAULT_FUNDING_AMOUNTS = exports.DEV_TOKEN_INFOS = exports.SOLANA_PROGRAM_ID = exports.WEBSERVER_DEV_URL = exports.DEV_URL = exports.DEFAULT_COMMITMENT = exports.SOLANA_LOCALNET_URL = exports.SOLANA_TESTNET_URL = exports.SOLANA_DEVNET_URL = exports.SOLANA_MAINNET_URL = void 0;
4
+ // Solana RPC endpoints
5
+ exports.SOLANA_MAINNET_URL = "https://api.mainnet-beta.solana.com";
6
+ exports.SOLANA_DEVNET_URL = "https://api.devnet.solana.com";
7
+ exports.SOLANA_TESTNET_URL = "https://api.testnet.solana.com";
8
+ exports.SOLANA_LOCALNET_URL = "http://localhost:8899";
9
+ // Default commitment level
10
+ exports.DEFAULT_COMMITMENT = "confirmed";
11
+ // Webserver configuration
12
+ const WEBSERVER_PORT = "80";
13
+ exports.DEV_URL = "http://localhost";
14
+ exports.WEBSERVER_DEV_URL = exports.DEV_URL + ":" + WEBSERVER_PORT;
15
+ // Program ID (will be supplied externally)
16
+ exports.SOLANA_PROGRAM_ID = "CVDFLCAjXhVWiPXH9nTCTpCgVzmDVoiPzNJYuccr1dqB";
17
+ // Sample token information for development
18
+ exports.DEV_TOKEN_INFOS = [
19
+ {
20
+ mint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC on devnet
21
+ precision: 6,
22
+ tokenId: 0,
23
+ name: "usdc",
24
+ },
25
+ {
26
+ mint: "So11111111111111111111111111111111111111112", // Wrapped SOL
27
+ precision: 9,
28
+ tokenId: 1,
29
+ name: "sol",
30
+ },
31
+ ];
32
+ // Default funding amounts for development
33
+ exports.DEFAULT_FUNDING_AMOUNTS = {
34
+ SOL: ["0.2", 9],
35
+ // USDC mint address
36
+ EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v: ["10000", 6],
37
+ };
38
+ // PDA seed constants
39
+ exports.PDA_SEEDS = {
40
+ CONTRACT_STORAGE: "contract_storage",
41
+ ASSET_WHITELISTED: "asset_whitelisted",
42
+ DEPOSIT_STORAGE: "deposit_storage",
43
+ BLOCK_STORAGE: "block_storage",
44
+ WITHDRAWAL_NULLIFIER: "withdrawal_nullifier",
45
+ AUTHORITY: "authority",
46
+ DA_FACT_STORAGE: "da_fact_storage",
47
+ };
@@ -0,0 +1,5 @@
1
+ export * from "./client";
2
+ export * from "./types";
3
+ export * from "./utils";
4
+ import { BRIDGE_IDL } from "../idl/bridge";
5
+ export { BRIDGE_IDL };
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.BRIDGE_IDL = void 0;
18
+ __exportStar(require("./client"), exports);
19
+ __exportStar(require("./types"), exports);
20
+ __exportStar(require("./utils"), exports);
21
+ // Re-export the IDL for convenience
22
+ const bridge_1 = require("../idl/bridge");
23
+ Object.defineProperty(exports, "BRIDGE_IDL", { enumerable: true, get: function () { return bridge_1.BRIDGE_IDL; } });
@@ -0,0 +1,118 @@
1
+ import { PublicKey } from "@solana/web3.js";
2
+ import { BN } from "@coral-xyz/anchor";
3
+ /**
4
+ * Configuration for Solana bridge
5
+ */
6
+ export interface SolanaBridgeConfig {
7
+ /** Solana RPC URL */
8
+ rpcUrl: string;
9
+ /** Program ID for the bridge program */
10
+ programId: string;
11
+ /** Commitment level for transactions */
12
+ commitment: "processed" | "confirmed" | "finalized";
13
+ /** Token information for supported SPL tokens */
14
+ tokenInfos: SPLTokenInfo[];
15
+ }
16
+ /**
17
+ * Information about an SPL token
18
+ */
19
+ export interface SPLTokenInfo {
20
+ /** Token mint address */
21
+ mint: string;
22
+ /** Token decimals */
23
+ precision: number;
24
+ /** Token ID in the Layer-N system */
25
+ tokenId: number;
26
+ /** Token name */
27
+ name: string;
28
+ }
29
+ /**
30
+ * Parameters for depositing SPL tokens
31
+ */
32
+ export interface DepositSplParams {
33
+ /** Amount to deposit (in token's smallest unit) */
34
+ amount: BN;
35
+ /** Mint address of the token to deposit */
36
+ mint: PublicKey;
37
+ /** User's token account to deposit from */
38
+ fromAccount: PublicKey;
39
+ /** Bridge's token account to deposit to */
40
+ toAccount: PublicKey;
41
+ }
42
+ /**
43
+ * Parameters for withdrawing tokens
44
+ */
45
+ export interface WithdrawalParams {
46
+ /** Withdrawal claim data */
47
+ claim: WithdrawalClaim;
48
+ /** User's token account to receive tokens */
49
+ toAccount: PublicKey;
50
+ /** Bridge's token account to withdraw from */
51
+ fromAccount: PublicKey;
52
+ }
53
+ /**
54
+ * Withdrawal claim data
55
+ */
56
+ export interface WithdrawalClaim {
57
+ /** Block ID containing the withdrawal */
58
+ blockId: BN;
59
+ /** Leaf index in the Merkle tree */
60
+ leafIndex: BN;
61
+ /** Amount to withdraw */
62
+ amount: BN;
63
+ /** Recipient address */
64
+ recipient: PublicKey;
65
+ /** Token mint address */
66
+ mint: PublicKey;
67
+ /** Merkle proof for the withdrawal */
68
+ proof: Buffer[];
69
+ }
70
+ /**
71
+ * Block facts data structure
72
+ */
73
+ export interface BlockFacts {
74
+ /** Previous state facts */
75
+ prevStateFacts: StateFacts;
76
+ /** Next state facts */
77
+ nextStateFacts: StateFacts;
78
+ /** Data availability commitment */
79
+ daCommitment: Buffer;
80
+ /** Withdrawal Merkle root */
81
+ withdrawalRoot: Buffer;
82
+ }
83
+ /**
84
+ * State facts data structure
85
+ */
86
+ export interface StateFacts {
87
+ /** Application state commitment */
88
+ appStateCommitment: Buffer;
89
+ /** Deposit root */
90
+ depositRoot: Buffer;
91
+ /** Last deposit index */
92
+ lastDepositIndex: BN;
93
+ /** Last action ID */
94
+ lastActionId: BN;
95
+ }
96
+ /**
97
+ * Transfer parameters
98
+ */
99
+ export interface TransferParams {
100
+ /** User public key */
101
+ user: PublicKey;
102
+ /** Token mint */
103
+ mint: PublicKey;
104
+ /** Amount to transfer */
105
+ amount: BN;
106
+ }
107
+ /**
108
+ * PDA seed types
109
+ */
110
+ export declare enum PdaSeedType {
111
+ ContractStorage = "contract_storage",
112
+ AssetWhitelisted = "asset_whitelisted",
113
+ DepositStorage = "deposit_storage",
114
+ BlockStorage = "block_storage",
115
+ WithdrawalNullifier = "withdrawal_nullifier",
116
+ Authority = "authority",
117
+ DaFactStorage = "da_fact_storage"
118
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PdaSeedType = void 0;
4
+ /**
5
+ * PDA seed types
6
+ */
7
+ var PdaSeedType;
8
+ (function (PdaSeedType) {
9
+ PdaSeedType["ContractStorage"] = "contract_storage";
10
+ PdaSeedType["AssetWhitelisted"] = "asset_whitelisted";
11
+ PdaSeedType["DepositStorage"] = "deposit_storage";
12
+ PdaSeedType["BlockStorage"] = "block_storage";
13
+ PdaSeedType["WithdrawalNullifier"] = "withdrawal_nullifier";
14
+ PdaSeedType["Authority"] = "authority";
15
+ PdaSeedType["DaFactStorage"] = "da_fact_storage";
16
+ })(PdaSeedType || (exports.PdaSeedType = PdaSeedType = {}));
@@ -0,0 +1,64 @@
1
+ import { Keypair, PublicKey } from "@solana/web3.js";
2
+ import { BN } from "@coral-xyz/anchor";
3
+ import { PdaSeedType } from "./types";
4
+ /**
5
+ * Convert a private key string to a Solana Keypair
6
+ *
7
+ * @param privateKey Private key as base58 string, hex string, or Uint8Array
8
+ * @returns Solana Keypair
9
+ */
10
+ export declare function keypairFromPrivateKey(privateKey: string | Uint8Array): Keypair;
11
+ /**
12
+ * Create a signing function for Solana transactions
13
+ *
14
+ * @param keypair Solana keypair
15
+ * @returns Signing function
16
+ */
17
+ export declare function makeSigningFunction(keypair: Keypair): (_message: Uint8Array) => Promise<Uint8Array>;
18
+ /**
19
+ * Convert a number to a BN with the specified number of decimals
20
+ *
21
+ * @param amount Amount as a number
22
+ * @param decimals Number of decimals
23
+ * @returns BN representation of the amount
24
+ */
25
+ export declare function bridgeToBN(amount: number, decimals: number): BN;
26
+ /**
27
+ * Format a BN as a human-readable number with the specified number of decimals
28
+ *
29
+ * @param amount Amount as a BN
30
+ * @param decimals Number of decimals
31
+ * @returns Formatted number
32
+ */
33
+ export declare function fromBN(amount: BN, decimals: number): number;
34
+ /**
35
+ * Derive a PDA (Program Derived Address) for the given seeds
36
+ *
37
+ * @param programId Program ID
38
+ * @param type PDA seed type
39
+ * @param seeds Additional seeds
40
+ * @returns [PDA, bump]
41
+ */
42
+ export declare function findPda(programId: PublicKey, type: PdaSeedType, ...seeds: (Buffer | PublicKey | string | number)[]): [PublicKey, number];
43
+ /**
44
+ * Convert a buffer to a hex string
45
+ *
46
+ * @param buffer Buffer to convert
47
+ * @returns Hex string
48
+ */
49
+ export declare function bufferToHex(buffer: Buffer | Uint8Array): string;
50
+ /**
51
+ * Convert a hex string to a buffer
52
+ *
53
+ * @param hex Hex string
54
+ * @returns Buffer
55
+ */
56
+ export declare function hexToBuffer(hex: string): Buffer;
57
+ /**
58
+ * Shorten a public key for display
59
+ *
60
+ * @param publicKey Public key to shorten
61
+ * @param chars Number of characters to show at the beginning and end
62
+ * @returns Shortened public key
63
+ */
64
+ export declare function shortenPublicKey(publicKey: PublicKey | string, chars?: number): string;