Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. let logger = require('../Utils/Logger')('NIS');
  4. let nem = require('nem-sdk').default;
  5.  
  6. /**
  7.  * Listener for all blockchain and website events
  8.  */
  9. class Blockchain
  10. {
  11.     constructor() {
  12.         this.isConnected = false;
  13.         this.hostname = null;
  14.         this.endpoint = null;
  15.         this.wallet = null;
  16.         this._onConfirmed = null;
  17.         this._onBlock = null;
  18.  
  19.         let KeyPair = nem.crypto.keyPair.create(process.env.APP_PRIVATE_KEY);
  20.         this.netId = process.env.NEM_NETWORK === 'testnet'
  21.             ? nem.model.network.data.testnet.id
  22.             : nem.model.network.data.mainnet.id;
  23.         this.wallet = nem.model.address.toAddress(KeyPair.publicKey.toString(), this.netId);
  24.         logger.info('Configured bot for wallet: ' + this.wallet);
  25.     }
  26.  
  27.     start(host) {
  28.         logger.info('Starting bot on host: ' + host);
  29.         this.setHostname(host);
  30.         this.connect(this.getConnector());
  31.     }
  32.  
  33.     getConnector() {
  34.         this.endpoint = nem.model.objects.create("endpoint")('http://'+this.hostname, nem.model.nodes.defaultPort);
  35.         let endpointWs = nem.model.objects.create("endpoint")('http://'+this.hostname, 7778);
  36.         return nem.com.websockets.connector.create(endpointWs, this.wallet);
  37.     }
  38.  
  39.     setHostname(hostname) {
  40.         this.hostname = hostname;
  41.     }
  42.  
  43.     onBlock(callback) {
  44.         this._onBlock = callback;
  45.     }
  46.  
  47.     onConfirmed(callback) {
  48.         this._onConfirmed = callback;
  49.     }
  50.  
  51.     connect(connector) {
  52.         if (!this.hostname || !this.wallet) {
  53.             logger.info('Cannot connect: invalid settings!');
  54.             this.isConnected = false;
  55.             return false;
  56.         }
  57.  
  58.         return connector.connect().then(() => {
  59.             logger.info('Connected to: '+ connector.endpoint.host);
  60.             this.isConnected = true;
  61.             if (this._onBlock !== null) {
  62.                 logger.info('Subscribing for new blocks...');
  63.                 nem.com.websockets.subscribe.chain.blocks(connector, (res) => {
  64.                     this._onBlock(res);
  65.                 });
  66.             }
  67.             if (this._onConfirmed !== null) {
  68.                 logger.info('Subscribing for new transactions...');
  69.                 connector.stompClient.send('');
  70.                 nem.com.websockets.subscribe.account.transactions.confirmed(connector, (res) => {
  71.                     this._onConfirmed(res);
  72.                 }, this.wallet);
  73.             }
  74.             nem.com.websockets.subscribe.errors(connector, function(res){
  75.                 logger.error(res);
  76.             });
  77.         }, (err) => {
  78.             logger.error('An error occured: ' + JSON.stringify(err));
  79.             this.reconnect();
  80.         });
  81.     }
  82.  
  83.     reconnect() {
  84.         this.isConnected = false;
  85.         if (!this.hostname || !this.wallet) {
  86.             logger.info('Cannot reconnect: invalid settings!');
  87.             return false;
  88.         }
  89.         logger.info('Reconnecting to: '+ this.hostname);
  90.         this.connect(this.getConnector());
  91.     }
  92. }
  93.  
  94. module.exports = new Blockchain();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement