Advertisement
Guest User

Untitled

a guest
Oct 13th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const WebSocket = require('ws');
  2.  
  3. const shardTemplate = (subscription, shardId) => ({
  4.     request: {
  5.         jsonrpc: "1.0",
  6.         method: "subcribenewshardblock",
  7.         params: [shardId],
  8.         id: 1
  9.     },
  10.     subcription: subscription,
  11.     type: 0
  12. });
  13.  
  14. const txTemplate = (subscription, transactionHash) => ({
  15.     request: {
  16.         jsonrpc: "1.0",
  17.         method: "subcribependingtransaction",
  18.         params: [transactionHash],
  19.         id: 1
  20.     },
  21.     subcription: subscription,
  22.     type: 0
  23. });
  24.  
  25. const shardBlocksSubscription = "1";
  26. const txSubscription = "2";
  27.  
  28. const ws = new WebSocket("ws://fullnode.incognito.best:19334");
  29.  
  30. const subscribeToShardBlock = (shardId) => {
  31.     ws.send(JSON.stringify(shardTemplate(shardBlocksSubscription, shardId)));
  32. }
  33.  
  34. const subscribeToTransaction = (transactionHash) => {
  35.     ws.send(JSON.stringify(txTemplate(txSubscription, transactionHash)));
  36. }
  37.  
  38. const processNewTransaction = console.log;
  39.  
  40. ws.on('open', () => {
  41.     [0,1,2,3,4,5,6,7].forEach(subscribeToShardBlock);
  42. });
  43.  
  44. ws.on('message', (data) => {
  45.     const result = JSON.parse(data).Result;    
  46.     if (result.Subscription === shardBlocksSubscription) {
  47.         result.Result.TxHashes.forEach(subscribeToTransaction);
  48.     }
  49.     else if (result.Subscription === txSubscription) {
  50.         processNewTransaction(result.Result);
  51.     }
  52. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement