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.
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.
NOTEcaver.contract.create is supported since caver-js v1.6.1.
Creates a new contract instance with all its methods and events defined in its JSON interface object.
Parameters
The options object contains the following:
Return Value
Example
constmyContract=newcaver.contract([...],'0x{address in hex}', { gasPrice:'25000000000' })
myContract.options
myContract.options
The options object for the contract instance. from, gas, gasPrice, feePayer and feeRatio are used as fallback values when sending transactions.
Properties
NOTEfeeDelegation, feePayer and feeRatio are supported since caver-js v1.6.1.
Example
>myContract.options{ address: [Getter/Setter], jsonInterface: [Getter/Setter], from: [Getter/Setter], feePayer: [Getter/Setter], feeDelegation: [Getter/Setter], feeRatio: [Getter/Setter], gasPrice: [Getter/Setter], gas: [Getter/Setter], data: [Getter/Setter]}>myContract.options.from ='0x1234567890123456789012345678901234567891'// default from address>myContract.options.gasPrice ='25000000000000'// default gas price in peb>myContract.options.gas =5000000// provide as fallback always 5M gas>myContract.options.feeDelegation =true// use fee delegation transaction>myContract.options.feePayer ='0x1234567890123456789012345678901234567891'// default fee payer address>myContract.options.feeRatio =20// default fee ratio when send partial fee delegation transaction
myContract.options.address
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
Example
>myContract.options.address'0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae'// set a contract address>myContract.options.address ='0x1234FFDD...'
myContract.options.jsonInterface
myContract.options.jsonInterface
The JSON interface object derived from the ABI of this contract myContract.
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.
NOTEcaver.wallet must contains keyring instances corresponding to from and feePayer in options or myContract.options to make signatures.
NOTEmyContract.deploy is supported since caver-js v1.6.1.
Parameters
Return Value
Promise returning PromiEvent: 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 is string.
error: It is fired if an error occurs during sending. On an out-of-gas error, the second parameter is the receipt. Its type is Error.
Example
// Deploy a smart contract without constructor arguments>myContract.deploy({ from:'0x{address in hex}', gas:1500000, },'0x{byte code}').on('error',function(error) { ... }).on('transactionHash',function(transactionHash) { ... }).on('receipt',function(receipt) {console.log(receipt.contractAddress) // contains the new contract address }).then(function(newContractInstance) {console.log(newContractInstance.options.address) // instance with the new contract address })// Deploy a smart contract with constructor arguments>myContract.deploy({ from:'0x{address in hex}', gas:1500000, },'0x{byte code}','keyString',...).on('error',function(error) { ... }).on('transactionHash',function(transactionHash) { ... }).on('receipt',function(receipt) {console.log(receipt.contractAddress) }).then(function(newContractInstance) {console.log(newContractInstance.options.address) })// Deploy a smart contract with fee delegation transaction (TxTypeFeeDelegatedSmartContractDeploy)>myContract.deploy({ from:'0x{address in hex}', feeDelegation:true, feePayer:'0x{address in hex}', gas:1500000, },'0x{byte code}').on('error',function(error) { ... }).on('transactionHash',function(transactionHash) { ... }).on('receipt',function(receipt) {console.log(receipt.contractAddress) }).then(function(newContractInstance) {console.log(newContractInstance.options.address) })// Deploy a smart contract with partial fee delegation transaction (TxTypeFeeDelegatedSmartContractDeployWithRatio)>myContract.deploy({ from:'0x{address in hex}', feeDelegation:true, feePayer:'0x{address in hex}', feeRatio:30, gas:1500000, },'0x{byte code}').on('error',function(error) { ... }).on('transactionHash',function(transactionHash) { ... }).on('receipt',function(receipt) {console.log(receipt.contractAddress) }).then(function(newContractInstance) {console.log(newContractInstance.options.address) })
myContract.deploy
myContract.deploy(options)
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
The options object can contain the following:
Return Value
The object contains the following:
NOTEmyContract.deploy({ data, arguments }).sign(options) and myContract.deploy({ data, arguments }).signAsFeePayer(options) are supported since caver-js v1.6.1.
Example
>myContract.deploy({ data:'0x12345...', arguments: [123,'My string'] }).send({ from:'0x1234567890123456789012345678901234567891', gas:1500000, value:0, },function(error, transactionHash) { ... }).on('error',function(error) { ... }).on('transactionHash',function(transactionHash) { ... }).on('receipt',function(receipt) {console.log(receipt.contractAddress) // contains the new contract address }).then(function(newContractInstance) {console.log(newContractInstance.options.address) // instance with the new contract address })// When the data is already set as an option to the contract itself>myContract.options.data ='0x12345...'>myContract.deploy({ arguments: [123,'My string'] }).send({ from:'0x1234567890123456789012345678901234567891', gas:1500000, value:0, }).then(function(newContractInstance) {console.log(newContractInstance.options.address) // instance with the new contract address })// Simply encoding>myContract.deploy({ data:'0x12345...', arguments: [123,'My string'] }).encodeABI()'0x12345...0000012345678765432'// Gas estimation>myContract.deploy({ data:'0x12345...', arguments: [123,'My string'] }).estimateGas(function(err, gas) {console.log(gas) })
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.
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.
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, but feePayer is not defined : Throws an error.
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.
NOTEmyContract.call is supported since caver-js v1.6.1.
Parameters
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.call('methodName').then(console.log)Jasmine>myContract.call({ from:'0x{address in hex}' },'methodName',123).then(console.log)Test Result
myContract.decodeFunctionCall
myContract.decodeFunctionCall(functionCall)
Decodes a function call and returns parameters.
NOTEmyContract.decodeFunctionCall is supported since caver-js v1.6.3.
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)
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.:
NOTEsign and signAsFeePayer are supported since caver-js v1.6.1.
Example
// Calling a method>myContract.methods.methodName(123).call({ ... },function(error, result) { ... })>myContract.methods.methodName(123).call({ ... }).then((result) => { ... })// Sending basic transaction and using the promise>myContract.methods.methodName(123).send({ from:'0x{address in hex}',... }).then(function(receipt) {// receipt can also be a new contract instance, when coming from a "contract.deploy({...}).send()"