This page introduces KAS SDK(caver-js-ext-kas/caver-java-ext-kas) section.
KAS SDK (Software Development Kit) is a development tool provided to easily use KAS in various development environments. KAS SDK is an extension library of Caver and supports JavaScript and Java environment like Caver. If you develop BApp with KAS SDK, you can use both KAS function and existing Caver function through a single library.
To use KAS SDK (caver-java extension) you need Java 8 or higher version to be pre-installed.
For inquires about this document or KAS, please visit Developer Forum.
You can find the latest KAS SDK (caver-java extension) in Release Page.
For inquires about this document or KAS, please visit Developer Forum.
Hello World
KAS SDK (caver-js extension)
In order to use KAS API with KAS SDK (caver-js extension), you need to set up "chain id" and "authentication key". You can initialize the setting to use KAS API service as shown below:
Passing chainId, accessKeyId and secretAccessKey to the constructor will internally call the caver.initKASAPI function, which initializes the authentication keys used in Node API, Wallet API, Token History API, Anchor API, KIP-17 and KIP-7. The initialization method for each KAS API service is as follows. You can optionally pass endpoint URL as the Endpoint URL. If a separate URL is not passed, the KAS Production URL will be set as default as shown below.
You can fetch the latest block information with KAS' Node API when using KAS SDK (caver-js extension) as shown below:
For the caver.initNodeAPI function, you can assign a provider for calling KAS Node API using the parameter useHttp. For more details, please refer to Calling KAS Node API using WebSocket Provider.
You can retrieve NFT contract list with KAS TokenHistory API when using KAS SDK (caver-js extension).
constCaverExtKAS=require('caver-js-ext-kas')constcaver=newCaverExtKAS(chainId, accessKeyId, secretAccessKey)// Query parameters are optional, so `caver.kas.tokenHistory.getNFTContractList` can be executed without query parameters.
constquery= { size:1 }constnftContractList=awaitcaver.kas.tokenHistory.getNFTContractList(query)console.log(nftContractList)
Executing the above code will print the list of NFT contracts as shown below:
In order to use KAS API with KAS SDK (caver-java extension), you need to set up "chain id" and "authentication key". You can initialize the setting to use KAS API service as shown below:
Using the above caver.initKASAPI function initializes the authentication keys used in Node API, Wallet API, Token History API, and Anchor API at the same time. The initialization method for each KAS API service is shown below. You can optionally pass the Endpoint URL as the last parameter. If a separate URL is not passed, the KAS Production URL described below will be set as the default.
When the above code is executed, the result of transmitting the anchoring data is displayed as below:
classAnchorBlockStatus { status: succeed}
Here is how you can deploy KIP-17 contracts with KAS KIP-17 API when using KAS SDK (caver-java extension).
String name ="My First KIP-17";String symbol ="MFK";String alias ="my-first-kip17";Kip17TransactionStatusResponse res =caver.kas.kip17.deploy(name, symbol, alias);System.out.println(res);
When the above codes are executed, the results of the KIP-17 DeployContract API will be displayed:
Using Smart Contracts with a Klaytn Account in KAS Wallet API
From KAS SDK v1.0.2, KASWallet, a wallet that operates using KAS Wallet API, is provided in caver. With this, you can use the caver.contract package and caver.kct using the Klaytn account of the KAS Wallet API.
Using caver.contract with a Klaytn Account in KAS Wallet API
caver.contract uses caver.wallet to deploy a smart contract to Klaytn and execute pre-deployed contracts. Since the caver.wallet of KAS SDK is a wallet that uses the KAS Wallet API, you can easily deploy or execute a smart contract on Klaytn by using the caver.contract in KAS SDK with the Klaytn account in KAS Wallet API.
Using caver.contract with a Klaytn account in KAS Wallet API is the same as the method to use the existing caver.contract of Caver. This section describes a simple example of deploying and executing a smart contract. Here, the Klaytn account used to deploy and run smart contracts is the Klaytn account managed by the KAS Wallet API, and this Klaytn account must own enough KLAY to send transactions.
KAS SDK(caver-js extension)
How to deploy a smart contract using caver-js-ext-kas is as follows.
// caver-js-ext-kasconstCaverExtKAS=require('caver-js-ext-kas')constchainId=1001constaccessKeyId='{access key ID of your KAS account}'constsecretAccessKey='{secret access key of your KAS account}'constcaver=newCaverExtKAS(chainId, accessKeyId, secretAccessKey)constabi= [ { constant:true, inputs: [{ name:'key', type:'string' }], name:'get', outputs: [{ name:'', type:'string' }], payable:false, stateMutability:'view', type:'function', }, { constant:false, inputs: [{ name:'key', type:'string' }, { name:'value', type:'string' }], name:'set', outputs: [], payable:false, stateMutability:'nonpayable', type:'function', },]constbyteCode= '0x608060405234801561001057600080fd5b5061051f806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063693ec85e1461003b578063e942b5161461016f575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102c1565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610134578082015181840152602081019050610119565b50505050905090810190601f1680156101615780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6102bf6004803603604081101561018557600080fd5b81019080803590602001906401000000008111156101a257600080fd5b8201836020820111156101b457600080fd5b803590602001918460018302840111640100000000831117156101d657600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192908035906020019064010000000081111561023957600080fd5b82018360208201111561024b57600080fd5b8035906020019184600183028401116401000000008311171561026d57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506103cc565b005b60606000826040518082805190602001908083835b602083106102f957805182526020820191506020810190506020830392506102d6565b6001836020036101000a03801982511681845116808217855250505050505090500191505090815260200160405180910390208054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c05780601f10610395576101008083540402835291602001916103c0565b820191906000526020600020905b8154815290600101906020018083116103a357829003601f168201915b50505050509050919050565b806000836040518082805190602001908083835b6020831061040357805182526020820191506020810190506020830392506103e0565b6001836020036101000a0380198251168184511680821785525050505050509050019150509081526020016040518091039020908051906020019061044992919061044e565b505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061048f57805160ff19168380011785556104bd565b828001600101855582156104bd579182015b828111156104bc5782518255916020019190600101906104a1565b5b5090506104ca91906104ce565b5090565b6104f091905b808211156104ec5760008160009055506001016104d4565b5090565b9056fea165627a7a723058203ffebc792829e0434ecc495da1b53d24399cd7fff506a4fd03589861843e14990029'
const contract =newcaver.contract(abi)// The from account that sends the SmartContractDeploy transaction must own enough KLAY.const deployed = await contract.deploy({ data: byteCode }).send({ from: '0x{the address of a Klaytn account in KAS Wallet API}', gas: 10000000 })
console.log(`Deployed contract address: ${deployed.options.address}`)
When the above code is executed, the address of the deployed smart contract is displayed as shown below.
The following is how to execute the smart contract deployed on Klaytn using caver-js-ext-kas.
// caver-js-ext-kasconstCaverExtKAS=require('caver-js-ext-kas')constchainId=1001constaccessKeyId='{access key ID of your KAS account}'constsecretAccessKey='{secret access key of your KAS account}'constcaver=newCaverExtKAS(chainId, accessKeyId, secretAccessKey)constabi= [ { constant:true, inputs: [{ name:'key', type:'string' }], name:'get', outputs: [{ name:'', type:'string' }], payable:false, stateMutability:'view', type:'function', }, { constant:false, inputs: [{ name:'key', type:'string' }, { name:'value', type:'string' }], name:'set', outputs: [], payable:false, stateMutability:'nonpayable', type:'function', },]constcontractAddress='0x5D3Cd9eB73f00BbECB949DCE08BB26019FcB599f'constcontract=newcaver.contract(abi, contractAddress)// The from account that sends the SmartContractExecution transaction must own enough KLAY.const receipt = await contract.methods.set('key', 'value').send({ from: '0x{the address of a Klaytn account in KAS Wallet API}', gas: 5000000 })
console.log(receipt)
When the above code is executed, the receipt of the SmartContractExecution transaction is output as shown below.
For more information about caver.contract of Caver, refer to caver-js or caver-java.
Using caver.kct.kip7 with a Klaytn account in KAS Wallet API
caver.kct.kip7 provides the functions to deploy KIP-7 token contracts to Klaytn using caver.wallet and execute pre-deployed KIP-7 token contracts. Since caver.wallet of KAS SDK is a wallet that uses KAS Wallet API, if you use caver.kct.kip7 of KAS SDK, you can easily deploy and execute KIP-7 token contract on Klaytn with a Klaytn account in KAS Wallet API.
The method to use caver.kct.kip7 using the Klaytn account in KAS Wallet API is the same as the method of using the existing caver.kct.kip7 of Caver. This section describes a simple example of deploying and executing a KIP-7 token contract. Here, the Klaytn account used to deploy and run smart contracts is the Klaytn account managed by the KAS Wallet API, and this Klaytn account must own enough KLAY to send transactions.
KAS SDK(caver-js extension)
How to deploy a KIP-7 token contract using caver-js-ext-kas is as follows.
// caver-js-ext-kasconstCaverExtKAS=require('caver-js-ext-kas')constcaver=newCaverExtKAS()constaccessKeyId='{access key ID of your KAS account}'constsecretAccessKey='{secret access key of your KAS account}'caver.initKASAPI(chainId, accessKeyId, secretAccessKey)constdeployed=awaitcaver.kct.kip7.deploy({ name:'Jasmine', symbol:'JAS', decimals:18, initialSupply:'100000000000000000000'},'0x{the address of a Klaytn account in KAS Wallet API}')console.log(`Deployed contract address: ${deployed.options.address}`)
When the above code is executed, the address of the deployed KIP-7 token contract is displayed as shown below.
The following is how to execute the KIP-7 token contract deployed on Klaytn using caver-js-ext-kas.
// caver-js-ext-kasconstCaverExtKAS=require('caver-js-ext-kas')constchainId=1001constaccessKeyId='{access key ID of your KAS account}'constsecretAccessKey='{secret access key of your KAS account}'constcaver=newCaverExtKAS(chainId, accessKeyId, secretAccessKey)constcontractAddress='0x42c3809EeED7c5C497067fE4092D1c354D3a01Cb'constkip7=newcaver.kct.kip7(contractAddress)const receipt = await kip7.transfer('0x{address in hex}', 1, { from: '0x{the address of a Klaytn account in KAS Wallet API}' })
console.log(receipt)
When the above code is executed, the receipt of the SmartContractExecution transaction is output as shown below.
For more information about caver.kct.kip7 of Caver, refer to caver-js or caver-java.
Using caver.kct.kip17 with a Klaytn Account in KAS Wallet API
caver.kct.kip17 provides the functions for deploying KIP-17 token contracts to Klaytn using caver.wallet and executing pre-deployed KIP-17 contracts. Since caver.wallet for KAS SDK is a wallet that uses KAS Wallet API, you can easily deploy and execute KIP-17 contracts on Klaytn with a Klaytn account in KAS Wallet API using caver.kct.kip17 in the KAS SDK.
Using caver.kct.kip17 with a Klaytn Account in KAS Wallet API is the same as using the existing caver.kct.kip17 in Caver. This section gives a simple example of how to deploy and execute a KIP-17 contract. Here, the Klaytn account used to deploy and run smart contracts is the Klaytn account managed by the KAS Wallet API, and this Klaytn account balance must own enough KLAY to send transactions.
KAS SDK provides KAS KIP-17 API. To use KAS KIP-17 API, please refer to Tutorial or API Reference Docs(JS, JAVA).
KAS SDK(caver-js extension)
How to deploy a KIP-17 token contract using caver-js-ext-kas is as follows.
// caver-js-ext-kasconstCaverExtKAS=require('caver-js-ext-kas')constchainId=1001constaccessKeyId='{access key ID of your KAS account}'constsecretAccessKey='{secret access key of your KAS account}'constcaver=newCaverExtKAS(chainId, accessKeyId, secretAccessKey)constdeployed=awaitcaver.kct.kip17.deploy({ name:'Jasmine', symbol:'JAS',},'0x{the address of a Klaytn account in KAS Wallet API}')console.log(`Deployed contract address: ${deployed.options.address}`)
When the above code is executed, the address of the deployed KIP-17 token contract is displayed as below:
The following is how to execute the KIP-17 token contract deployed on Klaytn using caver-js-ext-kas.
// caver-js-ext-kas
const CaverExtKAS = require('caver-js-ext-kas')
const chainId = 1001
const accessKeyId = '{access key ID of your KAS account}'
const secretAccessKey = '{secret access key of your KAS account}'
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)
const contractAddress = '0xAD719B194457D0641B410Ce75C4D22442533A781'
const kip17 = new caver.kct.kip17(contractAddress)
const receipt = await kip17.mintWithTokenURI('0x{address in hex}', tokenId, tokenURI, { from: '0x{the address of a Klaytn account in KAS Wallet API}' })
console.log(receipt)
When the above code is executed, the receipt of the SmartContractExecution transaction is output as shown below.
For more information about caver.kct.kip17 of Caver, refer to caver-js or caver-java.
Using KeyringContainer in KAS SDK
In KAS SDK, KASWallet which uses KAS Wallet API is provided by caver.wallet instead of KeyringContainer, an in-memory wallet provided by caver.wallet of existing caver. So, if you want to use the in-memory wallet KeyringContainer in KAS SDK, you need to instantiate it separately.
caver-js-ext-kas and caver-java-ext-kas both provide setWallet function in caver.contract and caver.kct to specify the wallet to use when deploying or executing smart contracts. Through this, you can flexibly specify and use the wallet to be used when deploying or executing smart contracts for each instance.
KAS SDK(caver-js extension)
First, how to create a KeyringContainer instance using caver-js-ext-kas is as follows.
const keyringContainer = new caver.keyringContainer()
How to use the keyringContainer created above with caver.contract is as follows.
The deployed contract instance returned by executing the above code is also an object that uses same keyringContainer. If you are using a contract that has already been deployed, you can specify to use the keyringContiainer using the setWallet function as shown below.
const keyringContainer = new caver.keyringContainer()
const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')
keyringContainer.add(keyring)
// Using keyringContainer when executing smart contracts
const contract = new caver.contract(abi, contractAddress)
contract.setWallet(keyringContainer)
const receipt = await contract.methods.set('key', 'value').send({ from: keyring.address, gas: 5000000 })
How to use the separately created keyringContainer in caver.kct is as follows. For static functions that deploy a KIP-7 or KIP-17 token contract called deploy, you can pass the wallet to be used as the last parameter.
How to deploy KIP-7 token contract using keyringContainer with caver.kct.kip7 is as follows.
const keyringContainer = new caver.keyringContainer()
const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')
keyringContainer.add(keyring)
// Passing keyringContainer as the last parameter creates an instance internally and calls setWallet
const kip7 = await caver.kct.kip7.deploy({
name: 'Jasmine',
symbol: 'JAS',
decimals: 18,
initialSupply: '100000000000000000000'
}, keyring.address, keyringContainer)
const receipt = await kip7.transfer('0x{address in hex}', 1, { from: keyring.address })
Like deploy of caver.contract, the instance returned as a result of deployment is an object using same keyringContainer.
To use the keyringContainer when executing the already deployed KIP-7 token contract, you need to call the setWallet function as shown below.
const keyringContainer = new caver.keyringContainer()
const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')
keyringContainer.add(keyring)
const kip7 = new caver.kct.kip7(contractAddress)
kip7.setWallet(keyringContainer)
const receipt = await kip7.transfer('0x{address in hex}', 1, { from: keyring.address })
How to deploy KIP-17 token contract using keyringContainer with caver.kct.kip17 is as follows.
const keyringContainer = new caver.keyringContainer()
const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')
keyringContainer.add(keyring)
// Passing keyringContainer as the last parameter creates an instance internally and calls setWallet
const kip17 = await caver.kct.kip17.deploy({
name: 'Jasmine',
symbol: 'JAS',
}, keyring.address, keyringContainer)
const receipt = await kip17.transfer('0x{address in hex}', 1, { from: keyring.address })
Like deploy of caver.contract, the instance returned as a result of deployment is an object using same keyringContainer.
To use the keyringContainer when executing the already deployed KIP-7 token contract, you need to call the setWallet function as shown below.
const keyringContainer = new caver.keyringContainer()
const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')
keyringContainer.add(keyring)
const kip17 = new caver.kct.kip17(contractAddress)
kip17.setWallet(keyringContainer)
const receipt = await kip17.mintWithTokenURI('0x{address in hex}', tokenId, tokenURI, { from: keyring.address })
KAS SDK(caver-java extension)
First, how to create a KeyringContainer instance using caver-java-ext-kas is as follows.
KeyringContainer container = new KeyringContainer();
How to use the keyringContainer created above with caver.contract is as follows.
The deployed contract instance returned by executing the above code is also an object that uses same keyringContainer. If you are using a contract that has already been deployed, you can specify to use the keyringContiainer using the setWallet function as shown below.
How to use the separately created keyringContainer in caver.kct is as follows. For static functions that deploy a KIP-7 or KIP-17 token contract called deploy, you can pass the wallet to be used as the last parameter.
How to deploy KIP-7 token contract using keyringContainer with caver.kct.kip7 is as follows.
Like deploy of caver.contract, the instance returned as a result of deployment is an object using same keyringContainer.
To use the keyringContainer when executing the already deployed KIP-17 token contract, you need to call the setWallet function as shown below.
KeyringContainer container = new KeyringContainer();
SingleKeyring executor_keyring = (SingleKeyring)container.add(KeyringFactory.createFromPrivateKey("0x{private key}"));
String contractAddress = "0xAD719B194457D0641B410Ce75C4D22442533A781";
KIP17 kip17 = new KIP17(caver, contractAddress);
kip17.setWallet(container);
String from = "0x{from address}";
String to = "0x{to address}";
String tokenURI = "tokenURI";
SendOptions sendOptions = new SendOptions(from);
TransactionReceipt.TransactionReceiptData receiptData = kip17.mintWithTokenURI(to, BigInteger.ZERO, tokenURI, sendOptions);
caver-java KASWalletException
An HTTP error of KAS Wallet API that occurs while executing KASWallet's method is stored in a KASAPIException instance, and thus a KASAPIException instance is thrown on error. KASAPIException is an exception class that extends RuntimeException, an unchecked exception, so you don't always need to catch it. However, if you need to check the details of the HTTP error that occurred while executing the KAS Wallet API, it is recommended to check the details of the KASAPIException using the try-catch statement.
The example below catches the KASAPIException instance storing the HTTP error response occured while exeuting KASWallet's getAccount() method.
Calling Klaytn Node API of KAS using WebSocket Provider
You can call the KAS Klaytn Node API using WebSocket Provider. To use WebSocket Provider, you have to specify the provider that you will be using for calling Node API during the initialization.
KAS SDK(caver-js extension)
First, here is how you can initialize WebSocket Provider when calling the CaverExtKAS contructor.
As the past parameter of the contructor, you can pass the object that can define the options required for initialization. The useNodeAPIWithHttp field of the options object can define the provider that is used for calling the Node API. The default value is true, using Http Provider. To use WebSocket Provider, pass useNodeAPIWithHttp defined as false. When you are using caver.initKASAPI instead of constructor, you can also pass the object for which the useNodeAPIWithHttp field is defined as the last parameter as shown below:
If you are calling a different initializing function for each API, you can use caver.initNodeAPI or caver.initNodeAPIWithWebSocket to initialize the Node API.
Using the caver.initNodeAPIWithWebSocket function will allow you to initialize, so that you can use WebSocket Provider without defining additional parameters.
For more information about caver-js, refer to here.
For more information about caver-java, refer to here.
For inquires about this document or KAS, please visit Developer Forum.