blockchain:ether.jsを使用したdapps開発
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
blockchain:ether.jsを使用したdapps開発 [2022/05/08 11:32] – dot | blockchain:ether.jsを使用したdapps開発 [2022/08/04 08:56] (現在) – dot | ||
---|---|---|---|
行 1: | 行 1: | ||
- | |||
====== ether.jsを使用したDApps開発 ====== | ====== ether.jsを使用したDApps開発 ====== | ||
ether.js と React を使用して DApps を開発します。 | ether.js と React を使用して DApps を開発します。 | ||
- | ===== Reactプロジェクトの生成 ===== | + | 完成したスースコードは [[https:// |
+ | 下記手順を実行するか、上記リポジトリを利用してください。 | ||
+ | |||
+ | ===== 前提 ===== | ||
+ | |||
+ | [[blockchain: | ||
+ | |||
+ | MetaMask を使用しますので、[[blockchain: | ||
+ | |||
+ | ===== Reactプロジェクトの生成 ===== | ||
<code PowerShell> | <code PowerShell> | ||
行 75: | 行 83: | ||
> cd react-etherjs | > cd react-etherjs | ||
</ | </ | ||
+ | |||
+ | ===== ether.js のインストール ===== | ||
+ | |||
+ | ether.js の npm パッケージ名は ethers なので、それをインストールします。 | ||
+ | |||
+ | <code PowerShell> | ||
+ | react-etherjs> | ||
+ | </ | ||
+ | |||
+ | ===== フロントエンドの実装 ===== | ||
+ | |||
+ | <code PowerShell> | ||
+ | react-etherjs> | ||
+ | </ | ||
+ | |||
+ | <code JavaScript> | ||
+ | import ' | ||
+ | |||
+ | import React, { useState } from " | ||
+ | import { ethers } from " | ||
+ | |||
+ | function App() { | ||
+ | const [provider, setProvider] = useState(null); | ||
+ | const [counter, setCounter] = useState(null); | ||
+ | const [account, setAccount] = useState(null); | ||
+ | const [count, setCount] = useState(null); | ||
+ | const [transactionHash, | ||
+ | const [transactionInfo, | ||
+ | |||
+ | // counterAddress と counterAbi は環境によって書き換える必要があります。 | ||
+ | // Counater.json を読み込んで利用するとよりスマート。 | ||
+ | const counterAddress = " | ||
+ | const counterAbi = [ | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ], | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }, | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | ]; | ||
+ | |||
+ | // | ||
+ | const connectMetaMask= () => { | ||
+ | if (window.ethereum && window.ethereum.isMetaMask) { | ||
+ | window.ethereum | ||
+ | .request({ method: " | ||
+ | .then((accounts) => { | ||
+ | setAccount(accounts[0]); | ||
+ | let provider = new ethers.providers.Web3Provider(window.ethereum); | ||
+ | setProvider(provider); | ||
+ | let signer = provider.getSigner(0); | ||
+ | setCounter(new ethers.Contract(counterAddress, | ||
+ | }) | ||
+ | .catch((error) => { | ||
+ | console.log(" | ||
+ | }); | ||
+ | } else { | ||
+ | console.log(" | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | // Counter.get でカウントの値を取得する関数 | ||
+ | const getCount = async () => { | ||
+ | let count = await counter.get(); | ||
+ | setCount(count.toNumber()); | ||
+ | }; | ||
+ | |||
+ | // Counter.inc を呼び出す関数 | ||
+ | const incCount = async () => { | ||
+ | let transaction = await counter.inc(); | ||
+ | // トランザクションが処理されるのを待つ | ||
+ | await transaction.wait(); | ||
+ | setTransactionHash(transaction.hash); | ||
+ | getCount(); | ||
+ | }; | ||
+ | |||
+ | // Counterdec を呼び出す関数 | ||
+ | const decCount = async () => { | ||
+ | let transaction = await counter.dec(); | ||
+ | // トランザクションが処理されるのを待つ | ||
+ | await transaction.wait(); | ||
+ | setTransactionHash(transaction.hash); | ||
+ | getCount(); | ||
+ | }; | ||
+ | |||
+ | // transactionHash からトランザクション情報を取得する関数 | ||
+ | const getTransactionInfo = async () => { | ||
+ | let transactionInfo = await provider.getTransaction(transactionHash); | ||
+ | setTransactionInfo(JSON.stringify(transactionInfo, | ||
+ | }; | ||
+ | |||
+ | return ( | ||
+ | <div className=" | ||
+ | <header className=" | ||
+ | <div> | ||
+ | Account: { account } | ||
+ | <div> | ||
+ | Count: { count } | ||
+ | </ | ||
+ | <div> | ||
+ | TransactionHash: | ||
+ | </ | ||
+ | <input type=" | ||
+ | <input type=" | ||
+ | <input type=" | ||
+ | <input type=" | ||
+ | <input type=" | ||
+ | </ | ||
+ | <div> | ||
+ | <pre> | ||
+ | { transactionInfo } | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | ); | ||
+ | } | ||
+ | |||
+ | export default App; | ||
+ | </ | ||
+ | |||
+ | ===== 実行 ===== | ||
+ | |||
+ | <code PowerShell> | ||
+ | react-etherjs> | ||
+ | </ | ||
+ | |||
+ | 「connect」をクリックすると MetaMask が立ち上がりますので、使用するアカウントを選択します。 | ||
+ | |||
+ | アカウントが選択できたら他のボタンをクリックすることで Counter スマートコントラクトの各メソッドが実行できます。 | ||
+ | |||
+ | {{: |
blockchain/ether.jsを使用したdapps開発.1652009535.txt.gz · 最終更新: 2022/05/08 11:32 by dot