Klaytn
Published in

Klaytn

How to use Tatum-SDK on Klaytn (Part 1)

Introduction

  • How to generate a wallet
  • Send KLAY to another wallet
  • Check wallet balance
  • Read blockchain info
  • Integrate with Web3-js

Prerequisites

Getting Started

Setup Environment

mkdir tatum-example
cd tatum-example
npm init -y

npm install @tatumio/klaytn
mkdir scripts
cd scripts
touch index.js

Initialize Tatum SDK

  1. Open your index.js file in a code editor and input the following to initialize the SDK
import  TatumKlaytnSDK  from '@tatumio/klaytn'
const klaytnSDK = TatumKlaytnSDK( apiKey: ‘’ )

Using Tatum SDK on Klaytn

1. How to Generate a Wallet

A). Creating a blockchain wallet:

async function generateWallet() 

const mnemonic, xpub = await klaytnSDK.wallet.generateWallet()

console.log(`Created a wallet with: " $mnemonic " and public key: " $xpub ".`)



generateWallet()
node scripts/index.js

B.) Generating an address from the wallet:

async function generateAddressFromXpub(xpub) {
const klaytnAddress = await klaytnSDK.wallet.generateAddressFromXPub(xpub, 1)

console.log(`Created a wallet address: " $klaytnAddress " from the given xpub`)

Const xpub = “”

generateAddressFromXpub(xpub)

C.) Generating a private key from the wallet:

async function generatePrivatekeyFromMnemonics(mnemonics) 
const klaytnPrivatekey = await klaytnSDK.wallet.generatePrivateKeyFromMnemonic(mnemonics, 1)
console.log(`Created a private key: " $klaytnPrivatekey " from the given mnemonic`)


Const mnemonics = “”

generatePrivatekeyFromMnemonics(mnemonics)
node scripts/index.js
generate private key from mnemonic
import  TatumKlaytnSDK  from '@tatumio/klaytn'

const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )


async function generateWallet()
const mnemonic, xpub = await klaytnSDK.wallet.generateWallet()
console.log(`Created a wallet with: " $mnemonic " and public key: " $xpub ".`)


async function generateAddressFromXpub(xpub)
const klaytnAddress = await klaytnSDK.wallet.generateAddressFromXPub(xpub, 1)
console.log(`Created a wallet address: " $klaytnAddress " from the given xpub`)


async function generatePrivatekeyFromMnemonics(mnemonics)
const klaytnPrivatekey = await klaytnSDK.wallet.generatePrivateKeyFromMnemonic(mnemonics, 1)
console.log(`Created a private key: " $klaytnPrivatekey " from the given mnemonic`)



const xpub= "";
const mnemonic = "";


generateWallet()
generateAddressFromXpub(xpub);
generatePrivatekeyFromMnemonics(mnemonic);

2. Check Balance of Address

async function checkBalance (address) 
const addressBalance = await klaytnSDK.blockchain.getBlockchainAccountBalance(address)
console.log(`This address " $address " has a balance of " $addressBalance.balance " `)


const address = "";

checkBalance(address)
node scripts/index.js

3. Query Data from the Blockchain

  • GetGasInfo
  • GetTransaction
  • GetBlock
  • GetCurrentBlock
  • GetTransactionCount
node scripts/read.js

GetGasInfo

async function getGasInfo () 
// Get gasLimit and gasPrice for a transaction
const gasInfo = await klaytnSDK.blockchain.estimateGas(
from: '0xfb99f8ae9b70a0c8cd96ae665bbaf85a7e01a2ef',
to: '0x687422eEA2cB73B5d3e242bA5456b782919AFc85',
amount: '100000',
)
console.log('Gas estimate: ', gasInfo)



getGasInfo()

GetTransaction

async function getTxInfo () 
// Get transaction details by hash
const transaction = await klaytnSDK.blockchain.getTransaction(
'0x9a4c09556204735c2818eaeed9fd0a530dba4f1ad6f771c7451a217ce373268d',
)
console.log('Transaction: ', transaction)


getTxInfo()

GetBlock

async function getBlock () 
// Get block by hash
const block = await klaytnSDK.blockchain.getBlock(
'0x9089a6d4d24ddebfec27f9db7527bd6bf2ba0628efa1ea67084d91aeaf43d7b5',
)
console.log('Block: ', block)


getBlock()

GetCurrentBlock

async function getCurrentBlock () 
// Get curent block
const currentBlock = await klaytnSDK.blockchain.getCurrentBlock()
console.log('Current block: ', currentBlock)


getCurrentBlock()

GetTransactionCount

async function getTransactionsCount () 
// Get transaction count of an address
const transactionsCount = await klaytnSDK.blockchain.getTransactionsCount(
'0x99abc3bbd2f001ce629bc4de078ba8f5e80f4af9',
)
console.log('Transactions count: ', transactionsCount)


