From 53fce1d873e92c3e56596b9b18827d280f9891fa Mon Sep 17 00:00:00 2001 From: sstefdev Date: Fri, 20 Dec 2024 14:18:30 +0100 Subject: [PATCH] fix: added fallbackn when invoice currency is unknown --- .../src/lib/dashboard/invoice-view.svelte | 102 ++++++++++-------- .../src/lib/view-requests.svelte | 30 ++++-- 2 files changed, 80 insertions(+), 52 deletions(-) diff --git a/packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte b/packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte index 4c2a583f..4799e129 100644 --- a/packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte +++ b/packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte @@ -64,6 +64,7 @@ let otherItems: any; let sellerInfo: SellerInfo[] = []; let buyerInfo: BuyerInfo[] = []; + let unknownCurrency = currency?.decimals === undefined; let isPayee = request?.payee?.value.toLowerCase() === address?.toLowerCase(); let unsupportedNetwork = false; let hexStringChain = "0x" + account?.chainId?.toString(16); @@ -155,7 +156,7 @@ } $: { - if (account && network) { + if (account && network && !unknownCurrency) { checkBalance(); } } @@ -504,7 +505,6 @@ token: paymentCurrencies[0].address as `0x${string}`, chainId: invoiceNetworkId, }); - ; userBalance = balance.formatted; hasEnoughBalance = balance.value >= BigInt(request.expectedAmount); } else { @@ -637,18 +637,18 @@

Payment Chain: {paymentCurrencies && paymentCurrencies.length > 0 - ? paymentCurrencies[0]?.network || "-" + ? paymentCurrencies[0]?.network || "Unknown" : ""}

Invoice Currency: - {currency?.symbol || "-"} + {currency?.symbol || "Unknown"}

Settlement Currency: {paymentCurrencies && paymentCurrencies.length > 0 - ? paymentCurrencies[0]?.symbol || "-" + ? paymentCurrencies[0]?.symbol || "Unknown" : ""}

@@ -674,27 +674,34 @@

{item.name || "-"}

{item.quantity || "-"} - {item.unitPrice - ? formatUnits(item.unitPrice, currency?.decimals ?? 18) - : "-"} - {item.discount + + {#if unknownCurrency} + Unknown + {:else} + {item.unitPrice + ? formatUnits(item.unitPrice, currency?.decimals ?? 18) + : "-"} + {/if} + + + {item.discount ? formatUnits(item.discount, currency?.decimals ?? 18) - : "-"} + : "-"} + {Number(item.tax.amount || "-")} - {truncateNumberString( - formatUnits( - // @ts-expect-error - calculateItemTotal(item), - currency?.decimals ?? 18 - ), - 2 - )} + + {#if unknownCurrency} + Unknown + {:else} + {truncateNumberString( + formatUnits( + calculateItemTotal(item), + currency?.decimals ?? 18 + ), + 2 + )} + {/if} + {/each} @@ -723,27 +730,34 @@

{item.quantity || "-"} - {item.unitPrice - ? formatUnits(item.unitPrice, currency?.decimals ?? 18) - : "-"} - {item.discount + + {#if unknownCurrency} + Unknown + {:else} + {item.unitPrice + ? formatUnits(item.unitPrice, currency?.decimals ?? 18) + : "-"} + {/if} + + + {item.discount ? formatUnits(item.discount, currency?.decimals ?? 18) - : "-"} + : "-"} + {Number(item.tax.amount || "-")} - {truncateNumberString( - formatUnits( - // @ts-expect-error - calculateItemTotal(item), - currency?.decimals ?? 18 - ), - 2 - )} + + {#if unknownCurrency} + Unknown + {:else} + {truncateNumberString( + formatUnits( + calculateItemTotal(item), + currency?.decimals ?? 18 + ), + 2 + )} + {/if} + {/each} @@ -832,7 +846,7 @@ {/if}
- {#if !isPayee && !unsupportedNetwork && !isPaid && !isRequestPayed && !isSigningTransaction} + {#if !isPayee && !unsupportedNetwork && !isPaid && !isRequestPayed && !isSigningTransaction && !unknownCurrency} {#if !hasEnoughBalance}
Insufficient funds: {Number(userBalance).toFixed(4)} diff --git a/packages/invoice-dashboard/src/lib/view-requests.svelte b/packages/invoice-dashboard/src/lib/view-requests.svelte index 5c5742fe..dd744b5b 100644 --- a/packages/invoice-dashboard/src/lib/view-requests.svelte +++ b/packages/invoice-dashboard/src/lib/view-requests.svelte @@ -119,7 +119,10 @@ cipherProvider = undefined; }; - const handleWalletChange = (account: GetAccountReturnType, previousAccount: GetAccountReturnType) => { + const handleWalletChange = ( + account: GetAccountReturnType, + previousAccount: GetAccountReturnType + ) => { if (account?.address !== previousAccount?.address) { handleWalletDisconnection(); handleWalletConnection(); @@ -132,7 +135,10 @@ onMount(() => { unwatchAccount = watchAccount(wagmiConfig, { - onChange(account: GetAccountReturnType, previousAccount: GetAccountReturnType) { + onChange( + account: GetAccountReturnType, + previousAccount: GetAccountReturnType + ) { tick().then(() => { handleWalletChange(account, previousAccount); }); @@ -279,6 +285,11 @@ currencyManager ); + const formattedAmount = + currencyInfo?.decimals !== undefined + ? formatUnits(BigInt(request.expectedAmount), currencyInfo.decimals) + : "Unknown"; + let paymentNetworkExtension = getPaymentNetworkExtension(request); let paymentCurrencies: ( | CurrencyTypes.ERC20Currency @@ -328,11 +339,8 @@ return { ...request, - formattedAmount: formatUnits( - BigInt(request.expectedAmount), - currencyInfo?.decimals ?? 18 - ), - currencySymbol: currencyInfo?.symbol ?? "-", + formattedAmount, + currencySymbol: currencyInfo?.symbol ?? "", paymentCurrencies, }; } @@ -710,7 +718,13 @@ {/if} - {#if request.formattedAmount.includes(".") && request.formattedAmount.split(".")[1].length > 5} + {#if request.formattedAmount === "Unknown"} + + Unknown + + {:else if request.formattedAmount.includes(".") && request.formattedAmount.split(".")[1].length > 5} {Number(request.formattedAmount).toFixed(5)}