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

Klaytn
Klaytn
Published in
16 min readApr 12, 2023

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

Introduction

Tatum is a unified tool and SDK. It gives developers an easy-to-use platform for creating and integrating Web3 into their projects. Tatum offers ready-to-go, standardized token smart contracts that can be deployed to any blockchain of your choice. This can be done in one API call and doesn’t require creating and deploying your own contracts.

In this guide, you’ll explore some of Tatum SDK’s smart contract functionalities ranging from:

  • Fungible Tokens(ERC20);
  • Non-Fungible Tokens(ERC721);
  • Multi-Token(ERC1155) and;
  • Invoking existing contracts methods

Prerequisites

Getting Started

This guide is a continuation of how to use Tatum SDK on Klaytn and as such to successfully complete this tutorial you must have installed Tatum-js and configured your working directory.

Using Tatum SDK on Klaytn

This section will discuss some of the Tatum SDK’s smart contract features, including the deployment of fungible tokens (ERC20), non-fungible tokens (ERC721), and multi-tokens(ERC1155).

Additionally, you’ll discover how to use the Tatum SDK to run the mint, transfer, and burn functions on each token contract in addition to invoking an existing contract method given its address and ABI.

For more information about these features, click here

Creating an ERC20 Token

With the following step-by-step guide, you will be able to deploy your ERC20 token and invoke core functions with a few lines of code, given that the required parameters are provided.

Set-up

  • Navigate to your existing project directory
  • Enter the existing scripts folder
  • Create a new file named `erc20.js` to house the code

Initialize Tatum SDK

You must initialize the SDK with your API KEY in order to use Tatum’s built-in features in your project. Don’t have an API KEY yet, create one here.

import  TatumKlaytnSDK  from '@tatumio/klaytn'
const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

Set variables

In your newly created `erc20.js` file, copy and paste the following variables. You can either paste your wallet address and private key from any wallet Infrastructure like MetaMask or generate one using the Tatum SDK as explained in this previous article.

// This address will DEPLOY, MINT and TRANSFER ERC20 to Receiver Address
const senderAddress = ‘’

const senderPrivateKey = ‘’

// This address will RECEIVE ERC20 token and BURN it

const receiverAddress = ‘’

const receiverPrivateKey = ‘’

NOTE

Working with private keys

The code to be used in this section is only for testing and demo purposes

Your private keys and mnemonics, however, should never leave your security perimeter when used in production. We advise using Tatum CLI from the command line or complex key management system, Tatum KMS, to correctly and securely construct wallets and operate with private keys.

A.) Deploying an ERC-20 Smart Contract

To deploy a pre-built ERC20 contract on Klaytn, you need to paste this snippet of code into your `erc20.js` file. The response contains a transaction ID, from which you can obtain the address of the smart contract you have deployed.

async function erc20Deploy () 
// deploys erc20 (fungible token) transaction

const erc20Deployed = await klaytnSDK.erc20.send.deploySignedTransaction(

symbol: 'MTK',

name: 'MyToken',

address: senderAddress,

supply: '10000',

fromPrivateKey: senderPrivateKey,

digits: 18,

)

console.log(`Here is the transaction id $erc20Deployed.txId`);

// This timer is used to ensure the transaction above is confirmed and added to the block

// In a real application, the wait mechanism must be implemented properly without using this.

setTimeout(() =>

getContractAddress(erc20Deployed.txId)

, 10000);



async function getContractAddress(erc20Deployed)

// fetch deployed contract address from transaction hash

const transaction = await klaytnSDK.blockchain.smartContractGetAddress("KLAY", erc20Deployed)

console.log(`Here is the contract address $transaction.contractAddress`);



erc20Deploy()

Run the command below, to see the outcome of the transaction.

node scripts/erc20.js

B.) Minting tokens to an Address

Newly created fungible tokens can be minted to a specified blockchain address. To mint new tokens, you need to provide the contract address of the ERC20 tokens deployed. You can use the contract address derived from the example above.

The response contains a transaction ID and the balance of the `receiver`. Paste this snippet of code below in your `erc20.js` file to see it in action

