Skip to content

Commit

Permalink
fix: refactor invalid reason for swap
Browse files Browse the repository at this point in the history
  • Loading branch information
onmax committed Aug 18, 2024
1 parent bb5a44b commit e353b26
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/components/modals/AssetTransferModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
@set-max="setMax()"
/>
<MessageTransition class="message-section">
<template v-if="p.invalidReason === 'insufficient-limit' && p.currentLimitCrypto">
<template v-if="p.invalidReason === InvalidSwapReason.ReachedLimit && p.currentLimitCrypto">
<!-- TEMP: wording TBD -->
<i18n path="Max swappable amount is {amount}">
<Amount
Expand All @@ -146,7 +146,7 @@
><br />
<a @click="$emit('set-max')">{{ $t("Sell max") }}</a>
</template>
<template v-else-if="p.invalidReason === 'insufficient-balance'">
<template v-else-if="p.invalidReason === InvalidSwapReason.InsufficientFunds">
{{ $t("Insufficient balance.") }}
<a @click="() => setMax()">{{ $t("Sell max") }}</a>
</template>
Expand Down Expand Up @@ -254,6 +254,7 @@ import {
AssetTransferMethod,
AssetTransferOptions,
AssetTransferParams,
InvalidSwapReason,
} from '@/composables/asset-transfer/types';
import {
PageHeader,
Expand Down Expand Up @@ -470,6 +471,7 @@ export default defineComponent({
cryptoAmount,
fiatAmount,
estimateError,
InvalidSwapReason,
};
},
components: {
Expand Down
8 changes: 7 additions & 1 deletion src/composables/asset-transfer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ export interface AssetTransferOptions {
type VueComponent = VueConstructor<Vue>;
type Computed<T> = Readonly<Ref<Readonly<T>>>;

export enum InvalidSwapReason {
None = '',
InsufficientFunds = 'insufficient-funds',
LimitReached = 'limit-reached',
}

// The object type that will be returned by the composable found in the same folder,
// which will be used in the SwapTransfer.vue component.
export interface AssetTransferParams {
Expand Down Expand Up @@ -60,7 +66,7 @@ export interface AssetTransferParams {

oasisLimitExceeded: Computed<boolean>;

invalidReason: Computed<string>;
invalidReason: Computed<InvalidSwapReason>;

canSign: Computed<boolean>;
sign: () => Promise<void>;
Expand Down
14 changes: 8 additions & 6 deletions src/composables/asset-transfer/useSinpeMovilSwap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { useSinpeMovilStore } from '@/stores/SinpeMovil';
import { SwapLimits, useSwapLimits } from '../useSwapLimits';
import SinpeUserInfo from '../../components/SinpeUserInfo.vue';
import AddressSelector from '../../components/AddressSelector.vue';
import { AssetTransferOptions, AssetTransferParams } from './types';
import { AssetTransferOptions, AssetTransferParams, InvalidSwapReason } from './types';
import { useConfig } from '../useConfig';
import { getHtlc } from '../../lib/OasisCrc';

Expand Down Expand Up @@ -106,8 +106,10 @@ export async function useSinpeMovilSwap(options: AssetTransferOptions): Promise<
return currentLimitUsd.value * usdRate;
});

// TODO move this to vue component
const insufficientBalance = computed(() => cryptoAmount.value > 0 && cryptoAmount.value > accountBalance.value);
const insufficientBalance = computed(() => {
if (!isSelling) return true;
return cryptoAmount.value > 0 && cryptoAmount.value > accountBalance.value;
});
const currentLimitCrypto = useCurrentLimitCrypto(currentLimitFiat);
const insufficientLimit = computed(() => {
const cryptoLimit = currentLimitCrypto?.value || Number.POSITIVE_INFINITY;
Expand Down Expand Up @@ -491,9 +493,9 @@ export async function useSinpeMovilSwap(options: AssetTransferOptions): Promise<
oasisLimitExceeded,

invalidReason: computed(() => {
if (insufficientBalance.value) return 'insufficient-balance';
if (insufficientLimit.value) return 'insufficient-limit';
return '';
if (insufficientBalance.value) return InvalidSwapReason.InsufficientFunds;
if (insufficientLimit.value) return InvalidSwapReason.LimitReached;
return InvalidSwapReason.None;
}),

canSign,
Expand Down

0 comments on commit e353b26

Please sign in to comment.