@thru/programs 0.3.4 → 0.3.6

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 (152) hide show
  1. package/README.md +124 -39
  2. package/dist/abi-manager/index.cjs +9568 -0
  3. package/dist/abi-manager/index.cjs.map +1 -0
  4. package/dist/abi-manager/index.d.cts +2169 -0
  5. package/dist/abi-manager/index.d.ts +2169 -0
  6. package/dist/abi-manager/index.js +4 -0
  7. package/dist/abi-manager/index.js.map +1 -0
  8. package/dist/amm/index.cjs +132 -33
  9. package/dist/amm/index.cjs.map +1 -1
  10. package/dist/amm/index.d.cts +1 -1
  11. package/dist/amm/index.d.ts +1 -1
  12. package/dist/amm/index.js +5 -2
  13. package/dist/amm/index.js.map +1 -1
  14. package/dist/bootstrap-addresses/index.cjs +118 -0
  15. package/dist/bootstrap-addresses/index.cjs.map +1 -0
  16. package/dist/bootstrap-addresses/index.d.cts +44 -0
  17. package/dist/bootstrap-addresses/index.d.ts +44 -0
  18. package/dist/bootstrap-addresses/index.js +5 -0
  19. package/dist/bootstrap-addresses/index.js.map +1 -0
  20. package/dist/chunk-6IC6CO5S.js +9443 -0
  21. package/dist/chunk-6IC6CO5S.js.map +1 -0
  22. package/dist/chunk-7DHFYBRW.js +51 -0
  23. package/dist/chunk-7DHFYBRW.js.map +1 -0
  24. package/dist/chunk-7FBCBZIY.js +41 -0
  25. package/dist/chunk-7FBCBZIY.js.map +1 -0
  26. package/dist/chunk-BNYDH4ZP.js +4351 -0
  27. package/dist/chunk-BNYDH4ZP.js.map +1 -0
  28. package/dist/{chunk-J7B7XYOI.js → chunk-CYG4PB65.js} +14 -41
  29. package/dist/chunk-CYG4PB65.js.map +1 -0
  30. package/dist/chunk-VNTPD4AR.js +37 -0
  31. package/dist/chunk-VNTPD4AR.js.map +1 -0
  32. package/dist/chunk-YX7BLFQZ.js +81 -0
  33. package/dist/chunk-YX7BLFQZ.js.map +1 -0
  34. package/dist/chunk-ZHF2PH5H.js +6536 -0
  35. package/dist/chunk-ZHF2PH5H.js.map +1 -0
  36. package/dist/clob/index.cjs +8461 -4922
  37. package/dist/clob/index.cjs.map +1 -1
  38. package/dist/clob/index.d.cts +1115 -53
  39. package/dist/clob/index.d.ts +1115 -53
  40. package/dist/clob/index.js +6817 -3395
  41. package/dist/clob/index.js.map +1 -1
  42. package/dist/deploy/index.cjs +24751 -0
  43. package/dist/deploy/index.cjs.map +1 -0
  44. package/dist/deploy/index.d.cts +180 -0
  45. package/dist/deploy/index.d.ts +180 -0
  46. package/dist/deploy/index.js +1849 -0
  47. package/dist/deploy/index.js.map +1 -0
  48. package/dist/helpers-DD7q61nr.d.cts +7 -0
  49. package/dist/helpers-DD7q61nr.d.ts +7 -0
  50. package/dist/manager/index.cjs +6680 -0
  51. package/dist/manager/index.cjs.map +1 -0
  52. package/dist/manager/index.d.cts +1279 -0
  53. package/dist/manager/index.d.ts +1279 -0
  54. package/dist/manager/index.js +5 -0
  55. package/dist/manager/index.js.map +1 -0
  56. package/dist/multicall/index.cjs +117 -40
  57. package/dist/multicall/index.cjs.map +1 -1
  58. package/dist/multicall/index.d.cts +5 -2
  59. package/dist/multicall/index.d.ts +5 -2
  60. package/dist/multicall/index.js +4 -1
  61. package/dist/oracle/index.cjs +122 -14
  62. package/dist/oracle/index.cjs.map +1 -1
  63. package/dist/oracle/index.d.cts +3 -1
  64. package/dist/oracle/index.d.ts +3 -1
  65. package/dist/oracle/index.js +7 -1
  66. package/dist/oracle/index.js.map +1 -1
  67. package/dist/passkey-manager/index.cjs +107 -6
  68. package/dist/passkey-manager/index.cjs.map +1 -1
  69. package/dist/passkey-manager/index.d.cts +1 -1
  70. package/dist/passkey-manager/index.d.ts +1 -1
  71. package/dist/passkey-manager/index.js +6 -3
  72. package/dist/passkey-manager/index.js.map +1 -1
  73. package/dist/uploader/index.cjs +4442 -0
  74. package/dist/uploader/index.cjs.map +1 -0
  75. package/dist/uploader/index.d.cts +761 -0
  76. package/dist/uploader/index.d.ts +761 -0
  77. package/dist/uploader/index.js +4 -0
  78. package/dist/uploader/index.js.map +1 -0
  79. package/package.json +27 -2
  80. package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
  81. package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
  82. package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
  83. package/src/abi-manager/accounts.ts +91 -0
  84. package/src/abi-manager/constants.ts +19 -0
  85. package/src/abi-manager/derivation.ts +86 -0
  86. package/src/abi-manager/errors.ts +48 -0
  87. package/src/abi-manager/index.test.ts +136 -0
  88. package/src/abi-manager/index.ts +56 -0
  89. package/src/abi-manager/instructions.ts +573 -0
  90. package/src/abi-manager/types.ts +150 -0
  91. package/src/amm/index.test.ts +2 -6
  92. package/src/amm/index.ts +2 -2
  93. package/src/bootstrap-addresses/index.test.ts +59 -0
  94. package/src/bootstrap-addresses/index.ts +58 -0
  95. package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
  96. package/src/clob/exchange-client.test.ts +115 -2
  97. package/src/clob/exchange-client.ts +52 -13
  98. package/src/clob/exchange.test.ts +18 -16
  99. package/src/clob/exchange.ts +4 -0
  100. package/src/clob/index.test.ts +140 -7
  101. package/src/clob/index.ts +304 -12
  102. package/src/clob/price.test.ts +32 -0
  103. package/src/clob/price.ts +77 -0
  104. package/src/deploy/accounts.ts +208 -0
  105. package/src/deploy/chain.ts +163 -0
  106. package/src/deploy/constants.ts +30 -0
  107. package/src/deploy/deploy.test.ts +349 -0
  108. package/src/deploy/errors.ts +72 -0
  109. package/src/deploy/index.ts +44 -0
  110. package/src/deploy/inspection.ts +146 -0
  111. package/src/deploy/seeds.ts +29 -0
  112. package/src/deploy/types.ts +178 -0
  113. package/src/deploy/uploader.test.ts +326 -0
  114. package/src/deploy/uploader.ts +558 -0
  115. package/src/deploy/validation-imports.test.ts +55 -0
  116. package/src/deploy/validation.ts +183 -0
  117. package/src/deploy/workflows.test.ts +422 -0
  118. package/src/deploy/workflows.ts +1088 -0
  119. package/src/helpers/bytes.test.ts +58 -0
  120. package/src/helpers/bytes.ts +28 -3
  121. package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
  122. package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
  123. package/src/manager/abi/thru/program/manager/types.ts +3814 -0
  124. package/src/manager/accounts.ts +44 -0
  125. package/src/manager/constants.ts +16 -0
  126. package/src/manager/derivation.ts +32 -0
  127. package/src/manager/errors.ts +47 -0
  128. package/src/manager/index.test.ts +108 -0
  129. package/src/manager/index.ts +68 -0
  130. package/src/manager/instructions.ts +281 -0
  131. package/src/manager/types.ts +95 -0
  132. package/src/manager/validation.ts +35 -0
  133. package/src/multicall/index.test.ts +11 -2
  134. package/src/multicall/index.ts +12 -5
  135. package/src/oracle/index.ts +4 -0
  136. package/src/passkey-manager/constants.ts +2 -1
  137. package/src/passkey-manager/validate.test.ts +2 -2
  138. package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
  139. package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
  140. package/src/uploader/accounts.ts +38 -0
  141. package/src/uploader/constants.ts +14 -0
  142. package/src/uploader/derivation.ts +34 -0
  143. package/src/uploader/errors.ts +25 -0
  144. package/src/uploader/index.test.ts +107 -0
  145. package/src/uploader/index.ts +34 -0
  146. package/src/uploader/instructions.ts +163 -0
  147. package/src/uploader/types.ts +67 -0
  148. package/src/utils/helpers.ts +121 -0
  149. package/tsup.config.ts +5 -0
  150. package/dist/chunk-J7B7XYOI.js.map +0 -1
  151. package/dist/chunk-P5OABVJI.js +0 -28
  152. package/dist/chunk-P5OABVJI.js.map +0 -1
