2020-05-08 03:24:40 -04:00
|
|
|
_section: Contract @<Contract> @SRC<contracts:class.Contract>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
A **Contract** is an abstraction of code that has been deployed
|
|
|
|
to the blockchain.
|
|
|
|
|
|
|
|
A Contract may be sent transactions, which will trigger its code
|
|
|
|
to be run with the input of the transaction data.
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Creating Instances @<Contract--creating>
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-04-17 22:09:50 -04:00
|
|
|
_property: new ethers.Contract(address, abi, signerOrProvider) @src<contracts:class.Contract>
|
2020-05-08 03:24:40 -04:00
|
|
|
|
2021-04-17 22:09:50 -04:00
|
|
|
_property: contract.attach(addressOrName) => [[Contract]] @<Contract-attach> @SRC<contracts:BaseContract.attach>
|
2020-05-08 03:24:40 -04:00
|
|
|
Returns a new instance of the **Contract** attached to a new
|
|
|
|
address. This is useful if there are multiple similar or identical
|
|
|
|
copies of a Contract on the network and you wish to interact with
|
|
|
|
each of them.
|
|
|
|
|
2021-04-17 22:09:50 -04:00
|
|
|
_property: contract.connect(providerOrSigner) => [[Contract]] @<Contract-connect> @SRC<contracts:BaseContract.connect>
|
2020-05-08 03:24:40 -04:00
|
|
|
Returns a new instance of the Contract, but connected to
|
|
|
|
//providerOrSigner//.
|
|
|
|
|
|
|
|
By passing in a [[Provider]], this will return a downgraded
|
|
|
|
**Contract** which only has read-only access (i.e. constant calls).
|
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
By passing in a [[Signer]]. this will return a **Contract** which
|
2020-05-08 03:24:40 -04:00
|
|
|
will act on behalf of that signer.
|
|
|
|
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Properties @<Contract--properties>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
|
|
|
_property: contract.address => string<[[address]]>
|
|
|
|
This is the address (or ENS name) the contract was constructed with.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.resolvedAddress => string<[[address]]>
|
2020-02-25 14:57:11 -05:00
|
|
|
This is a promise that will resolve to the address the **Contract**
|
|
|
|
object is attached to. If an [[address]] was provided to the constructor,
|
|
|
|
it will be equal to this; if an ENS name was provided, this will be the
|
|
|
|
resolved address.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.deployTransaction => [[providers-TransactionResponse]]
|
2020-02-25 14:57:11 -05:00
|
|
|
If the **Contract** object is the result of a ContractFactory deployment,
|
|
|
|
this is the transaction which was used to deploy the contract.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.interface => [[Interface]]
|
|
|
|
This is the ABI as an [[Interface]].
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.provider => [[Provider]]
|
2020-02-25 14:57:11 -05:00
|
|
|
If a provider was provided to the constructor, this is that provider. If
|
2020-05-08 03:24:40 -04:00
|
|
|
a signer was provided that had a [[Provider]], this is that provider.
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.signer => [[Signer]]
|
2020-02-25 14:57:11 -05:00
|
|
|
If a signer was provided to the constructor, this is that signer.
|
|
|
|
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Methods @<Contract--methods>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.deployed() => Promise<[[Contract]]> @<Contract-deployed> @SRC<contracts>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: Contract.isIndexed(value) => boolean @<Contract-isIndexed> @SRC<contracts>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Events @<Contract--events>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.queryFilter(event [ , fromBlockOrBlockHash [ , toBlock ]) => Promise<Array<Event>> @<Contract-queryFilter> @SRC<contracts>
|
2020-02-25 14:57:11 -05:00
|
|
|
Return Events that match the //event//.
|
|
|
|
|
2021-04-17 22:09:50 -04:00
|
|
|
_property: contract.listenerCount([ event ]) => number @<Contract-listenerCount> @SRC<contracts:BaseContract.listenerCount>
|
2020-02-25 14:57:11 -05:00
|
|
|
Return the number of listeners that are subscribed to //event//. If
|
|
|
|
no event is provided, returns the total count of all events.
|
|
|
|
|
2021-04-17 22:09:50 -04:00
|
|
|
_property: contract.listeners(event) => Array<Listener> @<Contract-listeners> @SRC<contracts:BaseContract.listeners>
|
2020-02-25 14:57:11 -05:00
|
|
|
Return a list of listeners that are subscribed to //event//.
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.off(event, listener) => this @<Contract-off> @SRC<contracts>
|
2020-02-25 14:57:11 -05:00
|
|
|
Unsubscribe //listener// to //event//.
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.on(event, listener) => this @<Contract-on> @SRC<contracts>
|
2020-02-25 14:57:11 -05:00
|
|
|
Subscribe to //event// calling //listener// when the event occurs.
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.once(event, listener) => this @<Contract-once> @SRC<contracts>
|
2020-02-25 14:57:11 -05:00
|
|
|
Subscribe once to //event// calling //listener// when the event
|
|
|
|
occurs.
|
|
|
|
|
2021-04-17 22:09:50 -04:00
|
|
|
_property: contract.removeAllListeners([ event ]) => this @<Contract-removeAllListeners> @SRC<contracts:BaseContract.removeAllListeners>
|
2020-02-25 14:57:11 -05:00
|
|
|
Unsubscribe all listeners for //event//. If no event is provided,
|
|
|
|
all events are unsubscribed.
|
|
|
|
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_subsection: Meta-Class @<Contract--metaclass>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
|
|
|
A Meta-Class is a Class which has any of its properties determined
|
|
|
|
at run-time. The **Contract** object uses a Contract's ABI to
|
|
|
|
determine what methods are available, so the following sections
|
|
|
|
describe the generic ways to interact with the properties added
|
|
|
|
at run-time during the **Contract** constructor.
|
|
|
|
|
|
|
|
|
2021-12-01 12:56:21 -05:00
|
|
|
_heading: Read-Only Methods (constant) @<Contract--readonly>
|
|
|
|
|
|
|
|
A constant method (denoted by ``pure`` or ``view`` in Solidity)
|
|
|
|
is read-only and evaluates a small amount of EVM code against the
|
|
|
|
current blockchain state and can be computed by asking a single
|
|
|
|
node, which can return a result. It is therefore free and does
|
|
|
|
not require any ether, but **cannot make changes** to the
|
|
|
|
blockchain state..
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.METHOD_NAME(...args [, overrides ]) => Promise<any> @<Contract-functionsCall>
|
2021-02-04 17:29:21 -05:00
|
|
|
The type of the result depends on the ABI. If the method returns a single
|
|
|
|
value, it will be returned directly, otherwise a [[Result]] object will
|
|
|
|
be returned with each parameter available positionally and if the parameter
|
|
|
|
is named, it will also be available by its name.
|
2020-02-25 14:57:11 -05:00
|
|
|
|
|
|
|
For values that have a simple meaning in JavaScript, the types are fairly
|
2022-02-03 15:34:40 -05:00
|
|
|
straightforward; strings and booleans are returned as JavaScript strings
|
2020-02-25 14:57:11 -05:00
|
|
|
and booleans.
|
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
For numbers, if the **type** is in the JavaScript safe range (i.e. less
|
2020-02-25 14:57:11 -05:00
|
|
|
than 53 bits, such as an ``int24`` or ``uint48``) a normal JavaScript
|
2020-05-08 03:24:40 -04:00
|
|
|
number is used. Otherwise a [[BigNumber]] is returned.
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
For bytes (both fixed length and dynamic), a [[DataHexString]] is returned.
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2021-04-02 03:55:53 -04:00
|
|
|
If the call reverts (or runs out of gas), a [CALL_EXCEPTION](errors--call-exception)
|
|
|
|
will be thrown which will include:
|
|
|
|
|
|
|
|
- ``error.address`` - the contract address
|
|
|
|
- ``error.args`` - the arguments passed into the method
|
|
|
|
- ``error.transaction`` - the transaction
|
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
The //overrides// object for a read-only method may include any of:
|
|
|
|
|
|
|
|
- ``overrides.from`` - the ``msg.sender`` (or ``CALLER``) to use during the
|
|
|
|
execution of the code
|
|
|
|
- ``overrides.value`` - the ``msg.value`` (or ``CALLVALUE``) to use during the
|
2021-07-01 23:32:53 -04:00
|
|
|
execution of the code
|
2021-02-04 17:29:21 -05:00
|
|
|
- ``overrides.gasPrice`` - the price to pay per gas (theoretically); since there
|
|
|
|
is no transaction, there is not going to be any fee charged, but the EVM still
|
|
|
|
requires a value to report to ``tx.gasprice`` (or ``GASPRICE``);
|
|
|
|
//most developers will not require this//
|
|
|
|
- ``overrides.gasLimit`` - the amount of gas (theoretically) to allow a node
|
|
|
|
to use during the execution of the code; since there is no transaction, there
|
|
|
|
is not going to be any fee charged, but the EVM still processes gas metering
|
|
|
|
so calls like ``gasleft`` (or ``GAS``) report meaningful values
|
|
|
|
- ``overrides.blockTag`` - a block tag to simulate the execution at, which
|
|
|
|
can be used for hypothetical historic analysis; note that many backends
|
|
|
|
do not support this, or may require paid plans to access as the node database
|
|
|
|
storage and processing requirements are much higher
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_property: contract.functions.METHOD_NAME(...args [, overrides ]) => Promise<[[Result]]>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
The result will always be a [[Result]], even if there is only a single
|
|
|
|
return value type.
|
|
|
|
|
|
|
|
This simplifies frameworks which wish to use the [[Contract]] object,
|
|
|
|
since they do not need to inspect the return types to unwrap simplified
|
|
|
|
functions.
|
|
|
|
|
|
|
|
Another use for this method is for error recovery. For example, if a
|
|
|
|
function result is an invalid UTF-8 string, the normal call using the
|
|
|
|
above meta-class function will throw an exception. This allows using the
|
|
|
|
Result access error to access the low-level bytes and reason for the error
|
|
|
|
allowing an alternate UTF-8 error strategy to be used.
|
|
|
|
|
|
|
|
Most developers should not require this.
|
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
The //overrides// are identical to the read-only operations above.
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_heading: Write Methods (non-constant) @<Contract--write>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
|
|
|
A non-constant method requires a transaction to be signed and requires
|
|
|
|
payment in the form of a fee to be paid to a miner. This transaction
|
|
|
|
will be verified by every node on the entire network as well by the
|
|
|
|
miner who will compute the new state of the blockchain after executing
|
|
|
|
it against the current state.
|
|
|
|
|
|
|
|
It cannot return a result. If a result is required, it should be logged
|
|
|
|
using a Solidity event (or EVM log), which can then be queried from the
|
|
|
|
transaction receipt.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.METHOD_NAME(...args [ , overrides ]) => Promise<[[providers-TransactionResponse]]> @<contract-functionsSend>
|
|
|
|
Returns a [[providers-TransactionResponse]] for the transaction after
|
2020-02-25 14:57:11 -05:00
|
|
|
it is sent to the network. This requires the **Contract** has a
|
|
|
|
signer.
|
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
The //overrides// object for write methods may include any of:
|
|
|
|
|
|
|
|
- ``overrides.gasPrice`` - the price to pay per gas
|
|
|
|
- ``overrides.gasLimit`` - the limit on the amount of gas to allow the transaction
|
|
|
|
to consume; any unused gas is returned at the gasPrice
|
|
|
|
- ``overrides.value`` - the amount of ether (in wei) to forward with the call
|
|
|
|
- ``overrides.nonce`` - the nonce to use for the [[Signer]]
|
|
|
|
|
2021-02-08 14:52:31 -05:00
|
|
|
If the ``wait()`` method on the returned [[providers-TransactionResponse]]
|
|
|
|
is called, there will be additional properties on the receipt:
|
|
|
|
|
|
|
|
- ``receipt.events`` - an array of the logs, with additional properties
|
|
|
|
(if the ABI included a description for the events)
|
|
|
|
- ``receipt.events[n].args`` - the parsed arguments
|
|
|
|
- ``receipt.events[n].decode`` - a method that can be used to parse the
|
|
|
|
log topics and data (this was used to compute ``args``)
|
|
|
|
- ``receipt.events[n].event`` - the name of the event
|
|
|
|
- ``receipt.events[n].eventSignature`` - the full signature of the event
|
|
|
|
- ``receipt.removeListener()`` - a method to remove the listener that trigger
|
|
|
|
this event
|
|
|
|
- ``receipt.getBlock()`` - a method to return the [Block](providers-Block) this event occurred in
|
|
|
|
- ``receipt.getTransaction()`` - a method to return the
|
|
|
|
[Transaction](providers-TransactionResponse) this event occurred in
|
|
|
|
- ``receipt.getTransactionReceipt()`` - a method to return the
|
|
|
|
[Transaction Receipt](providers-TransactionReceipt) this event occurred in
|
|
|
|
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_heading: Write Methods Analysis @<Contract--check>
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
There are several options to analyze properties and results of a
|
2020-02-25 14:57:11 -05:00
|
|
|
write method without actually executing it.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.estimateGas.METHOD_NAME(...args [ , overrides ]) => Promise<[[BigNumber]]> @<contract-estimateGas>
|
2020-02-25 14:57:11 -05:00
|
|
|
Returns the estimate units of gas that would be required to
|
|
|
|
execute the //METHOD_NAME// with //args// and //overrides//.
|
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
The //overrides// are identical to the overrides above for read-only
|
|
|
|
or write methods, depending on the type of call of //METHOD_NAME//.
|
|
|
|
|
2020-05-08 03:24:40 -04:00
|
|
|
_property: contract.populateTransaction.METHOD_NAME(...args [ , overrides ]) => Promise<[UnsignedTx](UnsignedTransaction)> @<contract-populateTransaction>
|
|
|
|
Returns an [[UnsignedTransaction]] which represents the transaction
|
2020-02-25 14:57:11 -05:00
|
|
|
that would need to be signed and submitted to the network to execute
|
2020-04-16 22:25:05 -04:00
|
|
|
//METHOD_NAME// with //args// and //overrides//.
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
The //overrides// are identical to the overrides above for read-only
|
|
|
|
or write methods, depending on the type of call of //METHOD_NAME//.
|
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
_property: contract.callStatic.METHOD_NAME(...args [ , overrides ]) => Promise<any> @<contract-callStatic>
|
2020-02-25 14:57:11 -05:00
|
|
|
Rather than executing the state-change of a transaction, it is possible
|
|
|
|
to ask a node to //pretend// that a call is not state-changing and
|
|
|
|
return the result.
|
|
|
|
|
2020-07-03 01:44:17 -04:00
|
|
|
This does not actually change any state, but is free. This in some cases
|
2020-02-25 14:57:11 -05:00
|
|
|
can be used to determine if a transaction will fail or succeed.
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
This otherwise functions the same as a [Read-Only Method](Contract--readonly).
|
2020-02-25 14:57:11 -05:00
|
|
|
|
2021-02-04 17:29:21 -05:00
|
|
|
The //overrides// are identical to the read-only operations above.
|
|
|
|
|
2020-06-12 03:38:55 -04:00
|
|
|
_heading: Event Filters @<Contract--filters>
|
2020-02-25 14:57:11 -05:00
|
|
|
An event filter is made up of topics, which are values logged in a
|
|
|
|
[[link-wiki-bloomfilter]], allowing efficient searching for entries
|
|
|
|
which match a filter.
|
|
|
|
|
|
|
|
_property: contract.filters.EVENT_NAME(...args) => Filter
|
|
|
|
Return a filter for //EVENT_NAME//, optionally filtering by additional
|
|
|
|
constraints.
|
|
|
|
|
|
|
|
Only ``indexed`` event parameters may be filtered. If a parameter is
|
|
|
|
null (or not provided) then any value in that field matches.
|