Skip to content

Instantly share code, notes, and snippets.

@igorbarinov
Forked from anonymous/etc.js
Created August 13, 2017 18:34
Show Gist options
  • Save igorbarinov/8d1c74f6efb05ba91e4081c5fc0e0a27 to your computer and use it in GitHub Desktop.
Save igorbarinov/8d1c74f6efb05ba91e4081c5fc0e0a27 to your computer and use it in GitHub Desktop.
go!
const Web3 = require('web3');
const net = require('net');
const { Pool, Client } = require('pg');
let web3 = new Web3('/Users/user/repos/private_net/here.ipc', net);
const pool = new Pool({
user: 'readonly',
host: 'localhost',
database: 'etc',
password: 'readonly',
port: 5432,
});
pool.connect();
function getTxs(txids) {
return new Promise((res, rej) => {
if (txids.length > 0) {
txids.forEach((id, index) => {
web3.eth.getTransaction(id).then((txHash) => {
if (txHash.input !== "0x") {
saveToDb(txHash.to, txHash.input);
}
}).then(() => {
if (txids.length === ++index) {
res()
}
})
})
} else {
res();
}
})
};
function getBlock(number) {
console.log(number);
if (number > 0) {
web3.eth.getBlock(number).then((block) => {
getTxs(block.transactions).then(() => {
getBlock(number - 1);
})
})
}
};
function main() {
web3.eth.getBlockNumber().then((number) => {
getBlock(number)
})
};
main()
async function saveToDb(address, input) {
const text = 'INSERT INTO contracts(address, input) VALUES($1, $2)';
const values = [address, input];
pool.query(text, values)
.then(res => console.log(res.rows[0]))
.catch(e => console.error(e.stack));
// await pool.end();
}
DELETE FROM contracts
WHERE c_id IN (SELECT c_id
FROM (SELECT c_id,
ROW_NUMBER() OVER (partition BY address, input ORDER BY c_id) AS rnum
FROM contracts) t
WHERE t.rnum > 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment