跳至主要内容

将以太坊应用程序迁移到 Kaia

本页面使用机器翻译自英语,可能包含错误或不清楚的语言。如需最准确的信息,请参阅英文原文。由于更新频繁,部分内容可能与英文原文有出入。请加入我们在 Crowdin 上的努力,帮助我们改进本页面的翻译。 (Crowdin translation page, Contributing guide)

目录

1. 导言

本教程旨在指导如何将以太坊应用程序迁移到 Kaia。 无需任何 Kaia 经验。 我们将只关注将以太坊应用程序迁移到 Kaia 所需的代码修改。

2. 先决条件

  • 熟悉支持 EVM 的开发工具和标准。
  • 构建 dApp 的基本知识。

3. Kaia 与以太坊兼容

Kaia 运行时环境与以太坊虚拟机兼容,可执行用 Solidity 编写的智能合约。 Kaia 的 RPC API 和其他客户端库与以太坊的 API 规范几乎完全相同。 因此,将以太坊应用程序迁移到 Kaia 非常简单。 这有助于开发人员轻松迁移到新的区块链平台。

4. 迁移应用程序

按照以下步骤将您的以太坊应用程序迁移到 Kaia:

  1. 配置您的合同工具和 SDK,以 Kaia Network - Kairos Testnet 为目标:

    • RPC 端点: https://public-en-kairos.node.kaia.io
    • WebSocket 端点(可选):wss://public-en-kairos.node.kaia.io/ws`。
    • 连锁编号:1001
  2. 使用 Kaia 钱包 创建账户,并从 Faucet 获取一些测试资金。

  3. 部署您的合同


// 使用 Hardhat,只需在 "hardhat.config.js "配置文件中添加以下网络即可:
networks:{
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],
}
}
// 然后运行命令进行部署:
// npx hardhat run scripts/deploy.js --network kaia
// npx hardhat run scripts/deploy.js --network kairos_testnet

  1. 使用 Kaia SDK 与合同互动。 您可以随意使用其他工具包,如 viemweb3.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 number
    const 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: UNLICENSED
    pragma 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: UNLICENSED
    pragma 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();

让这个页面变得更好