SDK

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.

KAS SDK(caver-js extension) and KAS SDK(caver-java extension) are the extended libraries ofcaver-js and caver-java respectively. You can use the existing caver-js/caver-java features, as well as the Node API, TokenHistory API, Wallet API, Anchor API, KIP-17 API and KIP-7 API of KAS.

KAS SDK Prerequisites

KAS SDK (caver-js extension)

To use KAS SDK (caver-js extension) you need the following packages.

KAS SDK (caver-js extension) works in the Node.js version below.

KAS SDK (caver-java extension)

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.

How to install KAS SDK

KAS SDK (caver-js extension)

KAS SDK (caver-js extension) can be installed using the command below.

If you want to install a specific version of KAS SDK (caver-js extension), use the command below.

KAS SDK (caver-java extension)

KAS SDK (caver-java extension) requires Java 8 or higher, and can be installed by the following environment.

maven

In maven project, you can installed Anroid KAS SDK(caver-java extension) by writing as follow.

gradle

In gradle project, you can installed Android KAS SDK(caver-java extension) by writing as follow.

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.

KAS Production URL

  • Node API: https://node-api.klaytnapi.com

  • Wallet API: https://wallet-api.klaytnapi.com

  • Anchor API: https://anchor-api.klaytnapi.com

  • Token History API: https://th-api.klaytnapi.com

  • KIP-17 API: https://kip17-api.klaytnapi.com

  • KIP-7 API: https://kip7-api.klaytnapi.com

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.

When the above codes are executed, the latest block number will be displayed as shown below:

Here's how to create an account using KAS' Wallet API using KAS SDK (caver-js extension):

When the above code is executed, the information of the account will be created in KAS as shown below.

You can retrieve NFT contract list with KAS TokenHistory API when using KAS SDK (caver-js extension).

Executing the above code will print the list of NFT contracts as shown below:

Here's how to use KAS SDK (caver-js extension) for sending anchoring data using KAS's Anchor API:

When the above codes are executed, the result of the anchoring data transmission will be displayed as shown below:

Here is how you can deploy KIP-17 contracts with KIP-17 API using KAS SDK (caver-js extension):

When the above codes are executed, the result of the KIP-17 DeployContract API will be displayed as shown below:

Below is a demonstration of how to deploy a KIP-7 contract with KAS KIP-7 API while using KAS SDK (caver-js extension).

const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

Executing the above code will return the following results of calling the KIP-7 contract:

KAS SDK (caver-java extension)

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.

KAS Production URL

  • Node API: https://node-api.klaytnapi.com

  • Wallet API: https://wallet-api.klaytnapi.com

  • Anchor API: https://anchor-api.klaytnapi.com

  • Token History API: https://th-api.klaytnapi.com

  • KIP-17 API: https://kip17-api.klaytnapi.com

The following demonstrates how to fetch the latest block information with KAS' Node API when using KAS SDK (caver-java extension):

When the above code is executed, the latest block number is displayed as shown below.

Here's how to create an account using KAS' Wallet API using KAS SDK (caver-java extension).

When the above code is executed, the information of the account created in KAS is displayed as below.

The following is how to retrieve NFT contract list using KAS' TokenHistory API using KAS SDK (caver-java extension).

Executing the above code will output the list of NFT contracts as shown below.

Here's how to use KAS SDK (caver-java extension) to send anchoring data using KAS's Anchor API:

When the above code is executed, the result of transmitting the anchoring data is displayed as below:

Here is how you can deploy KIP-17 contracts with KAS KIP-17 API when using KAS SDK (caver-java extension).

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.

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.

When the above code is executed, the receipt of the SmartContractExecution transaction is output as shown below.

KAS SDK(caver-java extension)

How to deploy a smart contract using caver-java-ext-kas is as follows.

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-java-ext-kas.

When the above code is executed, the transaction hash 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.

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.

When the above code is executed, the receipt of the SmartContractExecution transaction is output as shown below.

KAS SDK(caver-java extension)

How to deploy a KIP-7 token contract using caver-java-ext-kas is as follows.

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-java-ext-kas.

When the above code is executed, the transaction hash 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.

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.

When the above code is executed, the receipt of the SmartContractExecution transaction is output as shown below.

KAS SDK(caver-java extension)

You can deploy a KIP-17 contract using caver-java-ext-kas as shown below:

When the above code is executed, the address of the deployed KIP-17 token contract is displayed as shown below.

The following is how to execute the KIP-17 token contract deployed on Klaytn using caver-java-ext-kas.

When the above code is executed, the transaction hash 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.

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-7 token contract, you need to call the setWallet function as shown below.

How to deploy KIP-17 token contract using keyringContainer with caver.kct.kip17 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-7 token contract, you need to call the setWallet function as shown below.

KAS SDK(caver-java extension)

First, how to create a KeyringContainer instance using caver-java-ext-kas is as follows.

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-7 token contract, you need to call the setWallet function as shown below.

How to deploy KIP-17 token contract using keyringContainer with caver.kct.kip17 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.

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.

You pass the Boolean value of whether Http Provider will be used as a parameter when calling caver.initNodeAPI.

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.

Last updated

Was this helpful?