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 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:
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 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
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 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
Name
Type
Description
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
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., myContract.options.address
).
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
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 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
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 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
Name
Type
Description
options
object
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
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
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