blockchain:ether.jsを使用したdapps開発
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| blockchain:ether.jsを使用したdapps開発 [2022/05/08 11:55] – 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 を開発します。 | ||
| + | |||
| + | 完成したスースコードは [[https:// | ||
| + | |||
| + | 下記手順を実行するか、上記リポジトリを利用してください。 | ||
| + | |||
| + | ===== 前提 ===== | ||
| + | |||
| + | [[blockchain: | ||
| + | |||
| + | MetaMask を使用しますので、[[blockchain: | ||
| ===== Reactプロジェクトの生成 ===== | ===== Reactプロジェクトの生成 ===== | ||
| 行 83: | 行 92: | ||
| </ | </ | ||
| + | ===== フロントエンドの実装 ===== | ||
| - | ===== ether.js のインストール ===== | + | <code PowerShell> |
| + | react-etherjs> | ||
| + | </ | ||
| - | [[blockchain: | + | <code JavaScript> |
| + | import './App.css'; | ||
| - | :!: 本来は truffle-config.js の設定を変更して、Counter.json を react-etherjs\src\contracts に保存するようにするのが一般的です。 | + | import React, { useState } from "react"; |
| + | import { ethers } from " | ||
| - | <code PowerShell> | + | function App() { |
| - | react-etherjs> | + | 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()); | ||
| + | }; | ||
| - | Mode | + | // Counter.inc を呼び出す関数 |
| - | ---- | + | |
| - | d----- | + | 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(); | ||
| + | }; | ||
| - | react-etherjs> cp ..\contracts\Counter.sol .\src\contracts\. | + | // transactionHash からトランザクション情報を取得する関数 |
| + | const getTransactionInfo = async () => { | ||
| + | let transactionInfo = await provider.getTransaction(transactionHash); | ||
| + | setTransactionInfo(JSON.stringify(transactionInfo, | ||
| + | }; | ||
| + | |||
| + | return ( | ||
| + | <div className=" | ||
| + | <header className=" | ||
| + | < | ||
| + | Account: { account } | ||
| + | < | ||
| + | Count: { count } | ||
| + | </ | ||
| + | < | ||
| + | TransactionHash: | ||
| + | </ | ||
| + | <input type=" | ||
| + | <input type=" | ||
| + | <input type=" | ||
| + | <input type=" | ||
| + | <input type=" | ||
| + | </ | ||
| + | < | ||
| + | < | ||
| + | { transactionInfo } | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | </ | ||
| + | ); | ||
| + | } | ||
| + | |||
| + | export default App; | ||
| </ | </ | ||
| + | |||
| + | ===== 実行 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | react-etherjs> | ||
| + | </ | ||
| + | |||
| + | 「connect」をクリックすると MetaMask が立ち上がりますので、使用するアカウントを選択します。 | ||
| + | |||
| + | アカウントが選択できたら他のボタンをクリックすることで Counter スマートコントラクトの各メソッドが実行できます。 | ||
| + | |||
| + | {{: | ||
blockchain/ether.jsを使用したdapps開発.1652010915.txt.gz · 最終更新: by dot
