-
Notifications
You must be signed in to change notification settings - Fork 23
/
get_comp_holders.html
109 lines (91 loc) · 3.6 KB
/
get_comp_holders.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
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="../dependencies/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Get COMP Holders</title>
</head>
<body>
<h1>COMP Holders</h1>
<div id="web3-warning" class="hidden warning">
Make sure the example app is being served with an HTTP server. <br />
Please install MetaMask: <a href="https://metamask.io/">https://metamask.io/</a>
</div>
<div id="holders"></div>
</body>
<script type="text/javascript" src="../dependencies/handlebars.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/ethereum/[email protected]/dist/web3.min.js"></script>
<script type="text/javascript" src="../dependencies/comp-abi.js"></script>
<script type="text/javascript">
window.addEventListener('load', async (event) => {
let web3;
const web3Warning = document.getElementById('web3-warning');
if (typeof window.ethereum === 'undefined') {
console.error('Client does not have an active Web3 provider or the example app is not being run from an HTTP server.');
console.log('Go here to install MetaMask: https://metamask.io/');
alert(
'You need a Web3 provider to run this page. ' +
'Go here to install MetaMask:\n\n' +
'https://metamask.io/'
);
web3Warning.classList.remove('hidden');
}
await window.ethereum.request({ method: 'eth_requestAccounts' }); // enable ethereum
web3 = new Web3(window.ethereum);
const net = +window.ethereum.chainId;
// This example only works with Ropsten
if (net !== 3) {
alert('Please select the ROPSTEN network.');
}
const holderListContainer = document.getElementById('holders');
const holderListTemplate = Handlebars.compile(`
{{#each this}}
<div class="card">
<h4>{{ address }}</h4>
<label>COMP Tokens: </label> <span>{{ balance }}</span>
<br />
<label>Delegate Address: </label> <span>{{ delegate }}</span>
</div>
{{/each}}
`);
const compAddress = '0xf76d4a441e4ba86a923ce32b89aff89dbccaa075';
const compAbi = window.compAbi;
const comp = new web3.eth.Contract(compAbi, compAddress);
(async () => {
// Tell the user that we're waiting on a request to return data
holderListContainer.innerHTML = '<p style="text-align:center;">Fetching holders...</p>';
const transfers = await comp.getPastEvents('Transfer', {
fromBlock: 9728445,
toBlock: 'latest'
});
const pastHolders = [];
const pastHolderBalanceGets = [];
const delegateGets = [];
transfers.forEach((transfer, i) => {
const address = transfer.returnValues.to;
if (pastHolders.indexOf(address) > -1) return;
pastHolders.push(address);
pastHolderBalanceGets.push(comp.methods.balanceOf(address).call());
delegateGets.push(comp.methods.delegates(address).call());
});
const balances = await Promise.all(pastHolderBalanceGets);
const delegates = await Promise.all(delegateGets);
const holders = [];
balances.forEach((balance, i) => {
if (balance > 0) {
holders.push({
address: pastHolders[i],
balance: parseFloat(balance / 1e18).toFixed(4),
delegate: delegates[i] == 0 ? 'None' : delegates[i]
});
}
});
holders.sort((a,b) => {
return parseFloat(a.balance) < parseFloat(b.balance) ? 1 : -1;
});
console.log(holders);
holderListContainer.innerHTML = holderListTemplate(holders);
})();
});
</script>
</html>