Wallet.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <script setup>
  2. /* Import modules. */
  3. import numeral from 'numeral'
  4. /* Initialize stores. */
  5. import { useWalletStore } from '@/stores/wallet'
  6. const Wallet = useWalletStore()
  7. const cashoutAddr = ref(null)
  8. const isShowingDeposit = ref(false)
  9. const isShowingHistory = ref(false)
  10. const isShowingCashout = ref(false)
  11. const displayBalance = computed(() => {
  12. /* Validate asset. */
  13. if (!Wallet.asset || !Wallet.asset.amount) {
  14. return '0.00'
  15. }
  16. let decimalValue
  17. let bigIntValue
  18. if (Wallet.asset.group === '0') {
  19. decimalValue = Wallet.asset.satoshis * BigInt(1e4)
  20. } else {
  21. /* Validate amount type. */
  22. if (typeof Wallet.asset.amount !== 'bigint') {
  23. decimalValue = BigInt(0)
  24. } else {
  25. decimalValue = Wallet.asset.amount * BigInt(1e4)
  26. }
  27. decimalValue = Wallet.asset.amount * BigInt(1e4)
  28. }
  29. if (Wallet.asset?.decimal_places > 0) {
  30. bigIntValue = decimalValue / BigInt(10**Wallet.asset.decimal_places)
  31. } else {
  32. bigIntValue = decimalValue
  33. }
  34. return numeral(parseFloat(bigIntValue) / 1e4).format('0,0[.]00[0000]')
  35. })
  36. const displayBalanceUsd = computed(() => {
  37. /* Validate asset. */
  38. if (!Wallet.asset || !Wallet.asset.fiat || !Wallet.asset.fiat.USD) {
  39. return '$0.00'
  40. }
  41. /* Initialize locals. */
  42. let balanceUsd
  43. /* Set balance. */
  44. balanceUsd = Wallet.asset.fiat.USD || 0.00
  45. /* Return formatted value. */
  46. return numeral(balanceUsd).format('$0,0.00[0000]')
  47. })
  48. /**
  49. * Set Tab
  50. */
  51. const setTab = (_tab) => {
  52. /* Clear all tabs. */
  53. isShowingDeposit.value = false
  54. isShowingHistory.value = false
  55. isShowingCashout.value = false
  56. if (_tab === 'deposit') {
  57. isShowingDeposit.value = true
  58. }
  59. if (_tab === 'history') {
  60. isShowingHistory.value = true
  61. }
  62. if (_tab === 'withdraw') {
  63. isShowingCashout.value = true
  64. }
  65. }
  66. const createWallet = () => {
  67. if (!cashoutAddr.value) {
  68. alert('Please provide a Cashout address to continue your NEW Wallet setup.')
  69. } else {
  70. /* Set cashout address. */
  71. Wallet.setCashoutAddr(cashoutAddr.value)
  72. }
  73. /* Create new wallet. */
  74. Wallet.createWallet()
  75. }
  76. const init = () => {
  77. /* Set (starting) tab. */
  78. setTab('deposit')
  79. /* Initialize the Web wallet. */
  80. Wallet.init()
  81. }
  82. onMounted(() => {
  83. init()
  84. })
  85. onBeforeUnmount(() => {
  86. Wallet.cleanup()
  87. })
  88. </script>
  89. <template>
  90. <main v-if="!Wallet.isReady" class="flex flex-col gap-5">
  91. <p class="px-3 py-2 bg-amber-100 text-base font-medium border-2 border-yellow-200 rounded-lg shadow-md">
  92. Welcome to your Wally Dice game wallet.
  93. Click the button below to create a new wallet and begin playing.
  94. </p>
  95. <input
  96. class="w-full px-3 py-1 text-xl sm:text-2xl bg-amber-200 border-2 border-yellow-400 rounded-md shadow"
  97. type="text"
  98. v-model="cashoutAddr"
  99. placeholder="Enter a Cashout address"
  100. />
  101. <button @click="createWallet" class="px-3 py-2 text-2xl text-blue-100 font-medium bg-blue-500 border-2 border-blue-700 rounded-lg shadow hover:bg-blue-400">
  102. Create New Wallet
  103. </button>
  104. </main>
  105. <main v-else class="">
  106. <section class="px-5 py-3 bg-amber-100 border-2 border-yellow-400 rounded-lg shadow">
  107. <h3 class="text-sm text-yellow-700 font-medium tracking-widest uppercase">
  108. Available Balance
  109. </h3>
  110. <h2 class="flex justify-between items-end text-3xl text-amber-900 font-medium">
  111. <span>{{displayBalance}}</span>
  112. <span class="text-2xl">{{displayBalanceUsd}}</span>
  113. </h2>
  114. <div class="mt-1 pt-1 text-xs text-amber-900 border-t border-amber-400">
  115. <span class="tracking-wide font-medium uppercase">
  116. My Cashout Address
  117. </span>
  118. <NuxtLink :to="'https://explorer.nexa.org/address/' + Wallet.cashoutAddr" target="_blank" class="block tracking-tight hover:underline">
  119. {{Wallet.cashoutAddr}}
  120. </NuxtLink>
  121. </div>
  122. </section>
  123. <div class="mt-10">
  124. <div class="block">
  125. <nav class="isolate flex divide-x divide-gray-200 rounded-lg shadow" aria-label="Tabs">
  126. <!-- Current: "text-gray-900", Default: "text-gray-500 hover:text-gray-700" -->
  127. <button @click="setTab('deposit')" class="bg-gray-700 text-gray-100 rounded-l-lg group relative min-w-0 flex-1 overflow-hidden py-4 px-4 text-center text-lg font-medium hover:bg-gray-50 hover:text-gray-600 focus:z-10" aria-current="page">
  128. <span>Deposit</span>
  129. <span aria-hidden="true" class="bg-indigo-500 absolute inset-x-0 bottom-0 h-0.5"></span>
  130. </button>
  131. <button @click="setTab('withdraw')" class="bg-gray-700 text-gray-400 hover:text-gray-700 group relative min-w-0 flex-1 overflow-hidden py-4 px-4 text-center text-lg font-medium hover:bg-gray-50 hover:text-gray-600 focus:z-10">
  132. <span>Cashout</span>
  133. <span aria-hidden="true" class="bg-transparent absolute inset-x-0 bottom-0 h-0.5"></span>
  134. </button>
  135. <button @click="setTab('history')" class="bg-gray-700 text-gray-400 hover:text-gray-700 rounded-r-lg group relative min-w-0 flex-1 overflow-hidden py-4 px-4 text-center text-lg font-medium hover:bg-gray-50 hover:text-gray-600 focus:z-10">
  136. <span>History</span>
  137. <span aria-hidden="true" class="bg-transparent absolute inset-x-0 bottom-0 h-0.5"></span>
  138. </button>
  139. </nav>
  140. </div>
  141. </div>
  142. <div class="my-5">
  143. <MenuWalletDeposit v-if="isShowingDeposit" />
  144. <MenuWalletHistory v-if="isShowingHistory" />
  145. <MenuWalletCashout v-if="isShowingCashout" />
  146. </div>
  147. </main>
  148. </template>