ユーザ用ツール

サイト用ツール


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 logo from './logo.svg';
import './App.css';
 
import React, { useEffect, useState } from "react";
import { ethers } from "ethers";
import Counter from "./contracts/Counter.json";
 
function App() {
  const [count, setCount] = useState();
  const [transactionHash, setTransactionHash] = useState();
  const [transactionInfo, setTransactionInfo] = useState();
 
  return (
    <div className="App">
      <header className="App-header">
      <div>
        <div>
            Count: { count }
        </div>
        <div>
            Transaction: { transactionHash }
        </div>
        <input type="button" value="get" onClick="get();"/>
        <input type="button" value="inc" onClick="inc();"/>
        <input type="button" value="dec" onClick="dec();"/>
        <input type="button" value="getTransactionInfo" onClick="getTransactionInfo()"/>
      </div>
      <div>
        { transactionInfo }
      </div>
      </header>
    </div>
  );
}
 
export default App;
blockchain/ether.jsを使用したdapps開発.1652043736.txt.gz · 最終更新: by dot