-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
52 lines (46 loc) · 1.84 KB
/
index.js
File metadata and controls
52 lines (46 loc) · 1.84 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
import * as styles from './index.scss';
const axios = require('axios');
const ethers = require('ethers');
require('dotenv').config();
const ALCHEMY_URL = process.env.RINKEBY_URL;
function getLatestBlock(){
axios.post(ALCHEMY_URL, {
jsonrpc: "2.0",
id: 1,
method: "eth_getBlockByNumber",
params: [
"latest",
true // retrieve the full transaction object in transactions array
]
}).then((response) => {
let blocknum = BigInt(response.data.result.number).toString();
let transactions = response.data.result.transactions;
var table = document.createElement("table"), row, cellA, cellB, cellC, cellD;
for (let i=0; i<transactions.length; i++){
// (C2) ROWS & CELLS
row = table.insertRow();
cellA = row.insertCell();
cellB = row.insertCell();
cellC = row.insertCell();
cellD = row.insertCell();
cellE = row.insertCell();
cellF = row.insertCell();
// (C3) KEY & VALUE
cellA.innerHTML = "From: ";
cellB.innerHTML = `<a href="https://rinkeby.etherscan.io/address/${transactions[i].from}">${transactions[i].from}</a>`;
cellC.innerHTML = "To: ";
cellD.innerHTML = `<a href="https://rinkeby.etherscan.io/address/${transactions[i].to}">${transactions[i].to}</a>`;
cellE.innerHTML = "Value: ";
cellF.innerHTML = ethers.utils.formatEther(transactions[i].value) + " ETH";
}
//append the compiled table to the DOM
let removeOldData = document.getElementById("transactionTable").lastChild;
if(removeOldData)
document.getElementById("transactionTable").removeChild(removeOldData);
document.getElementById("transactionTable").appendChild(table);
document.getElementById("blocknum").innerHTML = blocknum;
});
};
document.getElementById("transfer-amount").addEventListener('click', () => {
getLatestBlock();
});