/ src / server / exchangeRate.js
exchangeRate.js
 1  // https://api.coinbase.com/v2/prices/btc-cad/spot
 2  // disabled config settings here, considering removing this integration
 3  import crypto from 'crypto'
 4  import request from 'superagent'
 5  import events from './events.js'
 6  import state from './state.js'
 7  import validators from './validators.js'
 8  
 9  function watchSpot(){
10      getRecordSpot()
11      setInterval( getRecordSpot, 1000 * 60 * 60 )
12  }
13  
14  function getRecordSpot(){
15      getPrice( (err, spot)=> {
16          if (!err){
17              events.spotUpdated(spot)
18          }
19      })
20  }
21  
22  function getPrice(callback){
23      request
24          .get('https://api.coinbase.com/v2/prices/btc-' + state.serverState.cash.currency + '/spot')
25          .end((err, res)=> {
26              if (err) return callback(err);
27              if ( validators.isAmount(res.body.data.amount) ) {
28                  callback(null, Number.parseFloat(res.body.data.amount))
29              } else {
30                  callback('invalid res?')
31              }
32          })
33  }
34  
35  export default {
36      getPrice,
37      watchSpot
38  }
39  
40  //success: {
41  //  "data": {
42  //    "amount": "57388.1",
43  //    "base": "BTC",
44  //    "currency": "CAD"
45  //  }
46  //}
47