@rocketh/deploy 0.10.9 → 0.10.11

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.
@@ -0,0 +1,325 @@
1
+ import { DeploymentConstruction, Deployment } from 'rocketh';
2
+ import { Chain, ContractFunctionName, ContractFunctionArgs, DecodeFunctionResultReturnType, WriteContractParameters, ReadContractParameters, Address } from 'viem';
3
+
4
+ interface Register {
5
+ }
6
+ type ResolvedRegister = {
7
+ /**
8
+ * TypeScript type to use for `address` values
9
+ * @default `0x${string}`
10
+ */
11
+ addressType: Register extends {
12
+ addressType: infer type;
13
+ } ? type : Register extends {
14
+ AddressType: infer type;
15
+ } ? type : DefaultRegister['addressType'];
16
+ /**
17
+ * TypeScript type to use for `int<M>` and `uint<M>` values, where `M > 48`
18
+ * @default bigint
19
+ */
20
+ bigIntType: Register extends {
21
+ bigIntType: infer type;
22
+ } ? type : Register extends {
23
+ BigIntType: infer type;
24
+ } ? type : DefaultRegister['bigIntType'];
25
+ /**
26
+ * TypeScript type to use for `bytes` values
27
+ * @default { inputs: `0x${string}`; outputs: `0x${string}`; }
28
+ */
29
+ bytesType: Register extends {
30
+ bytesType: infer type extends {
31
+ inputs: unknown;
32
+ outputs: unknown;
33
+ };
34
+ } ? type : Register extends {
35
+ BytesType: infer type extends {
36
+ inputs: unknown;
37
+ outputs: unknown;
38
+ };
39
+ } ? type : DefaultRegister['bytesType'];
40
+ /**
41
+ * TypeScript type to use for `int<M>` and `uint<M>` values, where `M <= 48`
42
+ * @default number
43
+ */
44
+ intType: Register extends {
45
+ intType: infer type;
46
+ } ? type : Register extends {
47
+ IntType: infer type;
48
+ } ? type : DefaultRegister['intType'];
49
+ /**
50
+ * Maximum depth for nested array types (e.g. string[][])
51
+ *
52
+ * Note: You probably only want to set this to a specific number if parsed types are returning as `unknown`
53
+ * and you want to figure out why. If you set this, you should probably also reduce `FixedArrayMaxLength`.
54
+ *
55
+ * @default false
56
+ */
57
+ arrayMaxDepth: Register extends {
58
+ arrayMaxDepth: infer type extends number | false;
59
+ } ? type : Register extends {
60
+ ArrayMaxDepth: infer type extends number | false;
61
+ } ? type : DefaultRegister['arrayMaxDepth'];
62
+ /**
63
+ * Lower bound for fixed array length
64
+ * @default 1
65
+ */
66
+ fixedArrayMinLength: Register extends {
67
+ fixedArrayMinLength: infer type extends number;
68
+ } ? type : Register extends {
69
+ FixedArrayMinLength: infer type extends number;
70
+ } ? type : DefaultRegister['fixedArrayMinLength'];
71
+ /**
72
+ * Upper bound for fixed array length
73
+ * @default 99
74
+ */
75
+ fixedArrayMaxLength: Register extends {
76
+ fixedArrayMaxLength: infer type extends number;
77
+ } ? type : Register extends {
78
+ FixedArrayMaxLength: infer type extends number;
79
+ } ? type : DefaultRegister['fixedArrayMaxLength'];
80
+ /**
81
+ * When set, validates {@link AbiParameter}'s `type` against {@link AbiType}
82
+ *
83
+ * Note: You probably only want to set this to `true` if parsed types are returning as `unknown`
84
+ * and you want to figure out why.
85
+ *
86
+ * @default false
87
+ */
88
+ strictAbiType: Register extends {
89
+ strictAbiType: infer type extends boolean;
90
+ } ? type : Register extends {
91
+ StrictAbiType: infer type extends boolean;
92
+ } ? type : DefaultRegister['strictAbiType'];
93
+ /** @deprecated Use `addressType` instead */
94
+ AddressType: ResolvedRegister['addressType'];
95
+ /** @deprecated Use `addressType` instead */
96
+ BigIntType: ResolvedRegister['bigIntType'];
97
+ /** @deprecated Use `bytesType` instead */
98
+ BytesType: ResolvedRegister['bytesType'];
99
+ /** @deprecated Use `intType` instead */
100
+ IntType: ResolvedRegister['intType'];
101
+ /** @deprecated Use `arrayMaxDepth` instead */
102
+ ArrayMaxDepth: ResolvedRegister['arrayMaxDepth'];
103
+ /** @deprecated Use `fixedArrayMinLength` instead */
104
+ FixedArrayMinLength: ResolvedRegister['fixedArrayMinLength'];
105
+ /** @deprecated Use `fixedArrayMaxLength` instead */
106
+ FixedArrayMaxLength: ResolvedRegister['fixedArrayMaxLength'];
107
+ /** @deprecated Use `strictAbiType` instead */
108
+ StrictAbiType: ResolvedRegister['strictAbiType'];
109
+ };
110
+ type DefaultRegister = {
111
+ /** Maximum depth for nested array types (e.g. string[][]) */
112
+ arrayMaxDepth: false;
113
+ /** Lower bound for fixed array length */
114
+ fixedArrayMinLength: 1;
115
+ /** Upper bound for fixed array length */
116
+ fixedArrayMaxLength: 99;
117
+ /** TypeScript type to use for `address` values */
118
+ addressType: `0x${string}`;
119
+ /** TypeScript type to use for `bytes` values */
120
+ bytesType: {
121
+ /** TypeScript type to use for `bytes` input values */
122
+ inputs: `0x${string}`;
123
+ /** TypeScript type to use for `bytes` output values */
124
+ outputs: `0x${string}`;
125
+ };
126
+ /** TypeScript type to use for `int<M>` and `uint<M>` values, where `M > 48` */
127
+ bigIntType: bigint;
128
+ /** TypeScript type to use for `int<M>` and `uint<M>` values, where `M <= 48` */
129
+ intType: number;
130
+ /** When set, validates {@link AbiParameter}'s `type` against {@link AbiType} */
131
+ strictAbiType: false;
132
+ /** @deprecated Use `arrayMaxDepth` instead */
133
+ ArrayMaxDepth: DefaultRegister['arrayMaxDepth'];
134
+ /** @deprecated Use `fixedArrayMinLength` instead */
135
+ FixedArrayMinLength: DefaultRegister['fixedArrayMinLength'];
136
+ /** @deprecated Use `fixedArrayMaxLength` instead */
137
+ FixedArrayMaxLength: DefaultRegister['fixedArrayMaxLength'];
138
+ /** @deprecated Use `addressType` instead */
139
+ AddressType: DefaultRegister['addressType'];
140
+ /** @deprecated Use `bytesType` instead */
141
+ BytesType: {
142
+ inputs: DefaultRegister['bytesType']['inputs'];
143
+ outputs: DefaultRegister['bytesType']['outputs'];
144
+ };
145
+ /** @deprecated Use `bigIntType` instead */
146
+ BigIntType: DefaultRegister['bigIntType'];
147
+ /** @deprecated Use `intType` instead */
148
+ IntType: DefaultRegister['intType'];
149
+ /** @deprecated Use `strictAbiType` instead */
150
+ StrictAbiType: DefaultRegister['strictAbiType'];
151
+ };
152
+
153
+ /**
154
+ * Combines members of an intersection into a readable type.
155
+ *
156
+ * @link https://twitter.com/mattpocockuk/status/1622730173446557697?s=20&t=NdpAcmEFXY01xkqU3KO0Mg
157
+ * @example
158
+ * type Result = Pretty<{ a: string } | { b: string } | { c: number, d: bigint }>
159
+ * // ^? type Result = { a: string; b: string; c: number; d: bigint }
160
+ */
161
+ type Pretty<type> = {
162
+ [key in keyof type]: type[key];
163
+ } & unknown;
164
+ /**
165
+ * Creates range between two positive numbers using [tail recursion](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-5.html#tail-recursion-elimination-on-conditional-types).
166
+ *
167
+ * @param start - Number to start range
168
+ * @param stop - Number to end range
169
+ * @returns Array with inclusive range from {@link start} to {@link stop}
170
+ *
171
+ * @example
172
+ * type Result = Range<1, 3>
173
+ * // ^? type Result = [1, 2, 3]
174
+ */
175
+ type Range<start extends number, stop extends number, result extends number[] = [], padding extends 0[] = [], current extends number = [...padding, ...result]['length'] & number> = current extends stop ? current extends start ? [current] : result extends [] ? [] : [...result, current] : current extends start ? Range<start, stop, [current], padding> : result extends [] ? Range<start, stop, [], [...padding, 0]> : Range<start, stop, [...result, current], padding>;
176
+
177
+ type MBytes = '' | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32;
178
+ type MBits = '' | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 | 88 | 96 | 104 | 112 | 120 | 128 | 136 | 144 | 152 | 160 | 168 | 176 | 184 | 192 | 200 | 208 | 216 | 224 | 232 | 240 | 248 | 256;
179
+ type SolidityAddress = 'address';
180
+ type SolidityBool = 'bool';
181
+ type SolidityBytes = `bytes${MBytes}`;
182
+ type SolidityFunction = 'function';
183
+ type SolidityString = 'string';
184
+ type SolidityTuple = 'tuple';
185
+ type SolidityInt = `${'u' | ''}int${MBits}`;
186
+ type SolidityFixedArrayRange = Range<ResolvedRegister['fixedArrayMinLength'], ResolvedRegister['fixedArrayMaxLength']>[number];
187
+ /**
188
+ * Recursively build arrays up to maximum depth
189
+ * or use a more broad type when maximum depth is switched "off"
190
+ */
191
+ type _BuildArrayTypes<T extends string, Depth extends readonly number[] = []> = ResolvedRegister['arrayMaxDepth'] extends false ? `${T}[${string}]` : Depth['length'] extends ResolvedRegister['arrayMaxDepth'] ? T : T extends `${any}[${SolidityFixedArrayRange | ''}]` ? _BuildArrayTypes<T | `${T}[${SolidityFixedArrayRange | ''}]`, [
192
+ ...Depth,
193
+ 1
194
+ ]> : _BuildArrayTypes<`${T}[${SolidityFixedArrayRange | ''}]`, [...Depth, 1]>;
195
+ type SolidityArrayWithoutTuple = _BuildArrayTypes<SolidityAddress | SolidityBool | SolidityBytes | SolidityFunction | SolidityInt | SolidityString>;
196
+ type SolidityArrayWithTuple = _BuildArrayTypes<SolidityTuple>;
197
+ type SolidityArray = SolidityArrayWithoutTuple | SolidityArrayWithTuple;
198
+ type AbiType = SolidityArray | SolidityAddress | SolidityBool | SolidityBytes | SolidityFunction | SolidityInt | SolidityString | SolidityTuple;
199
+ type ResolvedAbiType = ResolvedRegister['strictAbiType'] extends true ? AbiType : string;
200
+ type AbiInternalType = ResolvedAbiType | `address ${string}` | `contract ${string}` | `enum ${string}` | `struct ${string}`;
201
+ type AbiParameter = Pretty<{
202
+ type: ResolvedAbiType;
203
+ name?: string | undefined;
204
+ /** Representation used by Solidity compiler */
205
+ internalType?: AbiInternalType | undefined;
206
+ } & ({
207
+ type: Exclude<ResolvedAbiType, SolidityTuple | SolidityArrayWithTuple>;
208
+ } | {
209
+ type: SolidityTuple | SolidityArrayWithTuple;
210
+ components: readonly AbiParameter[];
211
+ })>;
212
+ type AbiEventParameter = AbiParameter & {
213
+ indexed?: boolean | undefined;
214
+ };
215
+ /**
216
+ * State mutability for {@link AbiFunction}
217
+ *
218
+ * @see https://docs.soliditylang.org/en/latest/contracts.html#state-mutability
219
+ */
220
+ type AbiStateMutability = 'pure' | 'view' | 'nonpayable' | 'payable';
221
+ /** ABI ["function"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */
222
+ type AbiFunction = {
223
+ type: 'function';
224
+ /**
225
+ * @deprecated use `pure` or `view` from {@link AbiStateMutability} instead
226
+ * @see https://github.com/ethereum/solidity/issues/992
227
+ */
228
+ constant?: boolean | undefined;
229
+ /**
230
+ * @deprecated Vyper used to provide gas estimates
231
+ * @see https://github.com/vyperlang/vyper/issues/2151
232
+ */
233
+ gas?: number | undefined;
234
+ inputs: readonly AbiParameter[];
235
+ name: string;
236
+ outputs: readonly AbiParameter[];
237
+ /**
238
+ * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead
239
+ * @see https://github.com/ethereum/solidity/issues/992
240
+ */
241
+ payable?: boolean | undefined;
242
+ stateMutability: AbiStateMutability;
243
+ };
244
+ /** ABI ["constructor"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */
245
+ type AbiConstructor = {
246
+ type: 'constructor';
247
+ inputs: readonly AbiParameter[];
248
+ /**
249
+ * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead
250
+ * @see https://github.com/ethereum/solidity/issues/992
251
+ */
252
+ payable?: boolean | undefined;
253
+ stateMutability: Extract<AbiStateMutability, 'payable' | 'nonpayable'>;
254
+ };
255
+ /** ABI ["fallback"](https://docs.soliditylang.org/en/latest/abi-spec.html#json) type */
256
+ type AbiFallback = {
257
+ type: 'fallback';
258
+ inputs?: readonly [] | undefined;
259
+ /**
260
+ * @deprecated use `payable` or `nonpayable` from {@link AbiStateMutability} instead
261
+ * @see https://github.com/ethereum/solidity/issues/992
262
+ */
263
+ payable?: boolean | undefined;
264
+ stateMutability: Extract<AbiStateMutability, 'payable' | 'nonpayable'>;
265
+ };
266
+ /** ABI ["receive"](https://docs.soliditylang.org/en/latest/contracts.html#receive-ether-function) type */
267
+ type AbiReceive = {
268
+ type: 'receive';
269
+ stateMutability: Extract<AbiStateMutability, 'payable'>;
270
+ };
271
+ /** ABI ["event"](https://docs.soliditylang.org/en/latest/abi-spec.html#events) type */
272
+ type AbiEvent = {
273
+ type: 'event';
274
+ anonymous?: boolean | undefined;
275
+ inputs: readonly AbiEventParameter[];
276
+ name: string;
277
+ };
278
+ /** ABI ["error"](https://docs.soliditylang.org/en/latest/abi-spec.html#errors) type */
279
+ type AbiError = {
280
+ type: 'error';
281
+ inputs: readonly AbiParameter[];
282
+ name: string;
283
+ };
284
+ /**
285
+ * Contract [ABI Specification](https://docs.soliditylang.org/en/latest/abi-spec.html#json)
286
+ */
287
+ type Abi = readonly (AbiConstructor | AbiError | AbiEvent | AbiFallback | AbiFunction | AbiReceive)[];
288
+
289
+ type EIP1193DATA = `0x${string}`;
290
+
291
+ declare module 'rocketh' {
292
+ interface Environment {
293
+ deploy: DeployFunction;
294
+ execute: ExecuteFunction;
295
+ read: ReadFunction;
296
+ executeByName: ExecuteFunctionByName;
297
+ readByName: ReadFunctionByName;
298
+ }
299
+ }
300
+ type DeployFunction = <TAbi extends Abi, TChain extends Chain = Chain>(name: string, args: DeploymentConstruction<TAbi>, options?: DeployOptions) => Promise<Deployment<TAbi> & {
301
+ updated: boolean;
302
+ }>;
303
+ type ExecuteFunction = <TAbi extends Abi, TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>, TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>>(deployment: Deployment<TAbi>, args: ExecutionArgs<TAbi, TFunctionName, TArgs>) => Promise<EIP1193DATA>;
304
+ type ExecuteFunctionByName = <TAbi extends Abi, TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>, TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>>(name: string, args: ExecutionArgs<TAbi, TFunctionName, TArgs>) => Promise<EIP1193DATA>;
305
+ type ReadFunction = <TAbi extends Abi, TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>, TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName>>(deployment: Deployment<TAbi>, args: ReadingArgs<TAbi, TFunctionName, TArgs>) => Promise<DecodeFunctionResultReturnType<TAbi, TFunctionName>>;
306
+ type ReadFunctionByName = <TAbi extends Abi, TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>, TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName>>(name: string, args: ReadingArgs<TAbi, TFunctionName, TArgs>) => Promise<DecodeFunctionResultReturnType<TAbi, TFunctionName>>;
307
+ type ExecutionArgs<TAbi extends Abi, TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>, TArgs extends ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>> = Omit<WriteContractParameters<TAbi, TFunctionName, TArgs>, 'address' | 'abi' | 'account' | 'nonce' | 'chain'> & {
308
+ account: string;
309
+ };
310
+ type ReadingArgs<TAbi extends Abi, TFunctionName extends ContractFunctionName<TAbi, 'pure' | 'view'>, TArgs extends ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName> = ContractFunctionArgs<TAbi, 'pure' | 'view', TFunctionName>> = Omit<ReadContractParameters<TAbi, TFunctionName, TArgs>, 'address' | 'abi' | 'account' | 'nonce'> & {
311
+ account?: string;
312
+ };
313
+ type DeployOptions = {
314
+ linkedData?: any;
315
+ deterministic?: boolean | `0x${string}`;
316
+ libraries?: {
317
+ [name: string]: Address;
318
+ };
319
+ } & ({
320
+ skipIfAlreadyDeployed?: boolean;
321
+ } | {
322
+ alwaysOverride?: boolean;
323
+ });
324
+
325
+ export type { DeployFunction, DeployOptions, ExecuteFunction, ExecuteFunctionByName, ExecutionArgs, ReadFunction, ReadFunctionByName, ReadingArgs };
package/dist/index.mjs ADDED
@@ -0,0 +1,349 @@
1
+ import { extendEnvironment } from 'rocketh';
2
+ import { encodeFunctionData, decodeFunctionResult, encodeDeployData, keccak256, encodePacked } from 'viem';
3
+ import { logs } from 'named-logs';
4
+
5
+ const logger = logs("rocketh-deploy");
6
+ async function broadcastTransaction(env, signer, params) {
7
+ if (signer.type === "wallet" || signer.type === "remote") {
8
+ return signer.signer.request({
9
+ method: "eth_sendTransaction",
10
+ params
11
+ });
12
+ } else {
13
+ const rawTx = await signer.signer.request({
14
+ method: "eth_signTransaction",
15
+ params
16
+ });
17
+ return env.network.provider.request({
18
+ method: "eth_sendRawTransaction",
19
+ params: [rawTx]
20
+ });
21
+ }
22
+ }
23
+ function linkRawLibrary(bytecode, libraryName, libraryAddress) {
24
+ const address = libraryAddress.replace("0x", "");
25
+ let encodedLibraryName;
26
+ if (libraryName.startsWith("$") && libraryName.endsWith("$")) {
27
+ encodedLibraryName = libraryName.slice(1, libraryName.length - 1);
28
+ } else {
29
+ encodedLibraryName = keccak256(encodePacked(["string"], [libraryName])).slice(2, 36);
30
+ }
31
+ const pattern = new RegExp(`_+\\$${encodedLibraryName}\\$_+`, "g");
32
+ if (!pattern.exec(bytecode)) {
33
+ throw new Error(`Can't link '${libraryName}' (${encodedLibraryName}) in
34
+ ----
35
+ ${bytecode}
36
+ ----
37
+ `);
38
+ }
39
+ return bytecode.replace(pattern, address);
40
+ }
41
+ function linkRawLibraries(bytecode, libraries) {
42
+ for (const libName of Object.keys(libraries)) {
43
+ const libAddress = libraries[libName];
44
+ bytecode = linkRawLibrary(bytecode, libName, libAddress);
45
+ }
46
+ return bytecode;
47
+ }
48
+ function linkLibraries(artifact, libraries) {
49
+ let bytecode = artifact.bytecode;
50
+ if (libraries) {
51
+ if (artifact.linkReferences) {
52
+ for (const [fileName, fileReferences] of Object.entries(artifact.linkReferences)) {
53
+ for (const [libName, fixups] of Object.entries(fileReferences)) {
54
+ const addr = libraries[libName];
55
+ if (addr === void 0) {
56
+ continue;
57
+ }
58
+ for (const fixup of fixups) {
59
+ bytecode = bytecode.substring(0, 2 + fixup.start * 2) + addr.substring(2) + bytecode.substring(2 + (fixup.start + fixup.length) * 2);
60
+ }
61
+ }
62
+ }
63
+ } else {
64
+ bytecode = linkRawLibraries(bytecode, libraries);
65
+ }
66
+ }
67
+ return bytecode;
68
+ }
69
+ extendEnvironment((env) => {
70
+ async function execute(deployment, args) {
71
+ const { account, ...viemArgs } = args;
72
+ let address;
73
+ if (account.startsWith("0x")) {
74
+ address = account;
75
+ } else {
76
+ if (env.namedAccounts) {
77
+ address = env.namedAccounts[account];
78
+ if (!address) {
79
+ throw new Error(`no address for ${account}`);
80
+ }
81
+ } else {
82
+ throw new Error(`no accounts setup, cannot get address for ${account}`);
83
+ }
84
+ }
85
+ const artifactToUse = deployment;
86
+ const abi = artifactToUse.abi;
87
+ const calldata = encodeFunctionData({
88
+ abi,
89
+ functionName: viemArgs.functionName,
90
+ args: viemArgs.args
91
+ });
92
+ const signer = env.addressSigners[address];
93
+ const txParam = {
94
+ to: deployment.address,
95
+ type: "0x2",
96
+ from: address,
97
+ chainId: `0x${env.network.chain.id.toString(16)}`,
98
+ data: calldata,
99
+ gas: viemArgs.gas && `0x${viemArgs.gas.toString(16)}`,
100
+ // gasPrice: viemArgs.gasPrice && `0x${viemArgs.gasPrice.toString(16)}` as `0x${string}`,
101
+ maxFeePerGas: viemArgs.maxFeePerGas && `0x${viemArgs.maxFeePerGas.toString(16)}`,
102
+ maxPriorityFeePerGas: viemArgs.maxPriorityFeePerGas && `0x${viemArgs.maxPriorityFeePerGas.toString(16)}`
103
+ // nonce: viemArgs.nonce && (`0x${viemArgs.nonce.toString(16)}` as `0x${string}`),
104
+ };
105
+ if (viemArgs.value) {
106
+ txParam.value = `0x${viemArgs.value?.toString(16)}`;
107
+ }
108
+ let txHash;
109
+ if (signer.type === "wallet" || signer.type === "remote") {
110
+ txHash = await signer.signer.request({
111
+ method: "eth_sendTransaction",
112
+ params: [txParam]
113
+ });
114
+ } else {
115
+ const rawTx = await signer.signer.request({
116
+ method: "eth_signTransaction",
117
+ params: [txParam]
118
+ });
119
+ txHash = await env.network.provider.request({
120
+ method: "eth_sendRawTransaction",
121
+ params: [rawTx]
122
+ });
123
+ }
124
+ const pendingExecution = {
125
+ type: "execution",
126
+ transaction: { hash: txHash, origin: address }
127
+ // description, // TODO
128
+ // TODO we should have the nonce, except for wallet like metamask where it is not usre you get the nonce you start with
129
+ };
130
+ await env.savePendingExecution(pendingExecution);
131
+ return txHash;
132
+ }
133
+ async function executeByName(name, args) {
134
+ const deployment = env.getOrNull(name);
135
+ if (!deployment) {
136
+ throw new Error(`no deployment named ${name}`);
137
+ }
138
+ return execute(deployment, args);
139
+ }
140
+ async function read(deployment, args) {
141
+ const { account, ...viemArgs } = args;
142
+ let address;
143
+ if (account) {
144
+ if (account.startsWith("0x")) {
145
+ address = account;
146
+ } else {
147
+ if (env.namedAccounts) {
148
+ address = env.namedAccounts[account];
149
+ if (!address) {
150
+ throw new Error(`no address for ${account}`);
151
+ }
152
+ } else {
153
+ throw new Error(`no accounts setup, cannot get address for ${account}`);
154
+ }
155
+ }
156
+ }
157
+ const artifactToUse = deployment;
158
+ const abi = artifactToUse.abi;
159
+ const calldata = encodeFunctionData({
160
+ abi,
161
+ functionName: viemArgs.functionName,
162
+ args: viemArgs.args
163
+ });
164
+ const result = await env.network.provider.request({
165
+ method: "eth_call",
166
+ params: [
167
+ {
168
+ to: deployment.address,
169
+ type: "0x2",
170
+ from: address,
171
+ chainId: `0x${env.network.chain.id.toString(16)}`,
172
+ data: calldata
173
+ // value: `0x${viemArgs.value?.toString(16)}` as `0x${string}`,
174
+ }
175
+ ]
176
+ });
177
+ const parsed = decodeFunctionResult({
178
+ abi,
179
+ functionName: viemArgs.functionName,
180
+ data: result,
181
+ args: viemArgs.args
182
+ });
183
+ return parsed;
184
+ }
185
+ async function readByName(name, args) {
186
+ const deployment = env.getOrNull(name);
187
+ if (!deployment) {
188
+ throw new Error(`no deployment named ${name}`);
189
+ }
190
+ return read(deployment, args);
191
+ }
192
+ async function deploy(name, args, options) {
193
+ const skipIfAlreadyDeployed = options && "skipIfAlreadyDeployed" in options && options.skipIfAlreadyDeployed;
194
+ const allwaysOverride = options && "allwaysOverride" in options && options.allwaysOverride;
195
+ if (allwaysOverride && skipIfAlreadyDeployed) {
196
+ throw new Error(`conflicting options: "allwaysOverride" and "skipIfAlreadyDeployed"`);
197
+ }
198
+ const existingDeployment = env.getOrNull(name);
199
+ if (existingDeployment && skipIfAlreadyDeployed) {
200
+ logger.info(`deployment for ${name} at ${existingDeployment.address}, skipIfAlreadyDeployed: true => we skip`);
201
+ return { ...existingDeployment, updated: false };
202
+ }
203
+ const { account, artifact, ...viemArgs } = args;
204
+ let address;
205
+ if (account.startsWith("0x")) {
206
+ address = account;
207
+ } else {
208
+ if (env.namedAccounts) {
209
+ address = env.namedAccounts[account];
210
+ if (!address) {
211
+ throw new Error(`no address for ${account}`);
212
+ }
213
+ } else {
214
+ throw new Error(`no accounts setup, cannot get address for ${account}`);
215
+ }
216
+ }
217
+ const artifactToUse = typeof artifact === "string" ? env.artifacts[artifact] : artifact;
218
+ const bytecode = linkLibraries(artifactToUse, options?.libraries);
219
+ const abi = artifactToUse.abi;
220
+ const argsToUse = {
221
+ ...viemArgs,
222
+ account,
223
+ abi,
224
+ bytecode
225
+ };
226
+ const calldata = encodeDeployData(argsToUse);
227
+ const argsData = `0x${calldata.replace(bytecode, "")}`;
228
+ if (existingDeployment) {
229
+ logger.info(`existing deployment for ${name} at ${existingDeployment.address}`);
230
+ }
231
+ if (existingDeployment && !allwaysOverride) {
232
+ const previousBytecode = existingDeployment.bytecode;
233
+ const previousArgsData = existingDeployment.argsData;
234
+ const last2Bytes = previousBytecode.slice(-4);
235
+ const cborLength = parseInt(last2Bytes, 16);
236
+ const previousBytecodeWithoutCBOR = previousBytecode.slice(0, -cborLength * 2);
237
+ const newBytecodeWithoutCBOR = bytecode.slice(0, -cborLength * 2);
238
+ if (previousBytecodeWithoutCBOR === newBytecodeWithoutCBOR && previousArgsData === argsData) {
239
+ return { ...existingDeployment, updated: false };
240
+ }
241
+ }
242
+ const partialDeployment = {
243
+ ...artifactToUse,
244
+ argsData,
245
+ linkedData: options?.linkedData
246
+ };
247
+ const signer = env.addressSigners[address];
248
+ const chainId = `0x${env.network.chain.id.toString(16)}`;
249
+ const maxFeePerGas = viemArgs.maxFeePerGas && `0x${viemArgs.maxFeePerGas.toString(16)}`;
250
+ const maxPriorityFeePerGas = viemArgs.maxPriorityFeePerGas && `0x${viemArgs.maxPriorityFeePerGas.toString(16)}`;
251
+ const params = [
252
+ {
253
+ type: "0x2",
254
+ from: address,
255
+ chainId,
256
+ data: calldata,
257
+ gas: viemArgs.gas && `0x${viemArgs.gas.toString(16)}`,
258
+ maxFeePerGas,
259
+ maxPriorityFeePerGas
260
+ // gasPrice: viemArgs.gasPrice && `0x${viemArgs.gasPrice.toString(16)}` as `0x${string}`,
261
+ // value: `0x${viemArgs.value?.toString(16)}` as `0x${string}`,
262
+ // nonce: viemArgs.nonce && (`0x${viemArgs.nonce.toString(16)}` as `0x${string}`),
263
+ }
264
+ ];
265
+ let expectedAddress = void 0;
266
+ if (options?.deterministic) {
267
+ const deterministicFactoryAddress = `0x4e59b44847b379578588920ca78fbf26c0b4956c`;
268
+ const deterministicFactoryDeployerAddress = `0x3fab184622dc19b6109349b94811493bf2a45362`;
269
+ const factoryDeploymentData = `0xf8a58085174876e800830186a08080b853604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf31ba02222222222222222222222222222222222222222222222222222222222222222a02222222222222222222222222222222222222222222222222222222222222222`;
270
+ const code = await env.network.provider.request({
271
+ method: "eth_getCode",
272
+ params: [deterministicFactoryAddress, "latest"]
273
+ });
274
+ if (code === "0x") {
275
+ const balanceHexString = await env.network.provider.request({
276
+ method: "eth_getBalance",
277
+ params: [deterministicFactoryDeployerAddress, "latest"]
278
+ });
279
+ const balance = BigInt(balanceHexString);
280
+ if (balance < 10000000000000000n) {
281
+ const need = 10000000000000000n - balance;
282
+ const balanceToSend = `0x${need.toString(16)}`;
283
+ const txHash3 = await broadcastTransaction(env, signer, [
284
+ {
285
+ type: "0x2",
286
+ chainId,
287
+ from: address,
288
+ to: deterministicFactoryDeployerAddress,
289
+ value: balanceToSend,
290
+ gas: `0x${BigInt(21e3).toString(16)}`,
291
+ maxFeePerGas,
292
+ maxPriorityFeePerGas
293
+ }
294
+ ]);
295
+ await env.savePendingExecution({
296
+ type: "execution",
297
+ // TODO different type ?
298
+ transaction: { hash: txHash3, origin: address }
299
+ });
300
+ }
301
+ const txHash2 = await env.network.provider.request({
302
+ method: "eth_sendRawTransaction",
303
+ params: [factoryDeploymentData]
304
+ });
305
+ await env.savePendingExecution({
306
+ type: "execution",
307
+ // TODO different type ?
308
+ transaction: { hash: txHash2, origin: address }
309
+ });
310
+ }
311
+ const salt = typeof options.deterministic === "string" ? `0x${options.deterministic.slice(2).padStart(64, "0")}` : "0x0000000000000000000000000000000000000000000000000000000000000000";
312
+ const bytecode2 = params[0].data || "0x";
313
+ expectedAddress = "0x" + keccak256(`0xff${deterministicFactoryAddress.slice(2)}${salt.slice(2)}${keccak256(bytecode2).slice(2)}`).slice(
314
+ -40
315
+ );
316
+ const codeAlreadyDeployed = await env.network.provider.request({
317
+ method: "eth_getCode",
318
+ params: [expectedAddress, "latest"]
319
+ });
320
+ if (codeAlreadyDeployed !== "0x") {
321
+ env.showMessage(`contract was already deterministically deployed at ${expectedAddress}`);
322
+ const deployment2 = await env.save(name, {
323
+ address: expectedAddress,
324
+ ...partialDeployment
325
+ });
326
+ return { ...deployment2, updated: true };
327
+ }
328
+ params[0].data = salt + (bytecode2.slice(2) || "");
329
+ params[0].to = deterministicFactoryAddress;
330
+ }
331
+ const txHash = await broadcastTransaction(env, signer, params);
332
+ const pendingDeployment = {
333
+ type: "deployment",
334
+ expectedAddress,
335
+ partialDeployment,
336
+ transaction: { hash: txHash, origin: address },
337
+ name
338
+ // TODO we should have the nonce, except for wallet like metamask where it is not usre you get the nonce you start with
339
+ };
340
+ const deployment = await env.savePendingDeployment(pendingDeployment);
341
+ return { ...deployment, updated: true };
342
+ }
343
+ env.deploy = deploy;
344
+ env.execute = execute;
345
+ env.executeByName = executeByName;
346
+ env.read = read;
347
+ env.readByName = readByName;
348
+ return env;
349
+ });