Migrating Ethereum App to Klaytn
Table of Contents
1. Introduction
This tutorial is intended to give a guide to migrate an Ethereum App to Klaytn. No previous Klaytn experience is needed. A simple blockchain app will be used as a sample to show how to migrate an Ethereum App to Klaytn.
We will focus only on the code modifications required to migrate an Ethereum App to Klaytn. If you need details on creating a Klaytn dApp, Please refer to CountDApp Tutorial.
Source Code Complete source code can be found on GitHub at https://github.com/klaytn/countbapp
Intended Audience
We assume that you have basic knowledge on React. Sample code is made with React.
Basic knowledge and experience in Blockchain app is required, but no previous Klaytn experience is needed.
Testing Environment
CountDApp is tested in the following environment.
MacOS Mojave 10.14.5
Node 10.16.0 (LTS)
npm 6.9.0
Python 2.7.10
2. Klaytn has Ethereum compatibility
Klaytn runtime environment is compatible with Ethereum Virtual Machine and executes smart contracts written in Solidity. Klaytn's RPC APIs and other client libraries maintain almost identical API specifications with Ethereum's whenever available. Therefore, it is fairly straightforward to migrate Ethereum Apps to Klaytn. This helps developers easily move to a new blockchain platform.
3. Change node connection from Ethereum to Klaytn
First, you need to change the library that makes a connection to the node. Then you will specify the node URL in 'rpcURL'. (FYI. The Ropsten testnet in ethereum will be shut down in Q4 2022. )
Ethereum
web3library connects to and communicates with Ethereum node.Ropsten testnetURL is assigned to 'rpcURL' .
Klaytn
caver-jslibrary is used to connect to and communicate with Klaytn node.Baobab testnetURL is assigned to 'rpcURL'.
src/klaytn/caver.js
4. Interact with Klaytn node: BlockNumber component
BlockNumber component
BlockNumber component gets the current block number every 1 second (1000ms).
By simply replacing the web3 library with caver-js, you can sync Klaytn's BlockNumber in real-time instead of Ethereum's BlockNumber.
Ethereum:
web3.eth.getBlockNumber()Klaytn:caver.klay.getBlockNumber()
For more detail about BlockNumber component, see CountDApp tutorial - Blocknumber Component.
5. Interact with the contract: Count component
Count component
To interact with the contract, we need to create an instance of the deployed contract. With the instance, we can read and write the contract's data.
Let's learn step by step how to migrate CountDApp from Ethereum to Klaytn!
5-1. Deploy
Countcontract on Klaytn5-2. Create a contract instance
5-3. Interact with contract
5-1. Deploy Count contract on Klaytn
Count contract on KlaytnThe first step is deploying Count contract on Klaytn and get the contract address. Most of the cases, you can use Etherem contracts on Klaytn without modification. See Porting Etherem Contract. In this guide, we will use Truffle to deploy the contract.
Change network properties in
truffle-config.jsto deploy the contract on Klaytn.Top up your account using KLAY faucet.
Type
$ truffle deploy --network baobab --resetCountcontract will be deployed on Baobab testnet, Klaytn.
truffle-config.js
For more details about deploying contracts, See CountDapp tutorial - Deploy Contract.
5-2. Create a contract instance
You can create a contract instance with the caver-js API. The contract instance creates a connection to Count contract. You can invoke contract methods through this instance.
Ethereum :
web3.eth.Contract(ABI, address)Klaytn :caver.klay.Contract(ABI, address)
src/components/Count.js
5-3. Interact with contract
The ABI (Application Binary Interface) used to create the Count contract instance allows the caver-js to invoke contract's methods as below. You can interact with Count contract as if it were a JavaScript object.
Read data (call)
CountContract.methods.count().call()Write data (send)
CountContract.methods.plus().send({ ... })CountContract.methods.minus().send({ ... })
Once you created a contract instance as in the previous step, you don't need to modify any code in using the contract methods afterward. dApp migration has been completed!
Full code: Count component
Count componentsrc/components/Count.js
Last updated