Skip to content

Commit 3ee2e6f

Browse files
fix: currency selection issues (#280)
1 parent 60bcac0 commit 3ee2e6f

File tree

6 files changed

+67
-60
lines changed

6 files changed

+67
-60
lines changed

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/create-invoice-form/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@requestnetwork/create-invoice-form",
3-
"version": "0.11.14",
3+
"version": "0.11.15",
44
"main": "./dist/web-component.umd.cjs",
55
"scripts": {
66
"dev": "vite dev",

packages/create-invoice-form/src/lib/create-invoice-form.svelte

Lines changed: 53 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<svelte:options customElement="create-invoice-form" />
22

33
<script lang="ts">
4-
import { getAccount, watchAccount, WatchAccountReturnType } from "@wagmi/core";
4+
import {
5+
getAccount,
6+
watchAccount,
7+
WatchAccountReturnType,
8+
} from "@wagmi/core";
59
import { Config as WagmiConfig } from "wagmi";
610
// Types
711
import { type GetAccountReturnType } from "@wagmi/core";
@@ -24,7 +28,7 @@
2428
import Status from "@requestnetwork/shared-components/status.svelte";
2529
import Modal from "@requestnetwork/shared-components/modal.svelte";
2630
import { EncryptionTypes, CipherProviderTypes } from "@requestnetwork/types";
27-
import { onDestroy, onMount, tick } from "svelte";
31+
import { onDestroy, onMount, tick } from "svelte";
2832
2933
interface CipherProvider extends CipherProviderTypes.ICipherProvider {
3034
disconnectWallet: () => void;
@@ -36,32 +40,19 @@
3640
export let currencies: CurrencyTypes.CurrencyInput[] = [];
3741
let cipherProvider: CipherProvider | undefined;
3842
39-
let account: GetAccountReturnType | undefined = wagmiConfig && getAccount(wagmiConfig);
43+
let account: GetAccountReturnType | undefined =
44+
wagmiConfig && getAccount(wagmiConfig);
4045
let isTimeout = false;
4146
let activeConfig = config ? config : defaultConfig;
4247
let mainColor = activeConfig.colors.main;
4348
let secondaryColor = activeConfig.colors.secondary;
4449
let currencyManager = initializeCurrencyManager(currencies);
4550
46-
let invoiceCurrencyDropdown: { clear: () => void; };
47-
let networkDropdown: { clear: () => void; };
48-
let currencyDropdown: { clear: () => void; };
49-
50-
const extractUniqueNetworkNames = (): string[] => {
51-
const networkSet = new Set<string>();
52-
53-
currencyManager.knownCurrencies.forEach(
54-
(currency: CurrencyTypes.CurrencyDefinition) => {
55-
if (currency.network) {
56-
networkSet.add(currency.network);
57-
}
58-
}
59-
);
60-
61-
return Array.from(networkSet);
62-
};
63-
64-
let networks: string[] = extractUniqueNetworkNames();
51+
let invoiceCurrencyDropdown: { clear: () => void };
52+
let networkDropdown: { clear: () => void };
53+
let currencyDropdown: { clear: () => void };
54+
let filteredSettlementCurrencies: CurrencyTypes.CurrencyDefinition[] = [];
55+
let networks: string[] = [];
6556
6657
let network: string | undefined = undefined;
6758
let currency: CurrencyTypes.CurrencyDefinition | undefined = undefined;
@@ -70,44 +61,55 @@
7061
const handleNetworkChange = (newNetwork: string) => {
7162
if (newNetwork) {
7263
currencyDropdown.clear();
64+
invoiceCurrency = invoiceCurrency?.type !== Types.RequestLogic.CURRENCY.ISO4217 ? currencyManager.knownCurrencies.find(currency => invoiceCurrency?.symbol === currency.symbol && currency.network === newNetwork) : invoiceCurrency;
7365
network = newNetwork;
74-
7566
currency = undefined;
7667
77-
filteredSettlementCurrencies = currencyManager.knownCurrencies.filter((currency: CurrencyTypes.CurrencyDefinition) => {
78-
if (!invoiceCurrency) {
79-
return false;
80-
}
81-
82-
// For ISO4217 currencies (like EUR)
83-
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
84-
const hasValidPath =
85-
currencyManager?.getConversionPath(
86-
invoiceCurrency,
87-
currency,
88-
currency?.network
89-
)?.length > 0;
90-
91-
return (
92-
currency.type !== Types.RequestLogic.CURRENCY.ISO4217 && hasValidPath
93-
);
94-
}
95-
96-
// For other currency types (like ERC20)
97-
return invoiceCurrency.hash === currency?.hash;
98-
});;
68+
filteredSettlementCurrencies = currencyManager.knownCurrencies.filter(
69+
(currency: CurrencyTypes.CurrencyDefinition) => {
70+
if (!invoiceCurrency) {
71+
return false;
72+
}
73+
74+
// For ISO4217 currencies (like EUR)
75+
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
76+
const hasValidPath =
77+
currencyManager?.getConversionPath(
78+
invoiceCurrency,
79+
currency,
80+
currency?.network,
81+
)?.length > 0;
82+
83+
return (
84+
currency.type !== Types.RequestLogic.CURRENCY.ISO4217 &&
85+
hasValidPath &&
86+
currency.network === newNetwork
87+
);
88+
}
89+
90+
// For other currency types (like ERC20)
91+
return invoiceCurrency.hash === currency?.hash;
92+
},
93+
);
9994
}
10095
};
10196
10297
let activeRequest: any = null;
10398
let canSubmit = false;
10499
let appStatus: APP_STATUS[] = [];
105100
let formData = getInitialFormData();
106-
let defaultCurrencies = currencyManager.knownCurrencies;
107-
let filteredSettlementCurrencies: CurrencyTypes.CurrencyDefinition[] = [];
101+
// Remove duplicate currencies and filter out currencies with '-' in the symbol
102+
let defaultCurrencies = Object.values(currencyManager.knownCurrencies.reduce(
103+
(unique: { [x: string]: any; }, currency: { symbol: string | number; }) => {
104+
if (!unique[currency.symbol] && !currency.symbol.includes('-')) unique[currency.symbol] = currency;
105+
106+
return unique;
107+
},
108+
{},
109+
));
108110
109111
const handleInvoiceCurrencyChange = (
110-
value: CurrencyTypes.CurrencyDefinition
112+
value: CurrencyTypes.CurrencyDefinition,
111113
) => {
112114
if (value !== invoiceCurrency) {
113115
networkDropdown.clear();
@@ -122,10 +124,10 @@
122124
if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) {
123125
networks = (getCurrencySupportedNetworksForConversion(
124126
invoiceCurrency.hash,
125-
currencyManager
127+
currencyManager,
126128
) ?? []) as string[];
127129
} else {
128-
networks = extractUniqueNetworkNames();
130+
networks = currencyManager.knownCurrencies.filter(currency => currency.symbol === invoiceCurrency?.symbol).map(currency => currency.network);
129131
}
130132
}
131133
};
@@ -257,7 +259,7 @@
257259
paymentNetwork: requestCreateParameters.paymentNetwork,
258260
contentData: requestCreateParameters.contentData,
259261
},
260-
[payeeEncryptionParams, payerEncryptionParams]
262+
[payeeEncryptionParams, payerEncryptionParams],
261263
);
262264
} else {
263265
request = await requestNetwork.createRequest({

packages/create-invoice-form/src/lib/invoice/form.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@
407407
bind:this={invoiceCurrencyDropdown}
408408
getValue={(currency) => currency.value.symbol}
409409
getDisplayValue={(currency) =>
410-
`${currency.value.symbol} ${currency.value.network ? `(${currency.value.network})` : ""}`}
410+
`${currency.value.symbol}`}
411411
placeholder="Invoice currency"
412412
items={defaultCurrencies
413413
?.filter((curr) => curr)

packages/invoice-dashboard/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@requestnetwork/invoice-dashboard",
3-
"version": "0.11.12",
3+
"version": "0.11.13",
44
"main": "./dist/web-component.umd.cjs",
55
"scripts": {
66
"dev": "vite dev",

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,9 +480,14 @@
480480
}
481481
482482
function formatBalance(value: number, maxDecimals: number = 4): string {
483-
return Number.isInteger(value)
484-
? value.toString()
485-
: value.toFixed(maxDecimals);
483+
try {
484+
return Number.isInteger(value)
485+
? value.toString()
486+
: value.toFixed(maxDecimals);
487+
} catch (error) {
488+
console.error("Error formatting balance:", error);
489+
return "-";
490+
}
486491
}
487492
488493
async function checkBalance() {

0 commit comments

Comments
 (0)