async function erc20Minted(contractAddress) 
// mint erc20(fungible token) to a specified address

const erc20Mint = await klaytnSDK.erc20.send.mintSignedTransaction(

to: receiverAddress,

contractAddress: contractAddress,

amount: '1',

fromPrivateKey: senderPrivateKey,

)

console.log(`Minted erc20 token/s with txID $erc20Mint.txId`)

// This timer is used to ensure the transaction above is confirmed and added to the block

// In a real application, the wait mechanism must be implemented properly without using this.

setTimeout(() =>

getErc20Balance(receiverAddress, contractAddress)

, 10000);



async function getErc20Balance(address, contractAddress)

// gets the number of the fungible tokens minted on a specific smart contract that a blockchain address holds.

const erc20Balance = await klaytnSDK.erc20.getErc20AccountBalance("KLAY", address, contractAddress);

console.log(erc20Balance);



Const contractAddress = “”

erc20Minted(contractAddress)

To see your transaction id and the receiver’s balance of the newly minted tokens, run the command below

node scripts/erc20.js

C.) Transferring ERC20 Tokens

Fungible tokens are transferred to a specified blockchain address by invoking the `transfer()` method. The contractAddress field can be replaced with that obtained from the previous example.

The response contains a transaction ID and the balance of the `sender` after the transfer. Paste this snippet of code below in your `erc20.js` file to see it in action

async function er20Transferred(contractAddress) 
// send erc20 (fungible token) transaction

const erc20Transfer = await klaytnSDK.erc20.send.transferSignedTransaction(

to: receiverAddress,

amount: '1',

contractAddress,

fromPrivateKey: senderPrivateKey,

digits: 18,

)

console.log(`Transferred erc20 token/s with txID $erc20Transfer.txId`)

// This timer is used to ensure the transaction above is confirmed and added to the block

// In a real application, the wait mechanism must be implemented properly without using this.

setTimeout(() =>

getErc20Balance(senderAddress, contractAddress)

, 10000);



er20Transferred(contractAddress)

To see your transaction id and the sender’s balance after a transfer, run the command below:

node scripts/erc20.js

D.) Burning ERC20 Tokens

Burning fungible tokens deletes the specified supply from the smart contract. The contractAddress field can be replaced with that obtained from the previous example.

The response contains a transaction ID and the balance of the `receiver` after the burning tokens. Paste this snippet of code below in your `erc20.js` file to see it in action

async function erc20Burned(contractAddress) 
// burn erc20 (fungible token) transaction

const erc20Burn = await klaytnSDK.erc20.send.burnSignedTransaction(

contractAddress: contractAddress,

amount: '1',

fromPrivateKey: receiverPrivateKey,

)

console.log(`Burned erc20 token/s with txID $erc20Burn.txId`)

// This timer is used to ensure the transaction above is confirmed and added to the block

// In a real application, the wait mechanism must be implemented properly without using this.

setTimeout(() =>

getErc20Balance(receiverAddress, contractAddress)

, 10000);



erc20Burned(contractAddress)

To see your transaction id and the receivers balance after burning tokens, run the command below:

node scripts/erc20.js

Full Code

import  TatumKlaytnSDK  from '@tatumio/klaytn'
const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

// This address will DEPLOY, MINT and TRANSFER ERC20 to Receiver Address

const senderAddress = ‘’

const senderPrivateKey = ‘’

// This address will RECEIVE ERC20 token and BURN it

const receiverAddress = ‘’

const receiverPrivateKey = ‘’

async function erc20Deploy ()

// deploys erc20 (fungible token) transaction

const erc20Deployed = await klaytnSDK.erc20.send.deploySignedTransaction(

symbol: 'MTK',

name: 'MyToken',

address: senderAddress,

supply: '10000',

fromPrivateKey: senderPrivateKey,

digits: 18,

)

console.log(`Here is the transaction id $erc20Deployed.txId`);

setTimeout(() =>

getContractAddress(erc20Deployed.txId)

, 10000);



async function getContractAddress(erc20Deployed)

// fetch deployed contract address from transaction hash

const transaction = await klaytnSDK.blockchain.smartContractGetAddress("KLAY", erc20Deployed)

