Deploy smart contract using KEN
Before you start, let's get familiar with several Klaytn-specific terms.
- Endpoint Node (EN): A node that handles the JSON-RPC API requests to the Klaytn network. Endpoint Node does not participate in the consensus.
- KLAY: Klaytn native coin.
- caver-js: A JavaScript implementation of Klaytn JSON-RPC APIs.
- Baobab: Klaytn testnet
- Cypress: Klaytn mainnet
This step by step guide will walk you through the process of launching an Endpoint Node (EN) of Baobab testnet and building a basic smart contract with your new account. The tutorial consists of two parts, setting up an EN and deploying a smart contract through your EN.
This guide uses the Baobab testnet because deploying a smart contract and submitting a transaction require transaction fees in KLAY. For the development purpose, testnet KLAY can be obtained from the Baobab faucet.
Launch an Endpoint Node β
Download and Initialize an Endpoint Node (EN) β
Unzip the provided ken binary package and copy the files into the klaytn folder.
Note: Please download appropriate package starting with ken
.
For Mac users, unzip the downloaded file with the following command.
$ tar zxf ken-baobab-vX.X.X-X-darwin-amd64.tar.gz$ export PATH=$PATH:$PWD/ken-darwin-amd64/bin
For Linux users, unzip the downloaded file with the following command.
$ tar zxf ken-baobab-vX.X.X-X-linux-amd64.tar.gz$ export PATH=$PATH:$PWD/ken-linux-amd64/bin
You should create a data directory to store the blockchain data. In this tutorial, we will create a kend_home
folder in the home directory.
$ mkdir -p ~/kend_home
Configuring the EN β
The configuration file, kend.conf
, is located under ken-xxxxx-amd64/conf/
. For the details of configurable parameters, you can refer to the EN Configuration Guide. To launch an EN of Baobab testnet, please update the kend.conf
file accordingly as follows.
# cypress, baobab is only available if you don't specify NETWORK_ID.NETWORK="baobab"# if you specify NETWORK_ID, a private network is created.NETWORK_ID=...RPC_API="klay,net" # net module should be opened for truffle later on....DATA_DIR=~/kend_home
Launching the EN β
To launch the EN, execute the following command.
$ kend start Starting kend: OK
Checking the EN β
To check if the EN is running, execute the following command.
$ kend statuskend is running
Checking the log of the EN β
To check the log of the EN, execute the following command.
$ tail -f ~/kend_home/logs/kend.out...INFO[03/26,15:37:49 +09] [5] Imported new chain segment blocks=1 txs=0 mgas=0.000 elapsed=2.135ms mgasps=0.000 number=71340 hash=f15511β¦c571da cache=155.56kB...
Troubleshooting β
Please refer to the Troubleshooting if you have trouble in launching the Klaytn Endpoint Node.
Top up your Account β
Attaching to the Console β
Klaytn Endpoint Node comes with JavaScript console. From the console command line, you can initiate part of Klaytn API calls to your EN. To attach to the JavaScript console, execute the following command.
$ ken attach ~/kend_home/klay.ipcWelcome to the Klaytn JavaScript console!instance: Klaytn/vX.X.X/XXXX-XXXX/goX.X.X datadir: ~/kend_home modules: admin:1.0 debug:1.0 governance:1.0 istanbul:1.0 klay:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 >
NOTE: You must wait until it downloads all the blocks. Enter klay.blockNumber
in a console and check whether it matches the current block number here
NOTE: Type klay
or personal
to get the list of available functions.
Creating a New Klaytn Account β
To create a new Klaytn account from the JavaScript console, execute the following command. Your private key will be encrypted with the passphrase you enter.
> personal.newAccount()Passphrase: # enter your passphraseRepeat passphrase:"0x75a59b94889a05c03c66c3c84e9d2f8308ca4abd" # created account address
Keystore file will be created under keystore
folder in the EN data directory, DATA_DIR
set in the kend.conf
. If you follows the quick start default guideline, it must be ~/kend_home/keystore/
.
$ ls ~/kend_home/keystore/UTC--2019-06-24T11-20-15.590879000Z--75a59b94889a05c03c66c3c84e9d2f8308ca4abd
Unlocking the Klaytn Account β
To unlock the created account, execute the following command. It unlocks the account for 300 seconds.
Note: If you want to manually set the unlock duration, refer to this link.
WARNING
: Unlocking an account could be very dangerous if not done carefully. There are chances that your tokens will be taken away by hackers if your EN is hacked by a hacker. To use safer method, refer to this deployment guide using private key
> personal.unlockAccount('75a59b94889a05c03c66c3c84e9d2f8308ca4abd') # account address to unlockUnlock account 75a59b94889a05c03c66c3c84e9d2f8308ca4abdPassphrase: # enter your passphrasetrue
Getting testnet KLAY from the Baobab Faucet β
-
Using the Baobab faucet in KlaytnWallet.
-
You can either create a new account from the Wallet or use the keystore file you created from the EN JavaScript console above to log into the Wallet.
-
Go to "KLAY Faucet" from the left pane menu, and click the "Run Faucet" button to get 150 KLAY.
You can run the KLAY Faucet once every 24 hours.
-
If you created a new account to get KLAY, then send the KLAY to your created account on the EN.
Checking the Balance in Your Account β
To see the balance of your account, execute the following command.
The default unit is peb (1 KLAY = 10^18 peb). More information about KLAY units can be found at Units of KLAY.
> klay.getBalance('75a59b94889a05c03c66c3c84e9d2f8308ca4abd') # enter your account address1e+21 # 1000 KLAY
Exiting the Console β
To leave the javascript console, execute the following command.
> exit$
Install Development Tools β
Installing caver-js β
We recommend to create a klaytn project directory such that:
$ mkdir $HOME/klaytn
You need
npm
andnode.js
installed to proceed. Please refer to get-npm and node.js for installation on your system.
βcaver-js is a JSON RPC framework for the Klaytn network (equivalent to web3.js in Ethereum). Before installing caver-js, you must generate package.json
file via npm init
command, and then type npm install caver-js
to install caver-js.
$ npm init # initialize npm at the klaytn project directory$ npm install caver-js
NOTE: If you already installed caver-js, please update it to the latest version.
$ npm cache clean --force # initialize npm cache$ npm install caver-js@latest # update caver-js to the latest version
If you receive the following errors while updating the caver-js, remove .git
folder in the websocket
directory.
npm ERR! path /Users/username/klaytn/node_modules/websocketnpm ERR! code EISGITnpm ERR! git /Users/username/klaytn/node_modules/websocket: Appears to be a git repo or submodule.npm ERR! git /Users/username/klaytn/node_modules/websocketnpm ERR! git Refusing to remove it. Update manually,npm ERR! git or move it out of the way first.npm ERR! A complete log of this run can be found in:npm ERR! /Users/username/.npm/_logs/2019-06-25T01_49_37_032Z-debug.logβ$ rm /Users/username/klaytn/node_modules/websocket/.git
Note: For all the function calls that begin with web3.eth...
in web3.js, should be replaced with caver.klay...
.
web3.eth.sendTransaction({ ... })
(X)
caver.klay.sendTransaction({ ... })
(O)
Installing Truffle β
In this tutorial, Truffle is used to compile and deploy smart contracts written in Solidity. Currently, Klaytn supports Truffle version 4.1.15. For further information about Truffle, refer to the following sites:
- Truffle repository - https://github.com/trufflesuite/truffleβ
- Truffle documents - https://trufflesuite.com/docsβ
We can install Truffle either globally or locally:
- Globally using npm by executing the following commands:
$ sudo npm install -g truffle@4.1.15$ cd /usr/local/lib/node_modules/truffle$ sudo npm install solc@0.5.6$ cd -
or
- Locally, i.e., in your local directory, by executing the followings:
# Assuming you are in $HOME/klaytn/.$ npm install truffle@4.1.15$ cd node_modules/truffle$ npm install solc@0.5.6$ cd -$ ln -s node_modules/truffle/build/cli.bundled.js truffle$ export PATH=`pwd`:$PATH
Installing vvisp β
vvisp is an easy-to-use cli tool/framework for developing smart contracts, provided by HEACHI LABS. You can easily set environment, deploy and execute Klaytn smart contracts with a single-command. It supports the Truffle framework, so developers familiar with Truffle can use vvisp without difficulty.
Here, we introduce how to install vvisp and use it to set up the Klaytn dApp development environment.
- vvisp repository - https://github.com/HAECHI-LABS/vvispβ
- vvisp document - https://github.com/HAECHI-LABS/vvisp/blob/dev/README_KLAYTN.mdβ
vvisp can be easily installed if you have npm or yarn by executing the following command:
$ npm install -g @haechi-labs/vvisp# or if you use yarn$ yarn global add @haechi-labs/vvisp
Upon installation, you can utilize the vvisp command to ensure it has been installed properly. NOTE: You should use version over v2.1.0.
$ vvispUsage: vvisp <command> [options]where <command> is one of: compile, console, deploy-contract, deploy-service, flatten, gen-script, initOptions: -v, --version output the version number -h, --help output usage informationCommands: compile [files...] compile the smart contracts console [script-api-path] run interactive shell to execute contract scripts deploy-contract <file> [arguments...] deploy the smart contracts deploy-service deploy or upgrade smart contract service using the deployment configure file flatten <files...> flatten the smart contracts gen-script [files...] generate javascript libraries communicating the smart contracts init [name] initialize directory to use vvisp# you can check installed version.$ vvisp --versionv2.1.0
Deploy a Smart Contract β
Now we are ready to develop and deploy Klaytn smart contracts!
Creating a Project Directory β
First of all, create a directory where the source code locates.
$ mkdir klaytn-testboard$ cd klaytn-testboard
Initializing Truffle β
Initialize Truffle for contract deployment.
$ truffle init
Writing a Simple Smart Contract in Solidity β
Create KlaytnGreeter.sol
at klaytn-testboard/contracts
directory.
$ cd contracts$ touch KlaytnGreeter.sol$ vi KlaytnGreeter.sol
Write the following code in KlaytnGreeter.sol.
pragma solidity 0.5.6;contract Mortal { /* Define variable owner of the type address */ address payable owner; /* This function is executed at initialization and sets the owner of the contract */ constructor () public { owner = msg.sender; } /* Function to recover the funds on the contract */ function kill() public payable { if (msg.sender == owner) selfdestruct(owner); }}contract KlaytnGreeter is Mortal { /* Define variable greeting of the type string */ string greeting; /* This runs when the contract is executed */ constructor (string memory _greeting) public { greeting = _greeting; } /* Main function */ function greet() public view returns (string memory) { return greeting; }}
Modifying the Migration Script β
$ cd ..$ cd migrations$ vi 1_initial_migration.js
Modify 1_initial_migration.js
as the following.
const Migrations = artifacts.require("./Migrations.sol");const KlaytnGreeter = artifacts.require("./KlaytnGreeter.sol");module.exports = function(deployer) { deployer.deploy(Migrations); deployer.deploy(KlaytnGreeter, 'Hello, Klaytn');};
Deploying a Smart Contract using Truffle β
Enter Klaytn's network information into truffle.js.
WARNING
: Currently Klaytn Baobab network's gasPrice is fixed to 25 Gpeb (It returns an error if you attempt to use any other number).
$ cd ..$ vi truffle-config.js
Modify configuration as below
// truffle-config.jsmodule.exports = { networks: { klaytn: { host: '127.0.0.1', port: 8551, from: '0x75a59b94889a05c03c66c3c84e9d2f8308ca4abd', // enter your account address network_id: '1001', // Baobab network id gas: 20000000, // transaction gas limit gasPrice: 25000000000, // gasPrice of Baobab is 25 Gpeb }, }, compilers: { solc: { version: "0.5.6" // Specify compiler's version to 0.5.6 } }};
Deploy the contract using the following command.
NOTE: Use --network
to select which network to deploy and --reset
to overwrite.
NOTE: Make sure that your Klaytn node is running.
Your contract address is displayed followed KlaytnGreeter
:
$ truffle deploy --network klaytn --resetUsing network 'klaytn'.Running migration: 1_initial_migration.js Deploying Migrations... ... 0x0f5108bd9e51fe6bf71dfc472577e3f55519e0b5d140a99bf65faf26830acfca Migrations: 0x97b1b3735c8f2326a262dbbe6c574a8ea1ba0b7d Deploying KlaytnGreeter... ... 0xcba53b6090cb4a118359b27293ba95116a8f35f66ae50fbd23ae1081ce9ffb9e KlaytnGreeter: [SAVE THIS ADDRESS!!] # this is your smart contract addressSaving successful migration to network... ... 0x14eb68727ca5a0ac767441c9b7ab077336f9311f71e9854d42c617aebceeec72Saving artifacts...
WARNING
: It returns an error when your account is locked.
Running migration: 1_initial_migration.js Replacing Migrations... ... undefinedError encountered, bailing. Network state unknown. Review successful transactions manually.Error: authentication needed: password or unlock
This is how you unlock your account.
> personal.unlockAccount('0x775a59b94889a05c03c66c3c84e9d2f8308ca4abd')Unlock account 0x75a59b94889a05c03c66c3c84e9d2f8308ca4abdPassphrase:true
And then you are ready to go. Try deploy again.
Check the Deployment β
Checking the Deployed Byte Code using caver-js β
Use getCode
for checking the byte code of the deployed smart contract.
First, create a test file and open it.
$ touch test-klaytn.js$ open test-klaytn.js
Write the following test code. Make sure you enter the contract address you just deployed.
// test-klaytn.jsconst Caver = require('caver-js');const caver = new Caver('http://127.0.0.1:8551');// enter your smart contract addressconst contractAddress = '0x65ca27ed42abeef230a37317a574058ff1372b34'caver.klay.getCode(contractAddress).then(console.log);
Run the code.
$ node test-klaytn.js0x60806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610051578063cfae321714610068575b600080fd5b34801561005d57600080fd5b506100666100f8565b005b34801561007457600080fd5b5061007d610189565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100bd5780820151818401526020810190506100a2565b50505050905090810190601f1680156100ea5780820380516001836020036101000a031916815260200191505b509250505060405180...
Calling functions in the Deployed Smart Contract β
Use JavaScript to call the greet()
in the contract.
NOTE: In order to call specific functions in smart contracts, you need an ABI (Application Binary Interface) file. When Truffle deploys your contract, it automatically creates .json file at ./build/contracts/
which contains abi
property.
Append the following lines to the test code written above.
// test-klaytn.jsconst Caver = require('caver-js');const caver = new Caver('http://127.0.0.1:8551');// enter your smart contract addressconst contractAddress = '0x65ca27ed42abeef230a37317a574058ff1372b34'caver.klay.getCode(contractAddress).then(console.log);// add linesconst KlaytnGreeter = require('./build/contracts/KlaytnGreeter.json');// enter your smart contract addressconst klaytnGreeter = new caver.klay.Contract(KlaytnGreeter.abi, contractAddress);klaytnGreeter.methods.greet().call().then(console.log);
Run the test code.
$ node test-klaytn.js0x60806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b514610051578063cfae321714610068575b600080fd5b34801561005d57600080fd5b506100666100f8565b005b34801561007457600080fd5b5061007d610189565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100bd5780820151... # This is from caver.klay.getCodeHello, Klaytn # This is from KlyatnGreeter.methods.greet()
If you got "Hello, Klaytn", you've completed the task. Congrats!