@@ -1295,6 +1295,40 @@ export class ContractEncoder {
1295
1295
) ;
1296
1296
}
1297
1297
1298
+ /**
1299
+ * **This method is asynchronous.**
1300
+ *
1301
+ * This method is similar to [[encodeTransaction]], except that instead of
1302
+ * encoding a function transaction, it encodes a creation transaction.
1303
+ *
1304
+ * Because this method does not perform overload resolution, it only returns
1305
+ * the resulting transaction options (including the encoded `data`), and does
1306
+ * not bother returning the ABI used (as this was user-supplied.)
1307
+ *
1308
+ * If the `allowOptions` flag is set in the `options` argument, the input may
1309
+ * contain an additional transaction options argument after the other
1310
+ * arguments. Any non-`data` options not specified in such a transaction
1311
+ * options argument will be simply omitted; it you want some options to have
1312
+ * defaults, it is up to the you to set these options as appropriate
1313
+ * afterwards.
1314
+ *
1315
+ * If the transaction options parameter has a `data` or a `to` option,
1316
+ * these option will be recognized but ignored.
1317
+ *
1318
+ * See [[encodeTransaction]] for documentation of the inputs.
1319
+ */
1320
+ public async encodeCreation (
1321
+ inputs : unknown [ ] ,
1322
+ options : Types . ResolveOptions = { }
1323
+ ) : Promise < Codec . Options > {
1324
+ const method = this . getConstructorMethod ( ) ;
1325
+ return await this . projectEncoder . encodeTxNoResolution (
1326
+ method ,
1327
+ inputs ,
1328
+ options
1329
+ ) ;
1330
+ }
1331
+
1298
1332
/**
1299
1333
* **This method is asynchronous.**
1300
1334
*
@@ -1350,36 +1384,12 @@ export class ContractEncoder {
1350
1384
abi : Abi . FunctionEntry | Abi . ConstructorEntry
1351
1385
) : Codec . Wrap . Method {
1352
1386
abi = < Abi . FunctionEntry | Abi . ConstructorEntry > Abi . normalizeEntry ( abi ) ; //just to be absolutely certain!
1353
- const allocations = this . projectEncoder . getAllocations ( ) ;
1354
1387
debug ( "got allocations" ) ;
1355
1388
switch ( abi . type ) {
1356
- case "constructor" : {
1357
- debug ( "constructor binary: %s" , this . constructorBinary ) ;
1358
- //first check that we have constructor binary, and that it's all linked
1359
- if ( ! this . constructorBinary || this . constructorBinary === "0x" ) {
1360
- throw new NoBytecodeError ( this . contract . contractName ) ;
1361
- } else if ( ! this . constructorBinary . match ( / ^ 0 x ( [ 0 - 9 a - f A - F ] { 2 } ) + $ / ) ) {
1362
- throw new UnlinkedContractError (
1363
- this . contract . contractName ,
1364
- this . artifact ? this . artifact . bytecode : undefined
1365
- ) ;
1366
- }
1367
- //otherwise, we're good to go!
1368
- const allocation =
1369
- //@ts -ignore: We set this up and checked this earlier
1370
- allocations . calldata . constructorAllocations [
1371
- < string > this . constructorContextHash
1372
- ] . input ;
1373
- const inputs = allocation . arguments . map (
1374
- input => ( { type : input . type , name : input . name || undefined } ) //convert "" to undefined
1375
- ) ;
1376
- return {
1377
- selector : this . constructorBinary ,
1378
- inputs,
1379
- abi
1380
- } ;
1381
- }
1382
- case "function" : {
1389
+ case "constructor" :
1390
+ return this . getConstructorMethod ( abi ) ;
1391
+ case "function" :
1392
+ const allocations = this . projectEncoder . getAllocations ( ) ;
1383
1393
const selector : string = Codec . AbiData . Utils . abiSelector ( abi ) ;
1384
1394
const allocation : Codec . AbiData . Allocate . CalldataAllocation = this
1385
1395
. deployedContextHash
@@ -1397,9 +1407,50 @@ export class ContractEncoder {
1397
1407
inputs,
1398
1408
abi
1399
1409
} ;
1400
- }
1401
1410
}
1402
1411
}
1412
+
1413
+ //if you already know the ABI, you can pass it in for convenience.
1414
+ //if you don't, we'll find it for you.
1415
+ private getConstructorMethod ( abi ?: Abi . ConstructorEntry ) : Codec . Wrap . Method {
1416
+ if ( ! abi ) {
1417
+ abi = this . getConstructorAbi ( ) ;
1418
+ }
1419
+ const allocations = this . projectEncoder . getAllocations ( ) ;
1420
+ debug ( "constructor binary: %s" , this . constructorBinary ) ;
1421
+ //first check that we have constructor binary, and that it's all linked
1422
+ if ( ! this . constructorBinary || this . constructorBinary === "0x" ) {
1423
+ throw new NoBytecodeError ( this . contract . contractName ) ;
1424
+ } else if ( ! this . constructorBinary . match ( / ^ 0 x ( [ 0 - 9 a - f A - F ] { 2 } ) + $ / ) ) {
1425
+ throw new UnlinkedContractError (
1426
+ this . contract . contractName ,
1427
+ this . artifact ? this . artifact . bytecode : undefined
1428
+ ) ;
1429
+ }
1430
+ //otherwise, we're good to go!
1431
+ const allocation =
1432
+ //@ts -ignore: We set this up and checked this earlier
1433
+ allocations . calldata . constructorAllocations [
1434
+ < string > this . constructorContextHash
1435
+ ] . input ;
1436
+ const inputs = allocation . arguments . map (
1437
+ input => ( { type : input . type , name : input . name || undefined } ) //convert "" to undefined
1438
+ ) ;
1439
+ return {
1440
+ selector : this . constructorBinary ,
1441
+ inputs,
1442
+ abi
1443
+ } ;
1444
+ }
1445
+
1446
+ private getConstructorAbi ( ) : Abi . ConstructorEntry {
1447
+ return (
1448
+ this . abi . find (
1449
+ ( abi : Abi . Entry ) : abi is Abi . ConstructorEntry =>
1450
+ abi . type === "constructor"
1451
+ ) || Codec . AbiData . Utils . DEFAULT_CONSTRUCTOR_ABI
1452
+ ) ;
1453
+ }
1403
1454
}
1404
1455
1405
1456
/**
@@ -1546,4 +1597,17 @@ export class ContractInstanceEncoder {
1546
1597
encoded . tx . to = this . toAddress ;
1547
1598
return encoded ;
1548
1599
}
1600
+
1601
+ /**
1602
+ * **This method is asynchronous.**
1603
+ *
1604
+ * This method functions identically to [[ContractEncoder.encodeCreation]].
1605
+ * The particular contract instance is ignored, only its class is used.
1606
+ */
1607
+ public async encodeCreation (
1608
+ inputs : unknown [ ] ,
1609
+ options : Types . ResolveOptions = { }
1610
+ ) : Promise < Codec . Options > {
1611
+ return await this . contractEncoder . encodeCreation ( inputs , options ) ;
1612
+ }
1549
1613
}
0 commit comments