console.log(`Here is the contract address $transaction.contractAddress`);



async function er20Transferred(contractAddress)

// send erc20 (fungible token) transaction

const erc20Transfer = await klaytnSDK.erc20.send.transferSignedTransaction(

to: receiverAddress,

amount: '1',

contractAddress,

fromPrivateKey: senderPrivateKey,

digits: 18,

)

console.log(`Transferred erc20 token/s with txID $erc20Transfer.txId`)

setTimeout(() =>

getErc20Balance(senderAddress, contractAddress)

, 10000);



async function erc20Minted(contractAddress)

// mint erc20(fungible token) to a specified address

const erc20Mint = await klaytnSDK.erc20.send.mintSignedTransaction(

to: receiverAddress,

contractAddress: contractAddress,

amount: '1',

fromPrivateKey: senderPrivateKey,

)

console.log(`Minted erc20 token/s with txID $erc20Mint.txId`)

setTimeout(() =>

getErc20Balance(receiverAddress, contractAddress)

, 10000);



async function erc20Burned(contractAddress)

// burn erc20 (fungible token) transaction

const erc20Burn = await klaytnSDK.erc20.send.burnSignedTransaction(

contractAddress: contractAddress,

amount: '3',

fromPrivateKey: receiverPrivateKey,

)

console.log(`Burned erc20 token/s with txID $erc20Burn.txId`)

setTimeout(() =>

getErc20Balance(receiverAddress, contractAddress)

, 10000);



async function getErc20Balance(address,contractAddress)

// gets the number of the fungible tokens minted on a specific smart contract that a blockchain address holds.

const erc20Balance = await klaytnSDK.erc20.getErc20AccountBalance("KLAY", address, contractAddress);

console.log(erc20Balance);



const contractAddress = “”

erc20Deploy()

er20Transferred(contractAddress);

getErc20Balance(receiverAddress,contractAddress);

erc20Burned(contractAddress);

erc20Minted(contractAddress);

Creating ERC721 Tokens

With the following step-by-step guide, you will be able to deploy your ERC721 token and invoke core functions with a few lines of code, given that the required parameters are filled out.

In this section, You would be able to do the following:

  • Deploy ERC721 contract
  • Mint single and multiple ERC721 tokens
  • Get NFT token Metadata
  • Transfer NFTs
  • Burn NFTs

Firstly, you need to create a new `erc721.js` file in the scripts folder and initialize the Tatum SDK, and set variables as you did in the `Creating ERC20 Tokens` section.

Secondly, paste the code below into your `erc721.js` file and run the command below to see the outcome of each function.

node scripts/erc721.js

A. Deploy an NFT smart contract

Deploy an NFT smart contract on the Klaytn blockchain with the following snippets of code. This deployed NFT smart contract allows you to mint NFTs(one NFT at a time or multiple NFTs at once), burn, and transfer NFTs.

async function erc721Deploy () 
// deploys erc721 (non-fungible token) transaction

const nftDeploy = await klaytnSDK.nft.send.deploySignedTransaction(

name: 'My NFT',

symbol: 'MNFT',

// your private key of the address that has coins

fromPrivateKey: senderPrivateKey,

)

console.log(`Here is the transaction id $nftDeploy.txId`);

setTimeout(() =>

getContractAddress(nftDeploy.txId)

, 10000);



async function getContractAddress(erc721Deployed)

// fetch deployed contract address from transaction hash

const transaction = await klaytnSDK.blockchain.smartContractGetAddress("KLAY", erc721Deployed)

console.log(`Here is the contract address $transaction.contractAddress`);



erc721Deploy()

Output

B. Mint an NFT

With these lines of code, you can mint an NFT natively on the Klaytn blockchain given your contract address and token id.

Note: This function requires the IPFS hash of your metadata. You can upload your metadata to IPFS by following this guide.

async function nftMint(contractAddress) 
// Mint NFTs on your own smart contract

// Put any number here. For a start 1

const tokenId = '';

const nftMinted = await klaytnSDK.nft.send.mintSignedTransaction(

chain: "KLAY",

tokenId,

contractAddress,

fromPrivateKey: senderPrivateKey,

to: senderAddress,

// uploaded metadata from ipfs

url: '’,

)

