caver.contract
The caver.contract
object makes it easy to interact with smart contracts on the Klaytn blockchain platform. When you create a new contract object, you have to provide the JSON interface for that smart contract and caver-js will automatically convert all calls with the contract object in javascript into low-level ABI calls over RPC for you.
This allows you to interact with smart contracts as if they were JavaScript objects.
caver.contract.create
Creates a new contract instance with all its methods and events defined in its JSON interface object. This function works the same as new caver.contract.
NOTE caver.contract.create
is supported since caver-js v1.6.1.
Parameters
See the new caver.contract.
Return Value
See the new caver.contract.
Example
caver.contract
Creates a new contract instance with all its methods and events defined in its JSON interface object.
Parameters
jsonInterface
object
The JSON interface for the contract to instantiate
address
string
(optional) The address of the smart contract to call. Can be added later using myContract.options.address = '0x1234..'
options
object
(optional) The options of the contract. See the table below for the details.
The options object contains the following:
from
string
(optional) The address from which transactions should be made.
gasPrice
string
(optional) The gas price in peb to use for transactions.
gas
number
(optional) The maximum gas provided for a transaction (gas limit).
data
string
(optional) The byte code of the contract. Used when the contract gets deployed.
feeDelegation
boolean
(optional) Whether to use fee delegation transaction.
feePayer
string
(optional) The address of the fee payer paying the transaction fee. When feeDelegation
is true
, the value is set to the feePayer
field in the transaction.
feeRatio
string
(optional) The ratio of the transaction fee the fee payer will be burdened with. If feeDelegation
is true
and feeRatio
is set to a valid value, a partial fee delegation transaction is used. The valid range of this is between 1 and 99. The ratio of 0, or 100 and above are not allowed.
Return Value
object
The contract instance with all its methods and events.
Example
myContract.options
The options
object for the contract instance. from
, gas
, gasPrice
, feePayer
and feeRatio
are used as fallback values when sending transactions.
Properties
address
string
The address where the contract is deployed.
jsonInterface
Array
The JSON interface of the contract.
from
string
The default address from which the contract deployment/execution transaction is sent. If the from
address is not defined when creating the transaction, this myContract.options.from
is always used to create the transaction.
gasPrice
string
The gas price in peb to use for transactions.
gas
number
The maximum gas provided for a transaction (gas limit).
data
string
The byte code of the contract. Used when the contract gets deployed.
feeDelegation
boolean
(optional) Whether to use fee delegation transaction.
feePayer
string
(optional) The address of the fee payer paying the transaction fee. When feeDelegation
is true
, the value is set to the feePayer
field in the transaction.
feeRatio
string
(optional) The ratio of the transaction fee the fee payer will be burdened with. If feeDelegation
is true
and feeRatio
is set to a valid value, a partial fee delegation transaction is used. The valid range of this is between 1 and 99. The ratio of 0, or 100 and above are not allowed.
NOTE feeDelegation
, feePayer
and feeRatio
are supported since caver-js v1.6.1.
Example
myContract.options.address
The address used for this contract instance myContract
. All transactions generated by caver-js from this contract will contain this address as the to
of the transaction.
Property
address
string | null
The address for this contract or null
if it is not yet set.
Example
myContract.options.jsonInterface
The JSON interface object derived from the ABI of this contract myContract
.
Property
jsonInterface
Array
The JSON interface for this contract. Re-setting this will regenerate the methods and events of the contract instance.
Example
myContract.clone
Clones the current contract instance.
Parameters
contractAddress
String
(optional) The address of the new contract. If omitted, it will be set to the address in the original instance (e.g., myContract.options.address
).
Return Value
object
The new cloned contract instance.
Example
myContract.deploy
Deploys the contract to the Klaytn network. After a successful deployment, the promise will be resolved with a new contract instance. Unlike the usability of the existing myContract.deploy function, this function sends a transaction directly to the Klaytn network. You don't need to call send()
with the returned object.
NOTE caver.wallet
must contains keyring instances corresponding to from
and feePayer
in options
or myContract.options
to make signatures.
NOTE myContract.deploy
is supported since caver-js v1.6.1.
Parameters
options
object
The options used for sending. See the table in methods.methodName.send for the details.
byteCode
string
The byte code of the contract.
parameters
Mixed
(optional) The parameters that get passed to the constructor on deployment.
Return Value
Promise
returning PromiEvent
: The promise will be resolved with the new contract instance.
PromiEvent
A promise combined event emitter. It will be resolved when the transaction receipt is available. If send()
is called from a myContract.deploy()
, then the promise will be resolved with the new contract instance.
For PromiEvent, the following events are available:
transactionHash
: it is fired right after the transaction is sent and a transaction hash is available. Its type isstring
.receipt
: It is fired when the transaction receipt is available. See caver.rpc.klay.getTransactionReceipt for more details. Its type isobject
.error
: It is fired if an error occurs during sending. On an out-of-gas error, the second parameter is the receipt. Its type isError
.
Example
myContract.deploy
Returns the object used when deploying the smart contract to the Klaytn. You can send the smart contract deploy transaction via calling myContract.deploy({ data, arguments }).send(options)
. After a successful deployment, the promise will be resolved with a new contract instance.
Parameters
options
object
The options object used for deployment. See the below table to find the description.
The options object can contain the following:
data
string
The byte code of the contract.
arguments
Array
(optional) The arguments that get passed to the constructor on deployment.
Return Value
object
An object in which arguments and functions for contract distribution are defined. See the below table to find the description.
The object contains the following:
arguments
Array
The arguments passed in options.arguments
.
function
The function that will deploy the contract to the Klaytn. The promise as the result of this function will be resolved with the new contract instance.
function
The function that will sign a smart contract deploy transaction as a sender. The sign function will return signed transaction.
function
The function that will sign a smart contract deploy transaction as a fee payer. The signAsFeePayer function will return signed transaction.
function
The function that will estimate the gas used for the deployment. The execution of this function does not deploy the contract.
function
The function that encodes the ABI of the deployment, which is contract data + constructor parameters. The execution of this function does not deploy the contract.
NOTE myContract.deploy({ data, arguments }).sign(options)
and myContract.deploy({ data, arguments }).signAsFeePayer(options)
are supported since caver-js v1.6.1.
Example
myContract.send
Submits a transaction to execute the function of the smart contract. This can alter the smart contract state.
The transaction type used for this function depends on the options
or the value defined in myContract.options
. If you want to use a fee-delegated transaction through myContract.send
, feeDelegation
and feePayer
should be set properly.
feeDelegation
is not defined or defined tofalse
: SmartContractExecutionfeeDelegation
is defined totrue
, butfeePayer
is not defined : Throws an error.feeDelegation
is defined totrue
andfeePayer
is defined, butfeeRatio
is not defined: FeeDelegatedSmartContractExecutionfeeDelegation
is defined totrue
andfeePayer
andfeeRatio
are defined: FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
must contains keyring instances corresponding to from
and feePayer
in options
or myContract.options
to make signatures.
NOTE myContract.send
is supported since caver-js v1.6.1.
Parameters
options
object
The options used for sending. See the table in methods.methodName.send for the details.
methodName
string
The method name of the contract function to execute.
parameters
Mixed
(optional) The parameters that get passed to the smart contract function.
Return Value
Promise
returns PromiEvent
PromiEvent
A promise combined event emitter. It will be resolved when the transaction receipt is available. The promise will be resolved with the new contract instance.
For PromiEvent, the following events are available:
transactionHash
: It is fired right after the transaction is sent and a transaction hash is available. Its type isstring
.receipt
: It is fired when the transaction receipt is available. See caver.rpc.klay.getTransactionReceipt for more details. Its type isobject
.error
: It is fired if an error occurs during sending. On an out-of-gas error, the second parameter is the receipt. Its type isError
.
Example
myContract.sign
Signs a smart contract transaction as a sender to deploy the smart contract or execute the function of the smart contract.
If a smart contract is deployed, 'constructor' can be entered in the methodName, such as myContract.sign({ from, ... }, 'constructor', byteCode, ...)
.
The transaction type used for this function depends on the options
or the value defined in myContract.options
. If you want to use a fee-delegated transaction through myContract.sign
, feeDelegation
should be defined as true
.
feeDelegation
is not defined or defined tofalse
: SmartContractDeploy / SmartContractExecutionfeeDelegation
is defined totrue
, butfeeRatio
is not defined: FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionfeeDelegation
is defined totrue
andfeeRatio
is defined: FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
must contains keyring instances corresponding to from
in options
or myContract.options
to make signatures.
NOTE myContract.sign
is supported since caver-js v1.6.1.
Parameters
options
object
The options used for sending. See the table in methods.methodName.send for the details.
methodName
string
The method name of the contract function to execute. If you want to sign a transaction for deploying the smart contract, use 'constructor' string instead of method name.
parameters
Mixed
(optional) The parameters that get passed to the smart contract function. If you want to sign a smart contract deploy transaction, pass the byteCode and constructor parameters.
Return Value
Promise
returning Transaction - The signed smart contract transaction.
Example
myContract.signAsFeePayer
Signs a smart contract transaction as a fee payer to deploy the smart contract or execute the function of the smart contract.
If a smart contract is deployed, 'constructor' can be entered in the methodName, such as myContract.signAsFeePayer({ from, feeDelegation: true, feePayer, ... }, 'constructor', byteCode, ...)
.
The transaction type used for this function depends on the options
or the value defined in myContract.options
. The signAsFeePayer
is a function that signs as a transaction fee payer, so feeDelegation
field must be defined as true
. Also, the address of the fee payer must be defined in the feePayer
field.
feeDelegation
is not defined : Throws an error.feeDelegation
is defined, butfeePayer
is not defined : Throws an error.feeDelegation
is defined totrue
andfeePayer
is defined, butfeeRatio
is not defined: FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionfeeDelegation
is defined totrue
andfeePayer
andfeeRatio
are defined: FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
must contains keyring instances corresponding to feePayer
in options
or myContract.options
to make signatures.
NOTE myContract.signAsFeePayer
is supported since caver-js v1.6.1.
Parameters
options
object
The options used for sending. See the table in methods.methodName.send for the details.
methodName
string
The method name of the contract function to execute. If you want to sign a transaction for deploying the smart contract, use 'constructor' string instead of method name.
parameters
Mixed
(optional) The parameters that get passed to the smart contract function. If you want to sign a smart contract deploy transaction, pass the byteCode and constructor parameters.
Return Value
Promise
returning Transaction - The signed smart contract transaction.
Example
myContract.call
Will call a constant method and execute its smart contract method in the Klaytn Virtual Machine without sending any transaction. Note that calling cannot alter the smart contract state.
NOTE myContract.call
is supported since caver-js v1.6.1.
Parameters
options
object
(optional) The options used for calling. See the table in methods.methodName.call for the details.
methodName
string
The method name of the contract function to call.
parameters
Mixed
(optional) The parameters that get passed to the smart contract function.
Return Value
Promise
returning Mixed
- The return value(s) of the smart contract method. If it returns a single value, it is returned as it is. If it has multiple return values, it returns an object with properties and indices.
Example
myContract.decodeFunctionCall
Decodes a function call and returns parameters.
NOTE myContract.decodeFunctionCall
is supported since caver-js v1.6.3.
Parameters
functionCall
string
The encoded function call string.
Return Value
object
An object which includes plain params. You can use result[0]
as it is provided to be accessed like an array in the order of the parameters.
Examples
myContract.methods
Creates a transaction object for that method, which then can be called, sent, estimated or ABI encoded.
The methods of this smart contract are available via:
Method name:
myContract.methods.methodName(123)
ormyContract.methods[methodName](123)
Method prototype:
myContract.methods['methodName(uint256)'](123)
Method signature:
myContract.methods['0x58cf5f10'](123)
This allows calling functions with the same name but different parameters from the JavaScript contract object.
cf) *function signature (function selector)
The first four bytes of the call data for a function call specifies the function to be called. It is the first (left, high-order in big-endian) four bytes of the Keccak-256 (SHA-3) hash of the signature of the function.
The function signature can be given via 2 different methods.
1. caver.abi.encodefunctionSignature('funcName(paramType1,paramType2,...)')
2. caver.utils.sha3('funcName(paramType1,paramType2,...)').substr(0, 10)
ex)
Parameters
Parameters of any method that belongs to this smart contract, defined in the JSON interface.
Return Value
Promise
returning object
- An object in which arguments and functions for contract execution are defined.:
arguments
Array
The arguments passed to this method.
function
The function that will call and execute a constant method in its smart contract on Klaytn Virtual Machine without sending a transaction (cannot alter the smart contract state).
function
The function that will send a transaction to the Klaytn and execute its method (can alter the smart contract state).
function
The function that will sign a transaction as a sender. The sign function will return signed transaction.
function
The function that will sign a transaction as a fee payer. The signAsFeePayer function will return signed transaction.
function
The function that encodes the ABI for this method. This can be sent using a transaction, calling the method, or passing into another smart contract method as its argument.
NOTE sign
and signAsFeePayer
are supported since caver-js v1.6.1.
Example
methods.methodName.call
Will call a constant method and execute its smart contract method in the Klaytn Virtual Machine without sending any transaction. Note that calling cannot alter the smart contract state. It is recommended to use myContract.call provided as a short-cut function.
Parameters
options
object
(optional) The options used for calling. See the table below for the details.
callback
function
(optional) This callback will be fired with the result of the smart contract method execution as the second argument, or with an error object as the first argument.
The options object can contain the following:
from
string
(optional) The address which calling contract methods should be made from.
gasPrice
string
(optional) The gas price in peb to use for this call.
gas
number
(optional) The maximum gas provided for this call (gas limit).
Return Value
Promise
returning Mixed
- The return value(s) of the smart contract method. If it returns a single value, it is returned as it is. If it has multiple return values, it returns an object with properties and indices.
Example
methods.methodName.send
Will send a transaction to deploy the smart contract or execute the function of the smart contract. This can alter the smart contract state. It is recommended to use myContract.send provided as a short-cut function.
If a smart contract is deployed, 'constructor' can be entered in the methodName, such as myContract.methods.constructor
or myContract.methods['constructor']
, but it is recommended to use the myContract.deploy function.
The transaction type used for this function depends on the options
or the value defined in myContract.options
. If you want to use a fee-delegated transaction through methods.methodName.send
, feeDelegation
and feePayer
should be set properly.
feeDelegation
is not defined or defined tofalse
: SmartContractDeploy / SmartContractExecutionfeeDelegation
is defined totrue
, butfeePayer
is not defined : Throws an error.feeDelegation
is defined totrue
andfeePayer
is defined, butfeeRatio
is not defined: FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionfeeDelegation
is defined totrue
andfeePayer
andfeeRatio
are defined: FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
must contains keyring instances corresponding to from
and feePayer
in options
or myContract.options
to make signatures.
Parameters
options
object
The options used for sending. See the table below for the details.
callback
function
(optional) This callback will be fired first with the "transactionHash", or with an error object as the first argument.
The options object can contain the following:
from
string
The address from which the transaction should be sent. If omitted, myContract.options.from
will be used.
gas
number
The maximum gas provided for this transaction (gas limit).
gasPrice
string
(optional) The gas price in peb to use for this transaction.
value
number | string | BN | Bignumber
(optional) The value in peb to be transferred to the address of the smart contract by this transaction.
feeDelegation
boolean
(optional, default false
) Whether to use fee delegation transaction. If omitted, myContract.options.feeDelegation
will be used.
feePayer
string
(optional) The address of the fee payer paying the transaction fee. When feeDelegation
is true
, the value is set to the feePayer
field in the transaction. If omitted, myContract.options.feePayer
will be used.
feeRatio
string
(optional) The ratio of the transaction fee the fee payer will be burdened with. If feeDelegation
is true
and feeRatio
is set to a valid value, a partial fee delegation transaction is used. The valid range of this is between 1 and 99. The ratio of 0, or 100 and above are not allowed. If omitted, myContract.options.feeRatio
will be used.
NOTE feeDelegation
, feePayer
and feeRatio
are supported since caver-js v1.6.1.
Return Value
Promise
returns PromiEvent
PromiEvent
A promise combined event emitter. It will be resolved when the transaction receipt is available. The promise will be resolved with the new contract instance.
For PromiEvent, the following events are available:
transactionHash
: It is fired right after the transaction is sent and a transaction hash is available. Its type isstring
.receipt
: It is fired when the transaction receipt is available. See caver.rpc.klay.getTransactionReceipt for more details. Its type isobject
.error
: It is fired if an error occurs during sending. On an out-of-gas error, the second parameter is the receipt. Its type isError
.
Example
methods.methodName.sign
Signs a smart contract transaction as a sender to deploy the smart contract or execute the function of the smart contract. It is recommended to use myContract.sign provided as a short-cut function.
If a smart contract is deployed, 'constructor' can be entered in the methodName, such as myContract.methods.constructor
or myContract.methods['constructor']
.
The transaction type used for this function depends on the options
or the value defined in myContract.options
. If you want to use a fee-delegated transaction through methods.methodName.sign
, feeDelegation
should be defined as true
.
feeDelegation
is not defined or defined tofalse
: SmartContractDeploy / SmartContractExecutionfeeDelegation
is defined totrue
, butfeeRatio
is not defined: FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionfeeDelegation
is defined totrue
andfeeRatio
is defined: FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
must contains keyring instances corresponding to from
in options
or myContract.options
to make signatures.
NOTE methods.methodName.sign
is supported since caver-js v1.6.1.
Parameters
options
object
The options used for creating a transaction. See the parameter table in methods.methodName.send for the details.
Return Value
Promise
returning Transaction - The signed smart contract transaction.
Example
methods.methodName.signAsFeePayer
Signs a smart contract transaction as a fee payer to deploy the smart contract or execute the function of the smart contract. It is recommended to use myContract.signAsFeePayer provided as a short-cut function.
If a smart contract is deployed, 'constructor' can be entered in the methodName, such as myContract.methods.constructor
or myContract.methods['constructor']
.
The transaction type used for this function depends on the options
or the value defined in myContract.options
. The signAsFeePayer
is a function that signs as a transaction fee payer, so feeDelegation
field must be defined as true
. Also, the address of the fee payer must be defined in the feePayer
field.
feeDelegation
is not defined : Throws an error.feeDelegation
is defined, butfeePayer
is not defined : Throws an error.feeDelegation
is defined totrue
andfeePayer
is defined, butfeeRatio
is not defined: FeeDelegatedSmartContractDeploy / FeeDelegatedSmartContractExecutionfeeDelegation
is defined totrue
andfeePayer
andfeeRatio
are defined: FeeDelegatedSmartContractDeployWithRatio / FeeDelegatedSmartContractExecutionWithRatio
NOTE caver.wallet
must contains keyring instances corresponding to feePayer
in options
or myContract.options
to make signatures.
NOTE methods.methodName.signAsFeePayer
is supported since caver-js v1.6.1.
Parameters
options
object
The options used for creating a transaction. See the parameter table in methods.methodName.send for the details.
Return Value
Promise
returning Transaction - The signed smart contract transaction.
Example
methods.methodName.estimateGas
Will estimate the gas that a method execution will take when executed in the Klaytn Virtual Machine. The estimation can differ from the actual gas used when later sending a transaction, as the state of the smart contract can be different at that time.
Parameters
options
object
(optional) The options used for calling. See the table below for the details.
callback
function
(optional) This callback will be fired with the result of the gas estimation as the second argument, or with an error object as the first argument.
The options object can contain the following:
from
string
(optional) The address from which calling the contract method should be made.
gas
number
(optional) The maximum gas provided for this call (gas limit). Setting a specific value helps to detect out of gas errors. If all gas is used, it will return the same number.
value
number | string | BN | Bignumber
(optional) The value in peb that would be transferred to the address of the smart contract if the transaction for executing this contract function was sent to Klaytn.
Return Value
Promise
returns number
number
The used gas for the simulated call/transaction.
Example
methods.methodName.encodeABI
Encodes the ABI for this method. This can be used to send a transaction or call a method, or pass it into another smart contract method as arguments.
Parameters
Parameters of any method that belongs to this smart contract, defined in the JSON interface.
Return Value
string
The encoded ABI byte code to send via a transaction or call.
Example
myContract.once
Subscribes to an event and unsubscribes immediately after the first event or error. Will only fire for a single event.
Parameters
event
string
The name of the event in the contract, or allEvents
to get all events.
options
object
(optional) The options used for subscription. See the table below for the details.
callback
function
This callback will be fired for the first event as the second argument, or an error as the first argument. See myContract.getPastEvents for details about the event structure.
The options object can contain the following:
filter
object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {mynumber: [12,13]}}
means all events where "mynumber" is 12 or 13.
topics
Array
(optional) This allows you to manually set the topics for the event filter. Given the filter property and event signature, topic[0]
would not be set automatically.
Return Value
Promise
returns object
- An event object. For more detail about event object, please refer to myContract.getPastEvents.
Example
myContract.subscribe
Subscribes to an event. This function works same as myContract.events.eventName.
You can unsubscribe an event by calling the unsubscribe
function of the subscription object returned by the subscribe
function.
NOTE myContract.subscribe
is supported since caver-js v1.9.1-rc.1.
Parameters
event
string
The name of the event in the contract, or allEvents
to get all events.
options
object
(optional) The options used for subscription. See the table below for the details.
callback
function
This callback will be fired for the first event as the second argument, or an error as the first argument. See myContract.getPastEvents for details about the event structure.
The options object can contain the following:
filter
object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {mynumber: [12,13]}}
means all events where "mynumber" is 12 or 13.
topics
Array
(optional) This allows you to manually set the topics for the event filter. Given the filter property and event signature, topic[0]
would not be set automatically.
Return Value
Promise
returns object
- An event object. For more detail about event object, please refer to myContract.getPastEvents.
Example
myContract.events
Subscribes to an event.
Parameters
options
object
(optional) The options used for subscription. See the table below for the details.
callback
function
(optional) This callback will be fired for each event as the second argument, or an error as the first argument.
The options object can contain the following:
filter
object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {mynumber: [12,13]}}
means all events where "mynumber" is 12 or 13.
fromBlock
number
(optional) The block number from which to get events.
topics
Array
(optional) This allows you to manually set the topics for the event filter. Given the filter property and event signature, topic[0]
would not be set automatically.
Return Value
EventEmitter
: The event emitter has the following events:
data
object
Fires on each incoming event with the event object as an argument.
connected
string
Fires once after the subscription successfully connected. It returns the subscription ID.
error
object
Fires when an error in the subscription occurs.
NOTE connected
is available with caver-js v1.5.7.
The structure of the returned event object
looks as follows:
event
string
The event name.
signature
string | null
The event signature, null
if it is an anonymous event.
address
string
Address which from this event originated.
returnValues
object
The return values coming from the event, e.g., {myVar: 1, myVar2: '0x234...'}
.
logIndex
number
Integer of the event index position in the block.
transactionIndex
number
Integer of the transaction's index position where the event was created.
transactionHash
32-byte string
Hash of the transaction this event was created in. null
when it is still pending.
blockHash
32-byte string
Hash of the block this event was created in. null
when it is still pending.
blocknumber
number
The block number this log was created in. null
when still pending.
raw.data
string
The data containing non-indexed log parameter.
raw.topics
Array
An array with a maximum of four 32-byte topics, and topic 1-3 contains indexed parameters of the event.
id
string
A log identifier. It is made through concatenating "log_" string with keccak256(blockHash + transactionHash + logIndex).substr(0, 8)
Example
events.allEvents
Same as myContract.events but receives all events from this smart contract. Optionally, the filter property can filter those events.
getPastEvents
Gets past events for this contract.
Parameters
event
string
The name of the event in the contract, or "allEvents"
to get all events.
options
object
(optional) The options used for subscription. See the table below for the details.
callback
function
(optional) This callback will be fired with an array of event logs as the second argument, or an error as the first argument.
To options object can contain the following:
filter
object
(optional) Lets you filter events by indexed parameters, e.g., {filter: {mynumber: [12,13]}}
means all events where "mynumber" is 12 or 13.
fromBlock
number
(optional) The block number from which to get events.
toBlock
number
(optional) The block number to get events up to (defaults to "latest"
).
topics
Array
(optional) This allows manually setting the topics for the event filter. Given the filter property and event signature, topic[0]
would not be set automatically.
Return Value
Promise
returns Array
- An array with the past event objects, matching the given event name and filter.
An event object can contain the following:
event
string
The event name.
signature
string | null
The event signature, null
if it’s an anonymous event.
address
string
Address this event originated from.
returnValues
object
The return values coming from the event, e.g. {myVar: 1, myVar2: '0x234...'}.
logIndex
number
The event index position in the block.
transactionIndex
number
The transaction’s index position the event was created in.
transactionHash
string
The hash of the transaction this event was created in.
blockHash
string
The hash of the block this event was created in. null when it’s still pending.
blockNumber
number
The block number this log was created in. null when still pending.
raw
object
An object defines data
and topic
. raw.data
containing non-indexed log parameter. raw.topic
is an array with a maximum of four 32 Byte topics, and topic 1-3 contains indexed parameters of the event.
Example
Last updated