ContractFactory

To deploy a Contract, additional information is needed that is not available on a Contract object itself.

Mainly, the bytecode (more specifically the initcode) of a contract is required.

The Contract Factory sends a special type of transaction, an initcode transaction (i.e. the to field is null, and the data field is the initcode) where the initcode will be evaluated and the result becomes the new code to be deployed as a new contract.

Creating Instances

new ethers.ContractFactory( interface , bytecode [ , signer ] )

Creates a new instance of a ContractFactory for the contract described by the interface and bytecode initcode.

ContractFactory.fromSolidity( compilerOutput [ , signer ] ) ContractFactory

Consumes the output of the Solidity compiler, extracting the ABI and bytecode from it, allowing for the various formats the solc compiler has emitted over its life.

contractFactory.connect( signer ) ContractFactory

Returns a new instance of the ContractFactory with the same interface and bytecode, but with a different signer.

Properties

contractFactory.interface Interface

The Contract interface.

contractFactory.bytecode string< DataHexString >

The bytecode (i.e. initcode) that this ContractFactory will use to deploy the Contract.

contractFactory.signer Signer

The Signer (if any) this ContractFactory will use to deploy instances of the Contract to the Blockchain.

Methods

contractFactory.attach( address ) Contract

Return an instance of a Contract attached to address. This is the same as using the Contract constructor with address and this the interface and signerOrProvider passed in when creating the ContractFactory.

contractFactory.getDeployTransaction( ...args [ , overrides ] ) UnsignedTransaction

Returns the unsigned transaction which would deploy this Contract with args passed to the Contract's constructor.

If the optional overrides is specified, they can be used to override the endowment value, transaction nonce, gasLimit or gasPrice.

contractFactory.deploy( ...args [ , overrides ] ) Promise< Contract >

Uses the signer to deploy the Contract with args passed into the constructor and returns a Contract which is attached to the address where this contract will be deployed once the transaction is mined.

The transaction can be found at contract.deployTransaction, and no interactions should be made until the transaction is mined.

If the optional overrides is specified, they can be used to override the endowment value, transaction nonce, gasLimit or gasPrice.

Deploying a Contract
// If your contract constructor requires parameters, the ABI // must include the constructor const abi = [ "constructor(address owner, uint256 initialValue)", "function value() view returns (uint)" ]; // The factory we use for deploying contracts factory = new ContractFactory(abi, bytecode, signer) // Deploy an instance of the contract contract = await factory.deploy("ricmoo.eth", 42); // The address is available immediately, but the contract // is NOT deployed yet contract.address // '0x00777F1b1439BDc7Ad1d4905A38Ec9f5400e0e26' // The transaction that the signer sent to deploy contract.deployTransaction // { // accessList: [], // chainId: 123456, // confirmations: 0, // data: '0x608060405234801561001057600080fd5b5060405161012e38038061012e8339818101604052604081101561003357600080fd5b81019080805190602001909291908051906020019092919050505081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060008190555050506088806100a66000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000805490509056fea2646970667358221220926465385af0e8706644e1ff3db7161af699dc063beaadd55405f2ccd6478d7564736f6c634300070400330000000000000000000000005555763613a12d8f3e73be831dff8598089d3dca000000000000000000000000000000000000000000000000000000000000002a', // from: '0x46E0726Ef145d92DEA66D38797CF51901701926e', // gasLimit: { BigNumber: "129862" }, // gasPrice: null, // hash: '0x293aeb5aaa782c45d56aa13e67c8dabe5a34726c68539302401c15a1699074fa', // maxFeePerGas: { BigNumber: "1500000014" }, // maxPriorityFeePerGas: { BigNumber: "1500000000" }, // nonce: 0, // r: '0x6708733f3523a5efc1a5bee891a3020c1d0a622f03ccaa437763241550d93ac7', // s: '0x3065f4437666753b4216bf8518e5c6221d0cf8f86b57857ac268c128a5c2d53c', // to: null, // type: 2, // v: 0, // value: { BigNumber: "0" }, // wait: [Function (anonymous)] // } // Wait until the transaction is mined (i.e. contract is deployed) // - returns the receipt // - throws on failure (the reciept is on the error) await contract.deployTransaction.wait() // { // blockHash: '0x14b2744b8a923827c35827aa47c99942be21c94da4fe2e03451a662e3bd0dbe6', // blockNumber: 60328, // byzantium: true, // confirmations: 1, // contractAddress: '0x00777F1b1439BDc7Ad1d4905A38Ec9f5400e0e26', // cumulativeGasUsed: { BigNumber: "129862" }, // effectiveGasPrice: { BigNumber: "1500000007" }, // events: [], // from: '0x46E0726Ef145d92DEA66D38797CF51901701926e', // gasUsed: { BigNumber: "129862" }, // logs: [], // logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', // status: 1, // to: null, // transactionHash: '0x293aeb5aaa782c45d56aa13e67c8dabe5a34726c68539302401c15a1699074fa', // transactionIndex: 0, // type: 2 // } // Now the contract is safe to interact with await contract.value() // { BigNumber: "42" }