blockchain:ether.jsを使用したdapps開発
差分
このページの2つのバージョン間の差分を表示します。
| 両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
| blockchain:ether.jsを使用したdapps開発 [2022/05/10 06: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プロジェクトの生成 ===== | ||
| 行 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> | ||
| </ | </ | ||
| 行 119: | 行 107: | ||
| const [provider, setProvider] = useState(null); | const [provider, setProvider] = useState(null); | ||
| const [counter, setCounter] = useState(null); | const [counter, setCounter] = useState(null); | ||
| - | const [account, setAccount] = useState(); | + | const [account, setAccount] = useState(null); |
| - | const [count, setCount] = useState(); | + | const [count, setCount] = useState(null); |
| - | const [transactionHash, | + | const [transactionHash, |
| - | const [transactionInfo, | + | const [transactionInfo, |
| + | // counterAddress と counterAbi は環境によって書き換える必要があります。 | ||
| + | // Counater.json を読み込んで利用するとよりスマート。 | ||
| const counterAddress = " | const counterAddress = " | ||
| const counterAbi = [ | const counterAbi = [ | ||
| 行 181: | 行 171: | ||
| 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); | let provider = new ethers.providers.Web3Provider(window.ethereum); | ||
| + | setProvider(provider); | ||
| let signer = provider.getSigner(0); | let signer = provider.getSigner(0); | ||
| setCounter(new ethers.Contract(counterAddress, | setCounter(new ethers.Contract(counterAddress, | ||
| - | setProvider(provider); | ||
| }) | }) | ||
| .catch((error) => { | .catch((error) => { | ||
| 行 205: | 行 195: | ||
| const incCount = async () => { | const incCount = async () => { | ||
| let transaction = await counter.inc(); | let transaction = await counter.inc(); | ||
| + | // トランザクションが処理されるのを待つ | ||
| await transaction.wait(); | await transaction.wait(); | ||
| setTransactionHash(transaction.hash); | setTransactionHash(transaction.hash); | ||
| 行 213: | 行 204: | ||
| const decCount = async () => { | const decCount = async () => { | ||
| let transaction = await counter.dec(); | let transaction = await counter.dec(); | ||
| + | // トランザクションが処理されるのを待つ | ||
| await transaction.wait(); | await transaction.wait(); | ||
| setTransactionHash(transaction.hash); | setTransactionHash(transaction.hash); | ||
| 行 253: | 行 245: | ||
| export default App; | export default App; | ||
| </ | </ | ||
| + | |||
| + | ===== 実行 ===== | ||
| + | |||
| + | <code PowerShell> | ||
| + | react-etherjs> | ||
| + | </ | ||
| + | |||
| + | 「connect」をクリックすると MetaMask が立ち上がりますので、使用するアカウントを選択します。 | ||
| + | |||
| + | アカウントが選択できたら他のボタンをクリックすることで Counter スマートコントラクトの各メソッドが実行できます。 | ||
| + | |||
| + | {{: | ||
blockchain/ether.jsを使用したdapps開発.1652165729.txt.gz · 最終更新: by dot
