13
13
using Nethereum . Contracts . MessageEncodingServices ;
14
14
15
15
using System . Diagnostics ;
16
+ using Nethereum . ABI . FunctionEncoding ;
17
+ using System . Threading ;
16
18
17
19
namespace Nethereum . Contracts . Create2Deployment
18
20
{
@@ -140,14 +142,8 @@ public async Task<string> DeployProxyAndGetContractAddressAsync(Create2Determini
140
142
if ( await HasProxyBeenDeployedAsync ( deployment . Address ) ) return deployment . Address ;
141
143
142
144
await ValidateAndSendEnoughBalanceForProxyDeploymentAndWaitForReceiptAsync ( deployment ) ;
143
- var currentLegacySetting = _ethApiContractService . TransactionManager . UseLegacyAsDefault ;
144
-
145
- _ethApiContractService . TransactionManager . UseLegacyAsDefault = true ;
146
-
147
145
var txn = await _ethApiContractService . Transactions . SendRawTransaction . SendRequestAsync ( deployment . RawTransaction ) ;
148
146
149
- _ethApiContractService . TransactionManager . UseLegacyAsDefault = currentLegacySetting ;
150
-
151
147
var receipt = await _ethApiContractService . TransactionManager . TransactionReceiptService . PollForReceiptAsync ( txn ) ;
152
148
var deployed = await HasProxyBeenDeployedAsync ( deployment . Address ) ;
153
149
if ( ! deployed ) throw new Exception ( "Proxy not deployed" ) ;
@@ -172,49 +168,57 @@ public async Task ValidateAndSendEnoughBalanceForProxyDeploymentAndWaitForReceip
172
168
}
173
169
}
174
170
175
- public string CalculateCreate2Address < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt ) where TDeploymentMessage : ContractDeploymentMessage
171
+ public string CalculateCreate2Address < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt , params ByteCodeLibrary [ ] byteCodeLibraries ) where TDeploymentMessage : ContractDeploymentMessage , new ( )
176
172
{
177
- var deploymentEncodingService = new DeploymentMessageEncodingService < TDeploymentMessage > ( ) ;
178
- var deploymentData = deploymentEncodingService . GetDeploymentData ( deploymentMessage ) ;
173
+ return deploymentMessage . CalculateCreate2Address ( deployerProxyAddress , salt , byteCodeLibraries ) ;
174
+ }
179
175
180
- return CalculateCreate2Address ( deployerProxyAddress , salt , deploymentData ) ;
176
+ public string CalculateCreate2Address < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt ) where TDeploymentMessage : ContractDeploymentMessage , new ( )
177
+ {
178
+ return deploymentMessage . CalculateCreate2Address ( deployerProxyAddress , salt ) ;
181
179
}
182
180
183
181
public string CalculateCreate2Address ( string deployerProxyAddress , string salt , string contractByteCode )
184
182
{
185
183
return ContractUtils . CalculateCreate2Address ( deployerProxyAddress , salt , contractByteCode ) ;
186
184
}
187
185
188
- public async Task < bool > CheckContractAlreadyDeployedAsync ( string deployerProxyAddress , string salt , string contractByteCode )
186
+ public async Task < bool > HasContractAlreadyDeployedAsync ( string deployerProxyAddress , string salt , string contractByteCode )
189
187
{
190
188
var create2Address = CalculateCreate2Address ( deployerProxyAddress , salt , contractByteCode ) ;
191
189
var code = await _ethApiContractService . GetCode . SendRequestAsync ( create2Address ) ;
192
190
return ! string . IsNullOrEmpty ( code ? . RemoveHexPrefix ( ) ) ;
193
191
}
194
192
195
- public async Task < bool > CheckContractAlreadyDeployedAsync < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt )
196
- where TDeploymentMessage : ContractDeploymentMessage
193
+ public async Task < bool > HasContractAlreadyDeployedAsync < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt )
194
+ where TDeploymentMessage : ContractDeploymentMessage , new ( )
197
195
{
198
196
var create2Address = CalculateCreate2Address ( deploymentMessage , deployerProxyAddress , salt ) ;
199
197
var code = await _ethApiContractService . GetCode . SendRequestAsync ( create2Address ) ;
200
198
return ! string . IsNullOrEmpty ( code ? . RemoveHexPrefix ( ) ) ;
201
199
}
202
200
203
- public async Task < bool > CheckContractAlreadyDeployedAsync ( string address )
201
+ public async Task < bool > HasContractAlreadyDeployedAsync ( string address )
204
202
{
205
203
var code = await _ethApiContractService . GetCode . SendRequestAsync ( address ) ;
206
204
return ! string . IsNullOrEmpty ( code ? . RemoveHexPrefix ( ) ) ;
207
205
}
208
206
209
- public async Task < string > DeployContractRequestAsync < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt )
210
- where TDeploymentMessage : ContractDeploymentMessage
207
+ public async Task < Create2ContractDeploymentTransactionResult > DeployContractRequestAsync < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt , params ByteCodeLibrary [ ] byteCodeLibraries )
208
+ where TDeploymentMessage : ContractDeploymentMessage , new ( )
211
209
{
212
- if ( await CheckContractAlreadyDeployedAsync ( deploymentMessage , deployerProxyAddress , salt ) )
213
- throw new Exception ( "Contract already deployed" ) ;
210
+ var address = CalculateCreate2Address ( deploymentMessage , deployerProxyAddress , salt , byteCodeLibraries ) ;
211
+ if ( await HasContractAlreadyDeployedAsync ( address ) )
212
+ {
213
+ return new Create2ContractDeploymentTransactionResult ( )
214
+ {
215
+ Address = address ,
216
+ AlreadyDeployed = true
217
+ } ;
218
+ }
214
219
215
220
var deploymentEncodingService = new DeploymentMessageEncodingService < TDeploymentMessage > ( ) ;
216
- var deploymentData = deploymentEncodingService . GetDeploymentData ( deploymentMessage ) ;
217
-
221
+ var deploymentData = deploymentEncodingService . GetDeploymentData ( deploymentMessage , byteCodeLibraries ) ;
218
222
219
223
var transactionInput = new TransactionInput ( )
220
224
{
@@ -225,44 +229,88 @@ public async Task<string> DeployContractRequestAsync<TDeploymentMessage>(TDeploy
225
229
226
230
var gas = await _ethApiContractService . Transactions . EstimateGas . SendRequestAsync ( transactionInput ) ;
227
231
transactionInput . Gas = gas ;
228
- return await _ethApiContractService . TransactionManager . SendTransactionAsync ( transactionInput ) ;
232
+ var txnHash = await _ethApiContractService . TransactionManager . SendTransactionAsync ( transactionInput ) ;
233
+ return new Create2ContractDeploymentTransactionResult ( )
234
+ {
235
+ TransactionHash = txnHash ,
236
+ Address = address
237
+ } ;
229
238
}
230
239
231
- public async Task < TransactionReceipt > DeployContractRequestAndWaitForReceiptAsync < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt )
232
- where TDeploymentMessage : ContractDeploymentMessage
240
+ public async Task < Create2ContractDeploymentTransactionReceiptResult > DeployContractRequestAndWaitForReceiptAsync < TDeploymentMessage > ( TDeploymentMessage deploymentMessage , string deployerProxyAddress , string salt , ByteCodeLibrary [ ] byteCodeLibraries = null , CancellationToken cancellationToken = default )
241
+ where TDeploymentMessage : ContractDeploymentMessage , new ( )
233
242
{
234
- var txnHash = await DeployContractRequestAsync ( deploymentMessage , deployerProxyAddress , salt ) ;
235
- var receipt = await _ethApiContractService . TransactionManager . TransactionReceiptService . PollForReceiptAsync ( txnHash ) ;
236
- if ( await CheckContractAlreadyDeployedAsync ( deploymentMessage , deployerProxyAddress , salt ) )
243
+ var deploymentTransactionResult = await DeployContractRequestAsync ( deploymentMessage , deployerProxyAddress , salt , byteCodeLibraries ) ;
244
+ if ( deploymentTransactionResult . AlreadyDeployed )
245
+ {
246
+ return new Create2ContractDeploymentTransactionReceiptResult ( )
247
+ {
248
+ Address = deploymentTransactionResult . Address ,
249
+ AlreadyDeployed = true
250
+ } ;
251
+ }
252
+ var receipt = await _ethApiContractService . TransactionManager . TransactionReceiptService . PollForReceiptAsync ( deploymentTransactionResult . TransactionHash , cancellationToken ) ;
253
+ if ( await HasContractAlreadyDeployedAsync ( deploymentTransactionResult . Address ) )
237
254
{
238
- return receipt ;
255
+ return new Create2ContractDeploymentTransactionReceiptResult ( )
256
+ {
257
+ Address = deploymentTransactionResult . Address ,
258
+ TransactionReceipt = receipt
259
+ } ;
239
260
}
240
261
throw new Exception ( "Contract not deployed" ) ;
241
262
}
242
263
243
- public async Task < string > DeployContractRequestAsync ( string deployerProxyAddress , string salt , string contractByteCode )
264
+ public async Task < Create2ContractDeploymentTransactionResult > DeployContractRequestAsync ( string deployerProxyAddress , string salt , string contractByteCode )
244
265
{
245
- if ( await CheckContractAlreadyDeployedAsync ( deployerProxyAddress , salt , contractByteCode ) )
246
- throw new Exception ( "Contract already deployed" ) ;
247
- var transactionInput = new TransactionInput ( )
266
+ var address = ContractUtils . CalculateCreate2Address ( deployerProxyAddress , salt , contractByteCode ) ;
267
+ if ( await HasContractAlreadyDeployedAsync ( address ) )
248
268
{
249
- From = _ethApiContractService . TransactionManager . Account . Address ,
250
- Data = salt . EnsureHexPrefix ( ) + contractByteCode . RemoveHexPrefix ( ) ,
251
- To = deployerProxyAddress
252
- } ;
269
+ return new Create2ContractDeploymentTransactionResult ( )
270
+ {
271
+ Address = address ,
272
+ AlreadyDeployed = true
273
+ } ;
274
+ }
275
+ else
276
+ {
277
+ var transactionInput = new TransactionInput ( )
278
+ {
279
+ From = _ethApiContractService . TransactionManager . Account . Address ,
280
+ Data = salt . EnsureHexPrefix ( ) + contractByteCode . RemoveHexPrefix ( ) ,
281
+ To = deployerProxyAddress
282
+ } ;
253
283
254
- var gas = await _ethApiContractService . Transactions . EstimateGas . SendRequestAsync ( transactionInput ) ;
255
- transactionInput . Gas = gas ;
256
- return await _ethApiContractService . TransactionManager . SendTransactionAsync ( transactionInput ) ;
284
+ var gas = await _ethApiContractService . Transactions . EstimateGas . SendRequestAsync ( transactionInput ) ;
285
+ transactionInput . Gas = gas ;
286
+ var txnHash = await _ethApiContractService . TransactionManager . SendTransactionAsync ( transactionInput ) ;
287
+ return new Create2ContractDeploymentTransactionResult ( )
288
+ {
289
+ TransactionHash = txnHash ,
290
+ Address = address
291
+ } ;
292
+ }
257
293
}
258
294
259
- public async Task < TransactionReceipt > DeployContractRequestAndWaitForReceiptAsync ( string deployerProxyAddress , string salt , string contractByteCode )
295
+ public async Task < Create2ContractDeploymentTransactionReceiptResult > DeployContractRequestAndWaitForReceiptAsync ( string deployerProxyAddress , string salt , string contractByteCode , CancellationToken cancellationToken = default )
260
296
{
261
- var txnHash = await DeployContractRequestAsync ( deployerProxyAddress , salt , contractByteCode ) ;
262
- var receipt = await _ethApiContractService . TransactionManager . TransactionReceiptService . PollForReceiptAsync ( txnHash ) ;
263
- if ( await CheckContractAlreadyDeployedAsync ( deployerProxyAddress , salt , contractByteCode ) )
297
+ var deploymentTransactionResult = await DeployContractRequestAsync ( deployerProxyAddress , salt , contractByteCode ) ;
298
+ if ( deploymentTransactionResult . AlreadyDeployed )
299
+ {
300
+ return new Create2ContractDeploymentTransactionReceiptResult ( )
301
+ {
302
+ Address = deploymentTransactionResult . Address ,
303
+ AlreadyDeployed = true
304
+ } ;
305
+ }
306
+ var receipt = await _ethApiContractService . TransactionManager . TransactionReceiptService . PollForReceiptAsync ( deploymentTransactionResult . TransactionHash , cancellationToken ) ;
307
+ if ( await HasContractAlreadyDeployedAsync ( deploymentTransactionResult . Address ) )
264
308
{
265
- return receipt ;
309
+ return new Create2ContractDeploymentTransactionReceiptResult ( )
310
+ {
311
+ Address = deploymentTransactionResult . Address ,
312
+ TransactionReceipt = receipt
313
+ } ;
266
314
}
267
315
throw new Exception ( "Contract not deployed" ) ;
268
316
}
0 commit comments