#
ContractFactory
source

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 ] )
source

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 ) ⇒ Contract

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 // '0x26E9685C018Bf3A401DFA632827e7e6C7D96b1C0' // The transaction that the signer sent to deploy contract.deployTransaction // { // blockHash: null, // blockNumber: null, // chainId: 1337, // confirmations: 0, // creates: '0x26E9685C018Bf3A401DFA632827e7e6C7D96b1C0', // data: ' ', // from: '0xf3e6942b256A60B596B24F633caE8aDB4983fCA6', // gasLimit: { BigNumber: "126462" }, // gasPrice: { BigNumber: "1" }, // hash: '0x4037630fdadbbe0aac0bf90eba61118e35ee5fc28329e2134bb2bad0bfc12684', // nonce: 1, // r: '0xc3bb79ea4600864cd110fe74d31d38bb3702c327fd5aef9feddf66903cc87a4f', // s: '0x35cc2ffe352c02b5fcfbbcd2e348001af670f64438d7a9b2c34756ec293a0784', // to: null, // transactionIndex: null, // v: 2709, // value: { BigNumber: "0" }, // wait: [Function] // } // Wait until the transaction is mined (i.e. contract is deployed) // - returns the receipt // - throws on failure (the reciept is on the error) contract.deployTransaction.wait() // { Promise: { // blockHash: '0x1715de2bdfec15a7f64fb79a8254699274be6776df244d24a04945a3218543e6', // blockNumber: 2, // byzantium: true, // confirmations: 1, // contractAddress: '0x26E9685C018Bf3A401DFA632827e7e6C7D96b1C0', // cumulativeGasUsed: { BigNumber: "126462" }, // from: '0xf3e6942b256A60B596B24F633caE8aDB4983fCA6', // gasUsed: { BigNumber: "126462" }, // logs: [], // logsBloom: ' ', // status: 1, // to: null, // transactionHash: '0x4037630fdadbbe0aac0bf90eba61118e35ee5fc28329e2134bb2bad0bfc12684', // transactionIndex: 0 // } } // Now the contract is safe to interact with contract.value() // { Promise: { BigNumber: "42" } }
The content of this site is licensed under the Creative Commons License. Generated on February 8, 2021, 3:25pm.