2022-09-05 23:57:11 +03:00
"use strict" ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
exports . ContractFactory = void 0 ;
const index _js _1 = require ( "../abi/index.js" ) ;
const index _js _2 = require ( "../address/index.js" ) ;
const index _js _3 = require ( "../utils/index.js" ) ;
const contract _js _1 = require ( "./contract.js" ) ;
// A = Arguments to the constructor
// I = Interface of deployed contracts
2023-06-02 00:52:58 +03:00
/ * *
* A * * ContractFactory * * is used to deploy a Contract to the blockchain .
* /
2022-09-05 23:57:11 +03:00
class ContractFactory {
2023-06-02 00:52:58 +03:00
/ * *
* The Contract Interface .
* /
2022-09-05 23:57:11 +03:00
interface ;
2023-06-02 00:52:58 +03:00
/ * *
* The Contract deployment bytecode . Often called the initcode .
* /
2022-09-05 23:57:11 +03:00
bytecode ;
2023-06-02 00:52:58 +03:00
/ * *
* The ContractRunner to deploy the Contract as .
* /
2022-09-05 23:57:11 +03:00
runner ;
2023-06-02 00:52:58 +03:00
/ * *
* Create a new * * ContractFactory * * with % % abi % % and % % bytecode % % ,
* optionally connected to % % runner % % .
*
* The % % bytecode % % may be the ` ` bytecode ` ` property within the
* standard Solidity JSON output .
* /
2022-09-05 23:57:11 +03:00
constructor ( abi , bytecode , runner ) {
const iface = index _js _1 . Interface . from ( abi ) ;
// Dereference Solidity bytecode objects and allow a missing `0x`-prefix
if ( bytecode instanceof Uint8Array ) {
2022-09-16 05:58:45 +03:00
bytecode = ( 0 , index _js _3 . hexlify ) ( ( 0 , index _js _3 . getBytes ) ( bytecode ) ) ;
2022-09-05 23:57:11 +03:00
}
else {
if ( typeof ( bytecode ) === "object" ) {
bytecode = bytecode . object ;
}
2023-02-19 06:18:42 +03:00
if ( ! bytecode . startsWith ( "0x" ) ) {
2022-09-05 23:57:11 +03:00
bytecode = "0x" + bytecode ;
}
2022-09-16 05:58:45 +03:00
bytecode = ( 0 , index _js _3 . hexlify ) ( ( 0 , index _js _3 . getBytes ) ( bytecode ) ) ;
2022-09-05 23:57:11 +03:00
}
( 0 , index _js _3 . defineProperties ) ( this , {
bytecode , interface : iface , runner : ( runner || null )
} ) ;
}
2023-06-14 04:47:44 +03:00
attach ( target ) {
return new contract _js _1 . BaseContract ( target , this . interface , this . runner ) ;
}
2023-06-02 00:52:58 +03:00
/ * *
* Resolves to the transaction to deploy the contract , passing % % args % %
* into the constructor .
* /
2022-09-05 23:57:11 +03:00
async getDeployTransaction ( ... args ) {
let overrides = { } ;
const fragment = this . interface . deploy ;
if ( fragment . inputs . length + 1 === args . length ) {
overrides = await ( 0 , contract _js _1 . copyOverrides ) ( args . pop ( ) ) ;
}
if ( fragment . inputs . length !== args . length ) {
throw new Error ( "incorrect number of arguments to constructor" ) ;
}
const resolvedArgs = await ( 0 , contract _js _1 . resolveArgs ) ( this . runner , fragment . inputs , args ) ;
const data = ( 0 , index _js _3 . concat ) ( [ this . bytecode , this . interface . encodeDeploy ( resolvedArgs ) ] ) ;
return Object . assign ( { } , overrides , { data } ) ;
}
2023-06-02 00:52:58 +03:00
/ * *
* Resolves to the Contract deployed by passing % % args % % into the
* constructor .
*
2024-02-02 11:25:03 +03:00
* This will resolve to the Contract before it has been deployed to the
2023-06-02 00:52:58 +03:00
* network , so the [ [ BaseContract - waitForDeployment ] ] should be used before
* sending any transactions to it .
* /
2022-09-05 23:57:11 +03:00
async deploy ( ... args ) {
const tx = await this . getDeployTransaction ( ... args ) ;
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assert ) ( this . runner && typeof ( this . runner . sendTransaction ) === "function" , "factory runner does not support sending transactions" , "UNSUPPORTED_OPERATION" , {
operation : "sendTransaction"
} ) ;
2022-09-05 23:57:11 +03:00
const sentTx = await this . runner . sendTransaction ( tx ) ;
const address = ( 0 , index _js _2 . getCreateAddress ) ( sentTx ) ;
return new contract _js _1 . BaseContract ( address , this . interface , this . runner , sentTx ) ;
}
2023-06-02 00:52:58 +03:00
/ * *
* Return a new * * ContractFactory * * with the same ABI and bytecode ,
* but connected to % % runner % % .
* /
2022-09-05 23:57:11 +03:00
connect ( runner ) {
return new ContractFactory ( this . interface , this . bytecode , runner ) ;
}
2023-06-02 00:52:58 +03:00
/ * *
* Create a new * * ContractFactory * * from the standard Solidity JSON output .
* /
2022-09-05 23:57:11 +03:00
static fromSolidity ( output , runner ) {
2022-11-09 10:57:02 +03:00
( 0 , index _js _3 . assertArgument ) ( output != null , "bad compiler output" , "output" , output ) ;
2022-09-05 23:57:11 +03:00
if ( typeof ( output ) === "string" ) {
output = JSON . parse ( output ) ;
}
const abi = output . abi ;
let bytecode = "" ;
if ( output . bytecode ) {
bytecode = output . bytecode ;
}
else if ( output . evm && output . evm . bytecode ) {
bytecode = output . evm . bytecode ;
}
return new this ( abi , bytecode , runner ) ;
}
}
exports . ContractFactory = ContractFactory ;
//# sourceMappingURL=factory.js.map