forked from BlockadeWeb3Labs/web3-contract-interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
188 lines (163 loc) · 5.38 KB
/
index.html
File metadata and controls
188 lines (163 loc) · 5.38 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Web3 Contract Interface</title>
<script src="./node_modules/web3/dist/web3.min.js"></script>
<style>
.container {
display: flex;
}
.container div {
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<div>
<h1>Smart Contract Interface</h1>
<p>
<label for="contract">Contract Address</label><br />
<input id="contract" type="text" size="50">
</p>
<p>
<label for="abi">Contract ABI (JSON)</label><br />
<textarea name="abi" id="abi" cols="50" rows="8">{}</textarea>
</p>
<p>
<strong>Chain ID:</strong> <span id="info-chain-id"></span><br />
<strong>Latest Block #:</strong> <span id="info-block-number"></span><br />
<strong>Protocol Version:</strong> <span id="info-protocol"></span><br />
<strong>Gas Price:</strong> <span id="info-gas-price"></span><br />
<strong>Your Address:</strong> <small id="info-address"></small>
</p>
</div>
<div>
<h1>Transaction Information</h1>
<p>
<label for="method">Method</label><br />
<input id="method" type="text" size="50">
</p>
<p>
<label for="values">Parameters (array)</label><br />
<textarea name="values" id="values" cols="50" rows="8">[]</textarea>
</p>
<p>
<button id="button" onClick="sendTx()">Send Transaction</button>
</p>
<p>
<strong>Result:</strong><br />
<span id="result">No transaction sent yet</span>
</p>
</div>
</div>
<script>
// Pull from local storage if previous values exist
document.getElementById('contract').value = localStorage.getItem('contract');
document.getElementById('method').value = localStorage.getItem('method');
document.getElementById('abi').value = localStorage.getItem('abi') || '{}';
document.getElementById('values').value = localStorage.getItem('values') || '[]';
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
window.ethereum.enable();
} else {
alert("Web3 is not injected.");
}
web3.eth.getProtocolVersion().then(function(protocolVersion) {
document.getElementById('info-protocol').innerHTML = protocolVersion;
});
web3.eth.getGasPrice().then(function(gasPrice) {
document.getElementById('info-gas-price').innerHTML = web3.utils.fromWei(gasPrice, 'gwei') + " gwei";
});
web3.eth.getBlockNumber().then(function(blockNumber) {
document.getElementById('info-block-number').innerHTML = blockNumber;
});
web3.eth.getChainId().then(function(chainId) {
document.getElementById('info-chain-id').innerHTML = chainId;
});
var account;
web3.eth.getAccounts().then(function(accounts) {
if (accounts.length) {
account = accounts[0];
document.getElementById('info-address').innerHTML = account;
} else {
web3.eth.requestAccounts().then(function(accounts) {
account = accounts[0];
document.getElementById('info-address').innerHTML = account;
});
}
})
function sendTx() {
if (!account) {
alert("No account approved to interact with this page");
return;
}
let contract = document.getElementById('contract').value;
let abi = document.getElementById('abi').value;
let method = document.getElementById('method').value;
let values = document.getElementById('values').value;
try {
abi = JSON.parse(abi);
} catch (ex) {
alert("Invalid JSON for ABI");
return;
}
try {
values = JSON.parse(values);
} catch (ex) {
alert("Invalid JSON for method parameters");
return;
}
// Store info locally for follow-up calls
localStorage.setItem('contract', contract);
localStorage.setItem('abi', JSON.stringify(abi));
localStorage.setItem('method', method);
localStorage.setItem('values', JSON.stringify(values));
try {
let contractWeb3 = new web3.eth.Contract(abi, contract, {
'from' : account
});
for (let func of abi) {
if (
func.hasOwnProperty('type') &&
func.type === 'function' &&
(func.stateMutability === 'view' || func.stateMutability === 'pure') &&
func.name === method
) {
document.getElementById('result').innerHTML = `Calling method ${method}...`;
contractWeb3.methods[method](...values).call((err, res) => {
document.getElementById('result').innerHTML = res;
});
return;
} else if (
func.hasOwnProperty('type') &&
func.type === 'function' &&
(func.stateMutability === 'payable' || func.stateMutability === 'nonpayable') &&
func.name === method
) {
document.getElementById('result').innerHTML = `Sending tx: ${method}...`;
contractWeb3.methods[method](...values).send()
.on('transactionHash', function(hash){
document.getElementById('result').innerHTML = `Submitted, tx hash ${hash}...`;
})
.on('receipt', function(receipt){
document.getElementById('result').innerHTML = "<pre>" + JSON.stringify(receipt, null, 2) + "</pre>";
})
.on('error', function(error, receipt) {
document.getElementById('result').innerHTML = error + "<br /><br />Receipt<br /><pre>" + JSON.stringify(receipt, null, 2) + "</pre>";
});
return;
}
}
} catch (ex) {
console.error(ex);
document.getElementById('result').innerHTML = String(ex);
return;
}
}
</script>
</body>
</html>