本文へスキップ

EthereumアプリをKaiaに移行する

このページは英語からの機械翻訳を使用しており、誤りや不明瞭な表現が含まれている可能性があります。最も正確な情報については、オリジナルの英語版をご覧ください。頻繁な更新のため、一部のコンテンツはオリジナルの英語になっている可能性があります。Crowdinでの取り組みに参加して、このページの翻訳改善にご協力ください。 (Crowdin translation page, Contributing guide)

目次

1. はじめに

このチュートリアルでは、EthereumアプリをKaiaに移行する方法を説明します。 カイアの経験は問わない。 ここでは、EthereumアプリをKaiaに移行するために必要なコード修正のみに焦点を当てます。 カイアの経験は問わない。 ここでは、EthereumアプリをKaiaに移行するために必要なコード修正のみに焦点を当てます。

2. 前提条件

  • EVMをサポートする開発者ツールや標準に精通している。
  • dAppを構築する基本的な知識

3. Kaia has Ethereum compatibility

Kaia実行環境はEthereum仮想マシンと互換性があり、Solidityで書かれたスマートコントラクトを実行する。 KaiaのRPC APIやその他のクライアント・ライブラリは、利用可能な限りEthereumとほぼ同じAPI仕様を維持している。 したがって、Ethereum AppsをKaiaに移行するのは非常に簡単です。 これは、開発者が新しいブロックチェーン・プラットフォームに簡単に移行するのに役立つ。

4. アプリの移行

以下の手順でEthereumアプリをKaiaに移行します:

  1. 契約ツールおよびSDKをKaia Network - Kairos Testnetをターゲットに設定します:

    • RPCエンドポイント: https://public-en-kairos.node.kaia.io
    • WebSocketエンドポイント(オプション):wss://public-en-kairos.node.kaia.io/ws`。
    • チェーン ID: 1001
  2. カイア・ウォレット](https://www.kaiawallet.io/)を使ってアカウントを作成し、[Faucet](https://faucet.kaia.io)からテスト資金を得る。

  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. カイアSDK](https://github.com/kaiachain/kaia-sdk)を使用して契約と対話します。 カイアSDK](https://github.com/kaiachain/kaia-sdk)を使用して契約と対話します。 viem](../../references/sdk/viem/viem.md)やweb3.pyのような他のツールキットも自由に使ってください。

    A. ブロックチェーンのデータを読む

    ブロック番号

    web3ライブラリをKaiaのRPCエンドポイントに置き換えるだけで、EthereumのBlockNumberの代わりにKaiaの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();

ページを改善してください。