-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyproject.js
157 lines (136 loc) · 5.24 KB
/
myproject.js
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
const loadWindow = () =>
{
console.log("Load successful")
// fetch data for ether price
var ethusd = ''
async function fetchEtherPrice()
{
let url = 'https://api.etherscan.io/api?module=stats&action=ethprice&apikey=BQACZHUPRA9RYUBU7T4W32DF71RKJIEKIS'
let response = await fetch(url)
let priceData = await response.json()
// console.log(priceData.result)
ethusd = priceData.result.ethusd
const ethbtc = priceData.result.ethbtc
const priceString = `$${ethusd} @ ${ethbtc} BTC/ETH`
// console.log(priceString)
print(priceString,"etherVal")
}
fetchEtherPrice().catch( err =>
{
console.log("Error in fetchEtherPrice()")
})
// fetch total ether supply
async function fetchTotalEtherSupply()
{
let url = 'https://api.etherscan.io/api?module=stats&action=ethsupply&apikey=YourApiKeyToken'
let response = await fetch(url)
let etherData = await response.json()
// console.log(etherData.result)
const totalEther = parseInt(etherData.result) / 1000000000000000000
// console.log(totalEther)
const marketCap = ((totalEther * parseInt(ethusd)) / 1000000000).toFixed(3)
// console.log(marketCap)
const marketCapString = ` $${marketCap} BILLION`
// console.log(marketCapString)
print(marketCapString,"marketCap")
}
fetchTotalEtherSupply().catch ( err =>
{
console.log("Error in fetchTotalEtherSupply()")
})
// fetch last block number
async function fetchLastBlockNum()
{
let url = 'https://api.etherscan.io/api?module=proxy&action=eth_blockNumber&apikey=YourApiKeyToken'
let response = await fetch(url)
let blockData = await response.json()
let lastBlockNum = parseInt(blockData.result, 16)
// console.log("last block number is: "+lastBlockNum)
print(lastBlockNum,"lastBlock")
fetchTotalNumOfTxns(blockData.result).catch(err =>
{
console.log("Error in fetchTotalNumOfTxns()")
})
fetchNetworkDifficulty(blockData.result).catch(err =>
{
console.log("Error in fetchNetworkDifficulty()")
})
}
fetchLastBlockNum().catch(err =>
{
console.log("Error in fetchLastBlockNum()")
})
// fetch total number of transactions
async function fetchTotalNumOfTxns(blockNum)
{
// console.log("this is block number in hex in another function "+blockNum)
let url = `https://api.etherscan.io/api?module=proxy&action=eth_getBlockTransactionCountByNumber&tag=${blockNum}&apikey=YourApiKeyToken`
let response = await fetch(url)
let txnData = await response.json()
let totalNumOfTxns = parseInt(txnData.result,16)
print(totalNumOfTxns,"txnCount")
}
// fetch network difficulty
async function fetchNetworkDifficulty(blockNum)
{
let url = `https://api.etherscan.io/api?module=proxy&action=eth_getBlockByNumber&tag=${blockNum}&boolean=true&apikey=YourApiKeyToken`
let response = await fetch(url)
let data = await response.json()
let difficulty = parseInt(data.result.difficulty,16)
let difficultyString = (difficulty / 1000000000000).toFixed(2)+ " TH"
print(difficultyString,"networkDifficulty")
// Find hash rate
let hashRate = (difficulty/15000000000).toFixed(2)
console.log(hashRate)
hashRateString = hashRate+" GH/s"
print(hashRateString,"hashRate")
}
// fetch account balance
async function fetchAccountBalance(address)
{
let url = `https://api.etherscan.io/api?module=account&action=balance&address=${address}&tag=latest&apikey=YourApiKeyToken`
let response = await fetch(url)
let balanceData = await response.json()
let accountBalance = "Your account balance is: "+balanceData.result
print(accountBalance,"accountBalance")
}
// function to print data to html
const print = (printString,elementId) =>
{
const element = document.getElementById(elementId)
element.innerHTML = printString
}
// Balance Check Form
let form = document.getElementById("balanceCheck")
form.addEventListener("submit",function(e)
{
e.preventDefault()
let accountAddress = document.getElementById("inputAddress").value
fetchAccountBalance(accountAddress).catch(err =>
{
console.log("Error in fetchAccountBalance()")
})
})
// Toggle Button
let element = document.getElementById("toggleBtn")
element.addEventListener("click",function(e)
{
var setInt = setInterval(refresh,15000)
function refresh()
{
if(element.checked == false)
{
clearInterval(setInt)
console.log("unchecked !")
}
else if(element.checked == true)
{
console.log("checked !")
fetchEtherPrice()
fetchTotalEtherSupply()
fetchLastBlockNum()
}
}
})
}
window.addEventListener('load', loadWindow())