@thru/programs 0.3.3 → 0.3.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.
- package/README.md +123 -37
- package/dist/abi-manager/index.cjs +9568 -0
- package/dist/abi-manager/index.cjs.map +1 -0
- package/dist/abi-manager/index.d.cts +2169 -0
- package/dist/abi-manager/index.d.ts +2169 -0
- package/dist/abi-manager/index.js +4 -0
- package/dist/abi-manager/index.js.map +1 -0
- package/dist/amm/index.cjs +1 -3
- package/dist/amm/index.cjs.map +1 -1
- package/dist/amm/index.d.cts +1 -1
- package/dist/amm/index.d.ts +1 -1
- package/dist/amm/index.js +2 -2
- package/dist/amm/index.js.map +1 -1
- package/dist/chunk-6IC6CO5S.js +9443 -0
- package/dist/chunk-6IC6CO5S.js.map +1 -0
- package/dist/chunk-7DRJVAYI.js +8587 -0
- package/dist/chunk-7DRJVAYI.js.map +1 -0
- package/dist/chunk-7FBCBZIY.js +41 -0
- package/dist/chunk-7FBCBZIY.js.map +1 -0
- package/dist/chunk-BNYDH4ZP.js +4351 -0
- package/dist/chunk-BNYDH4ZP.js.map +1 -0
- package/dist/chunk-OODN6LJA.js +6567 -0
- package/dist/chunk-OODN6LJA.js.map +1 -0
- package/dist/chunk-YX7BLFQZ.js +81 -0
- package/dist/chunk-YX7BLFQZ.js.map +1 -0
- package/dist/clob/index.cjs +19361 -6851
- package/dist/clob/index.cjs.map +1 -1
- package/dist/clob/index.d.cts +1243 -63
- package/dist/clob/index.d.ts +1243 -63
- package/dist/clob/index.js +6826 -2818
- package/dist/clob/index.js.map +1 -1
- package/dist/deploy/index.cjs +24734 -0
- package/dist/deploy/index.cjs.map +1 -0
- package/dist/deploy/index.d.cts +180 -0
- package/dist/deploy/index.d.ts +180 -0
- package/dist/deploy/index.js +1846 -0
- package/dist/deploy/index.js.map +1 -0
- package/dist/helpers-DD7q61nr.d.cts +7 -0
- package/dist/helpers-DD7q61nr.d.ts +7 -0
- package/dist/manager/index.cjs +6680 -0
- package/dist/manager/index.cjs.map +1 -0
- package/dist/manager/index.d.cts +1279 -0
- package/dist/manager/index.d.ts +1279 -0
- package/dist/manager/index.js +4 -0
- package/dist/manager/index.js.map +1 -0
- package/dist/passkey-manager/index.cjs +4 -5
- package/dist/passkey-manager/index.cjs.map +1 -1
- package/dist/passkey-manager/index.js +2 -2
- package/dist/token/index.js +1 -8585
- package/dist/token/index.js.map +1 -1
- package/dist/uploader/index.cjs +4442 -0
- package/dist/uploader/index.cjs.map +1 -0
- package/dist/uploader/index.d.cts +761 -0
- package/dist/uploader/index.d.ts +761 -0
- package/dist/uploader/index.js +4 -0
- package/dist/uploader/index.js.map +1 -0
- package/package.json +22 -2
- package/src/abi-manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/abi-manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/abi-manager/abi/thru/program/abi_manager/types.ts +7037 -0
- package/src/abi-manager/accounts.ts +91 -0
- package/src/abi-manager/constants.ts +19 -0
- package/src/abi-manager/derivation.ts +86 -0
- package/src/abi-manager/errors.ts +48 -0
- package/src/abi-manager/index.test.ts +136 -0
- package/src/abi-manager/index.ts +56 -0
- package/src/abi-manager/instructions.ts +573 -0
- package/src/abi-manager/types.ts +150 -0
- package/src/amm/index.test.ts +8 -2
- package/src/amm/index.ts +1 -1
- package/src/clob/abi/thru/program/clob/types.ts +6981 -3070
- package/src/clob/exchange-client.test.ts +592 -0
- package/src/clob/exchange-client.ts +953 -0
- package/src/clob/exchange.test.ts +18 -16
- package/src/clob/exchange.ts +4 -0
- package/src/clob/index.test.ts +134 -7
- package/src/clob/index.ts +303 -10
- package/src/clob/price.test.ts +32 -0
- package/src/clob/price.ts +77 -0
- package/src/deploy/accounts.ts +208 -0
- package/src/deploy/chain.ts +163 -0
- package/src/deploy/constants.ts +25 -0
- package/src/deploy/deploy.test.ts +349 -0
- package/src/deploy/errors.ts +72 -0
- package/src/deploy/index.ts +44 -0
- package/src/deploy/inspection.ts +146 -0
- package/src/deploy/seeds.ts +29 -0
- package/src/deploy/types.ts +178 -0
- package/src/deploy/uploader.test.ts +326 -0
- package/src/deploy/uploader.ts +558 -0
- package/src/deploy/validation-imports.test.ts +55 -0
- package/src/deploy/validation.ts +183 -0
- package/src/deploy/workflows.test.ts +422 -0
- package/src/deploy/workflows.ts +1088 -0
- package/src/helpers/bytes.test.ts +58 -0
- package/src/helpers/bytes.ts +28 -3
- package/src/manager/abi/thru/blockchain/state_proof/types.ts +1906 -0
- package/src/manager/abi/thru/common/primitives/types.ts +2428 -0
- package/src/manager/abi/thru/program/manager/types.ts +3814 -0
- package/src/manager/accounts.ts +44 -0
- package/src/manager/constants.ts +16 -0
- package/src/manager/derivation.ts +32 -0
- package/src/manager/errors.ts +47 -0
- package/src/manager/index.test.ts +108 -0
- package/src/manager/index.ts +68 -0
- package/src/manager/instructions.ts +281 -0
- package/src/manager/types.ts +95 -0
- package/src/manager/validation.ts +35 -0
- package/src/uploader/abi/thru/common/primitives/types.ts +2428 -0
- package/src/uploader/abi/thru/program/uploader/types.ts +3022 -0
- package/src/uploader/accounts.ts +38 -0
- package/src/uploader/constants.ts +14 -0
- package/src/uploader/derivation.ts +34 -0
- package/src/uploader/errors.ts +25 -0
- package/src/uploader/index.test.ts +107 -0
- package/src/uploader/index.ts +34 -0
- package/src/uploader/instructions.ts +163 -0
- package/src/uploader/types.ts +67 -0
- package/src/utils/helpers.ts +121 -0
- package/tsup.config.ts +4 -0
- package/dist/chunk-P5OABVJI.js +0 -28
- package/dist/chunk-P5OABVJI.js.map +0 -1
|
@@ -0,0 +1,1088 @@
|
|
|
1
|
+
import { Pubkey, createThruClient, type InstructionContext } from "@thru/sdk";
|
|
2
|
+
import type { Thru } from "@thru/sdk/client";
|
|
3
|
+
import {
|
|
4
|
+
createOfficialABIInstruction,
|
|
5
|
+
createOfficialABIMetaInstruction,
|
|
6
|
+
createUpgradeOfficialABIInstruction,
|
|
7
|
+
deriveProgramABIAddresses,
|
|
8
|
+
type ProgramABIAddresses,
|
|
9
|
+
} from "../abi-manager";
|
|
10
|
+
import {
|
|
11
|
+
createEphemeralProgramInstruction,
|
|
12
|
+
createPermanentProgramInstruction,
|
|
13
|
+
createUpgradeProgramInstruction,
|
|
14
|
+
deriveManagedProgramAddresses,
|
|
15
|
+
type InstructionData,
|
|
16
|
+
type ManagedProgramAddresses,
|
|
17
|
+
} from "../manager";
|
|
18
|
+
import { buildMulticallInstruction } from "../multicall";
|
|
19
|
+
import { bytesEqual } from "../helpers/bytes";
|
|
20
|
+
import { temporarySeed } from "./seeds";
|
|
21
|
+
import {
|
|
22
|
+
assertABIOpen,
|
|
23
|
+
assertManagedProgramAccount,
|
|
24
|
+
assertManagerOpen,
|
|
25
|
+
assertPubkeyBytes,
|
|
26
|
+
getOptionalAccount,
|
|
27
|
+
parseABIAccount,
|
|
28
|
+
parseABIMeta,
|
|
29
|
+
parseManagerMeta,
|
|
30
|
+
requireAccountData,
|
|
31
|
+
type ParsedABIAccount,
|
|
32
|
+
type ParsedManagerMeta,
|
|
33
|
+
} from "./accounts";
|
|
34
|
+
import {
|
|
35
|
+
creationProof,
|
|
36
|
+
emitProgress,
|
|
37
|
+
submitTransaction,
|
|
38
|
+
type DeploymentTransaction,
|
|
39
|
+
} from "./chain";
|
|
40
|
+
import {
|
|
41
|
+
ABI_MANAGER_PROGRAM_ADDRESS,
|
|
42
|
+
MANAGER_PROGRAM_ADDRESS,
|
|
43
|
+
MULTICALL_PROGRAM_ADDRESS,
|
|
44
|
+
} from "./constants";
|
|
45
|
+
import { DeployError, asDeployError } from "./errors";
|
|
46
|
+
import type {
|
|
47
|
+
DeployProgramABIRequest,
|
|
48
|
+
DeployProgramABIResult,
|
|
49
|
+
DeployProgramRequest,
|
|
50
|
+
DeployProgramResult,
|
|
51
|
+
DeployProgressEvent,
|
|
52
|
+
DeploymentOperation,
|
|
53
|
+
DeploymentRequestBase,
|
|
54
|
+
UpgradeProgramABIRequest,
|
|
55
|
+
UpgradeProgramABIResult,
|
|
56
|
+
UpgradeProgramRequest,
|
|
57
|
+
UpgradeProgramResult,
|
|
58
|
+
} from "./types";
|
|
59
|
+
import {
|
|
60
|
+
cleanupUpload,
|
|
61
|
+
prepareUpload,
|
|
62
|
+
uploadArtifact,
|
|
63
|
+
type PreparedUpload,
|
|
64
|
+
} from "./uploader";
|
|
65
|
+
import {
|
|
66
|
+
resolveSigner,
|
|
67
|
+
normalizeBytes,
|
|
68
|
+
validateABI,
|
|
69
|
+
validateProgramImage,
|
|
70
|
+
validateSeedAndChunkSize,
|
|
71
|
+
type ResolvedSigner,
|
|
72
|
+
} from "./validation";
|
|
73
|
+
|
|
74
|
+
interface Context {
|
|
75
|
+
operation: DeploymentOperation;
|
|
76
|
+
client: Thru;
|
|
77
|
+
signer: ResolvedSigner;
|
|
78
|
+
seed: string;
|
|
79
|
+
ephemeral: boolean;
|
|
80
|
+
chunkSize: number;
|
|
81
|
+
program: ManagedProgramAddresses;
|
|
82
|
+
abi: ProgramABIAddresses;
|
|
83
|
+
onProgress?: (event: DeployProgressEvent) => void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
interface ProgramState {
|
|
87
|
+
meta: ParsedManagerMeta;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface ABIState {
|
|
91
|
+
abi: ParsedABIAccount;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function createProgramInstruction(
|
|
95
|
+
context: Context,
|
|
96
|
+
upload: PreparedUpload,
|
|
97
|
+
sourceSize: number,
|
|
98
|
+
metaStateProof: Uint8Array,
|
|
99
|
+
programStateProof: Uint8Array,
|
|
100
|
+
): InstructionData {
|
|
101
|
+
const common = {
|
|
102
|
+
metaAccount: context.program.programMetaAccountBytes,
|
|
103
|
+
programAccount: context.program.programAccountBytes,
|
|
104
|
+
sourceBufferAccount: upload.addresses.bufferAccountBytes,
|
|
105
|
+
authorityAccount: context.signer.publicKey,
|
|
106
|
+
sourceSize,
|
|
107
|
+
seed: context.seed,
|
|
108
|
+
};
|
|
109
|
+
return context.ephemeral
|
|
110
|
+
? createEphemeralProgramInstruction(common)
|
|
111
|
+
: createPermanentProgramInstruction({
|
|
112
|
+
...common,
|
|
113
|
+
metaStateProof,
|
|
114
|
+
programStateProof,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function createABIMetaInstruction(
|
|
119
|
+
context: Context,
|
|
120
|
+
stateProof: Uint8Array,
|
|
121
|
+
): InstructionData {
|
|
122
|
+
const common = {
|
|
123
|
+
abiMetaAccount: context.abi.abiMetaAccountBytes,
|
|
124
|
+
programMetaAccount: context.program.programMetaAccountBytes,
|
|
125
|
+
authorityAccount: context.signer.publicKey,
|
|
126
|
+
};
|
|
127
|
+
return context.ephemeral
|
|
128
|
+
? createOfficialABIMetaInstruction({ ...common, ephemeral: true })
|
|
129
|
+
: createOfficialABIMetaInstruction({
|
|
130
|
+
...common,
|
|
131
|
+
stateProof,
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function createABIInstruction(
|
|
136
|
+
context: Context,
|
|
137
|
+
upload: PreparedUpload,
|
|
138
|
+
sourceSize: number,
|
|
139
|
+
stateProof: Uint8Array,
|
|
140
|
+
): InstructionData {
|
|
141
|
+
const common = {
|
|
142
|
+
abiMetaAccount: context.abi.abiMetaAccountBytes,
|
|
143
|
+
programMetaAccount: context.program.programMetaAccountBytes,
|
|
144
|
+
abiAccount: context.abi.abiAccountBytes,
|
|
145
|
+
sourceBufferAccount: upload.addresses.bufferAccountBytes,
|
|
146
|
+
authorityAccount: context.signer.publicKey,
|
|
147
|
+
sourceSize,
|
|
148
|
+
};
|
|
149
|
+
return context.ephemeral
|
|
150
|
+
? createOfficialABIInstruction({ ...common, ephemeral: true })
|
|
151
|
+
: createOfficialABIInstruction({
|
|
152
|
+
...common,
|
|
153
|
+
stateProof,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function upgradeProgramInstruction(
|
|
158
|
+
context: Context,
|
|
159
|
+
upload: PreparedUpload,
|
|
160
|
+
sourceSize: number,
|
|
161
|
+
): InstructionData {
|
|
162
|
+
return createUpgradeProgramInstruction({
|
|
163
|
+
metaAccount: context.program.programMetaAccountBytes,
|
|
164
|
+
programAccount: context.program.programAccountBytes,
|
|
165
|
+
sourceBufferAccount: upload.addresses.bufferAccountBytes,
|
|
166
|
+
sourceSize,
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
function upgradeABIInstruction(
|
|
171
|
+
context: Context,
|
|
172
|
+
upload: PreparedUpload,
|
|
173
|
+
sourceSize: number,
|
|
174
|
+
): InstructionData {
|
|
175
|
+
return createUpgradeOfficialABIInstruction({
|
|
176
|
+
abiMetaAccount: context.abi.abiMetaAccountBytes,
|
|
177
|
+
programMetaAccount: context.program.programMetaAccountBytes,
|
|
178
|
+
abiAccount: context.abi.abiAccountBytes,
|
|
179
|
+
sourceBufferAccount: upload.addresses.bufferAccountBytes,
|
|
180
|
+
authorityAccount: context.signer.publicKey,
|
|
181
|
+
sourceSize,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function progress(
|
|
186
|
+
context: Context,
|
|
187
|
+
phase: DeployProgressEvent["phase"],
|
|
188
|
+
status: DeployProgressEvent["status"],
|
|
189
|
+
fields: Partial<DeployProgressEvent> = {},
|
|
190
|
+
): void {
|
|
191
|
+
emitProgress(context.onProgress, {
|
|
192
|
+
operation: context.operation,
|
|
193
|
+
phase,
|
|
194
|
+
status,
|
|
195
|
+
...fields,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function createContext(
|
|
200
|
+
operation: DeploymentOperation,
|
|
201
|
+
request: DeploymentRequestBase,
|
|
202
|
+
programAddressAssertion?: string,
|
|
203
|
+
): Promise<Context> {
|
|
204
|
+
const client = request.client ?? createThruClient();
|
|
205
|
+
const chunkSize = validateSeedAndChunkSize(request.seed, request.chunkSize);
|
|
206
|
+
const signer = await resolveSigner(
|
|
207
|
+
client,
|
|
208
|
+
request.signer.address,
|
|
209
|
+
request.signer.privateKey,
|
|
210
|
+
);
|
|
211
|
+
const ephemeral = request.ephemeral ?? false;
|
|
212
|
+
const program = deriveManagedProgramAddresses(request.seed, ephemeral);
|
|
213
|
+
|
|
214
|
+
if (programAddressAssertion) {
|
|
215
|
+
let supplied: Pubkey;
|
|
216
|
+
try {
|
|
217
|
+
supplied = Pubkey.from(programAddressAssertion);
|
|
218
|
+
} catch (error) {
|
|
219
|
+
throw asDeployError(
|
|
220
|
+
error,
|
|
221
|
+
"INVALID_INPUT",
|
|
222
|
+
"programAddress is not a valid Thru address",
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
if (!supplied.equals(program.programAccountAddress)) {
|
|
226
|
+
throw new DeployError(
|
|
227
|
+
"INVALID_INPUT",
|
|
228
|
+
"programAddress does not match the address derived from seed and ephemeral",
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const abi = deriveProgramABIAddresses(
|
|
234
|
+
program.programAccountAddress,
|
|
235
|
+
ephemeral,
|
|
236
|
+
);
|
|
237
|
+
return {
|
|
238
|
+
operation,
|
|
239
|
+
client,
|
|
240
|
+
signer,
|
|
241
|
+
seed: request.seed,
|
|
242
|
+
ephemeral,
|
|
243
|
+
chunkSize,
|
|
244
|
+
program,
|
|
245
|
+
abi,
|
|
246
|
+
onProgress: request.onProgress,
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function addressDetails(context: Context): Record<string, string> {
|
|
251
|
+
return {
|
|
252
|
+
programMetaAccountAddress: context.program.programMetaAccountAddress,
|
|
253
|
+
programAccountAddress: context.program.programAccountAddress,
|
|
254
|
+
abiMetaAccountAddress: context.abi.abiMetaAccountAddress,
|
|
255
|
+
abiAccountAddress: context.abi.abiAccountAddress,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
async function assertAbsent(
|
|
260
|
+
context: Context,
|
|
261
|
+
entries: Array<[string, string]>,
|
|
262
|
+
): Promise<void> {
|
|
263
|
+
const accounts = await Promise.all(
|
|
264
|
+
entries.map(([, address]) => getOptionalAccount(context.client, address)),
|
|
265
|
+
);
|
|
266
|
+
const existing = entries
|
|
267
|
+
.filter((_, index) => accounts[index])
|
|
268
|
+
.map(([label]) => label);
|
|
269
|
+
if (existing.length) {
|
|
270
|
+
throw new DeployError(
|
|
271
|
+
"TARGET_EXISTS",
|
|
272
|
+
`deployment target already exists: ${existing.join(", ")}`,
|
|
273
|
+
{
|
|
274
|
+
operation: context.operation,
|
|
275
|
+
phase: "preflight",
|
|
276
|
+
addresses: addressDetails(context),
|
|
277
|
+
},
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
async function requireProgram(context: Context): Promise<ProgramState> {
|
|
283
|
+
const [metaAccount, programAccount] = await Promise.all([
|
|
284
|
+
getOptionalAccount(
|
|
285
|
+
context.client,
|
|
286
|
+
context.program.programMetaAccountAddress,
|
|
287
|
+
),
|
|
288
|
+
getOptionalAccount(context.client, context.program.programAccountAddress),
|
|
289
|
+
]);
|
|
290
|
+
if (!metaAccount || !programAccount) {
|
|
291
|
+
throw new DeployError(
|
|
292
|
+
"TARGET_NOT_FOUND",
|
|
293
|
+
"managed program or its metadata account is missing",
|
|
294
|
+
{
|
|
295
|
+
operation: context.operation,
|
|
296
|
+
phase: "preflight",
|
|
297
|
+
addresses: addressDetails(context),
|
|
298
|
+
},
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
const meta = parseManagerMeta(metaAccount);
|
|
302
|
+
assertManagerOpen(meta);
|
|
303
|
+
if (!bytesEqual(meta.authority, context.signer.publicKey)) {
|
|
304
|
+
throw new DeployError(
|
|
305
|
+
"SIGNER_MISMATCH",
|
|
306
|
+
"signer is not the managed program authority",
|
|
307
|
+
{
|
|
308
|
+
operation: context.operation,
|
|
309
|
+
phase: "preflight",
|
|
310
|
+
addresses: addressDetails(context),
|
|
311
|
+
},
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
assertManagedProgramAccount(programAccount);
|
|
315
|
+
return { meta };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
async function requireABI(context: Context): Promise<ABIState> {
|
|
319
|
+
const [metaAccount, abiAccount] = await Promise.all([
|
|
320
|
+
getOptionalAccount(context.client, context.abi.abiMetaAccountAddress),
|
|
321
|
+
getOptionalAccount(context.client, context.abi.abiAccountAddress),
|
|
322
|
+
]);
|
|
323
|
+
if (!metaAccount || !abiAccount) {
|
|
324
|
+
throw new DeployError(
|
|
325
|
+
"TARGET_NOT_FOUND",
|
|
326
|
+
"program ABI or its metadata account is missing",
|
|
327
|
+
{
|
|
328
|
+
operation: context.operation,
|
|
329
|
+
phase: "preflight",
|
|
330
|
+
addresses: addressDetails(context),
|
|
331
|
+
},
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
const meta = parseABIMeta(metaAccount);
|
|
335
|
+
const abi = parseABIAccount(abiAccount);
|
|
336
|
+
assertABIOpen(abi);
|
|
337
|
+
assertPubkeyBytes(
|
|
338
|
+
meta.program,
|
|
339
|
+
context.program.programAccountAddress,
|
|
340
|
+
"ABI metadata points to another program",
|
|
341
|
+
);
|
|
342
|
+
assertPubkeyBytes(
|
|
343
|
+
abi.abiMetaAccount,
|
|
344
|
+
context.abi.abiMetaAccountAddress,
|
|
345
|
+
"ABI account points to another metadata account",
|
|
346
|
+
);
|
|
347
|
+
return { abi };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
async function verifyProgram(
|
|
351
|
+
context: Context,
|
|
352
|
+
expectedBytes: Uint8Array | undefined,
|
|
353
|
+
expectedVersion: bigint,
|
|
354
|
+
): Promise<bigint> {
|
|
355
|
+
const [metaAccount, programAccount] = await Promise.all([
|
|
356
|
+
getOptionalAccount(
|
|
357
|
+
context.client,
|
|
358
|
+
context.program.programMetaAccountAddress,
|
|
359
|
+
),
|
|
360
|
+
getOptionalAccount(context.client, context.program.programAccountAddress),
|
|
361
|
+
]);
|
|
362
|
+
if (!metaAccount || !programAccount) {
|
|
363
|
+
throw new DeployError(
|
|
364
|
+
"VERIFICATION_FAILED",
|
|
365
|
+
"managed program readback is missing",
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
const meta = parseManagerMeta(metaAccount);
|
|
369
|
+
assertManagerOpen(meta);
|
|
370
|
+
assertPubkeyBytes(
|
|
371
|
+
meta.authority,
|
|
372
|
+
context.signer.publicKey,
|
|
373
|
+
"managed program authority mismatch",
|
|
374
|
+
);
|
|
375
|
+
if (meta.version !== expectedVersion) {
|
|
376
|
+
throw new DeployError(
|
|
377
|
+
"VERIFICATION_FAILED",
|
|
378
|
+
`program version mismatch: expected ${expectedVersion}, got ${meta.version}`,
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
assertManagedProgramAccount(programAccount);
|
|
382
|
+
if (
|
|
383
|
+
expectedBytes &&
|
|
384
|
+
!bytesEqual(
|
|
385
|
+
requireAccountData(programAccount, "program account"),
|
|
386
|
+
expectedBytes,
|
|
387
|
+
)
|
|
388
|
+
) {
|
|
389
|
+
throw new DeployError(
|
|
390
|
+
"VERIFICATION_FAILED",
|
|
391
|
+
"managed program bytes do not match the upload",
|
|
392
|
+
);
|
|
393
|
+
}
|
|
394
|
+
return meta.version;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
async function verifyABI(
|
|
398
|
+
context: Context,
|
|
399
|
+
expectedBytes: Uint8Array,
|
|
400
|
+
expectedRevision: bigint,
|
|
401
|
+
): Promise<bigint> {
|
|
402
|
+
const [metaAccount, abiAccount] = await Promise.all([
|
|
403
|
+
getOptionalAccount(context.client, context.abi.abiMetaAccountAddress),
|
|
404
|
+
getOptionalAccount(context.client, context.abi.abiAccountAddress),
|
|
405
|
+
]);
|
|
406
|
+
if (!metaAccount || !abiAccount) {
|
|
407
|
+
throw new DeployError(
|
|
408
|
+
"VERIFICATION_FAILED",
|
|
409
|
+
"program ABI readback is missing",
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
const meta = parseABIMeta(metaAccount);
|
|
413
|
+
const abi = parseABIAccount(abiAccount);
|
|
414
|
+
assertABIOpen(abi);
|
|
415
|
+
assertPubkeyBytes(
|
|
416
|
+
meta.program,
|
|
417
|
+
context.program.programAccountAddress,
|
|
418
|
+
"ABI metadata program mismatch",
|
|
419
|
+
);
|
|
420
|
+
assertPubkeyBytes(
|
|
421
|
+
abi.abiMetaAccount,
|
|
422
|
+
context.abi.abiMetaAccountAddress,
|
|
423
|
+
"ABI metadata link mismatch",
|
|
424
|
+
);
|
|
425
|
+
if (abi.revision !== expectedRevision) {
|
|
426
|
+
throw new DeployError(
|
|
427
|
+
"VERIFICATION_FAILED",
|
|
428
|
+
`ABI revision mismatch: expected ${expectedRevision}, got ${abi.revision}`,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
if (!bytesEqual(abi.content, expectedBytes)) {
|
|
432
|
+
throw new DeployError(
|
|
433
|
+
"VERIFICATION_FAILED",
|
|
434
|
+
"ABI contents do not match the upload",
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
return abi.revision;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function delay(milliseconds: number): Promise<void> {
|
|
441
|
+
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
async function retryVerification<T>(verify: () => Promise<T>): Promise<T> {
|
|
445
|
+
let lastError: unknown;
|
|
446
|
+
for (let attempt = 0; attempt < 8; attempt += 1) {
|
|
447
|
+
try {
|
|
448
|
+
return await verify();
|
|
449
|
+
} catch (error) {
|
|
450
|
+
lastError = error;
|
|
451
|
+
if (attempt < 7) await delay(250);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
throw asDeployError(
|
|
455
|
+
lastError,
|
|
456
|
+
"VERIFICATION_FAILED",
|
|
457
|
+
"Deployment readback verification failed",
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
async function commitAndVerify<T>(
|
|
462
|
+
context: Context,
|
|
463
|
+
transaction: DeploymentTransaction,
|
|
464
|
+
verify: () => Promise<T>,
|
|
465
|
+
): Promise<{ signature: string; verified: T }> {
|
|
466
|
+
progress(context, "commit", "started");
|
|
467
|
+
let signature: string;
|
|
468
|
+
try {
|
|
469
|
+
signature = await submitTransaction(
|
|
470
|
+
context.client,
|
|
471
|
+
context.signer,
|
|
472
|
+
context.operation,
|
|
473
|
+
"commit",
|
|
474
|
+
transaction,
|
|
475
|
+
);
|
|
476
|
+
} catch (error) {
|
|
477
|
+
if (
|
|
478
|
+
error instanceof DeployError &&
|
|
479
|
+
error.code === "OUTCOME_UNKNOWN" &&
|
|
480
|
+
error.transactionSignature
|
|
481
|
+
) {
|
|
482
|
+
try {
|
|
483
|
+
try {
|
|
484
|
+
const status = await context.client.transactions.getStatus(
|
|
485
|
+
error.transactionSignature,
|
|
486
|
+
);
|
|
487
|
+
const execution = status.executionResult;
|
|
488
|
+
if (
|
|
489
|
+
execution &&
|
|
490
|
+
(execution.vmError !== 0 || execution.userErrorCode !== 0n)
|
|
491
|
+
) {
|
|
492
|
+
throw new DeployError(
|
|
493
|
+
"TRANSACTION_FAILED",
|
|
494
|
+
"Deployment transaction failed",
|
|
495
|
+
{
|
|
496
|
+
operation: context.operation,
|
|
497
|
+
phase: "commit",
|
|
498
|
+
transactionSignature: error.transactionSignature,
|
|
499
|
+
vmError: execution.vmError,
|
|
500
|
+
userErrorCode: execution.userErrorCode,
|
|
501
|
+
},
|
|
502
|
+
);
|
|
503
|
+
}
|
|
504
|
+
} catch (statusError) {
|
|
505
|
+
if (statusError instanceof DeployError) throw statusError;
|
|
506
|
+
/* The final account read below is the second reconciliation source. */
|
|
507
|
+
}
|
|
508
|
+
const verified = await retryVerification(verify);
|
|
509
|
+
progress(context, "commit", "succeeded", {
|
|
510
|
+
signature: error.transactionSignature,
|
|
511
|
+
});
|
|
512
|
+
progress(context, "verification", "succeeded");
|
|
513
|
+
return { signature: error.transactionSignature, verified };
|
|
514
|
+
} catch (reconciliationError) {
|
|
515
|
+
if (
|
|
516
|
+
reconciliationError instanceof DeployError &&
|
|
517
|
+
reconciliationError.code === "TRANSACTION_FAILED"
|
|
518
|
+
) {
|
|
519
|
+
throw reconciliationError;
|
|
520
|
+
}
|
|
521
|
+
throw error;
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
throw error;
|
|
525
|
+
}
|
|
526
|
+
progress(context, "commit", "succeeded", { signature });
|
|
527
|
+
progress(context, "verification", "started");
|
|
528
|
+
const verified = await retryVerification(verify);
|
|
529
|
+
progress(context, "verification", "succeeded");
|
|
530
|
+
return { signature, verified };
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function cleanupAll(
|
|
534
|
+
context: Context,
|
|
535
|
+
uploads: PreparedUpload[],
|
|
536
|
+
): Promise<string[]> {
|
|
537
|
+
const warnings: string[] = [];
|
|
538
|
+
for (const upload of uploads) {
|
|
539
|
+
try {
|
|
540
|
+
const result = await cleanupUpload(
|
|
541
|
+
context.client,
|
|
542
|
+
context.signer,
|
|
543
|
+
upload,
|
|
544
|
+
context.onProgress,
|
|
545
|
+
);
|
|
546
|
+
if (result.status === "failed") {
|
|
547
|
+
const remaining = result.remainingAccountAddresses ?? [
|
|
548
|
+
upload.result.metaAccountAddress,
|
|
549
|
+
upload.result.bufferAccountAddress,
|
|
550
|
+
];
|
|
551
|
+
warnings.push(
|
|
552
|
+
`Failed to clean ${upload.artifact} upload; remaining accounts: ${remaining.join(", ")}: ${result.error}`,
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
} catch (error) {
|
|
556
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
557
|
+
upload.result.cleanup = {
|
|
558
|
+
status: "failed",
|
|
559
|
+
error: message,
|
|
560
|
+
remainingAccountAddresses: [
|
|
561
|
+
upload.result.metaAccountAddress,
|
|
562
|
+
upload.result.bufferAccountAddress,
|
|
563
|
+
],
|
|
564
|
+
};
|
|
565
|
+
warnings.push(
|
|
566
|
+
`Failed to clean ${upload.artifact} upload (${upload.result.metaAccountAddress}, ${upload.result.bufferAccountAddress}): ${message}`,
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return warnings;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
async function withCleanup<T>(
|
|
574
|
+
context: Context,
|
|
575
|
+
uploads: PreparedUpload[],
|
|
576
|
+
work: () => Promise<T>,
|
|
577
|
+
): Promise<{ value: T; warnings: string[] }> {
|
|
578
|
+
try {
|
|
579
|
+
const value = await work();
|
|
580
|
+
const warnings = await cleanupAll(context, uploads);
|
|
581
|
+
return { value, warnings };
|
|
582
|
+
} catch (error) {
|
|
583
|
+
await cleanupAll(context, uploads);
|
|
584
|
+
const deployError = asDeployError(error, "RPC_ERROR", "Deployment failed", {
|
|
585
|
+
operation: context.operation,
|
|
586
|
+
addresses: addressDetails(context),
|
|
587
|
+
});
|
|
588
|
+
deployError.cleanup = uploads.map((upload) => upload.result.cleanup);
|
|
589
|
+
deployError.addresses ??= addressDetails(context);
|
|
590
|
+
throw deployError;
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
async function multicall(
|
|
595
|
+
context: InstructionContext,
|
|
596
|
+
calls: Array<{ program: string; instructionData: InstructionData }>,
|
|
597
|
+
): Promise<Uint8Array> {
|
|
598
|
+
return buildMulticallInstruction(
|
|
599
|
+
await Promise.all(
|
|
600
|
+
calls.map(async (call) => ({
|
|
601
|
+
programIdx: context.getAccountIndex(call.program),
|
|
602
|
+
instructionData: await call.instructionData(context),
|
|
603
|
+
})),
|
|
604
|
+
),
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function commonResult(
|
|
609
|
+
context: Context,
|
|
610
|
+
signature: string,
|
|
611
|
+
version: bigint,
|
|
612
|
+
warnings: string[],
|
|
613
|
+
) {
|
|
614
|
+
return {
|
|
615
|
+
seed: context.seed,
|
|
616
|
+
ephemeral: context.ephemeral,
|
|
617
|
+
authorityAddress: context.signer.address,
|
|
618
|
+
programMetaAccountAddress: context.program.programMetaAccountAddress,
|
|
619
|
+
programAccountAddress: context.program.programAccountAddress,
|
|
620
|
+
programVersion: version,
|
|
621
|
+
transactionSignature: signature,
|
|
622
|
+
warnings,
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
export async function deployProgram(
|
|
627
|
+
request: DeployProgramRequest,
|
|
628
|
+
): Promise<DeployProgramResult> {
|
|
629
|
+
const operation: DeploymentOperation = "deployProgram";
|
|
630
|
+
const programBytes = normalizeBytes(request.program, "program");
|
|
631
|
+
const abiBytes =
|
|
632
|
+
request.abi === undefined ? undefined : normalizeBytes(request.abi, "abi");
|
|
633
|
+
validateProgramImage(programBytes);
|
|
634
|
+
const context = await createContext(operation, request);
|
|
635
|
+
if (abiBytes) await validateABI(abiBytes, context.client);
|
|
636
|
+
progress(context, "validation", "succeeded");
|
|
637
|
+
progress(context, "preflight", "started");
|
|
638
|
+
const absent: Array<[string, string]> = [
|
|
639
|
+
["program metadata", context.program.programMetaAccountAddress],
|
|
640
|
+
["program", context.program.programAccountAddress],
|
|
641
|
+
];
|
|
642
|
+
if (abiBytes) {
|
|
643
|
+
absent.push(
|
|
644
|
+
["ABI metadata", context.abi.abiMetaAccountAddress],
|
|
645
|
+
["ABI", context.abi.abiAccountAddress],
|
|
646
|
+
);
|
|
647
|
+
}
|
|
648
|
+
await assertAbsent(context, absent);
|
|
649
|
+
progress(context, "preflight", "succeeded");
|
|
650
|
+
|
|
651
|
+
const programUpload = await prepareUpload(
|
|
652
|
+
operation,
|
|
653
|
+
"program",
|
|
654
|
+
await temporarySeed(request.seed, "temporary"),
|
|
655
|
+
programBytes,
|
|
656
|
+
);
|
|
657
|
+
const abiUpload = abiBytes
|
|
658
|
+
? await prepareUpload(
|
|
659
|
+
operation,
|
|
660
|
+
"abi",
|
|
661
|
+
await temporarySeed(request.seed, "abi_temp"),
|
|
662
|
+
abiBytes,
|
|
663
|
+
)
|
|
664
|
+
: undefined;
|
|
665
|
+
const uploads = abiUpload ? [programUpload, abiUpload] : [programUpload];
|
|
666
|
+
|
|
667
|
+
const { value, warnings } = await withCleanup(context, uploads, async () => {
|
|
668
|
+
await uploadArtifact(
|
|
669
|
+
context.client,
|
|
670
|
+
context.signer,
|
|
671
|
+
programUpload,
|
|
672
|
+
context.chunkSize,
|
|
673
|
+
context.onProgress,
|
|
674
|
+
);
|
|
675
|
+
if (abiUpload) {
|
|
676
|
+
await uploadArtifact(
|
|
677
|
+
context.client,
|
|
678
|
+
context.signer,
|
|
679
|
+
abiUpload,
|
|
680
|
+
context.chunkSize,
|
|
681
|
+
context.onProgress,
|
|
682
|
+
);
|
|
683
|
+
}
|
|
684
|
+
const [programMetaProof, programProof] = await Promise.all([
|
|
685
|
+
creationProof(
|
|
686
|
+
context.client,
|
|
687
|
+
context.program.programMetaAccountAddress,
|
|
688
|
+
context.ephemeral,
|
|
689
|
+
),
|
|
690
|
+
creationProof(
|
|
691
|
+
context.client,
|
|
692
|
+
context.program.programAccountAddress,
|
|
693
|
+
context.ephemeral,
|
|
694
|
+
),
|
|
695
|
+
]);
|
|
696
|
+
|
|
697
|
+
let transaction: DeploymentTransaction;
|
|
698
|
+
if (!abiBytes || !abiUpload) {
|
|
699
|
+
transaction = {
|
|
700
|
+
program: MANAGER_PROGRAM_ADDRESS,
|
|
701
|
+
readWrite: [
|
|
702
|
+
context.program.programMetaAccountAddress,
|
|
703
|
+
context.program.programAccountAddress,
|
|
704
|
+
],
|
|
705
|
+
readOnly: [programUpload.result.bufferAccountAddress],
|
|
706
|
+
instructionData: createProgramInstruction(
|
|
707
|
+
context,
|
|
708
|
+
programUpload,
|
|
709
|
+
programBytes.length,
|
|
710
|
+
programMetaProof,
|
|
711
|
+
programProof,
|
|
712
|
+
),
|
|
713
|
+
};
|
|
714
|
+
} else {
|
|
715
|
+
const [abiMetaProof, abiProof] = await Promise.all([
|
|
716
|
+
creationProof(
|
|
717
|
+
context.client,
|
|
718
|
+
context.abi.abiMetaAccountAddress,
|
|
719
|
+
context.ephemeral,
|
|
720
|
+
),
|
|
721
|
+
creationProof(
|
|
722
|
+
context.client,
|
|
723
|
+
context.abi.abiAccountAddress,
|
|
724
|
+
context.ephemeral,
|
|
725
|
+
),
|
|
726
|
+
]);
|
|
727
|
+
transaction = {
|
|
728
|
+
program: MULTICALL_PROGRAM_ADDRESS,
|
|
729
|
+
stateUnits: 10_000,
|
|
730
|
+
memoryUnits: 10_000,
|
|
731
|
+
readWrite: [
|
|
732
|
+
context.program.programMetaAccountAddress,
|
|
733
|
+
context.program.programAccountAddress,
|
|
734
|
+
context.abi.abiMetaAccountAddress,
|
|
735
|
+
context.abi.abiAccountAddress,
|
|
736
|
+
],
|
|
737
|
+
readOnly: [
|
|
738
|
+
MANAGER_PROGRAM_ADDRESS,
|
|
739
|
+
ABI_MANAGER_PROGRAM_ADDRESS,
|
|
740
|
+
programUpload.result.bufferAccountAddress,
|
|
741
|
+
abiUpload.result.bufferAccountAddress,
|
|
742
|
+
],
|
|
743
|
+
instructionData: (tx) =>
|
|
744
|
+
multicall(tx, [
|
|
745
|
+
{
|
|
746
|
+
program: MANAGER_PROGRAM_ADDRESS,
|
|
747
|
+
instructionData: createProgramInstruction(
|
|
748
|
+
context,
|
|
749
|
+
programUpload,
|
|
750
|
+
programBytes.length,
|
|
751
|
+
programMetaProof,
|
|
752
|
+
programProof,
|
|
753
|
+
),
|
|
754
|
+
},
|
|
755
|
+
{
|
|
756
|
+
program: ABI_MANAGER_PROGRAM_ADDRESS,
|
|
757
|
+
instructionData: createABIMetaInstruction(context, abiMetaProof),
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
program: ABI_MANAGER_PROGRAM_ADDRESS,
|
|
761
|
+
instructionData: createABIInstruction(
|
|
762
|
+
context,
|
|
763
|
+
abiUpload,
|
|
764
|
+
abiBytes.length,
|
|
765
|
+
abiProof,
|
|
766
|
+
),
|
|
767
|
+
},
|
|
768
|
+
]),
|
|
769
|
+
};
|
|
770
|
+
}
|
|
771
|
+
return commitAndVerify(context, transaction, async () => ({
|
|
772
|
+
version: await verifyProgram(context, programBytes, 0n),
|
|
773
|
+
revision: abiBytes ? await verifyABI(context, abiBytes, 0n) : undefined,
|
|
774
|
+
}));
|
|
775
|
+
});
|
|
776
|
+
|
|
777
|
+
return {
|
|
778
|
+
...commonResult(context, value.signature, value.verified.version, warnings),
|
|
779
|
+
programSize: programBytes.length,
|
|
780
|
+
abiSize: abiBytes?.length,
|
|
781
|
+
abiMetaAccountAddress: abiBytes
|
|
782
|
+
? context.abi.abiMetaAccountAddress
|
|
783
|
+
: undefined,
|
|
784
|
+
abiAccountAddress: abiBytes ? context.abi.abiAccountAddress : undefined,
|
|
785
|
+
abiRevision: value.verified.revision,
|
|
786
|
+
programUpload: programUpload.result,
|
|
787
|
+
abiUpload: abiUpload?.result,
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
export async function deployProgramABI(
|
|
792
|
+
request: DeployProgramABIRequest,
|
|
793
|
+
): Promise<DeployProgramABIResult> {
|
|
794
|
+
const operation: DeploymentOperation = "deployProgramABI";
|
|
795
|
+
const abiBytes = normalizeBytes(request.abi, "abi");
|
|
796
|
+
const context = await createContext(
|
|
797
|
+
operation,
|
|
798
|
+
request,
|
|
799
|
+
request.programAddress,
|
|
800
|
+
);
|
|
801
|
+
await validateABI(abiBytes, context.client);
|
|
802
|
+
progress(context, "validation", "succeeded");
|
|
803
|
+
progress(context, "preflight", "started");
|
|
804
|
+
const programState = await requireProgram(context);
|
|
805
|
+
await assertAbsent(context, [
|
|
806
|
+
["ABI metadata", context.abi.abiMetaAccountAddress],
|
|
807
|
+
["ABI", context.abi.abiAccountAddress],
|
|
808
|
+
]);
|
|
809
|
+
progress(context, "preflight", "succeeded");
|
|
810
|
+
|
|
811
|
+
const abiUpload = await prepareUpload(
|
|
812
|
+
operation,
|
|
813
|
+
"abi",
|
|
814
|
+
await temporarySeed(request.seed, "abi_temp"),
|
|
815
|
+
abiBytes,
|
|
816
|
+
);
|
|
817
|
+
const { value, warnings } = await withCleanup(
|
|
818
|
+
context,
|
|
819
|
+
[abiUpload],
|
|
820
|
+
async () => {
|
|
821
|
+
await uploadArtifact(
|
|
822
|
+
context.client,
|
|
823
|
+
context.signer,
|
|
824
|
+
abiUpload,
|
|
825
|
+
context.chunkSize,
|
|
826
|
+
context.onProgress,
|
|
827
|
+
);
|
|
828
|
+
const [metaProof, abiProof] = await Promise.all([
|
|
829
|
+
creationProof(
|
|
830
|
+
context.client,
|
|
831
|
+
context.abi.abiMetaAccountAddress,
|
|
832
|
+
context.ephemeral,
|
|
833
|
+
),
|
|
834
|
+
creationProof(
|
|
835
|
+
context.client,
|
|
836
|
+
context.abi.abiAccountAddress,
|
|
837
|
+
context.ephemeral,
|
|
838
|
+
),
|
|
839
|
+
]);
|
|
840
|
+
const transaction: DeploymentTransaction = {
|
|
841
|
+
program: MULTICALL_PROGRAM_ADDRESS,
|
|
842
|
+
stateUnits: 10_000,
|
|
843
|
+
memoryUnits: 10_000,
|
|
844
|
+
readWrite: [
|
|
845
|
+
context.abi.abiMetaAccountAddress,
|
|
846
|
+
context.abi.abiAccountAddress,
|
|
847
|
+
],
|
|
848
|
+
readOnly: [
|
|
849
|
+
ABI_MANAGER_PROGRAM_ADDRESS,
|
|
850
|
+
context.program.programMetaAccountAddress,
|
|
851
|
+
abiUpload.result.bufferAccountAddress,
|
|
852
|
+
],
|
|
853
|
+
instructionData: (tx) =>
|
|
854
|
+
multicall(tx, [
|
|
855
|
+
{
|
|
856
|
+
program: ABI_MANAGER_PROGRAM_ADDRESS,
|
|
857
|
+
instructionData: createABIMetaInstruction(context, metaProof),
|
|
858
|
+
},
|
|
859
|
+
{
|
|
860
|
+
program: ABI_MANAGER_PROGRAM_ADDRESS,
|
|
861
|
+
instructionData: createABIInstruction(
|
|
862
|
+
context,
|
|
863
|
+
abiUpload,
|
|
864
|
+
abiBytes.length,
|
|
865
|
+
abiProof,
|
|
866
|
+
),
|
|
867
|
+
},
|
|
868
|
+
]),
|
|
869
|
+
};
|
|
870
|
+
return commitAndVerify(context, transaction, async () => ({
|
|
871
|
+
version: await verifyProgram(
|
|
872
|
+
context,
|
|
873
|
+
undefined,
|
|
874
|
+
programState.meta.version,
|
|
875
|
+
),
|
|
876
|
+
revision: await verifyABI(context, abiBytes, 0n),
|
|
877
|
+
}));
|
|
878
|
+
},
|
|
879
|
+
);
|
|
880
|
+
|
|
881
|
+
return {
|
|
882
|
+
...commonResult(context, value.signature, value.verified.version, warnings),
|
|
883
|
+
abiMetaAccountAddress: context.abi.abiMetaAccountAddress,
|
|
884
|
+
abiAccountAddress: context.abi.abiAccountAddress,
|
|
885
|
+
abiRevision: value.verified.revision,
|
|
886
|
+
abiSize: abiBytes.length,
|
|
887
|
+
abiUpload: abiUpload.result,
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
export async function upgradeProgram(
|
|
892
|
+
request: UpgradeProgramRequest,
|
|
893
|
+
): Promise<UpgradeProgramResult> {
|
|
894
|
+
const operation: DeploymentOperation = "upgradeProgram";
|
|
895
|
+
const programBytes = normalizeBytes(request.program, "program");
|
|
896
|
+
const abiBytes =
|
|
897
|
+
request.abi === undefined ? undefined : normalizeBytes(request.abi, "abi");
|
|
898
|
+
validateProgramImage(programBytes);
|
|
899
|
+
const context = await createContext(
|
|
900
|
+
operation,
|
|
901
|
+
request,
|
|
902
|
+
request.programAddress,
|
|
903
|
+
);
|
|
904
|
+
if (abiBytes) await validateABI(abiBytes, context.client);
|
|
905
|
+
progress(context, "validation", "succeeded");
|
|
906
|
+
progress(context, "preflight", "started");
|
|
907
|
+
const programState = await requireProgram(context);
|
|
908
|
+
const abiState = abiBytes ? await requireABI(context) : undefined;
|
|
909
|
+
progress(context, "preflight", "succeeded");
|
|
910
|
+
|
|
911
|
+
const programUpload = await prepareUpload(
|
|
912
|
+
operation,
|
|
913
|
+
"program",
|
|
914
|
+
await temporarySeed(request.seed, "upgrade_temporary"),
|
|
915
|
+
programBytes,
|
|
916
|
+
);
|
|
917
|
+
const abiUpload = abiBytes
|
|
918
|
+
? await prepareUpload(
|
|
919
|
+
operation,
|
|
920
|
+
"abi",
|
|
921
|
+
await temporarySeed(request.seed, "abi_upgrade"),
|
|
922
|
+
abiBytes,
|
|
923
|
+
)
|
|
924
|
+
: undefined;
|
|
925
|
+
const uploads = abiUpload ? [programUpload, abiUpload] : [programUpload];
|
|
926
|
+
const expectedVersion = programState.meta.version + 1n;
|
|
927
|
+
const expectedRevision = abiState ? abiState.abi.revision + 1n : undefined;
|
|
928
|
+
|
|
929
|
+
const { value, warnings } = await withCleanup(context, uploads, async () => {
|
|
930
|
+
await uploadArtifact(
|
|
931
|
+
context.client,
|
|
932
|
+
context.signer,
|
|
933
|
+
programUpload,
|
|
934
|
+
context.chunkSize,
|
|
935
|
+
context.onProgress,
|
|
936
|
+
);
|
|
937
|
+
if (abiUpload) {
|
|
938
|
+
await uploadArtifact(
|
|
939
|
+
context.client,
|
|
940
|
+
context.signer,
|
|
941
|
+
abiUpload,
|
|
942
|
+
context.chunkSize,
|
|
943
|
+
context.onProgress,
|
|
944
|
+
);
|
|
945
|
+
}
|
|
946
|
+
const transaction: DeploymentTransaction =
|
|
947
|
+
abiBytes && abiUpload
|
|
948
|
+
? {
|
|
949
|
+
program: MULTICALL_PROGRAM_ADDRESS,
|
|
950
|
+
stateUnits: 10_000,
|
|
951
|
+
memoryUnits: 10_000,
|
|
952
|
+
readWrite: [
|
|
953
|
+
context.program.programMetaAccountAddress,
|
|
954
|
+
context.program.programAccountAddress,
|
|
955
|
+
context.abi.abiAccountAddress,
|
|
956
|
+
],
|
|
957
|
+
readOnly: [
|
|
958
|
+
MANAGER_PROGRAM_ADDRESS,
|
|
959
|
+
ABI_MANAGER_PROGRAM_ADDRESS,
|
|
960
|
+
context.abi.abiMetaAccountAddress,
|
|
961
|
+
programUpload.result.bufferAccountAddress,
|
|
962
|
+
abiUpload.result.bufferAccountAddress,
|
|
963
|
+
],
|
|
964
|
+
instructionData: (tx) =>
|
|
965
|
+
multicall(tx, [
|
|
966
|
+
{
|
|
967
|
+
program: MANAGER_PROGRAM_ADDRESS,
|
|
968
|
+
instructionData: upgradeProgramInstruction(
|
|
969
|
+
context,
|
|
970
|
+
programUpload,
|
|
971
|
+
programBytes.length,
|
|
972
|
+
),
|
|
973
|
+
},
|
|
974
|
+
{
|
|
975
|
+
program: ABI_MANAGER_PROGRAM_ADDRESS,
|
|
976
|
+
instructionData: upgradeABIInstruction(
|
|
977
|
+
context,
|
|
978
|
+
abiUpload,
|
|
979
|
+
abiBytes.length,
|
|
980
|
+
),
|
|
981
|
+
},
|
|
982
|
+
]),
|
|
983
|
+
}
|
|
984
|
+
: {
|
|
985
|
+
program: MANAGER_PROGRAM_ADDRESS,
|
|
986
|
+
readWrite: [
|
|
987
|
+
context.program.programMetaAccountAddress,
|
|
988
|
+
context.program.programAccountAddress,
|
|
989
|
+
],
|
|
990
|
+
readOnly: [programUpload.result.bufferAccountAddress],
|
|
991
|
+
instructionData: upgradeProgramInstruction(
|
|
992
|
+
context,
|
|
993
|
+
programUpload,
|
|
994
|
+
programBytes.length,
|
|
995
|
+
),
|
|
996
|
+
};
|
|
997
|
+
return commitAndVerify(context, transaction, async () => ({
|
|
998
|
+
version: await verifyProgram(context, programBytes, expectedVersion),
|
|
999
|
+
revision:
|
|
1000
|
+
abiBytes && expectedRevision !== undefined
|
|
1001
|
+
? await verifyABI(context, abiBytes, expectedRevision)
|
|
1002
|
+
: undefined,
|
|
1003
|
+
}));
|
|
1004
|
+
});
|
|
1005
|
+
|
|
1006
|
+
return {
|
|
1007
|
+
...commonResult(context, value.signature, value.verified.version, warnings),
|
|
1008
|
+
programSize: programBytes.length,
|
|
1009
|
+
abiSize: abiBytes?.length,
|
|
1010
|
+
abiMetaAccountAddress: abiBytes
|
|
1011
|
+
? context.abi.abiMetaAccountAddress
|
|
1012
|
+
: undefined,
|
|
1013
|
+
abiAccountAddress: abiBytes ? context.abi.abiAccountAddress : undefined,
|
|
1014
|
+
abiRevision: value.verified.revision,
|
|
1015
|
+
programUpload: programUpload.result,
|
|
1016
|
+
abiUpload: abiUpload?.result,
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
export async function upgradeProgramABI(
|
|
1021
|
+
request: UpgradeProgramABIRequest,
|
|
1022
|
+
): Promise<UpgradeProgramABIResult> {
|
|
1023
|
+
const operation: DeploymentOperation = "upgradeProgramABI";
|
|
1024
|
+
const abiBytes = normalizeBytes(request.abi, "abi");
|
|
1025
|
+
const context = await createContext(
|
|
1026
|
+
operation,
|
|
1027
|
+
request,
|
|
1028
|
+
request.programAddress,
|
|
1029
|
+
);
|
|
1030
|
+
await validateABI(abiBytes, context.client);
|
|
1031
|
+
progress(context, "validation", "succeeded");
|
|
1032
|
+
progress(context, "preflight", "started");
|
|
1033
|
+
const programState = await requireProgram(context);
|
|
1034
|
+
const abiState = await requireABI(context);
|
|
1035
|
+
progress(context, "preflight", "succeeded");
|
|
1036
|
+
|
|
1037
|
+
const abiUpload = await prepareUpload(
|
|
1038
|
+
operation,
|
|
1039
|
+
"abi",
|
|
1040
|
+
await temporarySeed(request.seed, "abi_upgrade"),
|
|
1041
|
+
abiBytes,
|
|
1042
|
+
);
|
|
1043
|
+
const expectedRevision = abiState.abi.revision + 1n;
|
|
1044
|
+
const { value, warnings } = await withCleanup(
|
|
1045
|
+
context,
|
|
1046
|
+
[abiUpload],
|
|
1047
|
+
async () => {
|
|
1048
|
+
await uploadArtifact(
|
|
1049
|
+
context.client,
|
|
1050
|
+
context.signer,
|
|
1051
|
+
abiUpload,
|
|
1052
|
+
context.chunkSize,
|
|
1053
|
+
context.onProgress,
|
|
1054
|
+
);
|
|
1055
|
+
const transaction: DeploymentTransaction = {
|
|
1056
|
+
program: ABI_MANAGER_PROGRAM_ADDRESS,
|
|
1057
|
+
readWrite: [context.abi.abiAccountAddress],
|
|
1058
|
+
readOnly: [
|
|
1059
|
+
context.abi.abiMetaAccountAddress,
|
|
1060
|
+
context.program.programMetaAccountAddress,
|
|
1061
|
+
abiUpload.result.bufferAccountAddress,
|
|
1062
|
+
],
|
|
1063
|
+
instructionData: upgradeABIInstruction(
|
|
1064
|
+
context,
|
|
1065
|
+
abiUpload,
|
|
1066
|
+
abiBytes.length,
|
|
1067
|
+
),
|
|
1068
|
+
};
|
|
1069
|
+
return commitAndVerify(context, transaction, async () => ({
|
|
1070
|
+
version: await verifyProgram(
|
|
1071
|
+
context,
|
|
1072
|
+
undefined,
|
|
1073
|
+
programState.meta.version,
|
|
1074
|
+
),
|
|
1075
|
+
revision: await verifyABI(context, abiBytes, expectedRevision),
|
|
1076
|
+
}));
|
|
1077
|
+
},
|
|
1078
|
+
);
|
|
1079
|
+
|
|
1080
|
+
return {
|
|
1081
|
+
...commonResult(context, value.signature, value.verified.version, warnings),
|
|
1082
|
+
abiMetaAccountAddress: context.abi.abiMetaAccountAddress,
|
|
1083
|
+
abiAccountAddress: context.abi.abiAccountAddress,
|
|
1084
|
+
abiRevision: value.verified.revision,
|
|
1085
|
+
abiSize: abiBytes.length,
|
|
1086
|
+
abiUpload: abiUpload.result,
|
|
1087
|
+
};
|
|
1088
|
+
}
|