@reown/appkit-core-react-native 0.0.0-accounts-canary.1-20251023174733

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (234) hide show
  1. package/lib/commonjs/controllers/ApiController.js +410 -0
  2. package/lib/commonjs/controllers/ApiController.js.map +1 -0
  3. package/lib/commonjs/controllers/AssetController.js +30 -0
  4. package/lib/commonjs/controllers/AssetController.js.map +1 -0
  5. package/lib/commonjs/controllers/BlockchainApiController.js +384 -0
  6. package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -0
  7. package/lib/commonjs/controllers/ConnectionsController.js +425 -0
  8. package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
  9. package/lib/commonjs/controllers/EnsController.js +37 -0
  10. package/lib/commonjs/controllers/EnsController.js.map +1 -0
  11. package/lib/commonjs/controllers/EventsController.js +122 -0
  12. package/lib/commonjs/controllers/EventsController.js.map +1 -0
  13. package/lib/commonjs/controllers/LogController.js +188 -0
  14. package/lib/commonjs/controllers/LogController.js.map +1 -0
  15. package/lib/commonjs/controllers/ModalController.js +68 -0
  16. package/lib/commonjs/controllers/ModalController.js.map +1 -0
  17. package/lib/commonjs/controllers/OnRampController.js +502 -0
  18. package/lib/commonjs/controllers/OnRampController.js.map +1 -0
  19. package/lib/commonjs/controllers/OptionsController.js +96 -0
  20. package/lib/commonjs/controllers/OptionsController.js.map +1 -0
  21. package/lib/commonjs/controllers/PublicStateController.js +33 -0
  22. package/lib/commonjs/controllers/PublicStateController.js.map +1 -0
  23. package/lib/commonjs/controllers/RouterController.js +63 -0
  24. package/lib/commonjs/controllers/RouterController.js.map +1 -0
  25. package/lib/commonjs/controllers/SendController.js +192 -0
  26. package/lib/commonjs/controllers/SendController.js.map +1 -0
  27. package/lib/commonjs/controllers/SnackController.js +83 -0
  28. package/lib/commonjs/controllers/SnackController.js.map +1 -0
  29. package/lib/commonjs/controllers/SwapController.js +674 -0
  30. package/lib/commonjs/controllers/SwapController.js.map +1 -0
  31. package/lib/commonjs/controllers/ThemeController.js +45 -0
  32. package/lib/commonjs/controllers/ThemeController.js.map +1 -0
  33. package/lib/commonjs/controllers/TransactionsController.js +118 -0
  34. package/lib/commonjs/controllers/TransactionsController.js.map +1 -0
  35. package/lib/commonjs/controllers/WcController.js +73 -0
  36. package/lib/commonjs/controllers/WcController.js.map +1 -0
  37. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js +272 -0
  38. package/lib/commonjs/features/reown-authentication/ReownAuthentication.js.map +1 -0
  39. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js +48 -0
  40. package/lib/commonjs/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  41. package/lib/commonjs/features/reown-authentication/index.js +28 -0
  42. package/lib/commonjs/features/reown-authentication/index.js.map +1 -0
  43. package/lib/commonjs/index.js +202 -0
  44. package/lib/commonjs/index.js.map +1 -0
  45. package/lib/commonjs/package.json +1 -0
  46. package/lib/commonjs/utils/ApiUtil.js +43 -0
  47. package/lib/commonjs/utils/ApiUtil.js.map +1 -0
  48. package/lib/commonjs/utils/AssetUtil.js +27 -0
  49. package/lib/commonjs/utils/AssetUtil.js.map +1 -0
  50. package/lib/commonjs/utils/ConstantsUtil.js +226 -0
  51. package/lib/commonjs/utils/ConstantsUtil.js.map +1 -0
  52. package/lib/commonjs/utils/CoreHelperUtil.js +298 -0
  53. package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -0
  54. package/lib/commonjs/utils/EventUtil.js +25 -0
  55. package/lib/commonjs/utils/EventUtil.js.map +1 -0
  56. package/lib/commonjs/utils/FetchUtil.js +158 -0
  57. package/lib/commonjs/utils/FetchUtil.js.map +1 -0
  58. package/lib/commonjs/utils/LogUtils.js +131 -0
  59. package/lib/commonjs/utils/LogUtils.js.map +1 -0
  60. package/lib/commonjs/utils/StorageUtil.js +319 -0
  61. package/lib/commonjs/utils/StorageUtil.js.map +1 -0
  62. package/lib/commonjs/utils/SwapApiUtil.js +75 -0
  63. package/lib/commonjs/utils/SwapApiUtil.js.map +1 -0
  64. package/lib/commonjs/utils/SwapCalculationUtil.js +97 -0
  65. package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -0
  66. package/lib/commonjs/utils/WalletUtil.js +23 -0
  67. package/lib/commonjs/utils/WalletUtil.js.map +1 -0
  68. package/lib/module/controllers/ApiController.js +407 -0
  69. package/lib/module/controllers/ApiController.js.map +1 -0
  70. package/lib/module/controllers/AssetController.js +27 -0
  71. package/lib/module/controllers/AssetController.js.map +1 -0
  72. package/lib/module/controllers/BlockchainApiController.js +381 -0
  73. package/lib/module/controllers/BlockchainApiController.js.map +1 -0
  74. package/lib/module/controllers/ConnectionsController.js +422 -0
  75. package/lib/module/controllers/ConnectionsController.js.map +1 -0
  76. package/lib/module/controllers/EnsController.js +34 -0
  77. package/lib/module/controllers/EnsController.js.map +1 -0
  78. package/lib/module/controllers/EventsController.js +118 -0
  79. package/lib/module/controllers/EventsController.js.map +1 -0
  80. package/lib/module/controllers/LogController.js +185 -0
  81. package/lib/module/controllers/LogController.js.map +1 -0
  82. package/lib/module/controllers/ModalController.js +65 -0
  83. package/lib/module/controllers/ModalController.js.map +1 -0
  84. package/lib/module/controllers/OnRampController.js +499 -0
  85. package/lib/module/controllers/OnRampController.js.map +1 -0
  86. package/lib/module/controllers/OptionsController.js +93 -0
  87. package/lib/module/controllers/OptionsController.js.map +1 -0
  88. package/lib/module/controllers/PublicStateController.js +30 -0
  89. package/lib/module/controllers/PublicStateController.js.map +1 -0
  90. package/lib/module/controllers/RouterController.js +60 -0
  91. package/lib/module/controllers/RouterController.js.map +1 -0
  92. package/lib/module/controllers/SendController.js +189 -0
  93. package/lib/module/controllers/SendController.js.map +1 -0
  94. package/lib/module/controllers/SnackController.js +80 -0
  95. package/lib/module/controllers/SnackController.js.map +1 -0
  96. package/lib/module/controllers/SwapController.js +671 -0
  97. package/lib/module/controllers/SwapController.js.map +1 -0
  98. package/lib/module/controllers/ThemeController.js +42 -0
  99. package/lib/module/controllers/ThemeController.js.map +1 -0
  100. package/lib/module/controllers/TransactionsController.js +115 -0
  101. package/lib/module/controllers/TransactionsController.js.map +1 -0
  102. package/lib/module/controllers/WcController.js +70 -0
  103. package/lib/module/controllers/WcController.js.map +1 -0
  104. package/lib/module/features/reown-authentication/ReownAuthentication.js +268 -0
  105. package/lib/module/features/reown-authentication/ReownAuthentication.js.map +1 -0
  106. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js +43 -0
  107. package/lib/module/features/reown-authentication/ReownAuthenticationMessenger.js.map +1 -0
  108. package/lib/module/features/reown-authentication/index.js +5 -0
  109. package/lib/module/features/reown-authentication/index.js.map +1 -0
  110. package/lib/module/index.js +38 -0
  111. package/lib/module/index.js.map +1 -0
  112. package/lib/module/utils/ApiUtil.js +39 -0
  113. package/lib/module/utils/ApiUtil.js.map +1 -0
  114. package/lib/module/utils/AssetUtil.js +23 -0
  115. package/lib/module/utils/AssetUtil.js.map +1 -0
  116. package/lib/module/utils/ConstantsUtil.js +222 -0
  117. package/lib/module/utils/ConstantsUtil.js.map +1 -0
  118. package/lib/module/utils/CoreHelperUtil.js +294 -0
  119. package/lib/module/utils/CoreHelperUtil.js.map +1 -0
  120. package/lib/module/utils/EventUtil.js +21 -0
  121. package/lib/module/utils/EventUtil.js.map +1 -0
  122. package/lib/module/utils/FetchUtil.js +154 -0
  123. package/lib/module/utils/FetchUtil.js.map +1 -0
  124. package/lib/module/utils/LogUtils.js +121 -0
  125. package/lib/module/utils/LogUtils.js.map +1 -0
  126. package/lib/module/utils/StorageUtil.js +315 -0
  127. package/lib/module/utils/StorageUtil.js.map +1 -0
  128. package/lib/module/utils/SwapApiUtil.js +71 -0
  129. package/lib/module/utils/SwapApiUtil.js.map +1 -0
  130. package/lib/module/utils/SwapCalculationUtil.js +94 -0
  131. package/lib/module/utils/SwapCalculationUtil.js.map +1 -0
  132. package/lib/module/utils/WalletUtil.js +19 -0
  133. package/lib/module/utils/WalletUtil.js.map +1 -0
  134. package/lib/typescript/controllers/ApiController.d.ts +40 -0
  135. package/lib/typescript/controllers/ApiController.d.ts.map +1 -0
  136. package/lib/typescript/controllers/AssetController.d.ts +12 -0
  137. package/lib/typescript/controllers/AssetController.d.ts.map +1 -0
  138. package/lib/typescript/controllers/BlockchainApiController.d.ts +55 -0
  139. package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -0
  140. package/lib/typescript/controllers/ConnectionsController.d.ts +47 -0
  141. package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
  142. package/lib/typescript/controllers/EnsController.d.ts +10 -0
  143. package/lib/typescript/controllers/EnsController.d.ts.map +1 -0
  144. package/lib/typescript/controllers/EventsController.d.ts +25 -0
  145. package/lib/typescript/controllers/EventsController.d.ts.map +1 -0
  146. package/lib/typescript/controllers/LogController.d.ts +65 -0
  147. package/lib/typescript/controllers/LogController.d.ts.map +1 -0
  148. package/lib/typescript/controllers/ModalController.d.ts +17 -0
  149. package/lib/typescript/controllers/ModalController.d.ts.map +1 -0
  150. package/lib/typescript/controllers/OnRampController.d.ts +55 -0
  151. package/lib/typescript/controllers/OnRampController.d.ts.map +1 -0
  152. package/lib/typescript/controllers/OptionsController.d.ts +46 -0
  153. package/lib/typescript/controllers/OptionsController.d.ts.map +1 -0
  154. package/lib/typescript/controllers/PublicStateController.d.ts +12 -0
  155. package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -0
  156. package/lib/typescript/controllers/RouterController.d.ts +21 -0
  157. package/lib/typescript/controllers/RouterController.d.ts.map +1 -0
  158. package/lib/typescript/controllers/SendController.d.ts +39 -0
  159. package/lib/typescript/controllers/SendController.d.ts.map +1 -0
  160. package/lib/typescript/controllers/SnackController.d.ts +20 -0
  161. package/lib/typescript/controllers/SnackController.d.ts.map +1 -0
  162. package/lib/typescript/controllers/SwapController.d.ts +116 -0
  163. package/lib/typescript/controllers/SwapController.d.ts.map +1 -0
  164. package/lib/typescript/controllers/ThemeController.d.ts +14 -0
  165. package/lib/typescript/controllers/ThemeController.d.ts.map +1 -0
  166. package/lib/typescript/controllers/TransactionsController.d.ts +21 -0
  167. package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -0
  168. package/lib/typescript/controllers/WcController.d.ts +27 -0
  169. package/lib/typescript/controllers/WcController.d.ts.map +1 -0
  170. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts +174 -0
  171. package/lib/typescript/features/reown-authentication/ReownAuthentication.d.ts.map +1 -0
  172. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts +16 -0
  173. package/lib/typescript/features/reown-authentication/ReownAuthenticationMessenger.d.ts.map +1 -0
  174. package/lib/typescript/features/reown-authentication/index.d.ts +3 -0
  175. package/lib/typescript/features/reown-authentication/index.d.ts.map +1 -0
  176. package/lib/typescript/index.d.ts +29 -0
  177. package/lib/typescript/index.d.ts.map +1 -0
  178. package/lib/typescript/utils/ApiUtil.d.ts +7 -0
  179. package/lib/typescript/utils/ApiUtil.d.ts.map +1 -0
  180. package/lib/typescript/utils/AssetUtil.d.ts +6 -0
  181. package/lib/typescript/utils/AssetUtil.d.ts.map +1 -0
  182. package/lib/typescript/utils/ConstantsUtil.d.ts +79 -0
  183. package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -0
  184. package/lib/typescript/utils/CoreHelperUtil.d.ts +43 -0
  185. package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -0
  186. package/lib/typescript/utils/EventUtil.d.ts +5 -0
  187. package/lib/typescript/utils/EventUtil.d.ts.map +1 -0
  188. package/lib/typescript/utils/FetchUtil.d.ts +29 -0
  189. package/lib/typescript/utils/FetchUtil.d.ts.map +1 -0
  190. package/lib/typescript/utils/LogUtils.d.ts +15 -0
  191. package/lib/typescript/utils/LogUtils.d.ts.map +1 -0
  192. package/lib/typescript/utils/StorageUtil.d.ts +36 -0
  193. package/lib/typescript/utils/StorageUtil.d.ts.map +1 -0
  194. package/lib/typescript/utils/SwapApiUtil.d.ts +11 -0
  195. package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -0
  196. package/lib/typescript/utils/SwapCalculationUtil.d.ts +24 -0
  197. package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -0
  198. package/lib/typescript/utils/WalletUtil.d.ts +5 -0
  199. package/lib/typescript/utils/WalletUtil.d.ts.map +1 -0
  200. package/package.json +57 -0
  201. package/readme.md +9 -0
  202. package/src/controllers/ApiController.ts +454 -0
  203. package/src/controllers/AssetController.ts +32 -0
  204. package/src/controllers/BlockchainApiController.ts +418 -0
  205. package/src/controllers/ConnectionsController.ts +575 -0
  206. package/src/controllers/EnsController.ts +41 -0
  207. package/src/controllers/EventsController.ts +146 -0
  208. package/src/controllers/LogController.ts +250 -0
  209. package/src/controllers/ModalController.ts +68 -0
  210. package/src/controllers/OnRampController.ts +698 -0
  211. package/src/controllers/OptionsController.ts +143 -0
  212. package/src/controllers/PublicStateController.ts +37 -0
  213. package/src/controllers/RouterController.ts +112 -0
  214. package/src/controllers/SendController.ts +270 -0
  215. package/src/controllers/SnackController.ts +95 -0
  216. package/src/controllers/SwapController.ts +863 -0
  217. package/src/controllers/ThemeController.ts +47 -0
  218. package/src/controllers/TransactionsController.ts +150 -0
  219. package/src/controllers/WcController.ts +93 -0
  220. package/src/features/reown-authentication/ReownAuthentication.ts +475 -0
  221. package/src/features/reown-authentication/ReownAuthenticationMessenger.ts +80 -0
  222. package/src/features/reown-authentication/index.ts +2 -0
  223. package/src/index.ts +70 -0
  224. package/src/utils/ApiUtil.ts +50 -0
  225. package/src/utils/AssetUtil.ts +27 -0
  226. package/src/utils/ConstantsUtil.ts +303 -0
  227. package/src/utils/CoreHelperUtil.ts +377 -0
  228. package/src/utils/EventUtil.ts +22 -0
  229. package/src/utils/FetchUtil.ts +164 -0
  230. package/src/utils/LogUtils.ts +179 -0
  231. package/src/utils/StorageUtil.ts +405 -0
  232. package/src/utils/SwapApiUtil.ts +101 -0
  233. package/src/utils/SwapCalculationUtil.ts +145 -0
  234. package/src/utils/WalletUtil.ts +14 -0
@@ -0,0 +1,698 @@
1
+ import { subscribeKey as subKey } from 'valtio/utils';
2
+ import { proxy, subscribe as sub } from 'valtio';
3
+ import {
4
+ type OnRampPaymentMethod,
5
+ type OnRampCountry,
6
+ type OnRampFiatCurrency,
7
+ type OnRampQuote,
8
+ type OnRampFiatLimit,
9
+ type OnRampCryptoCurrency,
10
+ type OnRampServiceProvider,
11
+ type OnRampError,
12
+ type OnRampErrorTypeValues,
13
+ type OnRampCountryDefaults,
14
+ BlockchainOnRampError
15
+ } from '@reown/appkit-common-react-native';
16
+
17
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
18
+ import { OptionsController } from './OptionsController';
19
+ import { ConstantsUtil, OnRampErrorType } from '../utils/ConstantsUtil';
20
+ import { StorageUtil } from '../utils/StorageUtil';
21
+ import { SnackController } from './SnackController';
22
+ import { LogController } from './LogController';
23
+ import { EventsController } from './EventsController';
24
+ import { BlockchainApiController, EXCLUDED_ONRAMP_PROVIDERS } from './BlockchainApiController';
25
+ import { ConnectionsController } from './ConnectionsController';
26
+
27
+ // -- Helpers ------------------------------------------- //
28
+
29
+ let quotesAbortController: AbortController | null = null;
30
+
31
+ // -- Utils --------------------------------------------- //
32
+
33
+ const mapErrorMessage = (errorCode: string): OnRampError => {
34
+ const errorMap: Record<string, { type: OnRampErrorTypeValues; message: string }> = {
35
+ [OnRampErrorType.AMOUNT_TOO_LOW]: {
36
+ type: OnRampErrorType.AMOUNT_TOO_LOW,
37
+ message: 'The amount is too low'
38
+ },
39
+ [OnRampErrorType.AMOUNT_TOO_HIGH]: {
40
+ type: OnRampErrorType.AMOUNT_TOO_HIGH,
41
+ message: 'The amount is too high'
42
+ },
43
+ [OnRampErrorType.INVALID_AMOUNT]: {
44
+ type: OnRampErrorType.INVALID_AMOUNT,
45
+ message: 'Enter a valid amount'
46
+ },
47
+ [OnRampErrorType.INCOMPATIBLE_REQUEST]: {
48
+ type: OnRampErrorType.INCOMPATIBLE_REQUEST,
49
+ message: 'Enter a valid amount'
50
+ },
51
+ [OnRampErrorType.BAD_REQUEST]: {
52
+ type: OnRampErrorType.BAD_REQUEST,
53
+ message: 'Enter a valid amount'
54
+ },
55
+ [OnRampErrorType.NO_VALID_QUOTES]: {
56
+ type: OnRampErrorType.NO_VALID_QUOTES,
57
+ message: 'No quotes available'
58
+ }
59
+ };
60
+
61
+ return (
62
+ errorMap[errorCode] || {
63
+ type: OnRampErrorType.UNKNOWN,
64
+ message: 'Something went wrong. Please try again'
65
+ }
66
+ );
67
+ };
68
+
69
+ // -- Types --------------------------------------------- //
70
+ export interface OnRampControllerState {
71
+ countries: OnRampCountry[];
72
+ countriesDefaults?: OnRampCountryDefaults[];
73
+ selectedCountry?: OnRampCountry;
74
+ serviceProviders: OnRampServiceProvider[];
75
+ selectedServiceProvider?: OnRampServiceProvider;
76
+ paymentMethods: OnRampPaymentMethod[];
77
+ selectedPaymentMethod?: OnRampPaymentMethod;
78
+ purchaseCurrency?: OnRampCryptoCurrency;
79
+ purchaseCurrencies?: OnRampCryptoCurrency[];
80
+ paymentAmount?: number;
81
+ paymentCurrency?: OnRampFiatCurrency;
82
+ paymentCurrencies?: OnRampFiatCurrency[];
83
+ paymentCurrenciesLimits?: OnRampFiatLimit[];
84
+ quotes?: OnRampQuote[];
85
+ selectedQuote?: OnRampQuote;
86
+ widgetUrl?: string;
87
+ error?: OnRampError;
88
+ initialLoading?: boolean;
89
+ loading?: boolean;
90
+ quotesLoading: boolean;
91
+ }
92
+
93
+ type StateKey = keyof OnRampControllerState;
94
+
95
+ const defaultState = {
96
+ quotesLoading: false,
97
+ countries: [],
98
+ paymentMethods: [],
99
+ serviceProviders: [],
100
+ paymentAmount: undefined
101
+ };
102
+
103
+ // -- State --------------------------------------------- //
104
+ const state = proxy<OnRampControllerState>(defaultState);
105
+
106
+ // -- Controller ---------------------------------------- //
107
+ export const OnRampController = {
108
+ state,
109
+
110
+ subscribe(callback: (newState: OnRampControllerState) => void) {
111
+ return sub(state, () => callback(state));
112
+ },
113
+
114
+ subscribeKey<K extends StateKey>(key: K, callback: (value: OnRampControllerState[K]) => void) {
115
+ return subKey(state, key, callback);
116
+ },
117
+
118
+ async setSelectedCountry(country: OnRampCountry, updateCurrency = true) {
119
+ try {
120
+ state.selectedCountry = country;
121
+ state.loading = true;
122
+
123
+ if (updateCurrency) {
124
+ const currencyCode =
125
+ state.countriesDefaults?.find(d => d.countryCode === country.countryCode)
126
+ ?.defaultCurrencyCode || 'USD';
127
+
128
+ let currency = state.paymentCurrencies?.find(c => c.currencyCode === currencyCode);
129
+
130
+ if (!currency) {
131
+ currency = state.paymentCurrencies?.find(c => c.currencyCode === 'USD');
132
+ }
133
+
134
+ if (currency) {
135
+ this.setPaymentCurrency(currency);
136
+ }
137
+ }
138
+
139
+ await Promise.all([this.fetchPaymentMethods(), this.fetchCryptoCurrencies()]);
140
+ this.clearQuotes();
141
+
142
+ state.loading = false;
143
+
144
+ StorageUtil.setOnRampPreferredCountry(country);
145
+ } catch (error) {
146
+ LogController.sendError(error, 'OnRampController.ts', 'setSelectedCountry');
147
+ state.loading = false;
148
+ state.error = {
149
+ type: OnRampErrorType.FAILED_TO_LOAD_COUNTRIES,
150
+ message: 'Failed to load countries'
151
+ };
152
+ }
153
+ },
154
+
155
+ setSelectedPaymentMethod(paymentMethod: OnRampPaymentMethod) {
156
+ state.selectedPaymentMethod = paymentMethod;
157
+ },
158
+
159
+ setPurchaseCurrency(currency: OnRampCryptoCurrency) {
160
+ state.purchaseCurrency = currency;
161
+
162
+ EventsController.sendEvent({
163
+ type: 'track',
164
+ event: 'SELECT_BUY_ASSET',
165
+ properties: {
166
+ asset: currency.currencyCode
167
+ }
168
+ });
169
+
170
+ this.clearQuotes();
171
+ },
172
+
173
+ setPaymentCurrency(currency: OnRampFiatCurrency, updateAmount = true) {
174
+ state.paymentCurrency = currency;
175
+
176
+ StorageUtil.setOnRampPreferredFiatCurrency(currency);
177
+
178
+ if (updateAmount) {
179
+ state.paymentAmount = undefined;
180
+ }
181
+
182
+ this.clearQuotes();
183
+ this.clearError();
184
+ },
185
+
186
+ setPaymentAmount(amount?: number | string) {
187
+ state.paymentAmount = amount ? Number(amount) : undefined;
188
+ },
189
+
190
+ setSelectedQuote(quote?: OnRampQuote) {
191
+ state.selectedQuote = quote;
192
+ },
193
+
194
+ updateSelectedPurchaseCurrency() {
195
+ let selectedCurrency;
196
+ if (ConnectionsController.state.activeNetwork?.caipNetworkId) {
197
+ const defaultCurrency =
198
+ ConstantsUtil.NETWORK_DEFAULT_CURRENCIES[
199
+ ConnectionsController.state.activeNetwork
200
+ ?.caipNetworkId as keyof typeof ConstantsUtil.NETWORK_DEFAULT_CURRENCIES
201
+ ];
202
+ selectedCurrency = state.purchaseCurrencies?.find(c => c.currencyCode === defaultCurrency);
203
+ }
204
+
205
+ state.purchaseCurrency = selectedCurrency ?? undefined;
206
+ },
207
+
208
+ getServiceProviderImage(serviceProviderName?: string) {
209
+ if (!serviceProviderName) return undefined;
210
+
211
+ const provider = state.serviceProviders.find(p => p.serviceProvider === serviceProviderName);
212
+
213
+ return provider?.logos?.lightShort;
214
+ },
215
+
216
+ getCurrencyLimit(currency: OnRampFiatCurrency) {
217
+ return state.paymentCurrenciesLimits?.find(l => l.currencyCode === currency.currencyCode);
218
+ },
219
+
220
+ async fetchCountries() {
221
+ try {
222
+ let countries = await StorageUtil.getOnRampCountries();
223
+
224
+ if (!countries.length) {
225
+ countries = (await BlockchainApiController.fetchOnRampCountries()) ?? [];
226
+
227
+ if (countries.length) {
228
+ StorageUtil.setOnRampCountries(countries);
229
+ }
230
+ }
231
+
232
+ state.countries = countries;
233
+
234
+ const preferredCountry = await StorageUtil.getOnRampPreferredCountry();
235
+
236
+ if (preferredCountry) {
237
+ state.selectedCountry = preferredCountry;
238
+ } else {
239
+ const countryCode = CoreHelperUtil.getCountryFromTimezone();
240
+
241
+ state.selectedCountry =
242
+ countries.find(c => c.countryCode === countryCode) || countries[0] || undefined;
243
+ }
244
+ } catch (error) {
245
+ LogController.sendError(error, 'OnRampController.ts', 'fetchCountries');
246
+ state.error = {
247
+ type: OnRampErrorType.FAILED_TO_LOAD_COUNTRIES,
248
+ message: 'Failed to load countries'
249
+ };
250
+ }
251
+ },
252
+
253
+ async fetchCountriesDefaults() {
254
+ try {
255
+ let countriesDefaults = await StorageUtil.getOnRampCountriesDefaults();
256
+
257
+ if (!countriesDefaults.length) {
258
+ countriesDefaults = (await BlockchainApiController.fetchOnRampCountriesDefaults()) ?? [];
259
+
260
+ if (countriesDefaults.length) {
261
+ StorageUtil.setOnRampCountriesDefaults(countriesDefaults);
262
+ }
263
+ }
264
+
265
+ state.countriesDefaults = countriesDefaults;
266
+ } catch (error) {
267
+ LogController.sendError(error, 'OnRampController.ts', 'fetchCountriesDefaults');
268
+ state.error = {
269
+ type: OnRampErrorType.FAILED_TO_LOAD_COUNTRIES,
270
+ message: 'Failed to load countries defaults'
271
+ };
272
+ }
273
+ },
274
+
275
+ async fetchServiceProviders() {
276
+ try {
277
+ let serviceProviders = await StorageUtil.getOnRampServiceProviders();
278
+
279
+ if (!serviceProviders.length) {
280
+ serviceProviders = (await BlockchainApiController.fetchOnRampServiceProviders()) ?? [];
281
+
282
+ if (serviceProviders.length) {
283
+ StorageUtil.setOnRampServiceProviders(serviceProviders);
284
+ }
285
+ }
286
+
287
+ state.serviceProviders = serviceProviders || [];
288
+ } catch (error) {
289
+ LogController.sendError(error, 'OnRampController.ts', 'fetchServiceProviders');
290
+ state.error = {
291
+ type: OnRampErrorType.FAILED_TO_LOAD_PROVIDERS,
292
+ message: 'Failed to load service providers'
293
+ };
294
+ }
295
+ },
296
+
297
+ async fetchPaymentMethods() {
298
+ try {
299
+ const paymentMethods = await BlockchainApiController.fetchOnRampPaymentMethods({
300
+ countries: state.selectedCountry?.countryCode
301
+ });
302
+
303
+ const defaultCountryPaymentMethods =
304
+ state.countriesDefaults?.find(d => d.countryCode === state.selectedCountry?.countryCode)
305
+ ?.defaultPaymentMethods || [];
306
+
307
+ state.paymentMethods =
308
+ paymentMethods?.sort((a, b) => {
309
+ const aIndex = defaultCountryPaymentMethods?.indexOf(a.paymentMethod);
310
+ const bIndex = defaultCountryPaymentMethods?.indexOf(b.paymentMethod);
311
+
312
+ if (aIndex === -1 && bIndex === -1) return 0;
313
+ if (aIndex === -1) return 1;
314
+ if (bIndex === -1) return -1;
315
+
316
+ return aIndex - bIndex;
317
+ }) || [];
318
+
319
+ state.selectedPaymentMethod = state.paymentMethods[0];
320
+ } catch (error) {
321
+ LogController.sendError(error, 'OnRampController.ts', 'fetchPaymentMethods');
322
+ state.error = {
323
+ type: OnRampErrorType.FAILED_TO_LOAD_METHODS,
324
+ message: 'Failed to load payment methods'
325
+ };
326
+ state.paymentMethods = [];
327
+ state.selectedPaymentMethod = undefined;
328
+ }
329
+ },
330
+
331
+ async fetchCryptoCurrencies() {
332
+ try {
333
+ const cryptoCurrencies = await BlockchainApiController.fetchOnRampCryptoCurrencies({
334
+ countries: state.selectedCountry?.countryCode
335
+ });
336
+
337
+ state.purchaseCurrencies = cryptoCurrencies || [];
338
+
339
+ let selectedCurrency;
340
+ if (ConnectionsController.state.activeNetwork?.caipNetworkId) {
341
+ const defaultCurrency =
342
+ ConstantsUtil.NETWORK_DEFAULT_CURRENCIES[
343
+ ConnectionsController.state.activeNetwork
344
+ ?.caipNetworkId as keyof typeof ConstantsUtil.NETWORK_DEFAULT_CURRENCIES
345
+ ];
346
+ selectedCurrency = state.purchaseCurrencies?.find(c => c.currencyCode === defaultCurrency);
347
+ }
348
+
349
+ state.purchaseCurrency = selectedCurrency || undefined;
350
+ } catch (error) {
351
+ LogController.sendError(error, 'OnRampController.ts', 'fetchCryptoCurrencies');
352
+ state.error = {
353
+ type: OnRampErrorType.FAILED_TO_LOAD_CURRENCIES,
354
+ message: 'Failed to load crypto currencies'
355
+ };
356
+ state.purchaseCurrencies = [];
357
+ state.purchaseCurrency = undefined;
358
+ }
359
+ },
360
+
361
+ async fetchFiatCurrencies() {
362
+ try {
363
+ let fiatCurrencies = await StorageUtil.getOnRampFiatCurrencies();
364
+ let currencyCode = 'USD';
365
+ const countryCode = state.selectedCountry?.countryCode;
366
+
367
+ if (!fiatCurrencies.length) {
368
+ fiatCurrencies = (await BlockchainApiController.fetchOnRampFiatCurrencies()) ?? [];
369
+
370
+ if (fiatCurrencies.length) {
371
+ StorageUtil.setOnRampFiatCurrencies(fiatCurrencies);
372
+ }
373
+ }
374
+
375
+ state.paymentCurrencies = fiatCurrencies || [];
376
+
377
+ if (countryCode) {
378
+ currencyCode =
379
+ state.countriesDefaults?.find(d => d.countryCode === countryCode)?.defaultCurrencyCode ||
380
+ 'USD';
381
+ }
382
+
383
+ const preferredCurrency = await StorageUtil.getOnRampPreferredFiatCurrency();
384
+
385
+ const defaultCurrency =
386
+ preferredCurrency ||
387
+ fiatCurrencies?.find(c => c.currencyCode === currencyCode) ||
388
+ fiatCurrencies?.[0] ||
389
+ undefined;
390
+
391
+ if (defaultCurrency) {
392
+ this.setPaymentCurrency(defaultCurrency);
393
+ }
394
+ } catch (error) {
395
+ LogController.sendError(error, 'OnRampController.ts', 'fetchFiatCurrencies');
396
+ state.error = {
397
+ type: OnRampErrorType.FAILED_TO_LOAD_CURRENCIES,
398
+ message: 'Failed to load fiat currencies'
399
+ };
400
+ state.paymentCurrencies = [];
401
+ state.paymentCurrency = undefined;
402
+ }
403
+ },
404
+
405
+ abortGetQuotes(clearState = true) {
406
+ if (quotesAbortController) {
407
+ quotesAbortController.abort();
408
+ quotesAbortController = null;
409
+ }
410
+
411
+ if (clearState) {
412
+ this.clearQuotes();
413
+ state.quotesLoading = false;
414
+ state.error = undefined;
415
+ }
416
+ },
417
+
418
+ async getQuotes() {
419
+ if (!this.canGenerateQuote()) {
420
+ this.clearQuotes();
421
+
422
+ return;
423
+ }
424
+
425
+ this.abortGetQuotes(false);
426
+ quotesAbortController = new AbortController();
427
+ const currentSignal = quotesAbortController.signal;
428
+
429
+ try {
430
+ if (
431
+ !state.selectedCountry?.countryCode ||
432
+ !state.purchaseCurrency?.currencyCode ||
433
+ !state.paymentCurrency?.currencyCode ||
434
+ !ConnectionsController.state.activeAddress
435
+ ) {
436
+ throw new BlockchainOnRampError(OnRampErrorType.UNKNOWN, 'Invalid quote parameters');
437
+ }
438
+
439
+ state.quotesLoading = true;
440
+ state.selectedQuote = undefined;
441
+ state.selectedServiceProvider = undefined;
442
+ state.error = undefined;
443
+
444
+ const plainAddress = CoreHelperUtil.getPlainAddress(
445
+ ConnectionsController.state.activeAddress
446
+ );
447
+
448
+ if (!plainAddress) {
449
+ throw new Error('Invalid address');
450
+ }
451
+
452
+ const body = {
453
+ countryCode: state.selectedCountry.countryCode,
454
+ destinationCurrencyCode: state.purchaseCurrency.currencyCode,
455
+ sourceAmount: state.paymentAmount!,
456
+ sourceCurrencyCode: state.paymentCurrency.currencyCode,
457
+ walletAddress: plainAddress,
458
+ excludeProviders: EXCLUDED_ONRAMP_PROVIDERS
459
+ };
460
+
461
+ const response = await BlockchainApiController.getOnRampQuotes(body, currentSignal);
462
+
463
+ if (!response || !response.length) {
464
+ throw new BlockchainOnRampError(OnRampErrorType.NO_VALID_QUOTES, 'No valid quotes');
465
+ }
466
+
467
+ const quotes = response.sort((a, b) => b.customerScore - a.customerScore);
468
+
469
+ state.quotes = quotes;
470
+
471
+ //Replace payment method if it's not in the quotes
472
+ const isValidPaymentMethod =
473
+ state.selectedPaymentMethod &&
474
+ quotes.some(
475
+ quote => quote.paymentMethodType === state.selectedPaymentMethod?.paymentMethod
476
+ );
477
+
478
+ if (!isValidPaymentMethod) {
479
+ const countryMethods =
480
+ state.countriesDefaults?.find(d => d.countryCode === state.selectedCountry?.countryCode)
481
+ ?.defaultPaymentMethods || [];
482
+
483
+ const availableQuoteMethods = new Set(quotes.map(q => q.paymentMethodType));
484
+
485
+ let newPaymentMethodType: string | undefined;
486
+ for (const dpm of countryMethods) {
487
+ if (availableQuoteMethods.has(dpm)) {
488
+ newPaymentMethodType = dpm;
489
+ break;
490
+ }
491
+ }
492
+
493
+ if (newPaymentMethodType) {
494
+ state.selectedPaymentMethod =
495
+ state.paymentMethods.find(m => m.paymentMethod === newPaymentMethodType) ||
496
+ state.paymentMethods.find(
497
+ method => method.paymentMethod === quotes[0]?.paymentMethodType
498
+ );
499
+ } else {
500
+ state.selectedPaymentMethod = state.paymentMethods.find(
501
+ method => method.paymentMethod === quotes[0]?.paymentMethodType
502
+ );
503
+ }
504
+ }
505
+
506
+ state.selectedQuote = quotes.find(
507
+ quote => quote.paymentMethodType === state.selectedPaymentMethod?.paymentMethod
508
+ );
509
+
510
+ state.selectedServiceProvider = state.serviceProviders.find(
511
+ sp => sp.serviceProvider === state.selectedQuote?.serviceProvider
512
+ );
513
+ } catch (error: any) {
514
+ LogController.sendError(error, 'OnRampController.ts', 'getQuotes');
515
+ if (error.name === 'AbortError') {
516
+ // Do nothing, another request was made
517
+ return;
518
+ }
519
+
520
+ EventsController.sendEvent({
521
+ type: 'track',
522
+ event: 'BUY_FAIL',
523
+ properties: {
524
+ message: error?.message ?? error?.code ?? 'Error getting quotes'
525
+ }
526
+ });
527
+
528
+ this.clearQuotes();
529
+ state.error = mapErrorMessage(error?.code || 'UNKNOWN_ERROR');
530
+ } finally {
531
+ if (!currentSignal.aborted) {
532
+ state.quotesLoading = false;
533
+ }
534
+ }
535
+ },
536
+
537
+ canGenerateQuote(): boolean {
538
+ return !!(
539
+ state.selectedCountry?.countryCode &&
540
+ state.selectedPaymentMethod?.paymentMethod &&
541
+ state.purchaseCurrency?.currencyCode &&
542
+ state.paymentAmount &&
543
+ state.paymentAmount > 0 &&
544
+ state.paymentCurrency?.currencyCode &&
545
+ state.selectedCountry &&
546
+ !state.loading &&
547
+ ConnectionsController.state.activeAddress
548
+ );
549
+ },
550
+
551
+ async fetchFiatLimits() {
552
+ try {
553
+ let limits = await StorageUtil.getOnRampFiatLimits();
554
+
555
+ if (!limits.length) {
556
+ limits = (await BlockchainApiController.fetchOnRampFiatLimits()) ?? [];
557
+
558
+ if (limits.length) {
559
+ StorageUtil.setOnRampFiatLimits(limits);
560
+ }
561
+ }
562
+
563
+ state.paymentCurrenciesLimits = limits;
564
+ } catch (error) {
565
+ LogController.sendError(error, 'OnRampController.ts', 'fetchFiatLimits');
566
+ state.error = {
567
+ type: OnRampErrorType.FAILED_TO_LOAD_LIMITS,
568
+ message: 'Failed to load fiat limits'
569
+ };
570
+ state.paymentCurrenciesLimits = [];
571
+ }
572
+ },
573
+
574
+ async generateWidget({ quote }: { quote: OnRampQuote }) {
575
+ if (!ConnectionsController.state.activeAddress) {
576
+ throw new Error('No active address');
577
+ }
578
+
579
+ const metadata = OptionsController.state.metadata;
580
+ const eventProperties = {
581
+ asset: quote.destinationCurrencyCode,
582
+ network: state.purchaseCurrency?.chainName ?? '',
583
+ amount: quote.destinationAmount.toString(),
584
+ currency: quote.destinationCurrencyCode,
585
+ paymentMethod: quote.paymentMethodType,
586
+ provider: 'MELD',
587
+ serviceProvider: quote.serviceProvider
588
+ };
589
+
590
+ try {
591
+ if (!quote) {
592
+ throw new Error('Invalid quote');
593
+ }
594
+
595
+ const plainAddress = CoreHelperUtil.getPlainAddress(
596
+ ConnectionsController.state.activeAddress
597
+ );
598
+
599
+ if (!plainAddress) {
600
+ throw new Error('Invalid address');
601
+ }
602
+
603
+ const body = {
604
+ countryCode: quote.countryCode,
605
+ destinationCurrencyCode: quote.destinationCurrencyCode,
606
+ paymentMethodType: quote.paymentMethodType,
607
+ serviceProvider: quote.serviceProvider,
608
+ sourceAmount: quote.sourceAmount,
609
+ sourceCurrencyCode: quote.sourceCurrencyCode,
610
+ walletAddress: plainAddress,
611
+ redirectUrl: metadata?.redirect?.universal ?? metadata?.redirect?.native
612
+ };
613
+
614
+ const widget = await BlockchainApiController.getOnRampWidget(body);
615
+
616
+ if (!widget || !widget.widgetUrl) {
617
+ throw new Error('Invalid widget response');
618
+ }
619
+
620
+ EventsController.sendEvent({
621
+ type: 'track',
622
+ event: 'BUY_SUBMITTED',
623
+ properties: eventProperties
624
+ });
625
+
626
+ state.widgetUrl = widget.widgetUrl;
627
+
628
+ return widget;
629
+ } catch (e: any) {
630
+ LogController.sendError(e, 'OnRampController.ts', 'generateWidget', { quote });
631
+ EventsController.sendEvent({
632
+ type: 'track',
633
+ event: 'BUY_FAIL',
634
+ properties: {
635
+ ...eventProperties,
636
+ message: e?.message ?? e?.code ?? 'Error generating widget url'
637
+ }
638
+ });
639
+
640
+ state.error = mapErrorMessage(e?.code || 'UNKNOWN_ERROR');
641
+ SnackController.showInternalError({
642
+ shortMessage: 'Error creating purchase URL',
643
+ longMessage: e?.message ?? e?.code
644
+ });
645
+
646
+ return undefined;
647
+ }
648
+ },
649
+
650
+ clearError() {
651
+ state.error = undefined;
652
+ },
653
+
654
+ clearQuotes() {
655
+ state.quotes = [];
656
+ state.selectedQuote = undefined;
657
+ state.selectedServiceProvider = undefined;
658
+ },
659
+
660
+ async loadOnRampData() {
661
+ state.initialLoading = true;
662
+ state.error = undefined;
663
+
664
+ try {
665
+ await this.fetchCountries();
666
+ await this.fetchServiceProviders();
667
+
668
+ await Promise.all([
669
+ this.fetchCountriesDefaults(),
670
+ this.fetchPaymentMethods(),
671
+ this.fetchFiatLimits(),
672
+ this.fetchCryptoCurrencies(),
673
+ this.fetchFiatCurrencies()
674
+ ]);
675
+ } catch (error) {
676
+ LogController.sendError(error, 'OnRampController.ts', 'loadOnRampData');
677
+ if (!state.error) {
678
+ state.error = {
679
+ type: OnRampErrorType.FAILED_TO_LOAD,
680
+ message: 'Failed to load onramp data'
681
+ };
682
+ }
683
+ } finally {
684
+ state.initialLoading = false;
685
+ }
686
+ },
687
+
688
+ resetState() {
689
+ state.error = undefined;
690
+ state.quotesLoading = false;
691
+ state.quotes = [];
692
+ state.selectedQuote = undefined;
693
+ state.selectedServiceProvider = undefined;
694
+ state.widgetUrl = undefined;
695
+ state.paymentAmount = undefined;
696
+ this.updateSelectedPurchaseCurrency();
697
+ }
698
+ };