-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadfromchain.html
54 lines (52 loc) · 2.01 KB
/
readfromchain.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ethereum RPC Call</title>
</head>
<body>
<h1>Ethereum RPC Call Example</h1>
<button id="getOwner">Get Owner</button>
<p id="result">Owner will appear here...</p>
<script>
document.getElementById('getOwner').onclick = async function() {
const url = 'https://rpc-mammothon-g2-testnet-4a2w8v0xqy.t.conduit.xyz'; // NOTE: update this to mammoth RPC endpoint
const payload = {
jsonrpc: '2.0',
method: 'eth_call',
// NOTE: This payload can be anything to read from chain
params: [
{
to: '0x2Bb71AE6f5Bb52de5F535efD804e156ed2a35a8f', // address of the HTML contract
data: '0x8da5cb5b' // Keccak256 of owner() ABI
},
'latest'
],
id: 1
};
try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
console.log(response)
const data = await response.json();
console.log(data)
// Handle the result (totalSupply is in the data.result, and is in hex format)
if (data.result) {
const owner = '0x' + data.result.slice(26);
document.getElementById('result').textContent = 'Owner: ' + owner;
} else {
document.getElementById('result').textContent = 'Error fetching data.';
}
} catch (error) {
document.getElementById('result').textContent = 'Error: ' + error.message;
}
};
</script>
</body>
</html>