-
Notifications
You must be signed in to change notification settings - Fork 22
/
getsetemit.html
117 lines (116 loc) · 4.44 KB
/
getsetemit.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
<!-- https://github.com/web3examples/ethereum/blob/master/solidity_examples/GetSetEmit.sol -->
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://unpkg.com/web3@latest/dist/web3.min.js"></script>
</head>
<body>
<h1>GetSetEmit (select Goerli)</h1>
<pre id="log" style="width:100%;height:200px"></pre>
<script type="text/javascript">
function log(logstr) {
document.getElementById("log").innerHTML +=logstr+"\n";
}
const GetSetEmitABI= [
{ // ABI Log event Log(string message,address caller, uint8 value)
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "string",
"name": "message",
"type": "string"
},
{
"indexed": false,
"internalType": "address",
"name": "caller",
"type": "address"
},
{
"indexed": false,
"internalType": "uint8",
"name": "value",
"type": "uint8"
}
],
"name": "Log",
"type": "event"
},
{ // ABI Get function Get() public view returns (uint8)
"constant": true,
"inputs": [],
"name": "Get",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{ // ABI Set function Set(uint8 x) public returns (uint8)
"constant": false,
"inputs": [
{
"internalType": "uint8",
"name": "x",
"type": "uint8"
}
],
"name": "Set",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{ // ABI storedData uint8 public storedData
"constant": true,
"inputs": [],
"name": "storedData",
"outputs": [
{
"internalType": "uint8",
"name": "",
"type": "uint8"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
];
async function f() {
// Prepare
web3 = new Web3(Web3.givenProvider); // provider from metamask
log(`Version of web3.js: ${web3.version}`);
var result=await web3.eth.requestAccounts().catch(x=>log(x.message));
log(`Value from eth_requestAccounts: ${JSON.stringify(result)}`);
var acts=await web3.eth.getAccounts().catch(log);
log(`Here are the accounts: ${JSON.stringify(acts)}`);
// Connect contract
var GetSetEmitAddress="0x3aEA1b69A0de7619f3538f98ca90dD0013589a5f"; // Goerli address
var GetSetEmitContract=new web3.eth.Contract(GetSetEmitABI, GetSetEmitAddress);
// Get
result = await GetSetEmitContract.methods.Get().call({from: acts[0]}).catch(console.log);
log(`Get result: ${result}`);
// Set
log(`Wait about 20 seconds for the transaction to process`);
result = await GetSetEmitContract.methods.Set(6).send({from: acts[0]}).catch(console.log);
var decoded=result.events.Log.returnValues;
log(`Event emitted: message=${decoded.message} address=${decoded.caller} value=${decoded.value}`);
}
window.addEventListener('DOMContentLoaded', f);
</script>
</body>
</html>