Skip to content

Commit bc74471

Browse files
authored
fix: Expected Amount Display for Unknown Currencies in Invoice Dashboard (#291)
1 parent fa6f216 commit bc74471

File tree

2 files changed

+80
-52
lines changed

2 files changed

+80
-52
lines changed

packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
let otherItems: any;
6565
let sellerInfo: SellerInfo[] = [];
6666
let buyerInfo: BuyerInfo[] = [];
67+
let unknownCurrency = currency?.decimals === undefined;
6768
let isPayee = request?.payee?.value.toLowerCase() === address?.toLowerCase();
6869
let unsupportedNetwork = false;
6970
let hexStringChain = "0x" + account?.chainId?.toString(16);
@@ -155,7 +156,7 @@
155156
}
156157
157158
$: {
158-
if (account && network) {
159+
if (account && network && !unknownCurrency) {
159160
checkBalance();
160161
}
161162
}
@@ -504,7 +505,6 @@
504505
token: paymentCurrencies[0].address as `0x${string}`,
505506
chainId: invoiceNetworkId,
506507
});
507-
;
508508
userBalance = balance.formatted;
509509
hasEnoughBalance = balance.value >= BigInt(request.expectedAmount);
510510
} else {
@@ -637,18 +637,18 @@
637637
<h3 class="invoice-info-payment">
638638
<span style="font-weight: 500;">Payment Chain:</span>
639639
{paymentCurrencies && paymentCurrencies.length > 0
640-
? paymentCurrencies[0]?.network || "-"
640+
? paymentCurrencies[0]?.network || "Unknown"
641641
: ""}
642642
</h3>
643643
<h3 class="invoice-info-payment">
644644
<span style="font-weight: 500;">Invoice Currency:</span>
645-
{currency?.symbol || "-"}
645+
{currency?.symbol || "Unknown"}
646646
</h3>
647647

648648
<h3 class="invoice-info-payment">
649649
<span style="font-weight: 500;">Settlement Currency:</span>
650650
{paymentCurrencies && paymentCurrencies.length > 0
651-
? paymentCurrencies[0]?.symbol || "-"
651+
? paymentCurrencies[0]?.symbol || "Unknown"
652652
: ""}
653653
</h3>
654654

@@ -674,27 +674,34 @@
674674
<p class="truncate description-text">{item.name || "-"}</p>
675675
</th>
676676
<td>{item.quantity || "-"}</td>
677-
<td
678-
>{item.unitPrice
679-
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
680-
: "-"}</td
681-
>
682-
<td
683-
>{item.discount
677+
<td>
678+
{#if unknownCurrency}
679+
Unknown
680+
{:else}
681+
{item.unitPrice
682+
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
683+
: "-"}
684+
{/if}
685+
</td>
686+
<td>
687+
{item.discount
684688
? formatUnits(item.discount, currency?.decimals ?? 18)
685-
: "-"}</td
686-
>
689+
: "-"}
690+
</td>
687691
<td>{Number(item.tax.amount || "-")}</td>
688-
<td
689-
>{truncateNumberString(
690-
formatUnits(
691-
// @ts-expect-error
692-
calculateItemTotal(item),
693-
currency?.decimals ?? 18
694-
),
695-
2
696-
)}</td
697-
>
692+
<td>
693+
{#if unknownCurrency}
694+
Unknown
695+
{:else}
696+
{truncateNumberString(
697+
formatUnits(
698+
calculateItemTotal(item),
699+
currency?.decimals ?? 18
700+
),
701+
2
702+
)}
703+
{/if}
704+
</td>
698705
</tr>
699706
{/each}
700707
</tbody>
@@ -723,27 +730,34 @@
723730
</p>
724731
</th>
725732
<td>{item.quantity || "-"}</td>
726-
<td
727-
>{item.unitPrice
728-
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
729-
: "-"}</td
730-
>
731-
<td
732-
>{item.discount
733+
<td>
734+
{#if unknownCurrency}
735+
Unknown
736+
{:else}
737+
{item.unitPrice
738+
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
739+
: "-"}
740+
{/if}
741+
</td>
742+
<td>
743+
{item.discount
733744
? formatUnits(item.discount, currency?.decimals ?? 18)
734-
: "-"}</td
735-
>
745+
: "-"}
746+
</td>
736747
<td>{Number(item.tax.amount || "-")}</td>
737-
<td
738-
>{truncateNumberString(
739-
formatUnits(
740-
// @ts-expect-error
741-
calculateItemTotal(item),
742-
currency?.decimals ?? 18
743-
),
744-
2
745-
)}</td
746-
>
748+
<td>
749+
{#if unknownCurrency}
750+
Unknown
751+
{:else}
752+
{truncateNumberString(
753+
formatUnits(
754+
calculateItemTotal(item),
755+
currency?.decimals ?? 18
756+
),
757+
2
758+
)}
759+
{/if}
760+
</td>
747761
</tr>
748762
{/each}</tbody
749763
>
@@ -832,7 +846,7 @@
832846
</div>
833847
{/if}
834848
<div class="invoice-view-actions">
835-
{#if !isPayee && !unsupportedNetwork && !isPaid && !isRequestPayed && !isSigningTransaction}
849+
{#if !isPayee && !unsupportedNetwork && !isPaid && !isRequestPayed && !isSigningTransaction && !unknownCurrency}
836850
{#if !hasEnoughBalance}
837851
<div class="balance-warning">
838852
Insufficient funds: {Number(userBalance).toFixed(4)}

packages/invoice-dashboard/src/lib/view-requests.svelte

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@
119119
cipherProvider = undefined;
120120
};
121121
122-
const handleWalletChange = (account: GetAccountReturnType, previousAccount: GetAccountReturnType) => {
122+
const handleWalletChange = (
123+
account: GetAccountReturnType,
124+
previousAccount: GetAccountReturnType
125+
) => {
123126
if (account?.address !== previousAccount?.address) {
124127
handleWalletDisconnection();
125128
handleWalletConnection();
@@ -132,7 +135,10 @@
132135
133136
onMount(() => {
134137
unwatchAccount = watchAccount(wagmiConfig, {
135-
onChange(account: GetAccountReturnType, previousAccount: GetAccountReturnType) {
138+
onChange(
139+
account: GetAccountReturnType,
140+
previousAccount: GetAccountReturnType
141+
) {
136142
tick().then(() => {
137143
handleWalletChange(account, previousAccount);
138144
});
@@ -279,6 +285,11 @@
279285
currencyManager
280286
);
281287
288+
const formattedAmount =
289+
currencyInfo?.decimals !== undefined
290+
? formatUnits(BigInt(request.expectedAmount), currencyInfo.decimals)
291+
: "Unknown";
292+
282293
let paymentNetworkExtension = getPaymentNetworkExtension(request);
283294
let paymentCurrencies: (
284295
| CurrencyTypes.ERC20Currency
@@ -328,11 +339,8 @@
328339
329340
return {
330341
...request,
331-
formattedAmount: formatUnits(
332-
BigInt(request.expectedAmount),
333-
currencyInfo?.decimals ?? 18
334-
),
335-
currencySymbol: currencyInfo?.symbol ?? "-",
342+
formattedAmount,
343+
currencySymbol: currencyInfo?.symbol ?? "",
336344
paymentCurrencies,
337345
};
338346
}
@@ -710,7 +718,13 @@
710718
</td>
711719
{/if}
712720
<td>
713-
{#if request.formattedAmount.includes(".") && request.formattedAmount.split(".")[1].length > 5}
721+
{#if request.formattedAmount === "Unknown"}
722+
<Tooltip
723+
text="Cannot calculate the expected amount due to unknown decimals"
724+
>
725+
Unknown
726+
</Tooltip>
727+
{:else if request.formattedAmount.includes(".") && request.formattedAmount.split(".")[1].length > 5}
714728
<Tooltip text={request.formattedAmount}>
715729
{Number(request.formattedAmount).toFixed(5)}
716730
</Tooltip>

0 commit comments

Comments
 (0)