console.log(`Minted nft with txID: $nftMinted.txId`)



Const contractAddress = “”

nftMint(contractAddress)

Output

C. Mint Multiple NFT

Multiple NFT Tokens can be minted to a specified blockchain address using this method.

async function nftMintBatch(contractAddress) 
// Mint multiple NFTs on your own smart contract

const nftMinted = await klaytnSDK.nft.send.mintMultipleSignedTransaction(

chain: "KLAY",

tokenId: [“", ""],

contractAddress,

fromPrivateKey: senderPrivateKey,

to: [senderAddress, receiverAddress],

// uploaded metadata from ipfs

url: [“”, “PASTE IPFS HASH HERE”]

)

console.log(`Minted multiple nft with txID: $nftMinted.txId`)

Output

D. Get all minted NFTs in the collection

async function getContractNftBalance(contractAddress) 
// Get all minted NFTs in the collection. Returns all NFTs this contract minted.

const nftAccountBalance = await klaytnSDK.nft.getNFTAccountBalance(

"KLAY",

senderAddress,

contractAddress,

)

console.log(`Nfts on $contractAddress:`, nftAccountBalance)



getContractNftBalance(contractAddress)

Output

E. Get NFT token metadata

async function getNFTMetadata(contractAddress, tokenId) 
// Get NFT token metadata

const response = await klaytnSDK.nft.getNFTMetadataURI("KLAY", contractAddress, tokenId)

console.log(`Token metadata: $JSON.stringify(response)`)



Const tokenId = <”VALID TOKEN ID”>

getNFTMetadata(contractAddress, tokenId)

Output

F. Transfer NFT

Transfer an NFT from the smart contract to the specified blockchain address given the smart contract address

async function transferNFT(contractAddress, tokenId) 
const nftTransferred = await klaytnSDK.nft.send.transferSignedTransaction(

to: receiverAddress,

tokenId,

contractAddress,

fromPrivateKey: senderPrivateKey,

)

console.log(`Transferred nft with transaction hash: $nftTransferred.txId`)



transferNFT(contractAddress, tokenId)

Note: Make sure the sender has an NFT tied to its tokenId

Output

G. Burn NFT

Burning the NFT transfers it to address zero that no one can access.

async function burnNFT(contractAddress, tokenId) 
// Burn one NFT Token. This method destroys any NFT token from smart contract defined in contractAddress.

const nftBurned = await klaytnSDK.nft.send.burnSignedTransaction(

tokenId,

contractAddress,

fromPrivateKey: receiverPrivateKey,

)

console.log(`NFT burn transaction sent with txID: $nftBurned.txId`)



burnNFT(contractAddress, tokenId)

Note: Make sure the receiver has an NFT tied to its tokenId

Output

Full Code

import  TatumKlaytnSDK  from '@tatumio/klaytn'
const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

// This address will DEPLOY, MINT and TRANSFER ERC20 to Receiver Address

const senderAddress = ‘’

const senderPrivateKey = ‘’

// This address will RECEIVE ERC20 token and BURN it

const receiverAddress = ‘’

const receiverPrivateKey = ‘’

async function erc721Deploy ()

// deploys erc721 (non fungible token) transaction

const nftDeploy = await klaytnSDK.nft.send.deploySignedTransaction(

name: 'My NFT',

symbol: 'MNFT',

// your private key of the address that has coins

fromPrivateKey: senderPrivateKey,

)

console.log(`Here is the transaction id $nftDeploy.txId`);

setTimeout(() =>

getContractAddress(nftDeploy.txId)

, 10000);



async function getContractAddress(erc721Deployed)

// fetch deployed contract address from transaction hash

const transaction = await klaytnSDK.blockchain.smartContractGetAddress("KLAY", erc721Deployed)

console.log(`Here is the contract address $transaction.contractAddress`);



async function nftMint(contractAddress)

// Mint NFTs on your own smart contract

// Put any number here. For a start 1

const tokenId = '';

const nftMinted = await klaytnSDK.nft.send.mintSignedTransaction(

chain: "KLAY",

tokenId,

contractAddress,

fromPrivateKey: senderPrivateKey,

to: senderAddress,

// uploaded metadata from ipfs

url: '’,

)

console.log(`Minted nft with txID: $nftMinted.txId`)



async function nftMintBatch(contractAddress)

// Mint multiple NFTs on your own smart contract

const nftMinted = await klaytnSDK.nft.send.mintMultipleSignedTransaction(

chain: "KLAY",

tokenId: [“", ""],

contractAddress,

fromPrivateKey: senderPrivateKey,

to: [senderAddress, receiverAddress],

// uploaded metadata from ipfs

url: [“”, “PASTE IPFS HASH HERE”]

)

console.log(`Minted multiple nft with txID: $nftMinted.txId`)



async function getContractNftBalance(contractAddress)

// Get all minted NFTs in the collection. Returns all NFTs this contract minted.

const nftAccountBalance = await klaytnSDK.nft.getNFTAccountBalance(

"KLAY",

senderAddress,

contractAddress,

)

console.log(`Nfts on $contractAddress:`, nftAccountBalance)



async function getNFTMetadata(contractAddress, tokenId)

// Get NFT token metadata

const response = await klaytnSDK.nft.getNFTMetadataURI("KLAY", contractAddress, tokenId)

console.log(`Token metadata: $JSON.stringify(response)`)



async function transferNFT(contractAddress, tokenId)

// Transfer an NFT from the smart contract (the contractAddress parameter in the request body) to the specified blockchain address (the to parameter in the request body).

const nftTransferred = await klaytnSDK.nft.send.transferSignedTransaction(

to: receiverAddress,

tokenId,

contractAddress,

fromPrivateKey: senderPrivateKey,

)

console.log(`Transferred nft with transaction hash: $nftTransferred.txId`)



async function burnNFT(contractAddress, tokenId)

// Burn one NFT Token. This method destroys any NFT token from smart contract defined in contractAddress.

const nftBurned = await klaytnSDK.nft.send.burnSignedTransaction(

tokenId,

contractAddress,

fromPrivateKey: receiverPrivateKey,

)

console.log(`NFT burn transaction sent with txID: $nftBurned.txId`)



const contractAddress = "";

const tokenId = "";

erc721Deploy();

nftMint(contractAddress);

nftMintBatch(contractAddress);

getContractNftBalance(contractAddress);

getNFTMetadata(contractAddress, tokenId);

transferNFT(contractAddress, tokenId);

burnNFT(contractAddress, tokenId);

Creating ERC1155 Token

With the following step-by-step guide, you will be able to deploy your ERC1155 token, an equivalent of KIP37 on the klaytn blockchain. Further, you will invoke core functions with a few lines of code, given that the required parameters are filled out.

In this section, You would be able to do the following:

  • Deploy ERC1155 contract
  • Mint ERC1155 tokens
  • Transfer Multi-token(MT)
  • Burn Multi-tokens(MT)

Firstly, you need to create a new `erc1155.js` file in the scripts folder and initialize the Tatum SDK and set variables as you did in the `Creating ERC20 Tokens` section.

Secondly, paste the code below into your `erc721.js` file and run the command below to see the outcome of each function.

>node scripts/erc1155.js

A. Deploy the ERC1155 contract

This method creates a new ERC1155 Smart Contract (Multi Tokens) on the Klaytn blockchain. After deploying your ERC1155 contract, It is possible to mint, burn and transfer tokens.

async function erc1155Deploy () 
// deploys Multi token transaction

const multiTokenDeployed = await klaytnSDK.multiToken.send.deployMultiTokenTransaction(

// your private key of the address that has coins

fromPrivateKey: senderPrivateKey,

// uploaded metadata from ipfs

uri: 'ipfs://bafkreidinxo4cuds5ybcl72al5sywstc3m6i7lwmkz5wcdna7ccadjssm4',

)

console.log(`Deployed multi token with txID $multiTokenDeployed.txId`)

setTimeout(() =>

getContractAddress(multiTokenDeployed.txId)

, 10000);



async function getContractAddress(multiTokenDeployed)

// fetch deployed contract address from transaction hash

const transaction = await klaytnSDK.blockchain.smartContractGetAddress("KLAY", multiTokenDeployed)

console.log(`Here is the contract address $transaction.contractAddress`);



erc1155Deploy()

Note: This function requires the IPFS hash of your metadata. You can upload your metadata to IPFS by following this guide.

Output

B. Mint a Multi-token

With this method, you can mint a fixed amount of Multi Token to a specified blockchain address

async function mintmultiToken(contractAddress) 
// Paste a number lets say 1

const tokenId = ""

const multiTokenMinted = await klaytnSDK.multiToken.send.mintMultiTokenTransaction(

to: senderAddress,

tokenId,

amount: '1000',

fromPrivateKey: senderPrivateKey,

contractAddress,

)

console.log(`Multi Token mint transaction sent with txID: $multiTokenMinted.txId`)



const contractAddress = “”

mintmultiToken(contractAddress)

Output

C. Transfer a Multi-token

You can transfer a certain amount of Multi Token from an account to another account using this method

async function transferMT(contractAddress, tokenId) 
// Transfer multi-token from the smart contract to the specified blockchain address

const multiTokenTransferred = await klaytnSDK.multiToken.send.transferMultiTokenTransaction(

to: receiverAddress,

tokenId,

amount: '10',

fromPrivateKey: senderPrivateKey,

contractAddress,

)

console.log(`Sent Multi Token with txID: $multiTokenTransferred.txId`)



const tokenId=“”

transferMT(contractAddress, tokenId)

Output

D. Burn a Multi-token

This method burns a fixed amount of Multi Tokens by id. It destroys Multi Tokens from smart contract defined in contractAddress.

async function burnMT(contractAddress, tokenId) 
// Burn one multi-token. This method destroys any MT token from smart contract defined in contractAddress.

const multiTokenBurned = await klaytnSDK.multiToken.send.burnMultiTokenTransaction(

tokenId,

amount: '1',

fromPrivateKey: receiverPrivateKey,

contractAddress,

account: receiverAddress,

)

console.log(`Burned Multi Token/s with txID: $multiTokenBurned.txId`)



burnMT(contractAddress, tokenId)

Output

Full Code

import  TatumKlaytnSDK  from '@tatumio/klaytn'
const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

// This address will DEPLOY, MINT and TRANSFER ERC20 to Receiver Address

const senderAddress = ‘’

const senderPrivateKey = ‘’

// This address will RECEIVE ERC20 token and BURN it

const receiverAddress = ‘’

async function erc1155Deploy ()

// deploys Multi token transaction

const multiTokenDeployed = await klaytnSDK.multiToken.send.deployMultiTokenTransaction(

// your private key of the address that has coins

fromPrivateKey: senderPrivateKey,

// uploaded metadata from ipfs

uri: '’,

)

console.log(`Deployed multi token with txID $multiTokenDeployed.txId`)

setTimeout(() =>

getContractAddress(multiTokenDeployed.txId)

, 10000);



async function getContractAddress(multiTokenDeployed)

// fetch deployed contract address from transaction hash

const transaction = await klaytnSDK.blockchain.smartContractGetAddress("KLAY", multiTokenDeployed)

console.log(`Here is the contract address $transaction.contractAddress`);



async function mintmultiToken(contractAddress)

// Paste a number lets say 1

const tokenId = ""

const multiTokenMinted = await klaytnSDK.multiToken.send.mintMultiTokenTransaction(

to: senderAddress,

tokenId,

amount: '1000',

fromPrivateKey: senderPrivateKey,

contractAddress,

)

console.log(`Multi Token mint transaction sent with txID: $multiTokenMinted.txId`)



async function transferMT(contractAddress, tokenId)

// Transfer multi-token from the smart contract to the specified blockchain address

const multiTokenTransferred = await klaytnSDK.multiToken.send.transferMultiTokenTransaction(

to: receiverAddress,

tokenId,

amount: '10',

fromPrivateKey: senderPrivateKey,

contractAddress,

)

console.log(`Sent Multi Token with txID: $multiTokenTransferred.txId`)



async function burnMT(contractAddress, tokenId)

// Burn one multi-token. This method destroys any MT token from the smart contract defined in contractAddress.

const multiTokenBurned = await klaytnSDK.multiToken.send.burnMultiTokenTransaction(

tokenId,

amount: '1',

fromPrivateKey: receiverPrivateKey,

contractAddress,

account: receiverAddress,

)

console.log(`Burned Multi Token/s with txID: $multiTokenBurned.txId`)



const contractAddress = "0x5C128284F7846698134228e4a197664DE075AcDB";

const tokenId = "1"

erc1155Deploy();

mintmultiToken(contractAddress)

transferMT(contractAddress, tokenId);

burnMT(contractAddress, tokenId);

Invoke a method in a smart contract on Klaytn

In this section, you will use the Tatum SDK to invoke a method in an existing smart contract on Klaytn. To put things in perspective, your already deployed contract method can be executed given its contract address and ABI using Tatum-js.

You can execute a read-only or write method. A read-only method returns the output of the invoked method, while the transaction id for a write method is returned.

Set-up

  • Navigate to your existing project directory
  • Enter the existing scripts folder
  • Create a new file named `invoke.js` to house the code

Initialize Tatum SDK

You must initialize the SDK with your API KEY in order to use Tatum’s built-in features in your project. Don’t have an API KEY yet, create one here

import  TatumKlaytnSDK  from '@tatumio/klaytn'
const klaytnSDK = TatumKlaytnSDK( apiKey: '’ )

Set variables

In your newly created `invoke.js` file, copy and paste the following variables. You can either paste your wallet address and private key from any wallet Infrastructure like MetaMask or generate one using the Tatum SDK as explained in the previous article.

const senderPrivateKey = “”

Quickstart

To invoke an existing smart contract method, you need to get its contract address and ABI. In this tutorial, an existing Bank contract was deployed and as such, we would be calling both the getbalance and deposit functions.

To deploy your Bank contract, click this link to open on Remix. Once your contract has been deployed, copy the contract address from the Deployed Contracts tab and the ABI from the Solidity Compiler tab on Remix IDE.

Run the command below to see the outcome of each function.

>node scripts/invoke.js

A. Read Method

Call a read method in your smart contract with the following lines of code.

async function readFromSmartContract () 
const address = “”

// your previously deployed contract address

const contractAddress = “”

// smart contract read method

const data = await klaytnSDK.smartContract.send.smartContractReadMethodInvocationTransaction(

contractAddress: contractAddress,

methodABI:

"inputs": [



"internalType": "address",

"name": "_addr",

"type": "address"



],

"name": "getBalance",

"outputs": [



"internalType": "uint256",

"name": "",

"type": "uint256"



],

"stateMutability": "view",

"type": "function"

,

methodName: "getBalance",

params: [address]

)

console.log(`Smart contract data: $data.data`)

Output

B. Write Method

Invoke a write method in your smart-contract with the following lines of code.

async function invokeWriteMethod () 
// your previously deployed contract address

const contractAddress = “”

// smart contract read method

const data = await klaytnSDK.smartContract.send.smartContractMethodInvocationTransaction(

amount: "2",

contractAddress: contractAddress,

methodABI:

"inputs": [],

"name": "deposit",

"outputs": [],

"stateMutability": "payable",

"type": "function"

,

methodName: "deposit",

fromPrivateKey: senderPrivateKey,

params: []

)

console.log(`Smart contract tx id: $ data.txId `)

Output

Conclusion

Tatum positions itself as an open-source development platform for numerous blockchains because it helps developers in a variety of ways. In this tutorial, we explored working with smart contracts, and token contracts (ERC20, ERC721, and ERC1155), as well as invoking a method on an existing smart contract on Klaytn.

Having said that, users can also securely sign and broadcast transactions locally without sending sensitive data over the internet. This provides a high level of security for users when dealing with private keys and mnemonics. Want to try out this security-oriented functionality, do check out the Tatum Key Management System(KMS) to securely sign and broadcast transactions to the Klaytn blockchain.

If you want more information, visit Klaytn Docs and Tatum. If you have any questions, visit Klaytn Forum.

--

--