-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnapshot.js
More file actions
193 lines (168 loc) · 6.12 KB
/
snapshot.js
File metadata and controls
193 lines (168 loc) · 6.12 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const ethers = require('ethers');
const fs = require('fs');
const ObjectsToCsv = require('objects-to-csv');
const { Contract, Provider } = require('@pelith/ethers-multicall');
const provider = new ethers.providers.JsonRpcProvider('https://ethereum-rpc.publicnode.com');
var callProvider = new Provider(provider);
/// ****** CONFIG ****** ///
// If you're getting RPC errors, try lowering the batch size
// or increasing the sleep time between batches
var batchSize = 50;
var sleepTime = 1000; // ms
const llamasNFT = "0xe127cE638293FA123Be79C25782a5652581Db234";
const llamasNFTAbi = [{
"stateMutability":"view",
"type":"function",
"name":"ownerOf",
"inputs":[{"name":"token_id","type":"uint256"}],
"outputs":[{"name":"","type":"address"}]
}]
const llamaLocker = "0x99c3f30Bbc9137F6E917B03C74aEd8a4309B3E1b";
const llamaLockerAbi = [{
"stateMutability": "view",
"type": "function",
"inputs": [{"internalType": "uint256","name": "tokenId","type": "uint256"}
],
"name": "locks",
"outputs": [{
"internalType": "address",
"name": "owner",
"type": "address"
},
{
"internalType": "uint256",
"name": "lockedAt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}]
}]
const NFT = new Contract(llamasNFT, llamasNFTAbi);
const Locker = new Contract(llamaLocker, llamaLockerAbi);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
await callProvider.init();
if(process.argv.length < 3) {
console.log("Use: node snapshot.js blockNumber");
process.exit();
}
targetBlock = Number(process.argv[2]);
var latestBlock = await provider.getBlockNumber();
if(targetBlock > latestBlock) {
console.log("blockNumber exceeds latest block");
process.exit();
}
// check if snapshot already exists
if(fs.existsSync(`./snapshots/llamasSnapshot_${targetBlock}.json`)) {
console.log("Snapshot already completed at block "+targetBlock);
process.exit();
}
var owners = {};
var lockerIds = [];
// Unlocked NFT holders
var batches = [];
var currentBatchSize = 0;
var callQueue = [];
// call ownerOf for each tokenId in batches of batchSize, using multicall
for(var i=0; i<1111; ++i) {
callQueue.push(NFT.ownerOf(i));
currentBatchSize++;
if(currentBatchSize >= batchSize) {
batches.push(callQueue);
callQueue = [];
currentBatchSize = 0;
}
}
if(callQueue.length > 0) {
batches.push(callQueue);
}
for(var i=0; i<batches.length; ++i) {
console.log(`Processing naked batch ${i+1} of ${batches.length}`);
var results = await callProvider.all(batches[i], targetBlock);
for(var j=0; j<results.length; ++j) {
var owner = results[j];
if(owner == llamaLocker) {
lockerIds.push(i*batchSize+j);
} else {
if(owners[owner] == undefined) {
owners[owner] = {unlocked: 0, locked: 0};
}
owners[owner].unlocked++;
}
}
await sleep(sleepTime); // to avoid overloading the RPC
}
console.log(`Found ${Object.keys(owners).length} unique unlocked NFT holders`);
// get locker owners
batches = [];
currentBatchSize = 0;
callQueue = [];
console.log(lockerIds);
for(var i in lockerIds) {
callQueue.push(Locker.locks(lockerIds[i]));
currentBatchSize++;
if(currentBatchSize >= batchSize) {
batches.push(callQueue);
callQueue = [];
currentBatchSize = 0;
}
}
if(callQueue.length > 0) {
batches.push(callQueue);
}
var lockers = 0;
for(var i=0; i<batches.length; ++i) {
console.log(`Processing locker batch ${i+1} of ${batches.length}`);
var results = await callProvider.all(batches[i], targetBlock);
for(var j=0; j<results.length; ++j) {
var owner = results[j].owner;
//console.log(owner, lockerIds[i*batchSize+j]);
if(owners[owner] == undefined) {
owners[owner] = {unlocked: 0, locked: 0};
}
if(owners[owner].locked == 0) { lockers++; }
owners[owner].locked++;
}
await sleep(sleepTime); // to avoid overloading the RPC
}
console.log(`Found ${lockers} unique locked holders`);
// sort owners by locked
owners = Object.fromEntries(Object.entries(owners).sort(([,a],[,b]) => b.locked - a.locked));
// sanity check that unlocked + locked = 1111
var totalUnlocked = 0;
var totalLocked = 0;
for(const [address, counts] of Object.entries(owners)) {
totalUnlocked += counts.unlocked;
totalLocked += counts.locked;
}
var total = totalUnlocked + totalLocked;
console.log(`Total unlocked NFTs: ${totalUnlocked}`);
console.log(`Total locked NFTs: ${totalLocked}`);
console.log(`Total NFTs accounted for: ${total} / 1111`);
if(total != 1111) {
console.log("Error: total NFTs does not equal 1111");
process.exit();
}
// cleanup for JSON
var jsondata = {}
for(const [address, counts] of Object.entries(owners)) {
jsondata[address] = [counts.unlocked, counts.locked]
}
// Save to JSON
await fs.writeFileSync(`./snapshots/llamasSnapshot_${targetBlock}.json`, JSON.stringify(jsondata, null, 2));
console.log(`Saved ${Object.keys(owners).length} addresses to llamasSnapshot_${targetBlock}.json`);
// Save to CSV
var csvData = [];
for(const [address, counts] of Object.entries(owners)) {
csvData.push({address: address, unlocked: counts.unlocked, locked: counts.locked});
}
const csv = new ObjectsToCsv(csvData);
await csv.toDisk(`./snapshots/llamasSnapshot_${targetBlock}.csv`);
console.log(`Saved ${csvData.length} addresses to llamasSnapshot_${targetBlock}.csv`);
}
main();