Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IN-PROGRESS] Improve error priority. #459

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 37 additions & 17 deletions src/pages/swap/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ import { OfframpSummaryDialog } from '../../components/OfframpSummaryDialog';

import satoshipayLogo from '../../assets/logo/satoshipay.svg';

type ExchangeRateCache = Partial<Record<InputTokenType, Partial<Record<OutputTokenType, number>>>>;

export const SwapPage = () => {
const formRef = useRef<HTMLDivElement | null>(null);
const feeComparisonRef = useRef<FeeComparisonRef>(null);
const pendulumNode = usePendulumNode();

const [api, setApi] = useState<ApiPromise | null>(null);
const { isDisconnected, address } = useVortexAccount();
const [initializeFailedMessage, setInitializeFailedMessage] = useState<string | null>(null);
Expand All @@ -79,11 +81,18 @@ export const SwapPage = () => {
const [isOfframpSummaryDialogVisible, setIsOfframpSummaryDialogVisible] = useState(false);
const [cachedAnchorUrl, setCachedAnchorUrl] = useState<string | undefined>(undefined);
const [cachedId, setCachedId] = useState<string | undefined>(undefined);
const [termsAnimationKey, setTermsAnimationKey] = useState(0);
const [exchangeRateCache, setExchangeRateCache] = useState<ExchangeRateCache>({
usdc: { ars: 1200, eurc: 0.95 },
usdce: { ars: 1200, eurc: 0.95 },
usdt: { ars: 1200, eurc: 0.95 },
});

const { trackEvent } = useEventsContext();
const { selectedNetwork, setNetworkSelectorDisabled } = useNetwork();
const { walletAccount } = usePolkadotWalletState();
const pendulumNode = usePendulumNode();

const [termsAnimationKey, setTermsAnimationKey] = useState(0);
const {
error: signingServiceError,
isLoading: isSigningServiceLoading,
Expand Down Expand Up @@ -210,6 +219,10 @@ export const SwapPage = () => {
// Calculate the final amount after the offramp fees
const totalReceive = calculateTotalReceive(toAmount, toToken);
form.setValue('toAmount', totalReceive);
setExchangeRateCache((prev) => ({
...prev,
[from]: { ...prev[from], [to]: tokenOutAmount.data!.effectiveExchangeRate },
}));
} else if (!tokenOutAmount.isLoading || tokenOutAmount.error) {
form.setValue('toAmount', '0');
} else {
Expand Down Expand Up @@ -289,26 +302,30 @@ export const SwapPage = () => {
function getCurrentErrorMessage() {
if (isDisconnected) return;

if (typeof userInputTokenBalance === 'string') {
if (Big(userInputTokenBalance).lt(fromAmount ?? 0) && walletAccount) {
trackEvent({ event: 'form_error', error_message: 'insufficient_balance' });
return `Insufficient balance. Your balance is ${userInputTokenBalance} ${fromToken?.assetSymbol}.`;
}
}
//TODO - commented for testing and preview only. REMOVE COMMENT.
// if (typeof userInputTokenBalance === 'string') {
// if (Big(userInputTokenBalance).lt(fromAmount ?? 0) && walletAccount) {
// trackEvent({ event: 'form_error', error_message: 'insufficient_balance' });
// return `Insufficient balance. Your balance is ${userInputTokenBalance} ${fromToken?.assetSymbol}.`;
// }
// }

const amountOut = tokenOutAmount.data?.roundedDownQuotedAmountOut;

if (amountOut !== undefined) {
const maxAmountUnits = multiplyByPowerOfTen(Big(toToken.maxWithdrawalAmountRaw), -toToken.decimals);
const minAmountUnits = multiplyByPowerOfTen(Big(toToken.minWithdrawalAmountRaw), -toToken.decimals);
const maxAmountUnits = multiplyByPowerOfTen(Big(toToken.maxWithdrawalAmountRaw), -toToken.decimals);
const minAmountUnits = multiplyByPowerOfTen(Big(toToken.minWithdrawalAmountRaw), -toToken.decimals);

if (maxAmountUnits.lt(amountOut)) {
trackEvent({ event: 'form_error', error_message: 'more_than_maximum_withdrawal' });
return `Maximum withdrawal amount is ${stringifyBigWithSignificantDecimals(maxAmountUnits, 2)} ${
toToken.fiat.symbol
}.`;
}
const exchangeRate = tokenOutAmount.data?.effectiveExchangeRate || exchangeRateCache[from]?.[to];

if (fromAmount && exchangeRate && maxAmountUnits.lt(fromAmount.mul(exchangeRate))) {
console.log(exchangeRate, fromAmount!.mul(exchangeRate).toNumber());
trackEvent({ event: 'form_error', error_message: 'more_than_maximum_withdrawal' });
return `Maximum withdrawal amount is ${stringifyBigWithSignificantDecimals(maxAmountUnits, 2)} ${
toToken.fiat.symbol
}.`;
}

if (amountOut !== undefined) {
if (!config.test.overwriteMinimumTransferAmount && minAmountUnits.gt(amountOut)) {
trackEvent({ event: 'form_error', error_message: 'less_than_minimum_withdrawal' });
return `Minimum withdrawal amount is ${stringifyBigWithSignificantDecimals(minAmountUnits, 2)} ${
Expand All @@ -317,6 +334,9 @@ export const SwapPage = () => {
}
}

if (tokenOutAmount.error?.includes('Insufficient liquidity')) {
return 'The amount is temporarily not available. Please, try with a smaller amount.';
}
return tokenOutAmount.error;
}

Expand Down