Created
August 12, 2017 20:25
-
-
Save anonymous/732c0b0f862c3f7674a016ad41f8d4d4 to your computer and use it in GitHub Desktop.
go!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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