system.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Import modules. */
  2. import { defineStore } from 'pinia'
  3. /* Set constants. */
  4. const TICKER_INTERVAL = 30000
  5. /**
  6. * System Store
  7. */
  8. export const useSystemStore = defineStore('system', {
  9. state: () => ({
  10. /* Set constants. */
  11. ONE_SAT: BigInt('1'),
  12. ONE_NEX: BigInt('100'),
  13. ONE_KEX: BigInt('100000'),
  14. ONE_MEX: BigInt('100000000'),
  15. ONE_META: BigInt('1000000000000000000'),
  16. /* Set Nexa Exchange API endpoint. */
  17. NEXA_EXCHANGE_ENDPOINT: 'https://nexa.exchange',
  18. /* Initialize notifications. */
  19. _notif: {
  20. isShowing: false,
  21. icon: null,
  22. title: null,
  23. description: null,
  24. delay: 7000,
  25. },
  26. /**
  27. * Application Starts
  28. */
  29. _appStarts: 0,
  30. /**
  31. * Application Version
  32. */
  33. _appVersion: null,
  34. /**
  35. * Flags
  36. *
  37. * 1. Dark mode
  38. * 2. Unconfirmed transactions
  39. */
  40. _flags: null,
  41. /**
  42. * Locale
  43. *
  44. * Controls the localization language.
  45. * (default is english)
  46. */
  47. _locale: null,
  48. /**
  49. * Notices
  50. *
  51. * System notices that nag/remind the user of some important action or
  52. * information; which can be permanently disabled ("Do Not Show Again")
  53. * via checkbox and confirmation.
  54. *
  55. * NOTE: Unique 1-byte (hex) codes (up to 255) are used to reduce the size
  56. * of this storage field.
  57. */
  58. _notices: null,
  59. _ticker: null,
  60. }),
  61. getters: {
  62. ticker(_state) {
  63. return _state._ticker
  64. },
  65. usd(_state) {
  66. const usd = _state._ticker?.quote?.USD?.price
  67. const formatted = parseFloat((usd * 1000000.0).toFixed(4))
  68. return formatted
  69. },
  70. },
  71. actions: {
  72. /**
  73. * Initialize Application
  74. *
  75. * Performs startup activities.
  76. */
  77. init() {
  78. /* Increment application starts. */
  79. this._appStarts++
  80. /* Set ticker interval. */
  81. setInterval(this.updateTicker, TICKER_INTERVAL)
  82. /* Initial ticker update. */
  83. this.updateTicker()
  84. },
  85. /**
  86. * Update Ticker
  87. */
  88. async updateTicker() {
  89. this._ticker = await $fetch(this.NEXA_EXCHANGE_ENDPOINT + '/_ticker')
  90. .catch(err => console.error(err))
  91. // console.info('SYSTEM (update ticker):', this.ticker)
  92. }
  93. },
  94. })