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
Name | Type | Description |
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 |
options | object | (optional) The options of the contract. See the table below for the details. |
The options object contains the following:
Name | Type | Description |
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 |
feeRatio | string | (optional) The ratio of the transaction fee the fee payer will be burdened with. If |
Return Value
Type | Description |
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
Name | Type | Description |
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 |
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 |
feeRatio | string | (optional) The ratio of the transaction fee the fee payer will be burdened with. If |
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
Name | Type | Description |
address | string | | The address for this contract or |
Example
myContract.options.jsonInterface
The JSON interface object derived from the ABI of this contract myContract
.
Property
Name | Type | Description |
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
Name | Type | Description |
contractAddress | String | (optional) The address of the new contract. If omitted, it will be set to the address in the original instance (e.g., |
Return Value
Type | Description |
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
Name | Type | Description |
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.
Type | Description |
PromiEvent | A promise combined event emitter. It will be resolved when the transaction receipt is available. If |
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
Name | Type | Description |
options | object | The options object used for deployment. See the below table to find the description. |
The options object can contain the following:
Name | Type | Description |
data | string | The byte code of the contract. |
arguments | Array | (optional) The arguments that get passed to the constructor on deployment. |
Return Value
Type | Description |
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:
Name | Type | Description |
arguments | Array | The arguments passed in |
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
Name | Type | Description |
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
Type | Description |
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
Name | Type | Description |
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
Name | Type | Description |
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
Name | Type | Description |
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
Name | Type | Description |
functionCall | string | The encoded function call string. |
Return Value
Type | Description |
object | An object which includes plain params. You can use |
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.:
Name | Type | Description |
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 that function will estimate the gas used for the execution. | |
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