ethers.js/docs.wrm/api/contract/contract-factory.wrm

120 lines
4.7 KiB
Plaintext
Raw Normal View History

2020-05-08 10:24:40 +03:00
_section: ContractFactory @<ContractFactory> @SRC<contracts:class.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.
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
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]]
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.
_property: contractFactory.connect(signer) => [[ContractFactory]] @<ContractFactory-connect>
2020-05-08 10:24:40 +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]]
The [[Contract]] interface.
2020-05-08 10:24:40 +03:00
_property: contractFactory.bytecode => string<[[DataHexString]]>
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]]
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.
_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.
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
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-05-08 10:24:40 +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.
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
//_hide: const signer = localSigner;
//_hide: const ContractFactory = ethers.ContractFactory;
//_hide: const bytecode = "608060405234801561001057600080fd5b5060405161012e38038061012e8339818101604052604081101561003357600080fd5b81019080805190602001909291908051906020019092919050505081600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060008190555050506088806100a66000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c80633fa4f24514602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000805490509056fea2646970667358221220926465385af0e8706644e1ff3db7161af699dc063beaadd55405f2ccd6478d7564736f6c63430007040033";
2020-05-08 10:24:40 +03:00
// If your contract constructor requires parameters, the ABI
// must include the constructor
const abi = [
"constructor(address owner, uint256 initialValue)",
"function value() view returns (uint)"
2020-05-08 10:24:40 +03:00
];
// The factory we use for deploying contracts
factory = new ContractFactory(abi, bytecode, signer)
2020-05-08 10:24:40 +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
//_result:
2020-05-08 10:24:40 +03:00
contract.address
//_log:
2020-05-08 10:24:40 +03:00
// The transaction that the signer sent to deploy
//_result:
2020-05-08 10:24:40 +03:00
contract.deployTransaction
//_log:
2020-05-08 10:24:40 +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)
//_result:
await contract.deployTransaction.wait()
//_log:
2020-05-08 10:24:40 +03:00
// Now the contract is safe to interact with
//_result:
await contract.value()
//_log: