Last updated
Last updated
The concept of Meta-Classes is somewhat confusing, so we will go over a short example.
A meta-class is a class which is defined at run-time. A Contract is specified by an Application Binary Interface (ABI), which describes the methods and events it has. This description is passed the the object at run-time, and it creates a new Class, adding all the methods defined in the ABI at run-time.
Most often, any contract you will need to interact with will already be deployed, but for this example will will first deploy the contract.
new hethers.Contract( address , abi , providerOrSigner )
Creating a new instance of a Contract connects to an existing contract by specifying its address, and its abi (used to populate the class' methods) a providerOrSigner.
This is the address, the contract was constructed with.
If the Contract object is the result of a ContractFactory deployment, this is the transaction which was used to deploy the contract.
If a signer was provided to the constructor, this is that signer.
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.
Returns a new instance of the Contract, but connected to providerOrSigner.
Contract.isIndexed( value ) ⇒ boolean
See Meta-Class Filters for examples using events.
erc20.queryFilter( event [ ,
fromTimestamp [ , toTimestamp ] )
⇒ Promise< Array< Event > >Return Events that match the event.
erc20.listenerCount( [ event ] )
⇒ numberReturn the number of listeners that are subscribed to event. If no event is provided, returns the total count of all events.
erc20.listeners( event )
⇒ Array< Listener >Return a list of listeners that are subscribed to event.
erc20.off( event , listener )
⇒ thisUnsubscribe listener to event.
erc20.on( event , listener )
⇒ thisSubscribe to event calling listener when the event occurs.
erc20.once( event , listener )
⇒ thisSubscribe once to event calling listener when the event occurs.
erc20.removeAllListeners( [ event ] )
⇒ thisUnsubscribe all listeners for event. If no event is provided, all events are unsubscribed.
Since the Contract is a Meta-Class, the methods available here depend on the ABI which was passed into the Contract.
erc20.decimals( [ overrides ] )
⇒ Promise< number >Keep in mind that a signer must be connected in order to do Static calls
Returns the balance of owner for this ERC-20 token.
Keep in mind that a signer must be connected in order to do Static calls
erc20.symbol( [ overrides ] )
⇒ Promise< string >Returns the symbol of the token.
Keep in mind that a signer must be connected in order to do Static calls
Transfers amount tokens to target from the current signer. The return value (a boolean) is inaccessible during a write operation using a transaction. Other techniques (such as events) are required if this value is required. On-chain contracts calling the transfer
function have access to this result, which is why it is possible.
erc20.callStatic.transfer( target , amount [ , overrides ] )
⇒ Promise< boolean >Performs a dry-run of transferring amount tokens to target from the current signer, without actually signing or sending a transaction.
This can be used to preflight check that a transaction will be successful.
Returns an UnsignedTransaction which could be signed and submitted to the network to transaction amount tokens to target.
Since the Contract is a Meta-Class, the methods available here depend on the ABI which was passed into the Contract.
Returns a new Filter that can be used to query or to subscribe/unsubscribe to events.
If fromAddress is null or not provided, then any from address matches. If toAddress is null or not provided, then any to address matches.
This is the ABI as an .
If a provider was provided to the constructor, this is that provider. If a signer was provided that had a , this is that provider.
By passing in a , this will return a downgraded Contract which only has read-only access (i.e. querying filters).
By passing in a . this will return a Contract which will act on behalf of that signer.
Returns the number of decimal places used by this ERC-20 token. This can be used with when taking input from the user or when displaying the token amounts in the UI.