Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. const Web3 = require('web3');
  2. const ck_abi = require('./ck_abi.json');
  3. require('dotenv').config();
  4.  
  5. const endpoint1 = process.env.INFURA1;
  6. const contract_address = process.env.CK_CONTRACT;
  7. const web3 = new Web3(endpoint1);
  8. const jsonInterface = JSON.parse(ck_abi);
  9. const contract = new web3.eth.Contract(jsonInterface, contract_address);
  10. const birthInputs = jsonInterface[jsonInterface.length - 2].inputs;
  11.  
  12.  
  13. const barbaric = async () => {
  14.  
  15. //! NO API for that!
  16. const fromBlock = 4832686; //* first block of jan2018
  17. const lastBlock = 5174124; //* last block of feb2018
  18.  
  19. let counter = 0;
  20.  
  21. for (let i = fromBlock; i <= lastBlock; i+= 500) {
  22.  
  23. const toBlock = updateToBlock(i, lastBlock);
  24.  
  25. try {
  26.  
  27. //* Find every birth during that period of time
  28. //! throws if logs.length > 1000 - ~340K blocks to process!
  29. const logs = await contract.getPastEvents('Birth', {fromBlock: i , toBlock });
  30.  
  31. //* Decode each birth event to get the kitty's data
  32. for (const log of logs) {
  33.  
  34. //* The owner here is the original owner
  35. const bornKitty = getBornKitty(log);
  36.  
  37. //* Now we need to get the current owner from the Blockchain
  38. //! One Request per Cat = Super Resource consuming!
  39. const kitty = await fetchCurrentOwner(bornKitty);
  40.  
  41. counter++;
  42.  
  43. if (counter%100 === 0) {
  44. console.log(`-------------------------------------- ${counter} KITTIES FOUND --------------------------------------`);
  45. }
  46. console.log(kitty);
  47. }
  48.  
  49. } catch (e) {
  50. throw e;
  51. }
  52. }
  53.  
  54. return true;
  55.  
  56. };
  57.  
  58. const updateToBlock = (i, lastBlock) => {
  59. return i + 500 > lastBlock ? lastBlock : i + 500;
  60. }
  61.  
  62. const getBornKitty = log => {
  63. const decoded = web3.eth.abi.decodeLog(birthInputs, log.raw.data, log.returnValues)
  64. const bornKitty = {
  65. kittyId: web3.utils.hexToNumber(decoded.kittyId),
  66. first_owner: decoded.owner
  67. };
  68.  
  69. return bornKitty;
  70. }
  71.  
  72. const fetchCurrentOwner = async bornKitty => {
  73. const current_owner = await contract.methods.ownerOf(bornKitty.kittyId).call();
  74. bornKitty.current_owner = current_owner;
  75.  
  76. return bornKitty;
  77. }
  78.  
  79. barbaric().catch(e => console.error(e));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement