-
Notifications
You must be signed in to change notification settings - Fork 14
/
static.html
246 lines (221 loc) · 15.8 KB
/
static.html
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/web3.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<title>MetaMask Web3js Contract Integration</title>
</head>
<body>
<style>
.meta-gray {
-webkit-filter: grayscale(1);
}
.meta-normal {
-webkit-filter: grayscale(0);
}
</style>
<div class="container">
<div style="margin-top: 30px;" class="row text-center">
<div class="col-md-12">
<h3>MetaMask Web3js Smart Contract Integration</h3>
<p>The code here is adapted from an excellent article
<a href="http://blog.adnansiddiqi.me/build-your-first-solidity-dapp-with-web3-js-and-metamask/" target="_blank">
Build Your First Solidity Dapp With Web3.js and MetaMask
</a>
by Adnan Siddiqi with minor modifications for the
<a href="https://github.com/limcheekin/eth-dapps-nextjs-boiletplate/blob/main/contracts/Greeter.sol" target="_blank">Greeter smart contract</a>,
full credit should be given to him.
</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<ul>
<li>
First, if you are running the sample dApp from Google Chrome, you need to start the server from the <code>client</code> directory
using <code>npm run dev</code> due to the CORS origin error.
</li>
<li>
Start the truffle development environment with <code>truffle develop</code>, run <code>compile</code> and <code>migrate</code> after it is started.
</li>
<li>Copy the contract address under <code>Deploying 'Greeter'</code>section, something like <code>0xd3bb084045105850C2C9E59d5e5Ae8Bf90D40829</code>
and update the <code>let contractAddress = '0x...'</code> at line 101.</li>
<li>You're ready! Go to <a href="http://localhost:3000/static.html">localhost:3000/static.html</a>.
</ul>
</div>
</div>
<div class="row text-center">
<div class="col-md-12">
<img id="metaicon" class="meta-gray" width="100" height="100" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAABUFBMVEX////2jCTlfyWASB7ax7nYbyHGtakiHx/yiSTkfyX81K/3jiD0iiTZcCH4jSR9Rh7pgiXgeCNxZ2ETERJ+RRjshST7+fjieiPlfR+FSx728u/QvrF+RBXayr798+rPcyPaeSTDbSLkeRLv6eSfd1vojkPmgyvvsn+2ZSGdWCB6OwCOUB+7nomzk32tYSD659fxvJHk29X538qWaEeMWDHEq5r+8eXKcSKjWyD54Mv5s3X42Lzoizb6v4n3mTzkdwDduJrcwKvhmV7du6IAAACKVSrg08qsh27vzK6kaDrrnFrvsHvrmE2Zbk6OXTrwuYyhe2HGjV/4pVj5sWr4okznpXfWZQDuvZvgiEnjlFv92bXZfjP3lzD7zaT2hQb6u4BiTT6hnp1vRiJPNSM4KCBdWVfPzcwcIiQAFB4fFhA+OjnerIW6t7VQTEqAe3iViYEx3gNeAAASJklEQVR4nO2d+1PbSBLHjZ04FliyHCxZfmJsHsY8TcAE7LzYDXCEQC4kkGxC9nK3e9lLbpP//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BqIRRRyGWUEfKMhwKiac4fyE3JMVDSMLoJkwkTsQIDWPp9KJfTyRaR4Lm4iGc/Rqj6yYSawxjcLjVr1tX1cvdo1qlMl7CiHML68Yu4hHihH16ve/pgSHIWjoNSekhnP359xiAsab6KGAuLS6fEFvRerdWgaMcEX7+/PlulJrbpYhTfQlZb+l0q09fTMDuiiARpTjm3Z8R259/fv367ePHj+m4O2FGSYkoixuLW8HPRYKQyF2xBCG/ff327WPaVuy/VVJf4jQi9s2D6U3OcGa5a1rEmJW0R/F3xuLqfhtN9Wz5ZDNKdx1b0rnJGJQVH6DAbq0nYXUN9s3Bcp/4wVY45PAWo1H6+NJdgVdP6sy6BtVjKOvtxz+/y5L8mH4+EQsiXTPw1MVnN/uCLwnqXshwygBf+kjs5SHKSpvRfDQ4mA6Lm/yQ3numUwb50jXB/ff0Z02Cb9oFJ5x8lqQY0x9gIADRVP+Rlw8FloOtbfidGf3uSqAk8MVPhCPVXZUbKji1w+vt+hh2LcQKuqvbZUl8olHGktNWREkdZb0xbKvpEcGSljGJfBWQvxa0P7DwcME5JuN5RHJXmmD2Za8fNlHNsjU23ySI7K5BHQFdr89fcAKKaskKVq121O2KlDIe6eMee7TrOpAYqGYxdVutcrmsI32fewKXXm61MJOu/0BQE0000UQTTTTRRBP9v+p/4tipoxa7/F8AAAAASUVORK5CYII=" alt="">
</div>
</div>
<div style="margin-top: 30px;" class="row text-center">
<div class="col-md-12">
<button id="enableMetamask" class="btn btn-primary">Connect with Metamask</button>
</div>
</div>
<div class="row text-center">
<div class="col-md-12">
<span id="status"></span>
</div>
</div>
<section class="contract-section hidden">
<div style="margin-top: 30px;" class="row text-center">
<div class="col-md-12">
<h5>Contract Info</h5>
</div>
</div>
<div class="row text-center">
<div class="col-md-12">
<span>Current Value: <i id="getValue"></i></span>
</div>
</div>
<div class="row text-center">
<div class="col-md-4 offset-4">
<span>Set Value: <input id="value" type="text" class="form-control" value=""> <button id="setValue" class="btn btn-primary">Set Value</button></span>
</div>
</div>
<div class="row text-center">
<div class="col-md-4 offset-4">
<span>Greet: <input id="name" type="text" class="form-control" value=""> <button id="greet" class="btn btn-primary">Greet</button></span>
</div>
</div>
<div class="row text-center">
<div class="col-md-12">
<span><i id="greeting"></i></span>
</div>
</div>
</section>
</div>
<script>
let currentAccount = null;
let web3;
let abi;
let contractAddress = '0x224bCcf610Af261eAF38F9a97C121da9515754Da'
function handleAccountsChanged(accounts) {
console.log('Calling HandleChanged')
if (accounts.length === 0) {
console.log('Please connect to MetaMask.');
$('#enableMetamask').html('Connect with Metamask')
} else if (accounts[0] !== currentAccount) {
currentAccount = accounts[0];
$('#enableMetamask').html(currentAccount)
$('#status').html('')
if(currentAccount != null) {
// Set the button label
$('#enableMetamask').html(currentAccount)
}
}
console.log('WalletAddress in HandleAccountChanged ='+currentAccount)
}
function connect() {
console.log('Calling connect()')
ethereum
.request({ method: 'eth_requestAccounts' })
.then(handleAccountsChanged)
.catch((err) => {
if (err.code === 4001) {
// EIP-1193 userRejectedRequest error
// If this happens, the user rejected the connection request.
console.log('Please connect to MetaMask.');
$('#status').html('You refused to connect Metamask')
} else {
console.error(err);
}
});
}
function detectMetaMask() {
try {
if (typeof window.ethereum !== 'undefined') {
web3 = new Web3(window.ethereum);
return true
} else {
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:9545"));
return false
}
} catch (error) {
alert(error)
}
}
async function getValue() {
console.log('GetValue')
const contract = new web3.eth.Contract(
abi,
contractAddress
);
contract.methods.greeting().call().then(function (result) {
$('#getValue').html(result)
});
}
async function setValue() {
console.log('Set Value')
input_value = $('#value').val()
if(input_value.trim() == '') {
input_value = 0
}
if(!isNaN(input_value)) {
input_value = parseInt(input_value)
}
const contract = new web3.eth.Contract(
abi,
contractAddress
);
contract.methods.setGreeting(input_value).send({from:currentAccount}).then(function (result) {
console.log(result);
getValue();
});
}
async function greet() {
console.log('Greet')
input_value = $('#name').val()
if(input_value.trim() == '') {
input_value = '[Please enter your name]'
}
if(!isNaN(input_value)) {
input_value = parseInt(input_value)
}
const contract = new web3.eth.Contract(
abi,
contractAddress
);
contract.methods.greet(input_value).call().then(function (result) {
console.log(result);
$('#greeting').html(result)
});
}
</script>
<script>
$.getJSON("Greeter.json", function(result) {
abi = result.abi
//Fetch Value from Smart Contract
getValue()
});
$( document ).ready(function() {
m = detectMetaMask()
if(m) {
$('#metaicon').removeClass('meta-gray')
$('#metaicon').addClass('meta-normal')
$('#enableMetamask').attr('disabled',false)
connect() // Make sure the connected wallet is being returned
} else {
$('#enableMetamask').attr('disabled',true)
$('#metaicon').removeClass('meta-normal')
$('#metaicon').addClass('meta-gray')
}
$('#enableMetamask').click(function() {
connect()
});
$('#setValue').click(function() {
setValue()
});
$('#greet').click(function() {
greet()
});
})
</script>
</body>
</html>