blockchain:ether.jsを使用したdapps開発
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| blockchain:ether.jsを使用したdapps開発 [2022/05/09 07:40] – 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プロジェクトの生成 ===== | ||
| 行 81: | 行 90: | ||
| <code PowerShell> | <code PowerShell> | ||
| react-etherjs> | react-etherjs> | ||
| - | </ | ||
| - | |||
| - | ===== Counter.json のコピー ===== | ||
| - | |||
| - | [[blockchain: | ||
| - | |||
| - | :!: 本来は truffle-config.js の設定を変更して、Counter.json を react-etherjs\src\contracts に保存するようにするのが一般的です。 | ||
| - | |||
| - | <code PowerShell> | ||
| - | react-etherjs> | ||
| - | |||
| - | |||
| - | ディレクトリ: | ||
| - | |||
| - | |||
| - | Mode | ||
| - | ---- | ||
| - | d----- | ||
| - | |||
| - | |||
| - | react-etherjs> | ||
| </ | </ | ||
| 行 111: | 行 99: | ||
| <code JavaScript> | <code JavaScript> | ||
| - | import logo from ' | ||
| import ' | import ' | ||
| - | import React, { useEffect, | + | import React, { useState } from " |
| import { ethers } from " | import { ethers } from " | ||
| - | //import Counter from " | ||
| function App() { | function App() { | ||
| - | const [account, setAccount] = useState(); | + | |
| - | const [count, setCount] = useState(); | + | const [counter, setCounter] = useState(null); |
| - | const [transactionHash, | + | |
| - | const [transactionInfo, | + | const [count, setCount] = useState(null); |
| - | //const provider = new ethers.providers.Web3Provider(window.ethereum); | + | const [transactionHash, |
| + | const [transactionInfo, | ||
| - | //const provider = new ethers.providers.JsonRpcProvider(" | + | // counterAddress と counterAbi は環境によって書き換える必要があります。 |
| - | const counterAddress = "0x791C2E0e40B2b96f3aE2F5D3F3661478f73fB49D"; | + | |
| + | const counterAddress = "0x5E5A16AaFb816F04E09a05d1C03d98D7b1ee56C2"; | ||
| const counterAbi = [ | const counterAbi = [ | ||
| { | { | ||
| 行 177: | 行 165: | ||
| } | } | ||
| ]; | ]; | ||
| - | //const Counter = new ethers.Contract(counterAddress, | ||
| - | | + | |
| const connectMetaMask= () => { | const connectMetaMask= () => { | ||
| if (window.ethereum && window.ethereum.isMetaMask) { | if (window.ethereum && window.ethereum.isMetaMask) { | ||
| window.ethereum | window.ethereum | ||
| .request({ method: " | .request({ method: " | ||
| - | .then((result) => { | + | .then((accounts) => { |
| - | setAccount(result[0]); | + | 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) => { | .catch((error) => { | ||
| - | console.log(" | + | console.log(" |
| }); | }); | ||
| } else { | } else { | ||
| 行 195: | 行 186: | ||
| }; | }; | ||
| - | | + | // Counter.get でカウントの値を取得する関数 |
| - | | + | |
| - | // 先頭のアカウントを | + | |
| - | //let coinbase = accounts[0]; | + | |
| - | //return coinbase; | + | |
| - | }; | + | |
| const getCount = async () => { | const getCount = async () => { | ||
| - | | + | |
| - | | + | |
| }; | }; | ||
| - | const incCount = () => { | + | |
| + | | ||
| + | let transaction = await counter.inc(); | ||
| + | // トランザクションが処理されるのを待つ | ||
| + | await transaction.wait(); | ||
| + | setTransactionHash(transaction.hash); | ||
| + | getCount(); | ||
| }; | }; | ||
| - | const decCount = () => { | + | |
| + | | ||
| + | let transaction = await counter.dec(); | ||
| + | // トランザクションが処理されるのを待つ | ||
| + | await transaction.wait(); | ||
| + | setTransactionHash(transaction.hash); | ||
| + | getCount(); | ||
| }; | }; | ||
| - | const getTransactionInfo = () => { | + | |
| + | | ||
| + | let transactionInfo = await provider.getTransaction(transactionHash); | ||
| + | setTransactionInfo(JSON.stringify(transactionInfo, | ||
| }; | }; | ||
| 行 225: | 行 225: | ||
| </ | </ | ||
| <div> | <div> | ||
| - | | + | |
| </ | </ | ||
| <input type=" | <input type=" | ||
| 行 234: | 行 234: | ||
| </ | </ | ||
| <div> | <div> | ||
| - | { transactionInfo } | + | |
| + | | ||
| + | </ | ||
| </ | </ | ||
| </ | </ | ||
| 行 243: | 行 245: | ||
| export default App; | export default App; | ||
| </ | </ | ||
| + | |||
| + | ===== 実行 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | react-etherjs> | ||
| + | </ | ||
| + | |||
| + | 「connect」をクリックすると MetaMask が立ち上がりますので、使用するアカウントを選択します。 | ||
| + | |||
| + | アカウントが選択できたら他のボタンをクリックすることで Counter スマートコントラクトの各メソッドが実行できます。 | ||
| + | |||
| + | {{: | ||
blockchain/ether.jsを使用したdapps開発.1652082054.txt.gz · 最終更新: by dot
