-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
94 lines (82 loc) · 3.23 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import './App.css';
import { useState } from 'react';
import { ethers } from 'ethers';
import Greeter from './artifacts/contracts/Greeter.sol/Greeter.json'
import KowloonToken from './artifacts/contracts/KowloonToken.sol/KowloonToken.json'
// Hardhat addresses
const greeterAddress = '0x5FbDB2315678afecb367f032d93F642f64180aa3';
const tokenAddress = '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0';
// Ropsten addresses
// const greeterAddress = '0x046FD7e8BDF20AbbF30D3c8e6D4c157E2596201b';
// const tokenAddress = '';
function App() {
const [greeting, setGreetingValue] = useState();
const [userAccount, setUserAccount] = useState();
const [amount, setAmount] = useState();
async function requestAccount() {
await window.ethereum.request({
method: 'eth_requestAccounts',
});
}
async function fetchGreeting() {
if (typeof window.ethereum !== 'undefined') {
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(greeterAddress, Greeter.abi, provider);
try {
const data = await contract.greet();
console.log('data: ', data);
} catch (err) {
console.log("Error: ", err);
}
}
}
async function getBalance() {
if (typeof window.ethereum !== 'undefined') {
const [account] = await window.ethereum.request({
method: 'eth_requestAccounts',
});
const provider = new ethers.providers.Web3Provider(window.ethereum);
const contract = new ethers.Contract(tokenAddress, KowloonToken.abi, provider);
const balance = await contract.balanceOf(account);
console.log("Balance: ", balance.toString());
}
}
async function setGreeting() {
if (!greeting) return
if (typeof window.ethereum !== 'undefined') {
await requestAccount();
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(greeterAddress, Greeter.abi, signer);
const transaction = await contract.setGreeting(greeting);
await transaction.wait();
fetchGreeting();
}
}
async function sendCoins() {
if (typeof window.ethereum !== 'undefined') {
await requestAccount();
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const contract = new ethers.Contract(tokenAddress, KowloonToken.abi, signer);
const transaction = await contract.transfer(userAccount, amount);
await transaction.wait();
console.log(`${amount} Coins successfully sent to ${userAccount}`);
}
}
return (
<div className="App">
<header className="App-header">
<button onClick={fetchGreeting}>Fetch Greeting</button>
<button onClick={setGreeting}>Set Greeting</button>
<input onChange={e => setGreetingValue(e.target.value)} placeholder="Set greeting" />
<br />
<button onClick={getBalance}>Get Balance</button>
<button onClick={sendCoins}>Send Coins</button>
<input onChange={e => setUserAccount(e.target.value)} placeholder="Account ID" />
<input onChange={e => setAmount(e.target.value)} placeholder="Amount" />
</header>
</div>
)
}
export default App;