blockchain:ether.jsを使用したdapps開発
文書の過去の版を表示しています。
ether.jsを使用したDApps開発
ether.js と React を使用して DApps を開発します。
Reactプロジェクトの生成
ether.js のインストール
ether.js の npm パッケージ名は ethers なので、それをインストールします。
Counter.json のコピー
Truffleを使ったスマートコントラクト開発 で Counter スマートコントラクトを build すると Counter スマートコントラクトの情報(ABIやaddressなど)が truffle_project\build\contracts\Counter.json に保存されています。今回はその Counter.json を react-etherjs\src\contracts\Counter.json にコピーして使用します。
本来は truffle-config.js の設定を変更して、Counter.json を react-etherjs\src\contracts に保存するようにするのが一般的です。
react-etherjs> mkdir .\src\contracts ディレクトリ: C:\Users\miyazato\work\ethereum\react-etherjs\src Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 2022/05/08 20:50 contracts react-etherjs> cp ..\contracts\Counter.sol .\src\contracts\.
フロントエンドの実装
import './App.css'; import React, { useState } from "react"; import { ethers } from "ethers"; function App() { const [provider, setProvider] = useState(null); const [counter, setCounter] = useState(null); const [account, setAccount] = useState(); const [count, setCount] = useState(); const [transactionHash, setTransactionHash] = useState(); const [transactionInfo, setTransactionInfo] = useState(); const counterAddress = "0x5E5A16AaFb816F04E09a05d1C03d98D7b1ee56C2"; const counterAbi = [ { "constant": true, "inputs": [], "name": "count", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "inc", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [], "name": "dec", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" } ]; //メタマスクの接続 const connectMetaMask= () => { if (window.ethereum && window.ethereum.isMetaMask) { window.ethereum .request({ method: "eth_requestAccounts" }) .then((result) => { setAccount(result[0]); let provider = new ethers.providers.Web3Provider(window.ethereum); let signer = provider.getSigner(0); setCounter(new ethers.Contract(counterAddress, counterAbi, signer)); setProvider(provider); }) .catch((error) => { console.log("MetaMask error" + error) }); } else { console.log("Need to install MetaMask"); } }; // 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, null, 2)); }; return ( <div className="App"> <header className="App-header"> <div> Account: { account } <div> Count: { count } </div> <div> TransactionHash: { transactionHash } </div> <input type="button" value="connect" onClick={ connectMetaMask }/> <input type="button" value="get" onClick={ getCount }/> <input type="button" value="inc" onClick={ incCount }/> <input type="button" value="dec" onClick={ decCount }/> <input type="button" value="getTransactionInfo" onClick={ getTransactionInfo } /> </div> <div> <pre> { transactionInfo } </pre> </div> </header> </div> ); } export default App;
blockchain/ether.jsを使用したdapps開発.1652165729.txt.gz · 最終更新: by dot