@@ -0,0 +1,558 @@
1
+ import type { Account } from "@thru/sdk";
2
+ import type { Thru } from "@thru/sdk/client";
3
+ import {
4
+ createDestroyUploadInstruction,
5
+ createFinalizeUploadInstruction,
6
+ createUploadBufferInstruction,
7
+ createWriteUploadInstruction,
8
+ deriveUploadAddresses,
9
+ type UploadAddresses,
10
+ } from "../uploader";
11
+ import { bytesEqual, bytesToHex, sha256 } from "../helpers/bytes";
12
+ import {
13
+ getOptionalAccount,
14
+ parseUploaderMeta,
15
+ requireAccountData,
16
+ } from "./accounts";
17
+ import {
18
+ UPLOADER_PROGRAM_ADDRESS,
19
+ UPLOADER_STATE_FINALIZED,
20
+ UPLOADER_STATE_OPEN,
21
+ } from "./constants";
22
+ import { emitProgress, submitTransaction } from "./chain";
23
+ import { DeployError } from "./errors";
24
+ import type {
25
+ DeployProgressEvent,
26
+ DeploymentOperation,
27
+ UploadArtifactResult,
28
+ UploadCleanupResult,
29
+ } from "./types";
30
+ import type { ResolvedSigner } from "./validation";
31
+
32
+ export interface PreparedUpload {
33
+ operation: DeploymentOperation;
34
+ artifact: "program" | "abi";
35
+ seed: string;
36
+ bytes: Uint8Array;
37
+ hash: Uint8Array;
38
+ addresses: UploadAddresses;
39
+ result: UploadArtifactResult;
40
+ }
41
+
42
+ async function waitForUploadPair(
43
+ client: Thru,
44
+ metaAddress: string,
45
+ bufferAddress: string,
46
+ ): Promise<{ metaAccount?: Account; bufferAccount?: Account }> {
47
+ let metaAccount: Account | undefined;
48
+ let bufferAccount: Account | undefined;
49
+ for (let attempt = 0; attempt < 12; attempt += 1) {
50
+ [metaAccount, bufferAccount] = await Promise.all([
51
+ getOptionalAccount(client, metaAddress),
52
+ getOptionalAccount(client, bufferAddress),
53
+ ]);
54
+ if (metaAccount && bufferAccount) break;
55
+ if (attempt < 11) await new Promise((resolve) => setTimeout(resolve, 250));
56
+ }
57
+ return { metaAccount, bufferAccount };
58
+ }
59
+
60
+ async function waitForBufferContent(
61
+ client: Thru,
62
+ bufferAddress: string,
63
+ expected: Uint8Array,
64
+ offset = 0,
65
+ ): Promise<Account | undefined> {
66
+ let account: Account | undefined;
67
+ for (let attempt = 0; attempt < 12; attempt += 1) {
68
+ account = await getOptionalAccount(client, bufferAddress);
69
+ if (
70
+ account &&
71
+ bytesEqual(
72
+ requireAccountData(account, "uploader buffer account").slice(
73
+ offset,
74
+ offset + expected.length,
75
+ ),
76
+ expected,
77
+ )
78
+ ) {
79
+ return account;
80
+ }
81
+ if (attempt < 11) await new Promise((resolve) => setTimeout(resolve, 250));
82
+ }
83
+ return account;
84
+ }
85
+
86
+ export async function prepareUpload(
87
+ operation: DeploymentOperation,
88
+ artifact: "program" | "abi",
89
+ seed: string,
90
+ bytes: Uint8Array,
91
+ ): Promise<PreparedUpload> {
92
+ const hash = await sha256(bytes);
93
+ const addresses = deriveUploadAddresses(seed);
94
+ return {
95
+ operation,
96
+ artifact,
97
+ seed,
98
+ bytes,
99
+ hash,
100
+ addresses,
101
+ result: {
102
+ artifact,
103
+ size: bytes.length,
104
+ sha256: bytesToHex(hash),
105
+ metaAccountAddress: addresses.metaAccountAddress,
106
+ bufferAccountAddress: addresses.bufferAccountAddress,
107
+ reused: false,
108
+ transactionSignatures: [],
109
+ cleanup: { status: "not-needed" },
110
+ },
111
+ };
112
+ }
113
+
114
+ function assertUploaderOwner(account: Account, label: string): void {
115
+ if (!account.meta?.owner?.equals(UPLOADER_PROGRAM_ADDRESS)) {
116
+ throw new DeployError(
117
+ "UPLOAD_CONFLICT",
118
+ `${label} has an unexpected owner`,
119
+ );
120
+ }
121
+ }
122
+
123
+ function assertMatchingUpload(
124
+ upload: PreparedUpload,
125
+ signer: ResolvedSigner,
126
+ metaAccount: Account,
127
+ bufferAccount: Account,
128
+ ): number {
129
+ const meta = parseUploaderMeta(metaAccount);
130
+ assertUploaderOwner(bufferAccount, "uploader buffer account");
131
+ if (!bytesEqual(meta.authority, signer.publicKey)) {
132
+ throw new DeployError(
133
+ "UPLOAD_CONFLICT",
134
+ "temporary upload belongs to another authority",
135
+ );
136
+ }
137
+ if (!bytesEqual(meta.expectedHash, upload.hash)) {
138
+ throw new DeployError(
139
+ "UPLOAD_CONFLICT",
140
+ "temporary upload has a conflicting SHA-256",
141
+ );
142
+ }
143
+ const buffer = requireAccountData(bufferAccount, "uploader buffer account");
144
+ if (buffer.length !== upload.bytes.length) {
145
+ throw new DeployError(
146
+ "UPLOAD_CONFLICT",
147
+ "temporary upload has a conflicting size",
148
+ );
149
+ }
150
+ if (
151
+ meta.state !== UPLOADER_STATE_OPEN &&
152
+ meta.state !== UPLOADER_STATE_FINALIZED
153
+ ) {
154
+ throw new DeployError(
155
+ "UPLOAD_CONFLICT",
156
+ `temporary upload has invalid state ${meta.state}`,
157
+ );
158
+ }
159
+ return meta.state;
160
+ }
161
+
162
+ function event(
163
+ upload: PreparedUpload,
164
+ status: DeployProgressEvent["status"],
165
+ fields: Partial<DeployProgressEvent> = {},
166
+ ): DeployProgressEvent {
167
+ return {
168
+ operation: upload.operation,
169
+ phase: "upload",
170
+ status,
171
+ artifact: upload.artifact,
172
+ ...fields,
173
+ };
174
+ }
175
+
176
+ export async function uploadArtifact(
177
+ client: Thru,
178
+ signer: ResolvedSigner,
179
+ upload: PreparedUpload,
180
+ chunkSize: number,
181
+ onProgress?: (event: DeployProgressEvent) => void,
182
+ ): Promise<UploadArtifactResult> {
183
+ const metaAddress = upload.result.metaAccountAddress;
184
+ const bufferAddress = upload.result.bufferAccountAddress;
185
+ emitProgress(onProgress, event(upload, "started"));
186
+
187
+ let metaAccount = await getOptionalAccount(client, metaAddress);
188
+ let bufferAccount = await getOptionalAccount(client, bufferAddress);
189
+ if (!!metaAccount !== !!bufferAccount) {
190
+ throw new DeployError(
191
+ "UPLOAD_CONFLICT",
192
+ "temporary upload is only partly present",
193
+ );
194
+ }
195
+
196
+ if (!metaAccount || !bufferAccount) {
197
+ let signature: string | undefined;
198
+ let unknownOutcome: DeployError | undefined;
199
+ try {
200
+ signature = await submitTransaction(
201
+ client,
202
+ signer,
203
+ upload.operation,
204
+ "upload",
205
+ {
206
+ program: UPLOADER_PROGRAM_ADDRESS,
207
+ readWrite: [metaAddress, bufferAddress],
208
+ instructionData: createUploadBufferInstruction({
209
+ bufferAccount: upload.addresses.bufferAccountBytes,
210
+ metaAccount: upload.addresses.metaAccountBytes,
211
+ authorityAccount: signer.publicKey,
212
+ bufferSize: upload.bytes.length,
213
+ expectedHash: upload.hash,
214
+ seed: upload.seed,
215
+ }),
216
+ computeUnits: 50_000 + upload.bytes.length * 2,
217
+ stateUnits: 10_000,
218
+ memoryUnits: 10_000,
219
+ expiryAfter: 10,
220
+ },
221
+ );
222
+ } catch (error) {
223
+ if (!(error instanceof DeployError) || error.code !== "OUTCOME_UNKNOWN") {
224
+ throw error;
225
+ }
226
+ unknownOutcome = error;
227
+ signature = error.transactionSignature;
228
+ }
229
+ if (signature) upload.result.transactionSignatures.push(signature);
230
+ ({ metaAccount, bufferAccount } = await waitForUploadPair(
231
+ client,
232
+ metaAddress,
233
+ bufferAddress,
234
+ ));
235
+ if (!metaAccount || !bufferAccount) {
236
+ if (unknownOutcome) throw unknownOutcome;
237
+ throw new DeployError(
238
+ "VERIFICATION_FAILED",
239
+ "temporary upload creation was not visible",
240
+ );
241
+ }
242
+ emitProgress(
243
+ onProgress,
244
+ event(upload, "progress", { uploadStep: "create", signature }),
245
+ );
246
+ } else {
247
+ upload.result.reused = true;
248
+ }
249
+
250
+ const state = assertMatchingUpload(
251
+ upload,
252
+ signer,
253
+ metaAccount,
254
+ bufferAccount,
255
+ );
256
+ if (state === UPLOADER_STATE_FINALIZED) {
257
+ if (
258
+ !bytesEqual(
259
+ requireAccountData(bufferAccount, "uploader buffer account"),
260
+ upload.bytes,
261
+ )
262
+ ) {
263
+ throw new DeployError(
264
+ "UPLOAD_CONFLICT",
265
+ "finalized temporary upload contents do not match",
266
+ );
267
+ }
268
+ emitProgress(
269
+ onProgress,
270
+ event(upload, "succeeded", { uploadStep: "finalize" }),
271
+ );
272
+ return upload.result;
273
+ }
274
+
275
+ const totalChunks = Math.ceil(upload.bytes.length / chunkSize);
276
+ for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex += 1) {
277
+ const offset = chunkIndex * chunkSize;
278
+ const chunk = upload.bytes.slice(
279
+ offset,
280
+ Math.min(offset + chunkSize, upload.bytes.length),
281
+ );
282
+ const current = requireAccountData(
283
+ bufferAccount,
284
+ "uploader buffer account",
285
+ );
286
+ if (bytesEqual(current.slice(offset, offset + chunk.length), chunk)) {
287
+ emitProgress(
288
+ onProgress,
289
+ event(upload, "progress", {
290
+ uploadStep: "write",
291
+ completedChunks: chunkIndex + 1,
292
+ totalChunks,
293
+ }),
294
+ );
295
+ continue;
296
+ }
297
+
298
+ let written = false;
299
+ let lastError: unknown;
300
+ for (let attempt = 0; attempt < 3 && !written; attempt += 1) {
301
+ try {
302
+ const signature = await submitTransaction(
303
+ client,
304
+ signer,
305
+ upload.operation,
306
+ "upload",
307
+ {
308
+ program: UPLOADER_PROGRAM_ADDRESS,
309
+ readWrite: [metaAddress, bufferAddress],
310
+ instructionData: createWriteUploadInstruction({
311
+ bufferAccount: upload.addresses.bufferAccountBytes,
312
+ metaAccount: upload.addresses.metaAccountBytes,
313
+ data: chunk,
314
+ offset,
315
+ }),
316
+ computeUnits: 500_000_000,
317
+ stateUnits: 5_000,
318
+ memoryUnits: 5_000,
319
+ expiryAfter: 10_000,
320
+ },
321
+ );
322
+ upload.result.transactionSignatures.push(signature);
323
+ written = true;
324
+ } catch (error) {
325
+ lastError = error;
326
+ if (error instanceof DeployError && error.transactionSignature) {
327
+ upload.result.transactionSignatures.push(error.transactionSignature);
328
+ }
329
+ bufferAccount = await getOptionalAccount(client, bufferAddress);
330
+ if (
331
+ bufferAccount &&
332
+ bytesEqual(
333
+ requireAccountData(bufferAccount, "uploader buffer account").slice(
334
+ offset,
335
+ offset + chunk.length,
336
+ ),
337
+ chunk,
338
+ )
339
+ ) {
340
+ written = true;
341
+ } else if (
342
+ error instanceof DeployError &&
343
+ error.code === "TRANSACTION_FAILED"
344
+ ) {
345
+ throw error;
346
+ }
347
+ }
348
+ }
349
+ if (!written) throw lastError;
350
+ bufferAccount = await waitForBufferContent(
351
+ client,
352
+ bufferAddress,
353
+ chunk,
354
+ offset,
355
+ );
356
+ if (
357
+ !bufferAccount ||
358
+ !bytesEqual(
359
+ requireAccountData(bufferAccount, "uploader buffer account").slice(
360
+ offset,
361
+ offset + chunk.length,
362
+ ),
363
+ chunk,
364
+ )
365
+ ) {
366
+ throw new DeployError(
367
+ "VERIFICATION_FAILED",
368
+ "temporary upload chunk verification failed",
369
+ );
370
+ }
371
+ emitProgress(
372
+ onProgress,
373
+ event(upload, "progress", {
374
+ uploadStep: "write",
375
+ completedChunks: chunkIndex + 1,
376
+ totalChunks,
377
+ signature: upload.result.transactionSignatures.at(-1),
378
+ }),
379
+ );
380
+ }
381
+
382
+ const finalBuffer = await waitForBufferContent(
383
+ client,
384
+ bufferAddress,
385
+ upload.bytes,
386
+ );
387
+ if (
388
+ !finalBuffer ||
389
+ !bytesEqual(
390
+ requireAccountData(finalBuffer, "uploader buffer account"),
391
+ upload.bytes,
392
+ )
393
+ ) {
394
+ throw new DeployError(
395
+ "VERIFICATION_FAILED",
396
+ "temporary upload content verification failed",
397
+ );
398
+ }
399
+
400
+ let finalizeSignature: string | undefined;
401
+ let unknownFinalizeOutcome: DeployError | undefined;
402
+ try {
403
+ finalizeSignature = await submitTransaction(
404
+ client,
405
+ signer,
406
+ upload.operation,
407
+ "upload",
408
+ {
409
+ program: UPLOADER_PROGRAM_ADDRESS,
410
+ readWrite: [metaAddress, bufferAddress],
411
+ instructionData: createFinalizeUploadInstruction({
412
+ bufferAccount: upload.addresses.bufferAccountBytes,
413
+ metaAccount: upload.addresses.metaAccountBytes,
414
+ expectedHash: upload.hash,
415
+ }),
416
+ computeUnits: 50_000 + upload.bytes.length * 200,
417
+ stateUnits: 5_000,
418
+ memoryUnits: 5_000,
419
+ expiryAfter: 10_000,
420
+ },
421
+ );
422
+ } catch (error) {
423
+ if (!(error instanceof DeployError) || error.code !== "OUTCOME_UNKNOWN") {
424
+ throw error;
425
+ }
426
+ unknownFinalizeOutcome = error;
427
+ finalizeSignature = error.transactionSignature;
428
+ }
429
+ if (finalizeSignature) {
430
+ upload.result.transactionSignatures.push(finalizeSignature);
431
+ }
432
+ let finalizedMeta: Account | undefined;
433
+ for (let attempt = 0; attempt < 12; attempt += 1) {
434
+ finalizedMeta = await getOptionalAccount(client, metaAddress);
435
+ if (
436
+ finalizedMeta &&
437
+ parseUploaderMeta(finalizedMeta).state === UPLOADER_STATE_FINALIZED
438
+ )
439
+ break;
440
+ if (attempt < 11) await new Promise((resolve) => setTimeout(resolve, 250));
441
+ }
442
+ if (
443
+ !finalizedMeta ||
444
+ parseUploaderMeta(finalizedMeta).state !== UPLOADER_STATE_FINALIZED
445
+ ) {
446
+ if (unknownFinalizeOutcome) throw unknownFinalizeOutcome;
447
+ throw new DeployError(
448
+ "VERIFICATION_FAILED",
449
+ "temporary upload did not finalize",
450
+ );
451
+ }
452
+ emitProgress(
453
+ onProgress,
454
+ event(upload, "succeeded", {
455
+ uploadStep: "finalize",
456
+ signature: finalizeSignature,
457
+ }),
458
+ );
459
+ return upload.result;
460
+ }
461
+
462
+ export async function cleanupUpload(
463
+ client: Thru,
464
+ signer: ResolvedSigner,
465
+ upload: PreparedUpload,
466
+ onProgress?: (event: DeployProgressEvent) => void,
467
+ ): Promise<UploadCleanupResult> {
468
+ const metaAddress = upload.result.metaAccountAddress;
469
+ const bufferAddress = upload.result.bufferAccountAddress;
470
+ const meta = await getOptionalAccount(client, metaAddress);
471
+ const buffer = await getOptionalAccount(client, bufferAddress);
472
+ if (!meta && !buffer) {
473
+ upload.result.cleanup = { status: "not-needed" };
474
+ return upload.result.cleanup;
475
+ }
476
+ emitProgress(onProgress, {
477
+ operation: upload.operation,
478
+ phase: "cleanup",
479
+ status: "started",
480
+ artifact: upload.artifact,
481
+ });
482
+ let cleanupSignature: string | undefined;
483
+ let remainingAccountAddresses: string[] | undefined;
484
+ try {
485
+ if (!meta || !buffer)
486
+ throw new Error("temporary upload is only partly present");
487
+ const signature = await submitTransaction(
488
+ client,
489
+ signer,
490
+ upload.operation,
491
+ "cleanup",
492
+ {
493
+ program: UPLOADER_PROGRAM_ADDRESS,
494
+ readWrite: [metaAddress, bufferAddress],
495
+ instructionData: createDestroyUploadInstruction({
496
+ bufferAccount: upload.addresses.bufferAccountBytes,
497
+ metaAccount: upload.addresses.metaAccountBytes,
498
+ }),
499
+ computeUnits: 50_000,
500
+ stateUnits: 5_000,
501
+ memoryUnits: 5_000,
502
+ expiryAfter: 10_000,
503
+ },
504
+ );
505
+ cleanupSignature = signature;
506
+ let remaining = true;
507
+ for (let attempt = 0; attempt < 4 && remaining; attempt += 1) {
508
+ const [remainingMeta, remainingBuffer] = await Promise.all([
509
+ getOptionalAccount(client, metaAddress),
510
+ getOptionalAccount(client, bufferAddress),
511
+ ]);
512
+ remaining = !!remainingMeta || !!remainingBuffer;
513
+ remainingAccountAddresses = [
514
+ ...(remainingMeta ? [metaAddress] : []),
515
+ ...(remainingBuffer ? [bufferAddress] : []),
516
+ ];
517
+ if (remaining && attempt < 3) {
518
+ await new Promise((resolve) => setTimeout(resolve, 250));
519
+ }
520
+ }
521
+ if (remaining) {
522
+ throw new Error("temporary upload accounts remain after cleanup");
523
+ }
524
+ upload.result.cleanup = {
525
+ status: "succeeded",
526
+ transactionSignature: signature,
527
+ };
528
+ emitProgress(onProgress, {
529
+ operation: upload.operation,
530
+ phase: "cleanup",
531
+ status: "succeeded",
532
+ artifact: upload.artifact,
533
+ signature,
534
+ });
535
+ } catch (error) {
536
+ const message = error instanceof Error ? error.message : String(error);
537
+ if (error instanceof DeployError && error.transactionSignature) {
538
+ cleanupSignature ??= error.transactionSignature;
539
+ }
540
+ upload.result.cleanup = {
541
+ status: "failed",
542
+ error: message,
543
+ transactionSignature: cleanupSignature,
544
+ remainingAccountAddresses: remainingAccountAddresses ?? [
545
+ metaAddress,
546
+ bufferAddress,
547
+ ],
548
+ };
549
+ emitProgress(onProgress, {
550
+ operation: upload.operation,
551
+ phase: "cleanup",
552
+ status: "failed",
553
+ artifact: upload.artifact,
554
+ message,
555
+ });
556
+ }
557
+ return upload.result.cleanup;
558
+ }
@@ -0,0 +1,55 @@
1
+ import type { Thru } from "@thru/sdk/client";
2
+ import { describe, expect, it, vi } from "vitest";
3
+
4
+ import { validateABI } from "./validation";
5
+
6
+ const IMPORT_ADDRESS =
7
+ "ta1blxgaYR0dei5aldWJe1vbUtt-LkVEBOzNJtSRhHQcTG";
8
+
9
+ function abiAccount(contents: string): Uint8Array {
10
+ const encoded = new TextEncoder().encode(contents);
11
+ const data = new Uint8Array(45 + encoded.length);
12
+ const view = new DataView(data.buffer);
13
+ view.setBigUint64(32, 0n, true);
14
+ data[40] = 1;
15
+ view.setUint32(41, encoded.length, true);
16
+ data.set(encoded, 45);
17
+ return data;
18
+ }
19
+
20
+ describe("deployment ABI import validation", () => {
21
+ it("resolves imports with the target-chain client and preserves root YAML", async () => {
22
+ const dependency = `
23
+ abi:
24
+ package: test.dependency
25
+ abi-version: 1
26
+ package-version: "1.0.0"
27
+ description: dependency
28
+ imports: []
29
+ types: []
30
+ `;
31
+ const root = `
32
+ abi:
33
+ package: test.root
34
+ abi-version: 1
35
+ package-version: "1.0.0"
36
+ description: root
37
+ imports:
38
+ - type: onchain
39
+ address: ${IMPORT_ADDRESS}
40
+ target: abi
41
+ network: mainnet
42
+ revision: latest
43
+ types: []
44
+ `;
45
+ const getRaw = vi.fn(async () => ({
46
+ rawData: abiAccount(dependency),
47
+ }));
48
+ const client = { accounts: { getRaw } } as unknown as Thru;
49
+
50
+ await expect(
51
+ validateABI(new TextEncoder().encode(root), client),
52
+ ).resolves.toBe(root);
53
+ expect(getRaw).toHaveBeenCalledOnce();
54
+ });
55
+ });