将以太坊应用程序迁移到 Kaia
目录
1. 导言
本教程旨在指导如何将以太坊应用程序迁移到 Kaia。无需任何 Kaia 经验。我们将只关注将以太坊应用程序迁移到 Kaia 所需的代码修改。
2. 先决条件
- 熟悉支持 EVM 的开发工具和标准。
- 构建 dApp 的基本知识。
3. Kaia 与以太坊兼容
Kaia 运行时环境与以太坊虚拟机兼容,可执行用 Solidity 编写的智能合约。 Kaia 的 RPC API 和其他客户端库与以太坊的 API 规范几乎完全相同。因此,将以太坊应用程序迁移到 Kaia 非常简单。这有助于开发人员轻松迁移到新的区块链平台。
4. 迁移应用程序
按照以下步骤将您的以太坊应用程序迁移到 Kaia:
-
配置您的合同工具和 SDK,以 Kaia Network - Kairos Testnet 为目标:
- RPC 端点:
https://public-en-kairos.node.kaia.io - WebSocket 端点(可选):wss://public-en-kairos.node.kaia.io/ws`。
- 连锁编号:1001
- RPC 端点:
-
部署您的合同
- 硬礼帽
- 铸造厂
// using Hardhat, it will be enough to add the following networks to the "hardhat.config.js" configuration filenetworks: { kaia: { url: "https://public-en.node.kaia.io", accounts:[process.env.PRIVATE_KEY], }, kairos_testnet: { url: "https://public-en-kairos.node.kaia.io", accounts:[process.env.PRIVATE_KEY], }}// Then run the command to deploy :// npx hardhat run scripts/deploy.js --network kaia// npx hardhat run scripts/deploy.js --network kairos_testnet
forge create --rpc-url<rpc_url> --private-key<private_key> <path_to_contract>
-
使用 Kaia SDK 与合同互动。您可以随意使用其他工具包,如 viem 或 web3.py 。
A. 读取区块链数据
街区编号
只需用 Kaia 的 RPC 端点替换 web3 库,就可以实时同步 Kaia 的 BlockNumber,而不是以太坊的 BlockNumber。
const { JsonRpcProvider } = require("@kaiachain/ethers-ext/v6");// const SEPOLIA_TESTNET_RPC_URL = 'https://ethereum-sepolia-rpc.publicnode.com'const KAIROS_TESTNET_RPC_URL = 'https://public-en-kairos.node.kaia.io/'const provider = new JsonRpcProvider(KAIROS_TESTNET_RPC_URL);async function getKaiaBlockNumber() {// Get the current block numberconst blockNumber = await provider.getBlockNumber();console.log("Current Kaia block number:", blockNumber);}getKaiaBlockNumber()合同数据
const ethers = require("ethers");const provider = new ethers.providers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");/* compiled in remix.ethereum.org (compiler: 0.8.18, optimizer: false)// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.13;contract Counter {uint256 public number;event SetNumber(uint256 number);constructor(uint256 initNumber) {number = initNumber;}function setNumber(uint256 newNumber) public {number = newNumber;emit SetNumber(number);}function increment() public {number++;emit SetNumber(number);}}*/const abi = '[{"inputs":[{"internalType":"uint256","name":"initNumber","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"number","type":"uint256"}],"name":"SetNumber","type":"event"},{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]';const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";async function main() {const counter = new ethers.Contract(contractAddr, abi, provider);const number = await counter.number();console.log("number", number.toString());}main();B. 写入区块链
const ethers = require("ethers");const { Wallet } = require("@kaiachain/ethers-ext/v6");const senderAddr = "REPLACE WITH SENDER ADDRESS";const senderPriv = process.env.PRIVATE_KEY;const provider = new ethers.JsonRpcProvider("https://public-en-kairos.node.kaia.io");const wallet = new Wallet(senderPriv, provider);/* compiled in remix.ethereum.org (compiler: 0.8.18, optimizer: false)// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.13;contract Counter {uint256 public number;event SetNumber(uint256 number);constructor(uint256 initNumber) {number = initNumber;}function setNumber(uint256 newNumber) public {number = newNumber;emit SetNumber(number);}function increment() public {number++;emit SetNumber(number);}}*/const abi = '[{"inputs":[{"internalType":"uint256","name":"initNumber","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"number","type":"uint256"}],"name":"SetNumber","type":"event"},{"inputs":[],"name":"increment","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"number","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"newNumber","type":"uint256"}],"name":"setNumber","outputs":[],"stateMutability":"nonpayable","type":"function"}]';const contractAddr = "0x95Be48607498109030592C08aDC9577c7C2dD505";async function main() {const counter = new ethers.Contract(contractAddr, abi, wallet);console.log("number before", (await counter.number()).toString());const sentTx = await counter.increment();const receipt = await sentTx.wait();console.log("receipt", receipt);console.log("number after", (await counter.number()).toString());}main();