Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. const Web3 = require('web3');
  2. const net = require('net');
  3. const { Pool, Client } = require('pg');
  4.  
  5.  
  6. let web3 = new Web3('/Users/user/repos/private_net/here.ipc', net);
  7.  
  8. const pool = new Pool({
  9. user: 'readonly',
  10. host: 'localhost',
  11. database: 'etc',
  12. password: 'readonly',
  13. port: 5432,
  14. });
  15. pool.connect();
  16. function getTxs(txids) {
  17. return new Promise((res, rej) => {
  18. if (txids.length > 0) {
  19. txids.forEach((id, index) => {
  20. web3.eth.getTransaction(id).then((txHash) => {
  21. if (txHash.input !== "0x") {
  22. saveToDb(txHash.to, txHash.input);
  23. }
  24. }).then(() => {
  25. if (txids.length === ++index) {
  26. res()
  27. }
  28. })
  29. })
  30. } else {
  31. res();
  32. }
  33. })
  34.  
  35. };
  36.  
  37. function getBlock(number) {
  38. console.log(number);
  39. if (number > 0) {
  40. web3.eth.getBlock(number).then((block) => {
  41. getTxs(block.transactions).then(() => {
  42. getBlock(number - 1);
  43. })
  44. })
  45. }
  46. };
  47. function main() {
  48. web3.eth.getBlockNumber().then((number) => {
  49. getBlock(number)
  50. })
  51.  
  52. };
  53. main()
  54.  
  55.  
  56.  
  57. async function saveToDb(address, input) {
  58.  
  59.  
  60. const text = 'INSERT INTO contracts(address, input) VALUES($1, $2)';
  61. const values = [address, input];
  62. pool.query(text, values)
  63. .then(res => console.log(res.rows[0]))
  64. .catch(e => console.error(e.stack));
  65. // await pool.end();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement