ethers.js/docs/v5/api/contract/contract-factory/README.md

138 lines
6.1 KiB
Markdown
Raw Permalink Normal View History

2020-06-09 23:56:58 -04:00
-----
2020-07-03 01:54:56 -04:00
Documentation: [html](https://docs.ethers.io/)
2020-06-09 23:56:58 -04:00
-----
ContractFactory
===============
Creating Instances
------------------
2020-09-08 01:12:15 -04:00
#### **new ***ethers* . **ContractFactory**( interface , bytecode [ , signer ] )
2020-06-09 23:56:58 -04:00
2021-02-08 15:26:10 -05:00
Creates a new instance of a **ContractFactory** for the contract described by the *interface* and *bytecode* initcode.
2020-06-09 23:56:58 -04:00
#### *ContractFactory* . **fromSolidity**( compilerOutput [ , signer ] ) => *[ContractFactory](/v5/api/contract/contract-factory/)*
2021-02-08 15:26:10 -05:00
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.
2020-06-09 23:56:58 -04:00
#### *contractFactory* . **connect**( signer ) => *[Contract](/v5/api/contract/contract/)*
2021-02-08 15:26:10 -05:00
Returns a **new instance** of the ContractFactory with the same *interface* and *bytecode*, but with a different *signer*.
2020-06-09 23:56:58 -04:00
Properties
----------
#### *contractFactory* . **interface** => *[Interface](/v5/api/utils/abi/interface/)*
2021-02-08 15:26:10 -05:00
The [Contract](/v5/api/contract/contract/) interface.
2020-06-09 23:56:58 -04:00
#### *contractFactory* . **bytecode** => *string< [DataHexString](/v5/api/utils/bytes/#DataHexString) >*
2021-02-08 15:26:10 -05:00
The bytecode (i.e. initcode) that this **ContractFactory** will use to deploy the Contract.
2020-06-09 23:56:58 -04:00
#### *contractFactory* . **signer** => *[Signer](/v5/api/signer/#Signer)*
2021-02-08 15:26:10 -05:00
The [Signer](/v5/api/signer/#Signer) (if any) this ContractFactory will use to deploy instances of the Contract to the Blockchain.
2020-06-09 23:56:58 -04:00
Methods
-------
#### *contractFactory* . **attach**( address ) => *[Contract](/v5/api/contract/contract/)*
2020-11-22 23:07:13 -05:00
Return an instance of a [Contract](/v5/api/contract/contract/) attached to *address*. This is the same as using the [Contract constructor](/v5/api/contract/contract/#Contract--creating) with *address* and this the *interface* and *signerOrProvider* passed in when creating the ContractFactory.
2020-06-09 23:56:58 -04:00
2021-02-08 15:26:10 -05:00
#### *contractFactory* . **getDeployTransaction**( ...args [ , overrides ] ) => *[UnsignedTransaction](/v5/api/utils/transactions/#UnsignedTransaction)*
2020-06-09 23:56:58 -04:00
Returns the unsigned transaction which would deploy this Contract with *args* passed to the Contract's constructor.
2021-02-08 15:26:10 -05:00
If the optional *overrides* is specified, they can be used to override the endowment `value`, transaction `nonce`, `gasLimit` or `gasPrice`.
2020-06-09 23:56:58 -04:00
2021-02-08 15:26:10 -05:00
#### *contractFactory* . **deploy**( ...args [ , overrides ] ) => *Promise< [Contract](/v5/api/contract/contract/) >*
2020-06-09 23:56:58 -04:00
2020-11-22 23:07:13 -05:00
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.
2020-06-09 23:56:58 -04:00
2020-07-03 01:54:56 -04:00
The transaction can be found at `contract.deployTransaction`, and no interactions should be made until the transaction is mined.
2020-06-09 23:56:58 -04:00
2021-02-08 15:26:10 -05:00
If the optional *overrides* is specified, they can be used to override the endowment `value`, transaction `nonce`, `gasLimit` or `gasPrice`.
2020-06-09 23:56:58 -04:00
2021-02-08 15:26:10 -05:00
```javascript
2020-06-09 23:56:58 -04:00
// If your contract constructor requires parameters, the ABI
// must include the constructor
const abi = [
2021-02-08 15:26:10 -05:00
"constructor(address owner, uint256 initialValue)",
"function value() view returns (uint)"
2020-06-09 23:56:58 -04:00
];
2021-02-08 15:26:10 -05:00
// The factory we use for deploying contracts
factory = new ContractFactory(abi, bytecode, signer)
2020-06-09 23:56:58 -04:00
2021-02-08 15:26:10 -05:00
// Deploy an instance of the contract
contract = await factory.deploy("ricmoo.eth", 42);
2020-06-09 23:56:58 -04:00
// The address is available immediately, but the contract
// is NOT deployed yet
contract.address
2021-02-08 15:26:10 -05:00
// '0x26E9685C018Bf3A401DFA632827e7e6C7D96b1C0'
2020-06-09 23:56:58 -04:00
// The transaction that the signer sent to deploy
contract.deployTransaction
2021-02-08 15:26:10 -05:00
// {
// blockHash: null,
// blockNumber: null,
// chainId: 1337,
// confirmations: 0,
// creates: '0x26E9685C018Bf3A401DFA632827e7e6C7D96b1C0',
// data: '0x608060405234801561001057600080fd5b5060405161012e38038061012e8339818101604052604081101561003357600080fd5b81019080805190602001909291908051906020019092919050505081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060008190555050506088806100a66000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000805490509056fea2646970667358221220926465385af0e8706644e1ff3db7161af699dc063beaadd55405f2ccd6478d7564736f6c63430007040033000000000000000000000000ab7c8803962c0f2f5bbbe3fa8bf41cd82aa1923c000000000000000000000000000000000000000000000000000000000000002a',
// 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)
2020-06-09 23:56:58 -04:00
contract.deployTransaction.wait()
2021-02-08 15:26:10 -05:00
// { Promise: {
// blockHash: '0x1715de2bdfec15a7f64fb79a8254699274be6776df244d24a04945a3218543e6',
// blockNumber: 2,
// byzantium: true,
// confirmations: 1,
// contractAddress: '0x26E9685C018Bf3A401DFA632827e7e6C7D96b1C0',
// cumulativeGasUsed: { BigNumber: "126462" },
// from: '0xf3e6942b256A60B596B24F633caE8aDB4983fCA6',
// gasUsed: { BigNumber: "126462" },
// logs: [],
// logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
// status: 1,
// to: null,
// transactionHash: '0x4037630fdadbbe0aac0bf90eba61118e35ee5fc28329e2134bb2bad0bfc12684',
// transactionIndex: 0
// } }
2020-06-09 23:56:58 -04:00
2020-07-03 01:54:56 -04:00
// Now the contract is safe to interact with
2020-06-09 23:56:58 -04:00
contract.value()
2021-02-08 15:26:10 -05:00
// { Promise: { BigNumber: "42" } }
2020-06-09 23:56:58 -04:00
```