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

fix: Expected Amount Display for Unknown Currencies in Invoice Dashboard #291

Merged
Show file tree
Hide file tree
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
102 changes: 58 additions & 44 deletions packages/invoice-dashboard/src/lib/dashboard/invoice-view.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -155,7 +156,7 @@
}

$: {
if (account && network) {
if (account && network && !unknownCurrency) {
checkBalance();
}
}
Expand Down Expand Up @@ -504,7 +505,6 @@
token: paymentCurrencies[0].address as `0x${string}`,
chainId: invoiceNetworkId,
});
;
userBalance = balance.formatted;
hasEnoughBalance = balance.value >= BigInt(request.expectedAmount);
} else {
Expand Down Expand Up @@ -637,18 +637,18 @@
<h3 class="invoice-info-payment">
<span style="font-weight: 500;">Payment Chain:</span>
{paymentCurrencies && paymentCurrencies.length > 0
? paymentCurrencies[0]?.network || "-"
? paymentCurrencies[0]?.network || "Unknown"
: ""}
</h3>
<h3 class="invoice-info-payment">
<span style="font-weight: 500;">Invoice Currency:</span>
{currency?.symbol || "-"}
{currency?.symbol || "Unknown"}
</h3>

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

Expand All @@ -674,27 +674,34 @@
<p class="truncate description-text">{item.name || "-"}</p>
</th>
<td>{item.quantity || "-"}</td>
<td
>{item.unitPrice
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
: "-"}</td
>
<td
>{item.discount
<td>
{#if unknownCurrency}
Unknown
{:else}
{item.unitPrice
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
: "-"}
{/if}
</td>
<td>
{item.discount
? formatUnits(item.discount, currency?.decimals ?? 18)
: "-"}</td
>
: "-"}
</td>
<td>{Number(item.tax.amount || "-")}</td>
<td
>{truncateNumberString(
formatUnits(
// @ts-expect-error
calculateItemTotal(item),
currency?.decimals ?? 18
),
2
)}</td
>
<td>
{#if unknownCurrency}
Unknown
{:else}
{truncateNumberString(
formatUnits(
calculateItemTotal(item),
currency?.decimals ?? 18
),
2
)}
{/if}
</td>
</tr>
{/each}
</tbody>
Expand Down Expand Up @@ -723,27 +730,34 @@
</p>
</th>
<td>{item.quantity || "-"}</td>
<td
>{item.unitPrice
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
: "-"}</td
>
<td
>{item.discount
<td>
{#if unknownCurrency}
Unknown
{:else}
{item.unitPrice
? formatUnits(item.unitPrice, currency?.decimals ?? 18)
: "-"}
{/if}
</td>
<td>
{item.discount
? formatUnits(item.discount, currency?.decimals ?? 18)
: "-"}</td
>
: "-"}
</td>
<td>{Number(item.tax.amount || "-")}</td>
<td
>{truncateNumberString(
formatUnits(
// @ts-expect-error
calculateItemTotal(item),
currency?.decimals ?? 18
),
2
)}</td
>
<td>
{#if unknownCurrency}
Unknown
{:else}
{truncateNumberString(
formatUnits(
calculateItemTotal(item),
currency?.decimals ?? 18
),
2
)}
{/if}
</td>
</tr>
{/each}</tbody
>
Expand Down Expand Up @@ -832,7 +846,7 @@
</div>
{/if}
<div class="invoice-view-actions">
{#if !isPayee && !unsupportedNetwork && !isPaid && !isRequestPayed && !isSigningTransaction}
{#if !isPayee && !unsupportedNetwork && !isPaid && !isRequestPayed && !isSigningTransaction && !unknownCurrency}
{#if !hasEnoughBalance}
<div class="balance-warning">
Insufficient funds: {Number(userBalance).toFixed(4)}
Expand Down
30 changes: 22 additions & 8 deletions packages/invoice-dashboard/src/lib/view-requests.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -132,7 +135,10 @@

onMount(() => {
unwatchAccount = watchAccount(wagmiConfig, {
onChange(account: GetAccountReturnType, previousAccount: GetAccountReturnType) {
onChange(
account: GetAccountReturnType,
previousAccount: GetAccountReturnType
) {
tick().then(() => {
handleWalletChange(account, previousAccount);
});
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -328,11 +339,8 @@

return {
...request,
formattedAmount: formatUnits(
BigInt(request.expectedAmount),
currencyInfo?.decimals ?? 18
),
currencySymbol: currencyInfo?.symbol ?? "-",
formattedAmount,
currencySymbol: currencyInfo?.symbol ?? "",
paymentCurrencies,
};
}
Expand Down Expand Up @@ -710,7 +718,13 @@
</td>
{/if}
<td>
{#if request.formattedAmount.includes(".") && request.formattedAmount.split(".")[1].length > 5}
{#if request.formattedAmount === "Unknown"}
<Tooltip
text="Cannot calculate the expected amount due to unknown decimals"
>
Unknown
</Tooltip>
{:else if request.formattedAmount.includes(".") && request.formattedAmount.split(".")[1].length > 5}
<Tooltip text={request.formattedAmount}>
{Number(request.formattedAmount).toFixed(5)}
</Tooltip>
Expand Down
Loading