跳至主要內容

將以太坊應用程序遷移到 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. 部署您的合同


// using Hardhat, it will be enough to add the following networks to the "hardhat.config.js" configuration file
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],
}
}
// Then run the command to deploy :
// 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();

讓這個頁面變得更好