|
20 | 20 | import { calculateInvoiceTotals } from "@requestnetwork/shared-utils/invoiceTotals"; |
21 | 21 | import { |
22 | 22 | getCurrencySupportedNetworksForConversion, |
| 23 | + initializeCreateInvoiceCurrencyManager, |
23 | 24 | initializeCurrencyManager, |
24 | 25 | } from "@requestnetwork/shared-utils/initCurrencyManager"; |
25 | 26 | // Components |
|
29 | 30 | import Modal from "@requestnetwork/shared-components/modal.svelte"; |
30 | 31 | import { EncryptionTypes, CipherProviderTypes } from "@requestnetwork/types"; |
31 | 32 | import { onDestroy, onMount, tick } from "svelte"; |
| 33 | + import { CurrencyManager } from "@requestnetwork/currency"; |
32 | 34 |
|
33 | 35 | interface CipherProvider extends CipherProviderTypes.ICipherProvider { |
34 | 36 | disconnectWallet: () => void; |
|
37 | 39 | export let config: IConfig; |
38 | 40 | export let wagmiConfig: WagmiConfig; |
39 | 41 | export let requestNetwork: RequestNetwork | null | undefined; |
40 | | - export let currencies: CurrencyTypes.CurrencyInput[] = []; |
| 42 | + export let currencies: string[] = []; |
41 | 43 | let cipherProvider: CipherProvider | undefined; |
42 | 44 |
|
43 | 45 | let account: GetAccountReturnType | undefined = |
|
46 | 48 | let activeConfig = config ? config : defaultConfig; |
47 | 49 | let mainColor = activeConfig.colors.main; |
48 | 50 | let secondaryColor = activeConfig.colors.secondary; |
49 | | - let currencyManager = initializeCurrencyManager(currencies); |
| 51 | + let currencyManager: CurrencyManager; |
50 | 52 |
|
51 | 53 | let invoiceCurrencyDropdown: { clear: () => void }; |
52 | 54 | let networkDropdown: { clear: () => void }; |
|
58 | 60 | let currency: CurrencyTypes.CurrencyDefinition | undefined = undefined; |
59 | 61 | let invoiceCurrency: CurrencyTypes.CurrencyDefinition | undefined = undefined; |
60 | 62 |
|
| 63 | + let defaultCurrencies: any[] = []; |
| 64 | +
|
| 65 | + onMount(async () => { |
| 66 | + currencyManager = await initializeCreateInvoiceCurrencyManager(currencies); |
| 67 | +
|
| 68 | + defaultCurrencies = Object.values( |
| 69 | + currencyManager.knownCurrencies.reduce( |
| 70 | + ( |
| 71 | + unique: { [x: string]: any }, |
| 72 | + currency: { symbol: string | number } |
| 73 | + ) => { |
| 74 | + const baseSymbol = String(currency.symbol).split("-")[0]; |
| 75 | + if (!unique[baseSymbol]) { |
| 76 | + unique[baseSymbol] = { |
| 77 | + ...currency, |
| 78 | + symbol: baseSymbol, |
| 79 | + }; |
| 80 | + } |
| 81 | + return unique; |
| 82 | + }, |
| 83 | + {} |
| 84 | + ) |
| 85 | + ); |
| 86 | +
|
| 87 | + unwatchAccount = watchAccount(wagmiConfig, { |
| 88 | + onChange( |
| 89 | + account: GetAccountReturnType, |
| 90 | + previousAccount: GetAccountReturnType |
| 91 | + ) { |
| 92 | + tick().then(() => { |
| 93 | + handleWalletChange(account, previousAccount); |
| 94 | + }); |
| 95 | + }, |
| 96 | + }); |
| 97 | + }); |
| 98 | +
|
61 | 99 | const handleNetworkChange = (newNetwork: string) => { |
62 | 100 | if (newNetwork) { |
63 | 101 | currencyDropdown.clear(); |
64 | | - invoiceCurrency = invoiceCurrency?.type !== Types.RequestLogic.CURRENCY.ISO4217 ? currencyManager.knownCurrencies.find(currency => invoiceCurrency?.symbol === currency.symbol && currency.network === newNetwork) : invoiceCurrency; |
| 102 | + invoiceCurrency = |
| 103 | + invoiceCurrency?.type !== Types.RequestLogic.CURRENCY.ISO4217 |
| 104 | + ? currencyManager.knownCurrencies.find( |
| 105 | + (currency) => |
| 106 | + currency.symbol.split("-")[0] === |
| 107 | + invoiceCurrency?.symbol.split("-")[0] && |
| 108 | + currency.network === newNetwork |
| 109 | + ) |
| 110 | + : invoiceCurrency; |
65 | 111 | network = newNetwork; |
66 | 112 | currency = undefined; |
67 | 113 |
|
|
77 | 123 | currencyManager?.getConversionPath( |
78 | 124 | invoiceCurrency, |
79 | 125 | currency, |
80 | | - currency?.network, |
| 126 | + currency?.network |
81 | 127 | )?.length > 0; |
82 | 128 |
|
83 | 129 | return ( |
|
88 | 134 | } |
89 | 135 |
|
90 | 136 | // For other currency types (like ERC20) |
91 | | - return invoiceCurrency.hash === currency?.hash; |
92 | | - }, |
| 137 | + // Compare base symbols (without network suffix) |
| 138 | + const invoiceBaseSymbol = invoiceCurrency.symbol.split("-")[0]; |
| 139 | + const currencyBaseSymbol = currency.symbol.split("-")[0]; |
| 140 | + return ( |
| 141 | + invoiceBaseSymbol === currencyBaseSymbol && |
| 142 | + currency.network === newNetwork |
| 143 | + ); |
| 144 | + } |
93 | 145 | ); |
94 | 146 | } |
95 | 147 | }; |
|
98 | 150 | let canSubmit = false; |
99 | 151 | let appStatus: APP_STATUS[] = []; |
100 | 152 | 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 | 153 |
|
111 | 154 | const handleInvoiceCurrencyChange = ( |
112 | | - value: CurrencyTypes.CurrencyDefinition, |
| 155 | + value: CurrencyTypes.CurrencyDefinition |
113 | 156 | ) => { |
114 | 157 | if (value !== invoiceCurrency) { |
115 | 158 | networkDropdown.clear(); |
|
124 | 167 | if (invoiceCurrency.type === Types.RequestLogic.CURRENCY.ISO4217) { |
125 | 168 | networks = (getCurrencySupportedNetworksForConversion( |
126 | 169 | invoiceCurrency.hash, |
127 | | - currencyManager, |
| 170 | + currencyManager |
128 | 171 | ) ?? []) as string[]; |
129 | 172 | } else { |
130 | | - networks = currencyManager.knownCurrencies.filter(currency => currency.symbol === invoiceCurrency?.symbol).map(currency => currency.network); |
| 173 | + const baseSymbol = invoiceCurrency.symbol.split("-")[0]; |
| 174 | + networks = currencyManager.knownCurrencies |
| 175 | + .filter((currency) => { |
| 176 | + const currencyBaseSymbol = currency.symbol.split("-")[0]; |
| 177 | + return currencyBaseSymbol === baseSymbol; |
| 178 | + }) |
| 179 | + .map((currency) => currency.network); |
131 | 180 | } |
132 | 181 | } |
133 | 182 | }; |
|
152 | 201 | cipherProvider?.disconnectWallet(); |
153 | 202 | }; |
154 | 203 |
|
155 | | - const handleWalletChange = (account: GetAccountReturnType, previousAccount: GetAccountReturnType) => { |
| 204 | + const handleWalletChange = ( |
| 205 | + account: GetAccountReturnType, |
| 206 | + previousAccount: GetAccountReturnType |
| 207 | + ) => { |
156 | 208 | if (account?.address !== previousAccount?.address) { |
157 | 209 | handleWalletDisconnection(); |
158 | 210 | handleWalletConnection(); |
|
163 | 215 | } |
164 | 216 | }; |
165 | 217 |
|
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 | 218 | let unwatchAccount: WatchAccountReturnType | undefined; |
177 | 219 |
|
178 | 220 | onDestroy(() => { |
|
261 | 303 | paymentNetwork: requestCreateParameters.paymentNetwork, |
262 | 304 | contentData: requestCreateParameters.contentData, |
263 | 305 | }, |
264 | | - [payeeEncryptionParams, payerEncryptionParams], |
| 306 | + [payeeEncryptionParams, payerEncryptionParams] |
265 | 307 | ); |
266 | 308 | } else { |
267 | 309 | request = await requestNetwork.createRequest({ |
|
0 commit comments