controllers/currency.js

  1. const ObservableStore = require('obs-store')
  2. const extend = require('xtend')
  3. const log = require('loglevel')
  4. // every ten minutes
  5. const POLLING_INTERVAL = 600000
  6. class CurrencyController {
  7. /**
  8. * Controller responsible for managing data associated with the currently selected currency.
  9. *
  10. * @typedef {Object} CurrencyController
  11. * @param {object} opts Overrides the defaults for the initial state of this.store
  12. * @property {array} opts.initState initializes the the state of the CurrencyController. Can contain an
  13. * currentCurrency, conversionRate and conversionDate properties
  14. * @property {string} currentCurrency A 2-4 character shorthand that describes a specific currency, currently
  15. * selected by the user
  16. * @property {number} conversionRate The conversion rate from ETH to the selected currency.
  17. * @property {string} conversionDate The date at which the conversion rate was set. Expressed in in milliseconds
  18. * since midnight of January 1, 1970
  19. * @property {number} conversionInterval The id of the interval created by the scheduleConversionInterval method.
  20. * Used to clear an existing interval on subsequent calls of that method.
  21. *
  22. */
  23. constructor (opts = {}) {
  24. const initState = extend({
  25. currentCurrency: 'usd',
  26. conversionRate: 0,
  27. conversionDate: 'N/A',
  28. }, opts.initState)
  29. this.store = new ObservableStore(initState)
  30. }
  31. //
  32. // PUBLIC METHODS
  33. //
  34. /**
  35. * A getter for the currentCurrency property
  36. *
  37. * @returns {string} A 2-4 character shorthand that describes a specific currency, currently selected by the user
  38. *
  39. */
  40. getCurrentCurrency () {
  41. return this.store.getState().currentCurrency
  42. }
  43. /**
  44. * A setter for the currentCurrency property
  45. *
  46. * @param {string} currentCurrency The new currency to set as the currentCurrency in the store
  47. *
  48. */
  49. setCurrentCurrency (currentCurrency) {
  50. this.store.updateState({ currentCurrency })
  51. }
  52. /**
  53. * A getter for the conversionRate property
  54. *
  55. * @returns {string} The conversion rate from ETH to the selected currency.
  56. *
  57. */
  58. getConversionRate () {
  59. return this.store.getState().conversionRate
  60. }
  61. /**
  62. * A setter for the conversionRate property
  63. *
  64. * @param {number} conversionRate The new rate to set as the conversionRate in the store
  65. *
  66. */
  67. setConversionRate (conversionRate) {
  68. this.store.updateState({ conversionRate })
  69. }
  70. /**
  71. * A getter for the conversionDate property
  72. *
  73. * @returns {string} The date at which the conversion rate was set. Expressed in milliseconds since midnight of
  74. * January 1, 1970
  75. *
  76. */
  77. getConversionDate () {
  78. return this.store.getState().conversionDate
  79. }
  80. /**
  81. * A setter for the conversionDate property
  82. *
  83. * @param {number} conversionDate The date, expressed in milliseconds since midnight of January 1, 1970, that the
  84. * conversionRate was set
  85. *
  86. */
  87. setConversionDate (conversionDate) {
  88. this.store.updateState({ conversionDate })
  89. }
  90. /**
  91. * Updates the conversionRate and conversionDate properties associated with the currentCurrency. Updated info is
  92. * fetched from an external API
  93. *
  94. */
  95. async updateConversionRate () {
  96. let currentCurrency
  97. try {
  98. currentCurrency = this.getCurrentCurrency()
  99. const response = await fetch(`https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`)
  100. const parsedResponse = await response.json()
  101. this.setConversionRate(Number(parsedResponse.bid))
  102. this.setConversionDate(Number(parsedResponse.timestamp))
  103. } catch (err) {
  104. log.warn(`MetaMask - Failed to query currency conversion:`, currentCurrency, err)
  105. this.setConversionRate(0)
  106. this.setConversionDate('N/A')
  107. }
  108. }
  109. /**
  110. * Creates a new poll, using setInterval, to periodically call updateConversionRate. The id of the interval is
  111. * stored at the controller's conversionInterval property. If it is called and such an id already exists, the
  112. * previous interval is clear and a new one is created.
  113. *
  114. */
  115. scheduleConversionInterval () {
  116. if (this.conversionInterval) {
  117. clearInterval(this.conversionInterval)
  118. }
  119. this.conversionInterval = setInterval(() => {
  120. this.updateConversionRate()
  121. }, POLLING_INTERVAL)
  122. }
  123. }
  124. module.exports = CurrencyController