|
16 | 16 | import { CurrencyTypes } from "@requestnetwork/types"; |
17 | 17 | // Utils |
18 | 18 | import { getInitialFormData, prepareRequestParams } from "./utils"; |
19 | | - import { config as defaultConfig } from "@requestnetwork/shared-utils/config"; |
20 | | - import { calculateInvoiceTotals } from "@requestnetwork/shared-utils/invoiceTotals"; |
21 | 19 | import { |
| 20 | + config as defaultConfig, |
| 21 | + calculateInvoiceTotals, |
22 | 22 | getCurrencySupportedNetworksForConversion, |
23 | | - initializeCurrencyManager, |
24 | | - } from "@requestnetwork/shared-utils/initCurrencyManager"; |
| 23 | + initializeCreateInvoiceCurrencyManager, |
| 24 | + } from "@requestnetwork/shared-utils/index"; |
25 | 25 | // Components |
26 | 26 | import { InvoiceForm, InvoiceView } from "./invoice"; |
27 | 27 | import Button from "@requestnetwork/shared-components/button.svelte"; |
28 | 28 | import Status from "@requestnetwork/shared-components/status.svelte"; |
29 | 29 | import Modal from "@requestnetwork/shared-components/modal.svelte"; |
30 | 30 | import { EncryptionTypes, CipherProviderTypes } from "@requestnetwork/types"; |
31 | 31 | import { onDestroy, onMount, tick } from "svelte"; |
| 32 | + import { CurrencyManager } from "@requestnetwork/currency"; |
32 | 33 |
|
33 | 34 | interface CipherProvider extends CipherProviderTypes.ICipherProvider { |
34 | 35 | disconnectWallet: () => void; |
|
37 | 38 | export let config: IConfig; |
38 | 39 | export let wagmiConfig: WagmiConfig; |
39 | 40 | export let requestNetwork: RequestNetwork | null | undefined; |
40 | | - export let currencies: CurrencyTypes.CurrencyInput[] = []; |
| 41 | + export let currencies: string[] = []; |
41 | 42 | let cipherProvider: CipherProvider | undefined; |
42 | 43 |
|
43 | 44 | let account: GetAccountReturnType | undefined = |
|
46 | 47 | let activeConfig = config ? config : defaultConfig; |
47 | 48 | let mainColor = activeConfig.colors.main; |
48 | 49 | let secondaryColor = activeConfig.colors.secondary; |
49 | | - let currencyManager = initializeCurrencyManager(currencies); |
| 50 | + let currencyManager: CurrencyManager; |
50 | 51 |
|
51 | 52 | let invoiceCurrencyDropdown: { clear: () => void }; |
52 | 53 | let networkDropdown: { clear: () => void }; |
|
58 | 59 | let currency: CurrencyTypes.CurrencyDefinition | undefined = undefined; |
59 | 60 | let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined = undefined; |
60 | 61 |
|
| 62 | + let defaultCurrencies: any[] = []; |
| 63 | +
|
| 64 | + onMount(async () => { |
| 65 | + currencyManager = await initializeCreateInvoiceCurrencyManager(currencies); |
| 66 | +
|
| 67 | + defaultCurrencies = Object.values( |
| 68 | + currencyManager.knownCurrencies.reduce( |
| 69 | + ( |
| 70 | + unique: { [x: string]: any }, |
| 71 | + currency: { symbol: string | number } |
| 72 | + ) => { |
| 73 | + const baseSymbol = String(currency.symbol).split("-")[0]; |
| 74 | + if (!unique[baseSymbol]) { |
| 75 | + unique[baseSymbol] = { |
| 76 | + ...currency, |
| 77 | + symbol: baseSymbol, |
| 78 | + }; |
| 79 | + } |
| 80 | + return unique; |
| 81 | + }, |
| 82 | + {} |
| 83 | + ) |
| 84 | + ); |
| 85 | +
|
| 86 | + unwatchAccount = watchAccount(wagmiConfig, { |
| 87 | + onChange( |
| 88 | + account: GetAccountReturnType, |
| 89 | + previousAccount: GetAccountReturnType |
| 90 | + ) { |
| 91 | + tick().then(() => { |
| 92 | + handleWalletChange(account, previousAccount); |
| 93 | + }); |
| 94 | + }, |
| 95 | + }); |
| 96 | + }); |
| 97 | +
|
61 | 98 | const handleNetworkChange = (newNetwork: string) => { |
62 | 99 | if (newNetwork) { |
63 | 100 | currencyDropdown.clear(); |
64 | | - invoiceCurrency = invoiceCurrency?.type !== Types.RequestLogic.CURRENCY.ISO4217 ? currencyManager.knownCurrencies.find(currency => invoiceCurrency?.symbol === currency.symbol && currency.network === newNetwork) : invoiceCurrency; |
| 101 | + invoiceCurrency = |
| 102 | + invoiceCurrency?.type !== Types.RequestLogic.CURRENCY.ISO4217 |
| 103 | + ? currencyManager.knownCurrencies.find( |
| 104 | + (currency) => |
| 105 | + currency.symbol.split("-")[0] === |
| 106 | + invoiceCurrency?.symbol.split("-")[0] && |
| 107 | + currency.network === newNetwork |
| 108 | + ) |
| 109 | + : invoiceCurrency; |
65 | 110 | network = newNetwork; |
66 | 111 | currency = undefined; |
67 | 112 |
|
|
77 | 122 | currencyManager?.getConversionPath( |
78 | 123 | invoiceCurrency, |
79 | 124 | currency, |
80 | | - currency?.network, |
| 125 | + currency?.network |
81 | 126 | )?.length > 0; |
82 | 127 |
|
83 | 128 | return ( |
|
88 | 133 | } |
89 | 134 |
|
90 | 135 | // For other currency types (like ERC20) |
91 | | - return invoiceCurrency.hash === currency?.hash; |
92 | | - }, |
| 136 | + // Compare base symbols (without network suffix) |
| 137 | + const invoiceBaseSymbol = invoiceCurrency.symbol.split("-")[0]; |
| 138 | + const currencyBaseSymbol = currency.symbol.split("-")[0]; |
| 139 | + return ( |
| 140 | + invoiceBaseSymbol === currencyBaseSymbol && |
| 141 | + currency.network === newNetwork |
| 142 | + ); |
| 143 | + } |
93 | 144 | ); |
94 | 145 | } |
95 | 146 | }; |
|
98 | 149 | let canSubmit = false; |
99 | 150 | let appStatus: APP_STATUS[] = []; |
100 | 151 | let formData = getInitialFormData(); |
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 | | - )); |
110 | 152 |
|
111 | 153 | const handleInvoiceCurrencyChange = ( |
112 | | - value: CurrencyTypes.CurrencyDefinition, |
| 154 | + value: CurrencyTypes.CurrencyDefinition |
113 | 155 | ) => { |
114 | 156 | if (value !== invoiceCurrency) { |
115 | 157 | networkDropdown.clear(); |
|
119 | 161 | currency = undefined; |
120 | 162 | filteredSettlementCurrencies = []; |
121 | 163 | network = undefined; |
122 | | - networks = []; |
| 164 | +
|
| 165 | + const availableNetworks = new Set( |
| 166 | + currencyManager.knownCurrencies.map((currency) => currency.network) |
| 167 | + ); |
123 | 168 |
|
124 | 169 | if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) { |
125 | | - networks = (getCurrencySupportedNetworksForConversion( |
126 | | - invoiceCurrency.hash, |
127 | | - currencyManager, |
128 | | - ) ?? []) as string[]; |
| 170 | + const conversionNetworks = new Set( |
| 171 | + getCurrencySupportedNetworksForConversion( |
| 172 | + invoiceCurrency.hash, |
| 173 | + currencyManager |
| 174 | + ) |
| 175 | + ); |
| 176 | +
|
| 177 | + networks = [...availableNetworks].filter( |
| 178 | + (network) => |
| 179 | + conversionNetworks.has(network) && |
| 180 | + (config.supportedNetworks |
| 181 | + ? config.supportedNetworks.includes(network) |
| 182 | + : true) |
| 183 | + ); |
129 | 184 | } else { |
130 | | - networks = currencyManager.knownCurrencies.filter(currency => currency.symbol === invoiceCurrency?.symbol).map(currency => currency.network); |
| 185 | + const baseSymbol = invoiceCurrency.symbol.split("-")[0]; |
| 186 | + networks = [...availableNetworks].filter((network) => { |
| 187 | + const hasToken = currencyManager.knownCurrencies.some( |
| 188 | + (currency) => |
| 189 | + currency.network === network && |
| 190 | + currency.symbol.split("-")[0] === baseSymbol |
| 191 | + ); |
| 192 | +
|
| 193 | + return ( |
| 194 | + hasToken && |
| 195 | + (config.supportedNetworks |
| 196 | + ? config.supportedNetworks.includes(network) |
| 197 | + : true) |
| 198 | + ); |
| 199 | + }); |
131 | 200 | } |
| 201 | +
|
| 202 | + networks = [...new Set(networks)]; |
132 | 203 | } |
133 | 204 | }; |
134 | 205 |
|
|
152 | 223 | cipherProvider?.disconnectWallet(); |
153 | 224 | }; |
154 | 225 |
|
155 | | - const handleWalletChange = (account: GetAccountReturnType, previousAccount: GetAccountReturnType) => { |
| 226 | + const handleWalletChange = ( |
| 227 | + account: GetAccountReturnType, |
| 228 | + previousAccount: GetAccountReturnType |
| 229 | + ) => { |
156 | 230 | if (account?.address !== previousAccount?.address) { |
157 | 231 | handleWalletDisconnection(); |
158 | 232 | handleWalletConnection(); |
|
163 | 237 | } |
164 | 238 | }; |
165 | 239 |
|
166 | | - onMount(() => { |
167 | | - unwatchAccount = watchAccount(wagmiConfig, { |
168 | | - onChange(account: GetAccountReturnType, previousAccount: GetAccountReturnType) { |
169 | | - tick().then(() => { |
170 | | - handleWalletChange(account, previousAccount); |
171 | | - }); |
172 | | - }, |
173 | | - }); |
174 | | - }); |
175 | | -
|
176 | 240 | let unwatchAccount: WatchAccountReturnType | undefined; |
177 | 241 |
|
178 | 242 | onDestroy(() => { |
|
261 | 325 | paymentNetwork: requestCreateParameters.paymentNetwork, |
262 | 326 | contentData: requestCreateParameters.contentData, |
263 | 327 | }, |
264 | | - [payeeEncryptionParams, payerEncryptionParams], |
| 328 | + [payeeEncryptionParams, payerEncryptionParams] |
265 | 329 | ); |
266 | 330 | } else { |
267 | 331 | request = await requestNetwork.createRequest({ |
|
0 commit comments