-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.html
159 lines (122 loc) · 6.33 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="https://web3examples.com/logo.png">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta charset="UTF-8">
<script src="https://unpkg.com/web3@latest/dist/web3.min.js"></script>
</head>
<html>
<style>
.loader {
border: 8px solid #f3f3f3;
border-radius: 50%;
border-top: 8px solid #3498db;
width: 20px;
height: 20px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
text-align: center;
}
/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<h1>Web3Examples Faucet</h1>
My own Balance: <input type="text" name="balance" id="balance" value="0"/>ETH<br>
Faucet Balance: <input type="text" name="faucet" id="faucet" value="0"/>ETH<br>
<br>
Get / Give: <input type="text" name="amount" id="amount" value="0"/>ETH<br>
<br>
<div class="loader" id="spinner" style="display:none">#</div>
<input type="button" id="give" value="Give to faucet" style="border-radius: 10px;font-size : 30px;width: 200;height: 50;color:yellow;background-color:Red " class="primary" onclick="doTransferEth(addressSelf,addressFaucet,10)"/>
<input type="button" id="get" value="Get from faucet" style="border-radius: 10px;font-size : 30px;width: 200;height: 50;color:yellow;background-color:Green " class="primary" onclick="doTransferEth(addressFaucet,addressSelf,0.1)"/>
<br>
<pre id="log" style="width:100%;height:200px"></pre>
<script>
const privateKey= '0x0da19552d21de3da01e4a5ff72f6722b9a86c78ee6c6a46e5cdcf0fb5a936110'; // note very insecure, but for test ETH this is usable
var addressFaucet = 0;
var addressSelf = 0;
const amount =0.1;
var network;
async function EtherscanLink(pref,link) {
var nwType = await web3.eth.net.getNetworkType();
return `<a href="https://${nwType}.etherscan.io/${pref}/${link}" target="_blank" >${link}</a>`;
}
function log(str,...arguments) {
var logstr=arguments.toString();
document.getElementById("log").innerHTML +=str+" "+logstr+"\n";
}
function precise(x) {
return Number.parseFloat(x).toFixed(5);
}
async function doTransferEth(address1,address2,maxValue) {
var result = 0
var amount = document.getElementById("amount").value;
if (amount > maxValue) {
log (`Max amount to transfer is ${maxValue} ETH`);
return;
}
var amountWei=web3.utils.toWei(amount )
document.getElementById("give").style.display = "none";
document.getElementById("get").style.display = "none";
document.getElementById("spinner").style.display = "block";
result = await web3.eth.sendTransaction({from: address1,to: address2,gas: 200000,value: amountWei}).catch(x => log(`Error: ${x.code} ${x.message}`));
document.getElementById("spinner").style.display = "none";
document.getElementById("give").style.display = "block";
document.getElementById("get").style.display = "block";
console.log(result);
if (result)
log(`Transaction: ${await EtherscanLink("tx",result.transactionHash)}`);
updateBalances()
}
async function updateBalances() {
var weiBalance=await web3.eth.getBalance(addressSelf)
var ethBalance = web3.utils.fromWei(weiBalance, 'ether');
document.getElementById("balance").value = precise(ethBalance).toString();
var weiFaucetBalance=await web3.eth.getBalance(addressFaucet)
document.getElementById("faucet").value = precise(web3.utils.fromWei(weiFaucetBalance, 'ether'));
}
async function asyncloaded() {
if (typeof ethereum !== 'undefined')
await ethereum.enable().catch( (reason) => log('User rejected provider access',reason.message.substring(0,50)) )
if ((typeof ethereum == 'undefined')|| !Web3.givenProvider) { // Checking if web3 is accessible
log("Web3 interface is not found. "+
"Please install <a href='https://metamask.io'>Metamask</a>"+
" or use <a href='https://www.opera.com/crypto'>Opera Crypto (android)");
return; // stop this function
}
web3 = new Web3(Web3.givenProvider || ethereum); // switch to version 1.0 when using metamask; provide web3 functions for Opera
//typeof web3 === 'undefined' || web3 == 'undefined'
log(`web3 is present: ${web3.version}`); // note: use ` (back quote)
// var chainId = await web3.eth.getChainId();
var chainId = await web3.eth.getChainId();
//var chainId=parseInt(chainIdResult.result)
log (`Found chainid ${chainId}`);
// const chainInfo = await fetch(`https://raw.githubusercontent.com/ethereum-lists/chains/master/_data/chains/${chainId}.json`);
// const chainJson = await chainInfo.json();
// console.log(chainJson);
// log(`Chain id: ${chainId} name: ${chainJson.name} shortname: ${chainJson.shortName} <a href="${chainJson.infoURL}" target="_blank" >more info</a>`);
network = await web3.eth.net.getNetworkType().catch( (reason) => log(`Cannnot find network ${reason}`) );
log(`Ethereum network: ${network}`)
if (typeof network === 'undefined' || network == "main")
{ log("Please select a test network");return;}
addressFaucet = web3.eth.accounts.privateKeyToAccount(privateKey).address; // vanity address: '0x111Eb30919a11DA63423cECC8424969A5cF18099': ~Web3
web3.eth.accounts.wallet.add(privateKey);
console.log (addressFaucet);
var accounts=await web3.eth.getAccounts();
addressSelf = accounts[0];
log(`My own address: ${await EtherscanLink("address",addressSelf)}`);
log(`Faucet address: ${await EtherscanLink("address",addressFaucet)}`);
document.getElementById("amount").value = precise(0.01).toString();
updateBalances()
}
window.addEventListener('load', asyncloaded);
</script>
</html>