Skip to content

Instantly share code, notes, and snippets.

@igorls
Last active March 12, 2019 22:04
Show Gist options
  • Save igorls/8f920821ae62b9cd58cdfd539aba047a to your computer and use it in GitHub Desktop.
Save igorls/8f920821ae62b9cd58cdfd539aba047a to your computer and use it in GitHub Desktop.
const total_accounts = 10000000;
const batch_size = 1000;
const contract = 'eosio.token';
const table_name = 'accounts';
const eos_api = "http://127.0.0.1:8888";
const fetch = require('node-fetch');
const fs = require('fs');
let initTime, endTime;
let intervalTimer;
let accounts = new Set();
let is_ready = false;
let counter = 0;
let lastSize = 0;
const account_list = './accounts.txt';
if (fs.existsSync(account_list)) {
fs.unlinkSync(account_list);
}
const file = fs.createWriteStream(account_list, {flags: 'a'});
async function recursiveFetchTableRows(code, table, start, limit, batch) {
const req = {
code: code,
table: table,
limit: batch,
upper_bound: null
};
if (start !== 0) {
req['lower_bound'] = start;
} else {
req['lower_bound'] = null;
}
let result;
try {
const response = await fetch(eos_api + '/v1/chain/get_table_by_scope', {
method: 'POST',
body: JSON.stringify(req),
headers: {'Content-Type': 'application/json'},
});
result = (await response.json()).rows;
if (!result) {
console.log('No more results!');
stopScanning();
} else {
for (const row of result) {
accounts.add(row['scope']);
file.write(row['scope'] + '\n');
// await processTableRow(row['scope']);
counter++;
}
const len = accounts.size;
const last = result[result.length - 1]['scope'];
if (len < limit) {
if (lastSize !== len) {
lastSize = len;
setTimeout(async () => {
const _batch = (limit > (len + batch)) ? batch : (limit - len + 1);
await recursiveFetchTableRows(code, table, last, limit, _batch);
}, 5);
} else {
console.log('Table end reached!');
stopScanning();
}
} else {
console.log('Limit reached!');
stopScanning();
}
}
} catch (e) {
console.log(e);
stopScanning();
}
}
async function processTableRow(account) {
const req = {
code: contract,
table: table_name,
scope: account,
json: true
};
const response = await fetch(eos_api + '/v1/chain/get_table_rows', {
method: 'POST',
body: JSON.stringify(req),
headers: {'Content-Type': 'application/json'},
});
const result = (await response.json()).rows[0];
if (result) {
// console.log(account, result);
const balance = parseFloat(result['balance'].split(" ")[0]);
if(balance > 10000) {
file.write(account + "," + balance + '\n');
}
}
}
function stopScanning() {
console.log('Iteration is over!');
endTime = Date.now();
console.log(`${accounts.size} entries scanned in ${(endTime - initTime) / 1000} seconds!`);
console.log('---------------- End ---------------------');
clearInterval(intervalTimer);
is_ready = true;
}
(async () => {
console.log('---------------- Start ---------------------');
console.log(`Contract: ${contract}`);
console.log(`Table: ${table_name}`);
intervalTimer = setInterval(() => {
console.log(`${counter} rows/s - ${accounts.size}`);
counter = 0;
}, 1000);
initTime = Date.now();
await recursiveFetchTableRows(contract, table_name, 0, total_accounts, batch_size);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment