blockchain:ether.jsを使用したdapps開発
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
blockchain:ether.jsを使用したdapps開発 [2022/05/08 12:53] – 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 [count, setCount] = useState(); | + | |
- | const [transactionHash, | + | const [counter, setCounter] = useState(null); |
- | const [transactionInfo, | + | const [account, setAccount] = 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 ( | return ( | ||
<div className=" | <div className=" | ||
<header className=" | <header className=" | ||
- | | + | |
- | <p> | + | Account: |
- | Edit & | + | < |
- | </p> | + | Count: { count } |
- | <a | + | </ |
- | className="App-link" | + | < |
- | href="https:// | + | TransactionHash: |
- | | + | </ |
- | rel="noopener noreferrer" | + | <input type="button" |
- | > | + | <input type=" |
- | | + | <input type=" |
- | </a> | + | <input type="button" |
+ | < | ||
+ | </ | ||
+ | <div> | ||
+ | | ||
+ | | ||
+ | </pre> | ||
+ | </div> | ||
</ | </ | ||
</ | </ | ||
行 145: | 行 245: | ||
export default App; | export default App; | ||
</ | </ | ||
+ | |||
+ | ===== 実行 ===== | ||
+ | |||
+ | <code PowerShell> | ||
+ | react-etherjs> | ||
+ | </ | ||
+ | |||
+ | 「connect」をクリックすると MetaMask が立ち上がりますので、使用するアカウントを選択します。 | ||
+ | |||
+ | アカウントが選択できたら他のボタンをクリックすることで Counter スマートコントラクトの各メソッドが実行できます。 | ||
+ | |||
+ | {{: |
blockchain/ether.jsを使用したdapps開発.1652014392.txt.gz · 最終更新: 2022/05/08 12:53 by dot