Skip to content

sirano11/caver-js-ext-kas

 
 

Repository files navigation

caver-js-ext-kas

caver-js-ext-kas is caver-js's extension library for using KAS (Klaytn API Service).

Table of contents

Build/Install

To try it out, install caver-js-ext-kas with npm like following command:

$ npm install caver-js-ext-kas

Note "package.json" file should exist on the same install path. If it does not exist, "package.json" should be generated via npm init.

To install a specific version of caver-js, try the following command:

$ npm install caver-js-ext-kas@X.X.X

You can build it with the command below.

$ npm run build

The result of the build is located in "dist/" and is named "caver-js-ext-kas.min.js".

Test

This library contains tests. Create .env file to define environment variables before performing the test like below.

SENDER_PRV_KEY_JS=''
ACCESS_KEY=''
SECRET_ACCESS_KEY=''
PRESET=
FEE_PAYER_ADDR=''
OPERATOR=''

You just need to fill in the values in the format above. After creating the .env file you can run the test using the command below.

$ npm run test

Getting Started

Set Auth

You can use KAS' Node API, Token History API, Wallet API, Anchor API and KIP17 API through this library. To use KAS, you need your "access key id", "secret access key" and chain id of the Klaytn blochain network.

Set your authorization using the constructor or the caver.initKASAPI function as shown below. Below code sets the authentication key used by the node api, token history api, wallet api, anchor api and kip17 api at once.

// Set an authorization through constructor
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)
// Set an authorization through 'caver.initKASAPI' function
const caver = new CaverExtKAS()
caver.initKASAPI(chainId, accessKeyId, secretAccessKey)

The following describes how to set auth key for each node, tokenHistory, wallet, anchor and kip17 api.

Each initialization function is provided so that you can pass an optional endpoint url as the last parameter. If the endpoint url is not passed as the last parameter, the KAS production url is set by default.

caver.initNodeAPI(chainId, accessKeyId, secretAccessKey [, url])
caver.initTokenHistoryAPI(chainId, accessKeyId, secretAccessKey [, url])
caver.initWalletAPI(chainId, accessKeyId, secretAccessKey [, url])
caver.initAnchorAPI(chainId, accessKeyId, secretAccessKey [, url])
caver.initKIP17API(chainId, accessKeyId, secretAccessKey [, url])

caver.wallet in CaverExtKAS is a KASWallet that internally connects the KAS Wallet API since caver-js-ext-kas v1.0.2.

If you want to use the in-memory wallet provided by the caver-js as it is, create and use an instance of KeyringContainer as shown below.

const caver = new CaverExtKAS()

// Create a KeyringContainer instance
const keyringContainer = new caver.keyringContainer()

// Create a keyring from private key
const keyring = keyringContainer.keyring.createFromPrivateKey('0x{private key}')

// Add a keyring to the keyringContainer
keyringContainer.add(keyring)

// Sign with the keyring added to keyringContainer
await keyringContainer.sign(keyring.address, transaction)

Use Node API

You can now use KAS's Node API through caver-js-ext-kas. You can send a Node API request to the KAS as shown below and check the results.

const blockNumber = await caver.rpc.klay.getBlockNumber()
console.log(blockNumber)

Use Token History API

You can now use KAS's Token History API through caver-js-ext-kas. You can send a Token History API request to the KAS as shown below and check the results.

const ftContracts = await caver.kas.tokenHistory.getFTContractList()
console.log(ftContracts)

The query options used in the token history api can be used as follows.

const queryOptions = new caver.kas.tokenHistory.queryOptions({ kind, range, size ... })
console.log(queryOptions)

Enum used in query option used in token history api is as follows.

caver.kas.tokenHistory.queryOptions.kind // KLAY, FT, NFT
caver.kas.tokenHistory.queryOptions.status // COMPLETED, PROCESSING, FAILED, CANCELLED
caver.kas.tokenHistory.queryOptions.type // KIP7, KIP17, ERC20, ERC721

Use Wallet API

You can now use KAS's Wallet API through caver-js-ext-kas. You can send a Wallet API request to the KAS as shown below and check the results.

const account = await caver.kas.wallet.createAccount()
console.log(account)

The query options used in the wallet api can be used as follows.

const queryOptions = new caver.kas.wallet.queryOptions({ size, fromTimestamp, toTimestamp, ... })
console.log(queryOptions)

Use Anchor API

You can now use KAS's Anchor API through caver-js-ext-kas. You can send a Anchor API request to the KAS as shown below and check the results.

const operators = await caver.kas.anchor.getOperatorList()
console.log(operators)

The query options used in the anchor api can be used as follows.

const queryOptions = new caver.kas.anchor.queryOptions({ size, fromTimestamp, toTimestamp, ... })
console.log(queryOptions)

Use KIP17 API

You can now use KAS's KIP17 API through caver-js-ext-kas. You can send a KIP17 API request to the KAS as shown below and check the results.

const contracts = await caver.kas.kip17.getContractList()
console.log(contracts)

The query options used in the kip17 api can be used as follows.

const queryOptions = new caver.kas.kip17.queryOptions({ size, cursor })
console.log(queryOptions)

Use KIP7, KIP17 and Contract with a Klaytn account in KAS Wallet API

You can use caver's Contract, KIP7 and KIP17 as it is by using the account stored in KAS.

Since CaverExtKAS wallet is a KASWallet that connects with and operates with KAS Wallet API, Contract, KIP7 and KIP17 can be used the same as the existing caver-js.

Here, we introduce a simple example using Contract, KIP7, and KIP17 respectively. Please refer to Contract, KIP7 and KIP17 of Klaytn Docs for detailed usage.

The example below introduces how to use caver.contract.

// Deploy contract
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

const abi = [{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}, ...]

const contract = new caver.contract(abi)

const deployed = await contract.deploy({
	data:  '0x60806...',
}).send({
	from: '0x{from address}', // An account corresponding to the address must exist in KAS.
	gas: '0x4bfd200',
	value: '0x0',
})
// Execute contract
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

const abi = [{"constant":true,"inputs":[],"name":"count","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}, ...]

const contract = new caver.contract(abi, '0x{contract address}')

const receipt = await contract.methods.set('k', 'v').send({ from: '0x{from address}', gas:'0x4bfd200' }) // An account corresponding to the address must exist in KAS.

The example below introduces how to use caver.kct.kip7.

// Deploy KIP-7
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

const kip7 = await caver.kct.kip7.deploy({
    name: 'Jasmine',
    symbol: 'JAS',
    decimals: 18,
    initialSupply: '100000000000000000000',
}, '0x{from address}') // An account corresponding to the address must exist in KAS.
// Execute KIP-7 contract
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

const kip7 = new caver.kct.kip7('0x{contract address}')
const receipt = await kip7.transfer('0x{to address}', 1, { from: '0x{from address}' })

The example below introduces how to use caver.kct.kip17.

// Deploy KIP-17
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

const kip7 = await caver.kct.kip17.deploy({
    name: 'Jasmine',
    symbol: 'JAS',
}, '0x{from address}') // An account corresponding to the address must exist in KAS.
// Execute KIP-17 contract
const caver = new CaverExtKAS(chainId, accessKeyId, secretAccessKey)

const kip17 = new caver.kct.kip17('0x{contract address}')
const receipt = await kip17.transferFrom('0x{from address}', '0x{to address}', tokenId, { from: '0x{from address}' })

Other Docs

KAS Docs caver-java-ext-kas API Reference

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 99.8%
  • Other 0.2%