2020-05-08 10:24:40 +03:00
|
|
|
_section: ContractFactory @<ContractFactory> @SRC<contracts:class.ContractFactory>
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
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.
|
2020-05-08 10:24:40 +03:00
|
|
|
|
|
|
|
_subsection: Creating Instances @<ContractFactory--creating>
|
|
|
|
|
2020-09-06 08:57:06 +03:00
|
|
|
_property: new ethers.ContractFactory(interface, bytecode [ , signer ]) @SRC<contracts:constructor.ContractFactory>
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
Creates a new instance of a **ContractFactory** for the contract described
|
|
|
|
by the //interface// and //bytecode// initcode.
|
|
|
|
|
2020-05-08 10:24:40 +03:00
|
|
|
_property: ContractFactory.fromSolidity(compilerOutput [ , signer ]) => [[ContractFactory]]
|
|
|
|
|
2020-12-09 02:29:56 +03: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.
|
|
|
|
|
2021-11-30 18:24:06 +03:00
|
|
|
_property: contractFactory.connect(signer) => [[ContractFactory]] @<ContractFactory-connect>
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
Returns a **new instance** of the ContractFactory with the same //interface//
|
|
|
|
and //bytecode//, but with a different //signer//.
|
2020-05-08 10:24:40 +03:00
|
|
|
|
|
|
|
_subsection: Properties @<ContractFactory--properties>
|
|
|
|
|
|
|
|
_property: contractFactory.interface => [[Interface]]
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
The [[Contract]] interface.
|
|
|
|
|
2020-05-08 10:24:40 +03:00
|
|
|
_property: contractFactory.bytecode => string<[[DataHexString]]>
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
The bytecode (i.e. initcode) that this **ContractFactory** will
|
|
|
|
use to deploy the Contract.
|
|
|
|
|
2020-05-08 10:24:40 +03:00
|
|
|
_property: contractFactory.signer => [[Signer]]
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
The [[Signer]] (if any) this ContractFactory will use to deploy instances
|
|
|
|
of the Contract to the Blockchain.
|
|
|
|
|
2020-05-08 10:24:40 +03:00
|
|
|
|
|
|
|
_subsection: Methods @<ContractFactory--methods>
|
|
|
|
|
2020-06-12 10:38:55 +03:00
|
|
|
_property: contractFactory.attach(address) => [[Contract]] @<ContractFactory-attach>
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-10-03 19:30:15 +03:00
|
|
|
Return an instance of a [[Contract]] attached to //address//. This is the
|
2020-06-12 10:38:55 +03:00
|
|
|
same as using the [Contract constructor](Contract--creating) with
|
2020-11-23 07:03:50 +03:00
|
|
|
//address// and this the //interface// and //signerOrProvider// passed
|
2020-05-08 10:24:40 +03:00
|
|
|
in when creating the ContractFactory.
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
_property: contractFactory.getDeployTransaction(...args [ , overrides ]) => [[UnsignedTransaction]]
|
2020-05-08 10:24:40 +03:00
|
|
|
|
|
|
|
Returns the unsigned transaction which would deploy this Contract with //args// passed
|
|
|
|
to the Contract's constructor.
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
If the optional //overrides// is specified, they can be used to
|
|
|
|
override the endowment ``value``, transaction ``nonce``, ``gasLimit`` or
|
|
|
|
``gasPrice``.
|
|
|
|
|
|
|
|
_property: contractFactory.deploy(...args [ , overrides ]) => Promise<[[Contract]]> @<ContractFactory-deploy>
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-07-03 08:44:17 +03:00
|
|
|
Uses the signer to deploy the Contract with //args// passed into the constructor and
|
2020-12-09 02:29:56 +03:00
|
|
|
returns a Contract which is attached to the address where this contract **will** be
|
2020-07-03 08:44:17 +03:00
|
|
|
deployed once the transaction is mined.
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-07-03 08:44:17 +03:00
|
|
|
The transaction can be found at ``contract.deployTransaction``, and no interactions
|
2020-05-08 10:24:40 +03:00
|
|
|
should be made until the transaction is mined.
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
If the optional //overrides// is specified, they can be used to
|
|
|
|
override the endowment ``value``, transaction ``nonce``, ``gasLimit`` or
|
|
|
|
``gasPrice``.
|
|
|
|
|
|
|
|
_code: Deploying a Contract @lang<javascript>
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2021-06-04 08:17:56 +03:00
|
|
|
//_hide: const signer = localSigner;
|
|
|
|
//_hide: const ContractFactory = ethers.ContractFactory;
|
|
|
|
//_hide: const bytecode = "608060405234801561001057600080fd5b5060405161012e38038061012e8339818101604052604081101561003357600080fd5b81019080805190602001909291908051906020019092919050505081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060008190555050506088806100a66000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000805490509056fea2646970667358221220926465385af0e8706644e1ff3db7161af699dc063beaadd55405f2ccd6478d7564736f6c63430007040033";
|
2020-12-09 02:29:56 +03:00
|
|
|
|
2020-05-08 10:24:40 +03:00
|
|
|
// If your contract constructor requires parameters, the ABI
|
|
|
|
// must include the constructor
|
|
|
|
const abi = [
|
2020-12-09 02:29:56 +03:00
|
|
|
"constructor(address owner, uint256 initialValue)",
|
|
|
|
"function value() view returns (uint)"
|
2020-05-08 10:24:40 +03:00
|
|
|
];
|
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
// The factory we use for deploying contracts
|
|
|
|
factory = new ContractFactory(abi, bytecode, signer)
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
// Deploy an instance of the contract
|
|
|
|
contract = await factory.deploy("ricmoo.eth", 42);
|
2020-05-08 10:24:40 +03:00
|
|
|
|
|
|
|
// The address is available immediately, but the contract
|
|
|
|
// is NOT deployed yet
|
2021-06-04 08:17:56 +03:00
|
|
|
//_result:
|
2020-05-08 10:24:40 +03:00
|
|
|
contract.address
|
2021-06-04 08:17:56 +03:00
|
|
|
//_log:
|
2020-05-08 10:24:40 +03:00
|
|
|
|
|
|
|
// The transaction that the signer sent to deploy
|
2021-06-04 08:17:56 +03:00
|
|
|
//_result:
|
2020-05-08 10:24:40 +03:00
|
|
|
contract.deployTransaction
|
2021-06-04 08:17:56 +03:00
|
|
|
//_log:
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-12-09 02:29:56 +03:00
|
|
|
// Wait until the transaction is mined (i.e. contract is deployed)
|
|
|
|
// - returns the receipt
|
|
|
|
// - throws on failure (the reciept is on the error)
|
2021-06-04 08:17:56 +03:00
|
|
|
//_result:
|
|
|
|
await contract.deployTransaction.wait()
|
|
|
|
//_log:
|
2020-05-08 10:24:40 +03:00
|
|
|
|
2020-07-03 08:44:17 +03:00
|
|
|
// Now the contract is safe to interact with
|
2021-06-04 08:17:56 +03:00
|
|
|
//_result:
|
|
|
|
await contract.value()
|
|
|
|
//_log:
|