getTransactionsCount()
import  TatumKlaytnSDK  from '@tatumio/klaytn'

const klaytnSDK = TatumKlaytnSDK( apiKey: ‘’ )

async function getGasInfo ()
// Get gasLimit and gasPrice for a transaction
const gasInfo = await klaytnSDK.blockchain.estimateGas(
from: '0xfb99f8ae9b70a0c8cd96ae665bbaf85a7e01a2ef',
to: '0x687422eEA2cB73B5d3e242bA5456b782919AFc85',
amount: '100000',
)
console.log('Gas estimate: ', gasInfo)

async function getTxInfo ()
// Get transaction details by hash
const transaction = await klaytnSDK.blockchain.getTransaction(
'0x9a4c09556204735c2818eaeed9fd0a530dba4f1ad6f771c7451a217ce373268d',
)
console.log('Transaction: ', transaction)


async function getBlock ()
// Get block by hash
const block = await klaytnSDK.blockchain.getBlock(
'0x9089a6d4d24ddebfec27f9db7527bd6bf2ba0628efa1ea67084d91aeaf43d7b5',
)
console.log('Block: ', block)

async function getCurrentBlock ()
// Get current block
const currentBlock = await klaytnSDK.blockchain.getCurrentBlock()
console.log('Current block: ', currentBlock)

async function getTransactionsCount ()
// Get transaction count of an address
const transactionsCount = await klaytnSDK.blockchain.getTransactionsCount(
'0x99abc3bbd2f001ce629bc4de078ba8f5e80f4af9',
)
console.log('Transactions count: ', transactionsCount)



getGasInfo()
getTxInfo()
getBlock()
getCurrentBlock()
getTransactionsCount()

4. Send Native Transaction

import  TatumKlaytnSDK  from '@tatumio/klaytn'

const klaytnSDK = TatumKlaytnSDK( apiKey: ‘’ )

async function sendNativeTx ()
const senderPrivateKey = ''

const receiverAddress = ''

// send native transaction using private key
const txId = await klaytnSDK.transaction.send.transferSignedTransaction(
to: receiverAddress,
amount: '0.0001',
fromPrivateKey: senderPrivateKey,
)

console.log(`Transaction using private key was sent txID =`, txId)

// Note: you can also send native transaction using signatureId
// signatureId from Tatum KMS - https://docs.tatum.io/private-key-management/tatum-key-management-system-kms



sendNativeTx()

5. Use Web3-js with Tatum

import  TatumKlaytnSDK  from '@tatumio/klaytn'

const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

const web3response = await klaytnSDK.httpDriver(
jsonrpc: '2.0',
method: 'web3_clientVersion',
params: [],
id: 2,
)

const web3 = klaytnSDK.web3Client()
async function getBlockNumberWithWeb3 () 
// gets the current block number
const blockNumber = await web3.eth.getBlockNumber()
console.log(blockNumber);


getBlockNumberWithWeb3()
node scripts/web3.js
async function getBalanceWithWeb3 () 
// Returns the KLAY balance of address in web
const myAddress = '' //TODO: replace this address with your own public address
const receipt = await web3.eth.getBalance(myAddress);
console.log(receipt);


getBalanceWithWeb3()
async function sendTransactionWithWeb3 () 
// sends transaction to the Klaytn blockchain
const privateKey = "";
const senderAddress = "";
const receiverAddress = "";

const tx = await web3.eth.accounts.signTransaction(
from: senderAddress,
to: receiverAddress,
value: 90000000000,
gasPrice: 250000000000,
gas: 21000,
, privateKey)

const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction)
console.log(receipt);


sendTransactionWithWeb3()
import  TatumKlaytnSDK  from '@tatumio/klaytn'

const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

const web3response = await klaytnSDK.httpDriver(
jsonrpc: '2.0',
method: 'web3_clientVersion',
params: [],
id: 2,
)

const web3 = klaytnSDK.web3Client()

async function getBlockNumberWithWeb3 ()
const blockNumber = await web3.eth.getBlockNumber()
console.log(blockNumber);



async function getBalanceWithWeb3 ()
const myAddress = '' //TODO: replace this address with your own public address
const receipt = await web3.eth.getBalance(myAddress);
console.log(receipt);


async function sendTransactionWithWeb3 ()
const privateKey = "";
const senderAddress = "";
const receiverAddress = "";

const tx = await web3.eth.accounts.signTransaction(
from: senderAddress,
to: receiverAddress,
value: 90000000000,
gasPrice: 250000000000,
gas: 21000,
, privateKey)

const receipt = await web3.eth.sendSignedTransaction(tx.rawTransaction)
console.log(receipt);



getBlockNumberWithWeb3();
getBalanceWithWeb3();
sendTransactionWithWeb3();

Conclusion

--

--

The metaverse blockchain for all

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store