ウォレットをWEBアプリに接続しよう
🌅 window.ethereum
を設定する
Webアプリケーション上で、ユーザーがイーサリアムネットワークと通信するためには、Webアプリケーションはユーザーのウォレット情報を取得する必要があります。
これから、あなたのWebアプリケーションにウォレットを接続したユーザーに、スマートコントラクトを呼び出す権限を付与する機能を実装していき ます。これは、Webサイトへの認証機能です。
ターミナル上で、packages/client/src
に移動し、その中にあるApp.js
をVS Codeで開きましょう。
下記のように、App.js
の中身を更新します。
App.js
はあなたのWebアプリケーションのフロントエンド機能を果たします。
import React, { useEffect } from "react";
import "./App.css";
const App = () => {
const checkIfWalletIsConnected = () => {
/*
* window.ethereumにアクセスできることを確認します。
*/
const { ethereum } = window;
if (!ethereum) {
console.log("Make sure you have MetaMask!");
} else {
console.log("We have the ethereum object", ethereum);
}
};
/*
* WEBページがロードされたときに下記の関数を実行します。
*/
useEffect(() => {
checkIfWalletIsConnected();
}, []);
return (
<div className="mainContainer">
<div className="dataContainer">
<div className="header">
<span role="img" aria-label="hand-wave">
👋
</span>{" "}
WELCOME!
</div>
<div className="bio">
イーサリアムウォレットを接続して、「
<span role="img" aria-label="hand-wave">
👋
</span>
(wave)」を送ってください
<span role="img" aria-label="shine">
✨
</span>
</div>
<button className="waveButton" onClick={null}>
Wave at Me
</button>
</div>
</div>
);
};
export default App;