@@ -52,11 +52,12 @@ export type DeployContractResult<TContract extends Contract = Contract> = {
52
52
/**
53
53
* `ContractFactory` provides utilities for deploying and configuring contracts.
54
54
*/
55
- export default class ContractFactory {
55
+ export default class ContractFactory < TContract extends Contract = Contract > {
56
56
bytecode : BytesLike ;
57
57
interface : Interface ;
58
58
provider ! : Provider | null ;
59
59
account ! : Account | null ;
60
+ storageSlots : StorageSlot [ ] ;
60
61
61
62
/**
62
63
* Create a ContractFactory instance.
@@ -68,7 +69,8 @@ export default class ContractFactory {
68
69
constructor (
69
70
bytecode : BytesLike ,
70
71
abi : JsonAbi | Interface ,
71
- accountOrProvider : Account | Provider | null = null
72
+ accountOrProvider : Account | Provider | null = null ,
73
+ storageSlots : StorageSlot [ ] = [ ]
72
74
) {
73
75
// Force the bytecode to be a byte array
74
76
this . bytecode = arrayify ( bytecode ) ;
@@ -99,6 +101,8 @@ export default class ContractFactory {
99
101
this . provider = accountOrProvider ;
100
102
this . account = null ;
101
103
}
104
+
105
+ this . storageSlots = storageSlots ;
102
106
}
103
107
104
108
/**
@@ -118,17 +122,19 @@ export default class ContractFactory {
118
122
* @returns The CreateTransactionRequest object for deploying the contract.
119
123
*/
120
124
createTransactionRequest ( deployOptions ?: DeployContractOptions & { bytecode ?: BytesLike } ) {
121
- const storageSlots = deployOptions ?. storageSlots
122
- ?. map ( ( { key, value } ) => ( {
125
+ const storageSlots = ( deployOptions ?. storageSlots ?? [ ] )
126
+ . concat ( this . storageSlots )
127
+ . map ( ( { key, value } ) => ( {
123
128
key : hexlifyWithPrefix ( key ) ,
124
129
value : hexlifyWithPrefix ( value ) ,
125
130
} ) )
131
+ . filter ( ( el , index , self ) => self . findIndex ( ( s ) => s . key === el . key ) === index )
126
132
. sort ( ( { key : keyA } , { key : keyB } ) => keyA . localeCompare ( keyB ) ) ;
127
133
128
134
const options = {
129
135
salt : randomBytes ( 32 ) ,
130
- ...deployOptions ,
131
- storageSlots : storageSlots || [ ] ,
136
+ ...( deployOptions ?? { } ) ,
137
+ storageSlots,
132
138
} ;
133
139
134
140
if ( ! this . provider ) {
@@ -192,16 +198,16 @@ export default class ContractFactory {
192
198
* @param deployOptions - Options for deploying the contract.
193
199
* @returns A promise that resolves to the deployed contract instance.
194
200
*/
195
- async deploy < TContract extends Contract = Contract > (
201
+ async deploy < T extends Contract = TContract > (
196
202
deployOptions : DeployContractOptions = { }
197
- ) : Promise < DeployContractResult < TContract > > {
203
+ ) : Promise < DeployContractResult < T > > {
198
204
const account = this . getAccount ( ) ;
199
205
const { consensusParameters } = account . provider . getChain ( ) ;
200
206
const maxContractSize = consensusParameters . contractParameters . contractMaxSize . toNumber ( ) ;
201
207
202
208
return this . bytecode . length > maxContractSize
203
209
? this . deployAsBlobTx ( deployOptions )
204
- : this . deployAsCreateTx ( deployOptions ) ;
210
+ : this . deployAsCreateTx < T > ( deployOptions ) ;
205
211
}
206
212
207
213
/**
@@ -210,9 +216,9 @@ export default class ContractFactory {
210
216
* @param deployOptions - Options for deploying the contract.
211
217
* @returns A promise that resolves to the deployed contract instance.
212
218
*/
213
- async deployAsCreateTx < TContract extends Contract = Contract > (
219
+ async deployAsCreateTx < T extends Contract = TContract > (
214
220
deployOptions : DeployContractOptions = { }
215
- ) : Promise < DeployContractResult < TContract > > {
221
+ ) : Promise < DeployContractResult < T > > {
216
222
const account = this . getAccount ( ) ;
217
223
const { consensusParameters } = account . provider . getChain ( ) ;
218
224
const maxContractSize = consensusParameters . contractParameters . contractMaxSize . toNumber ( ) ;
@@ -230,7 +236,7 @@ export default class ContractFactory {
230
236
231
237
const waitForResult = async ( ) => {
232
238
const transactionResult = await transactionResponse . waitForResult < TransactionType . Create > ( ) ;
233
- const contract = new Contract ( contractId , this . interface , account ) as TContract ;
239
+ const contract = new Contract ( contractId , this . interface , account ) as T ;
234
240
235
241
return { contract, transactionResult } ;
236
242
} ;
@@ -248,11 +254,11 @@ export default class ContractFactory {
248
254
* @param deployOptions - Options for deploying the contract.
249
255
* @returns A promise that resolves to the deployed contract instance.
250
256
*/
251
- async deployAsBlobTx < TContract extends Contract = Contract > (
257
+ async deployAsBlobTx < T extends Contract = TContract > (
252
258
deployOptions : DeployContractOptions = {
253
259
chunkSizeMultiplier : CHUNK_SIZE_MULTIPLIER ,
254
260
}
255
- ) : Promise < DeployContractResult < TContract > > {
261
+ ) : Promise < DeployContractResult < T > > {
256
262
const account = this . getAccount ( ) ;
257
263
const { configurableConstants, chunkSizeMultiplier } = deployOptions ;
258
264
if ( configurableConstants ) {
@@ -362,7 +368,7 @@ export default class ContractFactory {
362
368
txIdResolver ( createRequest . getTransactionId ( account . provider . getChainId ( ) ) ) ;
363
369
const transactionResponse = await account . sendTransaction ( createRequest ) ;
364
370
const transactionResult = await transactionResponse . waitForResult < TransactionType . Create > ( ) ;
365
- const contract = new Contract ( contractId , this . interface , account ) as TContract ;
371
+ const contract = new Contract ( contractId , this . interface , account ) as T ;
366
372
367
373
return { contract, transactionResult } ;
368
374
} ;
0 commit comments