|
| 1 | +<template> |
| 2 | + <Form |
| 3 | + ref="formRef" |
| 4 | + class="flex h-full flex-col divide-y divide-gray-200 bg-white shadow-xl" |
| 5 | + :validation-schema="validation" |
| 6 | + @submit="transfer" |
| 7 | + > |
| 8 | + <h3 class="text-xl font-semibold px-4 sm:px-6 py-4 text-gray-900">Transfer Balance</h3> |
| 9 | + <div class="h-0 flex-1 overflow-y-auto"> |
| 10 | + <div class="flex flex-1 flex-col justify-between"> |
| 11 | + <div class="divide-y divide-gray-200 px-4 sm:px-6"> |
| 12 | + <div class="space-y-6 pt-6 pb-5"> |
| 13 | + <FormInput |
| 14 | + v-model="recipient" |
| 15 | + name="recipient" |
| 16 | + label="Recipient" |
| 17 | + description="The recipient account who is going to receive the transfer." |
| 18 | + required |
| 19 | + tooltip="Wallet Address" |
| 20 | + /> |
| 21 | + <FormInput |
| 22 | + v-model="amount" |
| 23 | + name="amount" |
| 24 | + label="Amount" |
| 25 | + description="The amount to transfer." |
| 26 | + type="number" |
| 27 | + required |
| 28 | + :prefix="currencySymbol" |
| 29 | + /> |
| 30 | + <FormInput |
| 31 | + v-if="useAppStore().advanced" |
| 32 | + v-model="idempotencyKey" |
| 33 | + name="idempotencyKey" |
| 34 | + label="Idempotency Key" |
| 35 | + description="The idempotency key to set. It is recommended to use a UUID for this." |
| 36 | + tooltip="In mathematical and computer science terms, idempotency is a property of certain operations that can be applied repeated times without changing the initial result of the application." |
| 37 | + readmore="Idempotency Key" |
| 38 | + /> |
| 39 | + <FormCheckbox |
| 40 | + v-if="useAppStore().advanced" |
| 41 | + v-model="keepAlive" |
| 42 | + name="keepAlive" |
| 43 | + label="Keep Alive" |
| 44 | + description="If true, the transaction will fail if the balance drops below the minimum requirement. Defaults to False." |
| 45 | + /> |
| 46 | + <FormCheckbox |
| 47 | + v-if="useAppStore().advanced" |
| 48 | + v-model="skipValidation" |
| 49 | + name="skipValidation" |
| 50 | + label="Skip validation" |
| 51 | + description="Skip all validation rules, use with caution. Defaults to false." |
| 52 | + /> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + <div class="flex space-x-3 flex-shrink-0 justify-end px-4 py-4"> |
| 58 | + <Btn @click="closeSlide">Cancel</Btn> |
| 59 | + <Btn :loading="isLoading" :disabled="isLoading" primary is-submit>Transfer</Btn> |
| 60 | + </div> |
| 61 | + </Form> |
| 62 | +</template> |
| 63 | + |
| 64 | +<script setup lang="ts"> |
| 65 | +import { Form } from 'vee-validate'; |
| 66 | +import * as yup from 'yup'; |
| 67 | +import { computed, ref } from 'vue'; |
| 68 | +import FormInput from '~/components/FormInput.vue'; |
| 69 | +import FormCheckbox from '~/components/FormCheckbox.vue'; |
| 70 | +import Btn from '~/components/Btn.vue'; |
| 71 | +import { ApiService } from '~/api'; |
| 72 | +import snackbar from '~/util/snackbar'; |
| 73 | +import { currencySymbolByNetwork, formatData, formatPriceToENJ, snackbarErrors } from '~/util'; |
| 74 | +import { useAppStore } from '~/store'; |
| 75 | +import { |
| 76 | + addressRequiredSchema, |
| 77 | + booleanNotRequiredSchema, |
| 78 | + numberRequiredSchema, |
| 79 | + stringNotRequiredSchema, |
| 80 | +} from '~/util/schemas'; |
| 81 | +
|
| 82 | +const emit = defineEmits(['close']); |
| 83 | +
|
| 84 | +const isLoading = ref(false); |
| 85 | +const recipient = ref(''); |
| 86 | +const amount = ref(''); |
| 87 | +const idempotencyKey = ref(''); |
| 88 | +const keepAlive = ref(false); |
| 89 | +const skipValidation = ref(false); |
| 90 | +const formRef = ref(); |
| 91 | +
|
| 92 | +const currencySymbol = computed(() => currencySymbolByNetwork(useAppStore().config.network)); |
| 93 | +
|
| 94 | +const validation = yup.object({ |
| 95 | + recipient: addressRequiredSchema, |
| 96 | + amount: numberRequiredSchema.typeError('Amount must be a number').min(0), |
| 97 | + idempotencyKey: stringNotRequiredSchema, |
| 98 | + keepALive: booleanNotRequiredSchema, |
| 99 | + skipValidation: booleanNotRequiredSchema, |
| 100 | +}); |
| 101 | +
|
| 102 | +const transfer = async () => { |
| 103 | + await formRef.value?.validate(); |
| 104 | + if (!formRef.value?.getMeta().valid) return; |
| 105 | +
|
| 106 | + try { |
| 107 | + isLoading.value = true; |
| 108 | + const res = await ApiService.transferBalance( |
| 109 | + formatData({ |
| 110 | + recipient: recipient.value, |
| 111 | + amount: formatPriceToENJ(amount.value) ?? null, |
| 112 | + keepAlive: keepAlive.value, |
| 113 | + idempotencyKey: idempotencyKey.value, |
| 114 | + skipValidation: skipValidation.value, |
| 115 | + }) |
| 116 | + ); |
| 117 | +
|
| 118 | + const id = res.data?.TransferBalance?.id; |
| 119 | +
|
| 120 | + if (id) { |
| 121 | + snackbar.success({ |
| 122 | + title: 'Transfer Balance', |
| 123 | + text: `Transfer Balance successfully with transaction id: ${id}`, |
| 124 | + event: id, |
| 125 | + }); |
| 126 | + closeSlide(); |
| 127 | + } |
| 128 | + } catch (e) { |
| 129 | + if (snackbarErrors(e)) return; |
| 130 | + snackbar.error({ |
| 131 | + title: 'Transfer Balance', |
| 132 | + text: 'Transfer Balance failed', |
| 133 | + }); |
| 134 | + } finally { |
| 135 | + isLoading.value = false; |
| 136 | + } |
| 137 | +}; |
| 138 | +
|
| 139 | +const closeSlide = () => { |
| 140 | + emit('close'); |
| 141 | +}; |
| 142 | +</script> |
0 commit comments