@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,863 @@
1
+ import { subscribeKey as subKey } from 'valtio/utils';
2
+ import { proxy, subscribe as sub } from 'valtio';
3
+ import {
4
+ NumberUtil,
5
+ type CaipAddress,
6
+ type CaipNetworkId,
7
+ type SwapInputTarget,
8
+ type SwapTokenWithBalance
9
+ } from '@reown/appkit-common-react-native';
10
+
11
+ import { ConstantsUtil } from '../utils/ConstantsUtil';
12
+ import { SwapApiUtil } from '../utils/SwapApiUtil';
13
+ import { BlockchainApiController } from './BlockchainApiController';
14
+ import { OptionsController } from './OptionsController';
15
+ import { SwapCalculationUtil } from '../utils/SwapCalculationUtil';
16
+ import { SnackController } from './SnackController';
17
+ import { LogController } from './LogController';
18
+ import { RouterController } from './RouterController';
19
+ import { CoreHelperUtil } from '../utils/CoreHelperUtil';
20
+ import { TransactionsController } from './TransactionsController';
21
+ import { EventsController } from './EventsController';
22
+ import { ConnectionsController } from './ConnectionsController';
23
+
24
+ // -- Constants ---------------------------------------- //
25
+ export const INITIAL_GAS_LIMIT = 150000;
26
+ export const TO_AMOUNT_DECIMALS = 6;
27
+
28
+ // -- Types --------------------------------------------- //
29
+ type TransactionParams = {
30
+ data: string;
31
+ to: string;
32
+ gas: bigint;
33
+ gasPrice: bigint;
34
+ value: bigint;
35
+ toAmount: string;
36
+ };
37
+
38
+ class TransactionError extends Error {
39
+ shortMessage?: string;
40
+
41
+ constructor(message?: string, shortMessage?: string) {
42
+ super(message);
43
+ this.name = 'TransactionError';
44
+ this.shortMessage = shortMessage;
45
+ }
46
+ }
47
+
48
+ export interface SwapControllerState {
49
+ // Loading states
50
+ loadingTokens: boolean;
51
+ loadingPrices: boolean;
52
+ loadingQuote?: boolean;
53
+ loadingApprovalTransaction?: boolean;
54
+ loadingBuildTransaction?: boolean;
55
+ loadingTransaction?: boolean;
56
+
57
+ // Error states
58
+ fetchError: boolean;
59
+
60
+ // Approval & Swap transaction states
61
+ approvalTransaction: TransactionParams | undefined;
62
+ swapTransaction: TransactionParams | undefined;
63
+ transactionError?: string;
64
+
65
+ // Input values
66
+ sourceToken?: SwapTokenWithBalance;
67
+ sourceTokenAmount: string;
68
+ sourceTokenPriceInUSD: number;
69
+ toToken?: SwapTokenWithBalance;
70
+ toTokenAmount: string;
71
+ toTokenPriceInUSD: number;
72
+ networkPrice: string;
73
+ networkTokenSymbol: string;
74
+ inputError: string | undefined;
75
+
76
+ // Request values
77
+ slippage: number;
78
+
79
+ // Tokens
80
+ tokens?: SwapTokenWithBalance[];
81
+ suggestedTokens?: SwapTokenWithBalance[];
82
+ popularTokens?: SwapTokenWithBalance[];
83
+ foundTokens?: SwapTokenWithBalance[];
84
+ myTokensWithBalance?: SwapTokenWithBalance[];
85
+ tokensPriceMap: Record<string, number>;
86
+
87
+ // Calculations
88
+ gasFee: string;
89
+ gasPriceInUSD?: number;
90
+ priceImpact: number | undefined;
91
+ maxSlippage: number | undefined;
92
+ providerFee: string | undefined;
93
+ }
94
+
95
+ type StateKey = keyof SwapControllerState;
96
+
97
+ // -- State --------------------------------------------- //
98
+ const initialState: SwapControllerState = {
99
+ // Loading states
100
+ loadingTokens: false,
101
+ loadingPrices: false,
102
+ loadingQuote: false,
103
+ loadingApprovalTransaction: false,
104
+ loadingBuildTransaction: false,
105
+ loadingTransaction: false,
106
+
107
+ // Error states
108
+ fetchError: false,
109
+
110
+ // Approval & Swap transaction states
111
+ approvalTransaction: undefined,
112
+ swapTransaction: undefined,
113
+ transactionError: undefined,
114
+
115
+ // Input values
116
+ sourceToken: undefined,
117
+ sourceTokenAmount: '',
118
+ sourceTokenPriceInUSD: 0,
119
+ toToken: undefined,
120
+ toTokenAmount: '',
121
+ toTokenPriceInUSD: 0,
122
+ networkPrice: '0',
123
+ networkTokenSymbol: '',
124
+ inputError: undefined,
125
+
126
+ // Request values
127
+ slippage: ConstantsUtil.CONVERT_SLIPPAGE_TOLERANCE,
128
+
129
+ // Tokens
130
+ tokens: undefined,
131
+ popularTokens: undefined,
132
+ suggestedTokens: undefined,
133
+ foundTokens: undefined,
134
+ myTokensWithBalance: undefined,
135
+ tokensPriceMap: {},
136
+
137
+ // Calculations
138
+ gasFee: '0',
139
+ gasPriceInUSD: 0,
140
+ priceImpact: undefined,
141
+ maxSlippage: undefined,
142
+ providerFee: undefined
143
+ };
144
+
145
+ const state = proxy<SwapControllerState>({ ...initialState });
146
+
147
+ // -- Controller ---------------------------------------- //
148
+ export const SwapController = {
149
+ state,
150
+
151
+ subscribe(callback: (newState: SwapControllerState) => void) {
152
+ return sub(state, () => callback(state));
153
+ },
154
+
155
+ subscribeKey<K extends StateKey>(key: K, callback: (value: SwapControllerState[K]) => void) {
156
+ return subKey(state, key, callback);
157
+ },
158
+
159
+ getParams() {
160
+ const { activeAddress, activeNamespace, activeNetwork, connection } =
161
+ ConnectionsController.state;
162
+ const address = CoreHelperUtil.getPlainAddress(activeAddress);
163
+
164
+ if (!activeNamespace || !activeNetwork) {
165
+ throw new Error('No active namespace or network found to swap the tokens from.');
166
+ }
167
+
168
+ const networkAddress: CaipAddress = `${activeNetwork.caipNetworkId}:${ConstantsUtil.NATIVE_TOKEN_ADDRESS[activeNamespace]}`;
169
+
170
+ if (!address) {
171
+ throw new Error('No address found to swap the tokens from.');
172
+ }
173
+
174
+ const invalidToToken = !state.toToken?.address || !state.toToken?.decimals;
175
+ const invalidSourceToken =
176
+ !state.sourceToken?.address ||
177
+ !state.sourceToken?.decimals ||
178
+ state.sourceToken.address === state.toToken?.address ||
179
+ !NumberUtil.bigNumber(state.sourceTokenAmount).isGreaterThan(0);
180
+ const invalidSourceTokenAmount = !state.sourceTokenAmount;
181
+
182
+ return {
183
+ networkAddress,
184
+ network: activeNetwork,
185
+ fromAddress: address,
186
+ fromCaipAddress: activeAddress,
187
+ sourceTokenAddress: state.sourceToken?.address,
188
+ toTokenAddress: state.toToken?.address,
189
+ toTokenAmount: state.toTokenAmount,
190
+ toTokenDecimals: state.toToken?.decimals,
191
+ sourceTokenAmount: state.sourceTokenAmount,
192
+ sourceTokenDecimals: state.sourceToken?.decimals,
193
+ invalidToToken,
194
+ invalidSourceToken,
195
+ invalidSourceTokenAmount,
196
+ availableToSwap:
197
+ activeAddress && !invalidToToken && !invalidSourceToken && !invalidSourceTokenAmount,
198
+ isAuthConnector: !!connection?.properties?.provider
199
+ };
200
+ },
201
+
202
+ switchTokens() {
203
+ if (state.loadingTokens) {
204
+ return;
205
+ }
206
+
207
+ let newSourceToken = state.toToken ? { ...state.toToken } : undefined;
208
+ const sourceTokenWithBalance = state.myTokensWithBalance?.find(
209
+ token => token.address === newSourceToken?.address
210
+ );
211
+
212
+ if (sourceTokenWithBalance) {
213
+ newSourceToken = sourceTokenWithBalance;
214
+ }
215
+
216
+ const newToToken = state.sourceToken ? { ...state.sourceToken } : undefined;
217
+ const newSourceTokenAmount =
218
+ newSourceToken && state.toTokenAmount === '' ? '1' : state.toTokenAmount;
219
+
220
+ this.setSourceToken(newSourceToken);
221
+ this.setToToken(newToToken);
222
+
223
+ this.setSourceTokenAmount(newSourceTokenAmount);
224
+ this.setToTokenAmount('');
225
+ this.swapTokens();
226
+ },
227
+
228
+ async fetchTokens() {
229
+ try {
230
+ const { networkAddress } = this.getParams();
231
+
232
+ state.loadingTokens = true;
233
+ await this.getTokenList();
234
+ await this.getNetworkTokenPrice();
235
+ await this.getMyTokensWithBalance();
236
+
237
+ const networkToken = state.tokens?.find(token => token.address === networkAddress);
238
+
239
+ if (networkToken) {
240
+ state.networkTokenSymbol = networkToken.symbol;
241
+ }
242
+
243
+ // Set default source token if not set
244
+ if (!state.sourceToken && state.myTokensWithBalance?.length) {
245
+ const sourceToken =
246
+ state.myTokensWithBalance?.find(token => token.address.startsWith(networkAddress)) ||
247
+ state.myTokensWithBalance?.[0];
248
+
249
+ this.setSourceToken(sourceToken);
250
+ this.setSourceTokenAmount('1');
251
+ }
252
+ } catch (error) {
253
+ LogController.sendError(error, 'SwapController.ts', 'initializeState');
254
+ SnackController.showError('Failed to initialize swap');
255
+ RouterController.goBack();
256
+ } finally {
257
+ state.loadingTokens = false;
258
+ }
259
+ },
260
+
261
+ async getTokenList() {
262
+ const tokens = await SwapApiUtil.getTokenList();
263
+
264
+ state.tokens = tokens;
265
+ state.popularTokens = tokens.sort((aTokenInfo, bTokenInfo) => {
266
+ if (aTokenInfo.symbol < bTokenInfo.symbol) {
267
+ return -1;
268
+ }
269
+ if (aTokenInfo.symbol > bTokenInfo.symbol) {
270
+ return 1;
271
+ }
272
+
273
+ return 0;
274
+ });
275
+ state.suggestedTokens = tokens.filter(token => {
276
+ if (ConstantsUtil.SWAP_SUGGESTED_TOKENS.includes(token.symbol)) {
277
+ return true;
278
+ }
279
+
280
+ return false;
281
+ }, {});
282
+ },
283
+
284
+ async getMyTokensWithBalance(forceUpdate?: CaipAddress[]) {
285
+ await ConnectionsController.fetchBalance(forceUpdate);
286
+ const swapBalances = SwapApiUtil.mapBalancesToSwapTokens(ConnectionsController.state.balances);
287
+ if (!swapBalances) {
288
+ return;
289
+ }
290
+
291
+ await this.getInitialGasPrice();
292
+ this.setBalances(swapBalances);
293
+ },
294
+
295
+ getFilteredPopularTokens(balances?: SwapTokenWithBalance[]) {
296
+ if (!balances) {
297
+ return state.popularTokens;
298
+ }
299
+
300
+ return state.popularTokens?.filter(token => !balances.some(t => t.address === token.address));
301
+ },
302
+
303
+ setSourceToken(sourceToken: SwapTokenWithBalance | undefined) {
304
+ if (!sourceToken) {
305
+ state.sourceToken = sourceToken;
306
+ state.sourceTokenAmount = '';
307
+ state.sourceTokenPriceInUSD = 0;
308
+
309
+ return;
310
+ }
311
+
312
+ state.sourceToken = sourceToken;
313
+ this.setTokenPrice(sourceToken.address, 'sourceToken');
314
+ },
315
+
316
+ setSourceTokenAmount(amount: string) {
317
+ state.sourceTokenAmount = amount;
318
+
319
+ if (amount === '') {
320
+ state.toTokenAmount = '';
321
+ }
322
+ },
323
+
324
+ async getAddressPrice(address: CaipAddress) {
325
+ const [namespace, chain] = address.split(':');
326
+ const networkId: CaipNetworkId = `${namespace}:${chain}`;
327
+
328
+ const existPrice = state.tokensPriceMap[address];
329
+
330
+ if (existPrice) {
331
+ return existPrice;
332
+ }
333
+
334
+ const response = await BlockchainApiController.fetchTokenPrice({
335
+ projectId: OptionsController.state.projectId,
336
+ addresses: [address],
337
+ caipNetworkId: networkId
338
+ });
339
+ const fungibles = response?.fungibles || [];
340
+ const allTokens = [...(state.tokens || []), ...(state.myTokensWithBalance || [])];
341
+ const symbol = allTokens?.find(token => token.address === address)?.symbol;
342
+ const price = fungibles.find(p => p.symbol.toLowerCase() === symbol?.toLowerCase())?.price || 0;
343
+ const priceAsFloat = parseFloat(price.toString());
344
+
345
+ state.tokensPriceMap[address] = priceAsFloat;
346
+
347
+ return priceAsFloat;
348
+ },
349
+
350
+ async getNetworkTokenPrice() {
351
+ const { networkAddress } = this.getParams();
352
+ const [namespace, chain] = networkAddress.split(':');
353
+ const networkId: CaipNetworkId = `${namespace}:${chain}`;
354
+
355
+ const response = await BlockchainApiController.fetchTokenPrice({
356
+ projectId: OptionsController.state.projectId,
357
+ addresses: [networkAddress],
358
+ caipNetworkId: networkId
359
+ });
360
+
361
+ const token = response?.fungibles?.[0];
362
+ const price = token?.price.toString() || '0';
363
+ state.tokensPriceMap[networkAddress] = parseFloat(price);
364
+ state.networkTokenSymbol = token?.symbol || '';
365
+ state.networkPrice = price;
366
+ },
367
+
368
+ async getInitialGasPrice() {
369
+ const res = await SwapApiUtil.fetchGasPrice();
370
+
371
+ if (!res) {
372
+ return { gasPrice: null, gasPriceInUsd: null };
373
+ }
374
+
375
+ const value = res.standard;
376
+ const gasFee = BigInt(value);
377
+ const gasLimit = BigInt(INITIAL_GAS_LIMIT);
378
+ const gasPrice = SwapCalculationUtil.getGasPriceInUSD(state.networkPrice, gasLimit, gasFee);
379
+
380
+ state.gasFee = value;
381
+ state.gasPriceInUSD = gasPrice;
382
+
383
+ return { gasPrice: gasFee, gasPriceInUSD: state.gasPriceInUSD };
384
+ },
385
+
386
+ getProviderFeePrice() {
387
+ return SwapCalculationUtil.getProviderFeePrice(
388
+ state.sourceTokenAmount,
389
+ state.sourceTokenPriceInUSD
390
+ );
391
+ },
392
+
393
+ setBalances(balances: SwapTokenWithBalance[]) {
394
+ state.myTokensWithBalance = balances;
395
+
396
+ balances.forEach(token => {
397
+ state.tokensPriceMap[token.address] = token.price || 0;
398
+ });
399
+ },
400
+
401
+ setToToken(toToken: SwapTokenWithBalance | undefined) {
402
+ if (!toToken) {
403
+ state.toToken = toToken;
404
+ state.toTokenAmount = '';
405
+ state.toTokenPriceInUSD = 0;
406
+
407
+ return;
408
+ }
409
+
410
+ state.toToken = toToken;
411
+ this.setTokenPrice(toToken.address, 'toToken');
412
+ },
413
+
414
+ setToTokenAmount(amount: string) {
415
+ state.toTokenAmount = amount
416
+ ? NumberUtil.formatNumberToLocalString(amount, TO_AMOUNT_DECIMALS)
417
+ : '';
418
+ },
419
+
420
+ async setTokenPrice(address: CaipAddress, target: SwapInputTarget) {
421
+ let price = state.tokensPriceMap[address] || 0;
422
+
423
+ if (!price) {
424
+ state.loadingPrices = true;
425
+ price = await this.getAddressPrice(address);
426
+ }
427
+
428
+ if (target === 'sourceToken') {
429
+ state.sourceTokenPriceInUSD = price;
430
+ } else if (target === 'toToken') {
431
+ state.toTokenPriceInUSD = price;
432
+ }
433
+
434
+ if (state.loadingPrices) {
435
+ state.loadingPrices = false;
436
+ }
437
+
438
+ if (this.getParams().availableToSwap) {
439
+ this.swapTokens();
440
+ }
441
+ },
442
+
443
+ // -- Swap ---------------------------------------------- //
444
+ async swapTokens() {
445
+ const address = ConnectionsController.state.activeAddress;
446
+ const sourceToken = state.sourceToken;
447
+ const toToken = state.toToken;
448
+ const haveSourceTokenAmount = NumberUtil.bigNumber(state.sourceTokenAmount).isGreaterThan(0);
449
+
450
+ if (!toToken || !sourceToken || state.loadingPrices || !haveSourceTokenAmount || !address) {
451
+ return;
452
+ }
453
+
454
+ state.loadingQuote = true;
455
+
456
+ const amountDecimal = NumberUtil.bigNumber(state.sourceTokenAmount)
457
+ .multipliedBy(10 ** sourceToken.decimals)
458
+ .integerValue();
459
+
460
+ try {
461
+ const quoteResponse = await BlockchainApiController.fetchSwapQuote({
462
+ userAddress: address,
463
+ projectId: OptionsController.state.projectId,
464
+ from: sourceToken.address,
465
+ to: toToken.address,
466
+ gasPrice: state.gasFee,
467
+ amount: amountDecimal.toString()
468
+ });
469
+
470
+ const quoteToAmount = quoteResponse?.quotes?.[0]?.toAmount;
471
+
472
+ if (!quoteToAmount) {
473
+ state.loadingQuote = false;
474
+
475
+ return;
476
+ }
477
+
478
+ const toTokenAmount = NumberUtil.bigNumber(quoteToAmount)
479
+ .dividedBy(10 ** toToken.decimals)
480
+ .toString();
481
+
482
+ this.setToTokenAmount(toTokenAmount);
483
+
484
+ const isInsufficientToken = this.hasInsufficientToken(
485
+ state.sourceTokenAmount,
486
+ sourceToken.address
487
+ );
488
+
489
+ if (isInsufficientToken) {
490
+ state.inputError = 'Insufficient balance';
491
+ } else {
492
+ state.inputError = undefined;
493
+ this.setTransactionDetails();
494
+ }
495
+ } catch (error) {
496
+ LogController.sendError(error, 'SwapController.ts', 'getQuote');
497
+ SnackController.showError('Failed to get swap quote');
498
+ } finally {
499
+ state.loadingQuote = false;
500
+ }
501
+ },
502
+
503
+ // -- Create Transactions -------------------------------------- //
504
+ async getTransaction() {
505
+ const { fromCaipAddress, availableToSwap } = this.getParams();
506
+ const sourceToken = state.sourceToken;
507
+ const toToken = state.toToken;
508
+
509
+ if (!fromCaipAddress || !availableToSwap || !sourceToken || !toToken || state.loadingQuote) {
510
+ return undefined;
511
+ }
512
+
513
+ try {
514
+ state.loadingBuildTransaction = true;
515
+ const hasAllowance = await SwapApiUtil.fetchSwapAllowance({
516
+ userAddress: fromCaipAddress,
517
+ tokenAddress: sourceToken.address,
518
+ sourceTokenAmount: state.sourceTokenAmount,
519
+ sourceTokenDecimals: sourceToken.decimals
520
+ });
521
+
522
+ let transaction: TransactionParams | undefined;
523
+
524
+ if (hasAllowance) {
525
+ transaction = await this.createSwapTransaction();
526
+ } else {
527
+ transaction = await this.createAllowanceTransaction();
528
+ }
529
+
530
+ state.loadingBuildTransaction = false;
531
+ state.fetchError = false;
532
+
533
+ return transaction;
534
+ } catch (error) {
535
+ RouterController.goBack();
536
+ SnackController.showError('Failed to check allowance');
537
+ state.loadingBuildTransaction = false;
538
+ state.approvalTransaction = undefined;
539
+ state.swapTransaction = undefined;
540
+ state.fetchError = true;
541
+
542
+ return undefined;
543
+ }
544
+ },
545
+
546
+ async createAllowanceTransaction() {
547
+ const { fromCaipAddress, fromAddress, sourceTokenAddress, toTokenAddress } = this.getParams();
548
+
549
+ if (!fromCaipAddress || !toTokenAddress) {
550
+ return undefined;
551
+ }
552
+
553
+ if (!sourceTokenAddress) {
554
+ throw new Error('createAllowanceTransaction - No source token address found.');
555
+ }
556
+
557
+ try {
558
+ const response = await BlockchainApiController.generateApproveCalldata({
559
+ projectId: OptionsController.state.projectId,
560
+ from: sourceTokenAddress,
561
+ to: toTokenAddress,
562
+ userAddress: fromCaipAddress
563
+ });
564
+
565
+ if (!response) {
566
+ throw new Error('createAllowanceTransaction - No response from generateApproveCalldata');
567
+ }
568
+ const gasLimit = await ConnectionsController.estimateGas({
569
+ address: fromAddress as `0x${string}`,
570
+ to: CoreHelperUtil.getPlainAddress(response.tx.to) as `0x${string}`,
571
+ data: response.tx.data
572
+ });
573
+
574
+ const transaction = {
575
+ data: response.tx.data,
576
+ to: CoreHelperUtil.getPlainAddress(response.tx.from) as `0x${string}`,
577
+ gas: gasLimit,
578
+ gasPrice: BigInt(response.tx.eip155.gasPrice),
579
+ value: BigInt(response.tx.value),
580
+ toAmount: state.toTokenAmount
581
+ };
582
+
583
+ state.swapTransaction = undefined;
584
+ state.approvalTransaction = {
585
+ data: transaction.data,
586
+ to: transaction.to,
587
+ gas: transaction.gas ?? BigInt(0),
588
+ gasPrice: transaction.gasPrice,
589
+ value: transaction.value,
590
+ toAmount: transaction.toAmount
591
+ };
592
+
593
+ return {
594
+ data: transaction.data,
595
+ to: transaction.to,
596
+ gas: transaction.gas ?? BigInt(0),
597
+ gasPrice: transaction.gasPrice,
598
+ value: transaction.value,
599
+ toAmount: transaction.toAmount
600
+ };
601
+ } catch (error) {
602
+ RouterController.goBack();
603
+ SnackController.showError('Failed to create approval transaction');
604
+ state.approvalTransaction = undefined;
605
+ state.swapTransaction = undefined;
606
+ state.fetchError = true;
607
+
608
+ return undefined;
609
+ }
610
+ },
611
+
612
+ async createSwapTransaction() {
613
+ const { networkAddress, fromCaipAddress, sourceTokenAmount } = this.getParams();
614
+ const sourceToken = state.sourceToken;
615
+ const toToken = state.toToken;
616
+
617
+ if (!fromCaipAddress || !sourceTokenAmount || !sourceToken || !toToken) {
618
+ return undefined;
619
+ }
620
+
621
+ const amount = ConnectionsController.parseUnits(
622
+ sourceTokenAmount,
623
+ sourceToken.decimals
624
+ )?.toString();
625
+
626
+ try {
627
+ const response = await BlockchainApiController.generateSwapCalldata({
628
+ projectId: OptionsController.state.projectId,
629
+ userAddress: fromCaipAddress,
630
+ from: sourceToken.address,
631
+ to: toToken.address,
632
+ amount: amount as string
633
+ });
634
+
635
+ if (!response) {
636
+ throw new Error('createSwapTransaction - No response from generateSwapCalldata');
637
+ }
638
+
639
+ const isSourceNetworkToken = sourceToken.address === networkAddress;
640
+
641
+ const gas = BigInt(response.tx.eip155.gas);
642
+ const gasPrice = BigInt(response.tx.eip155.gasPrice);
643
+
644
+ const transaction = {
645
+ data: response.tx.data,
646
+ to: CoreHelperUtil.getPlainAddress(response.tx.to) as `0x${string}`,
647
+ gas,
648
+ gasPrice,
649
+ value: isSourceNetworkToken ? BigInt(amount ?? '0') : BigInt('0'),
650
+ toAmount: state.toTokenAmount
651
+ };
652
+
653
+ state.gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(state.networkPrice, gas, gasPrice);
654
+
655
+ state.approvalTransaction = undefined;
656
+ state.swapTransaction = transaction;
657
+
658
+ return transaction;
659
+ } catch (error) {
660
+ RouterController.goBack();
661
+ SnackController.showError('Failed to create transaction');
662
+ state.approvalTransaction = undefined;
663
+ state.swapTransaction = undefined;
664
+ state.fetchError = true;
665
+
666
+ return undefined;
667
+ }
668
+ },
669
+
670
+ async sendTransactionForApproval(data: TransactionParams) {
671
+ const { fromAddress, network } = this.getParams();
672
+ state.loadingApprovalTransaction = true;
673
+
674
+ SnackController.showLoading('Approve limit increase in your wallet');
675
+
676
+ try {
677
+ await ConnectionsController.sendTransaction({
678
+ address: fromAddress as `0x${string}`,
679
+ to: data.to as `0x${string}`,
680
+ data: data.data as `0x${string}`,
681
+ value: BigInt(data.value),
682
+ gasPrice: BigInt(data.gasPrice),
683
+ chainNamespace: ConnectionsController.state.activeNamespace,
684
+ network
685
+ });
686
+
687
+ await this.swapTokens();
688
+ await this.getTransaction();
689
+ state.approvalTransaction = undefined;
690
+ } catch (err) {
691
+ const error = err as TransactionError;
692
+ state.transactionError = error?.shortMessage as unknown as string;
693
+
694
+ SnackController.showError(error?.shortMessage ?? 'Transaction error');
695
+ } finally {
696
+ state.loadingApprovalTransaction = false;
697
+ }
698
+ },
699
+
700
+ async sendTransactionForSwap(data: TransactionParams | undefined) {
701
+ if (!data) {
702
+ return undefined;
703
+ }
704
+ const { fromAddress, isAuthConnector, network } = this.getParams();
705
+
706
+ state.loadingTransaction = true;
707
+
708
+ const snackbarSuccessMessage = `Swapped ${state.sourceToken?.symbol} to ${state.toToken?.symbol}`;
709
+
710
+ SnackController.showLoading('Confirm transaction in your wallet');
711
+
712
+ try {
713
+ const forceUpdateAddresses = [state.sourceToken?.address, state.toToken?.address].filter(
714
+ Boolean
715
+ ) as CaipAddress[];
716
+
717
+ const transactionHash = await ConnectionsController.sendTransaction({
718
+ address: fromAddress as `0x${string}`,
719
+ to: data.to as `0x${string}`,
720
+ data: data.data as `0x${string}`,
721
+ gas: data.gas,
722
+ gasPrice: BigInt(data.gasPrice),
723
+ value: data.value,
724
+ chainNamespace: ConnectionsController.state.activeNamespace,
725
+ network
726
+ });
727
+
728
+ state.loadingTransaction = false;
729
+
730
+ SnackController.showSuccess(snackbarSuccessMessage, true);
731
+ EventsController.sendEvent({
732
+ type: 'track',
733
+ event: 'SWAP_SUCCESS',
734
+ properties: {
735
+ network: ConnectionsController.state.activeNetwork?.caipNetworkId,
736
+ swapFromToken: this.state.sourceToken?.symbol || '',
737
+ swapToToken: this.state.toToken?.symbol || '',
738
+ swapFromAmount: this.state.sourceTokenAmount || '',
739
+ swapToAmount: this.state.toTokenAmount || '',
740
+ isSmartAccount: ConnectionsController.state.accountType === 'smartAccount'
741
+ }
742
+ });
743
+ RouterController.replace(isAuthConnector ? 'Account' : 'AccountDefault');
744
+ SwapController.clearTokens();
745
+ SwapController.getMyTokensWithBalance(forceUpdateAddresses);
746
+
747
+ setTimeout(() => {
748
+ if (ConnectionsController.state.activeAddress) {
749
+ TransactionsController.fetchTransactions(ConnectionsController.state.activeAddress, true);
750
+ }
751
+ }, 5000);
752
+
753
+ return transactionHash;
754
+ } catch (err) {
755
+ const error = err as TransactionError;
756
+ LogController.sendError(error, 'SwapController.ts', 'sendTransactionForSwap', {
757
+ sourceToken: this.state.sourceToken?.symbol,
758
+ toToken: this.state.toToken?.symbol,
759
+ amount: this.state.sourceTokenAmount
760
+ });
761
+ state.transactionError = error?.shortMessage;
762
+ state.loadingTransaction = false;
763
+ SnackController.showError(error?.shortMessage ?? 'Transaction error');
764
+ EventsController.sendEvent({
765
+ type: 'track',
766
+ event: 'SWAP_ERROR',
767
+ properties: {
768
+ message: error?.shortMessage ?? error?.message ?? 'Unknown',
769
+ network: ConnectionsController.state.activeNetwork?.caipNetworkId,
770
+ swapFromToken: this.state.sourceToken?.symbol || '',
771
+ swapToToken: this.state.toToken?.symbol || '',
772
+ swapFromAmount: this.state.sourceTokenAmount || '',
773
+ swapToAmount: this.state.toTokenAmount || '',
774
+ isSmartAccount: ConnectionsController.state.accountType === 'smartAccount'
775
+ }
776
+ });
777
+
778
+ return undefined;
779
+ }
780
+ },
781
+
782
+ clearTransactionLoaders() {
783
+ state.loadingApprovalTransaction = false;
784
+ state.loadingBuildTransaction = false;
785
+ state.loadingTransaction = false;
786
+ },
787
+
788
+ clearTokens() {
789
+ state.sourceToken = initialState.sourceToken;
790
+ state.sourceTokenAmount = initialState.sourceTokenAmount;
791
+ state.sourceTokenPriceInUSD = initialState.sourceTokenPriceInUSD;
792
+ state.toToken = initialState.toToken;
793
+ state.toTokenAmount = initialState.toTokenAmount;
794
+ state.toTokenPriceInUSD = initialState.toTokenPriceInUSD;
795
+ state.inputError = initialState.inputError;
796
+ state.loadingApprovalTransaction = initialState.loadingApprovalTransaction;
797
+ state.loadingBuildTransaction = initialState.loadingBuildTransaction;
798
+ state.loadingTransaction = initialState.loadingTransaction;
799
+ state.fetchError = initialState.fetchError;
800
+ state.transactionError = initialState.transactionError;
801
+ state.swapTransaction = initialState.swapTransaction;
802
+ state.approvalTransaction = initialState.approvalTransaction;
803
+ },
804
+
805
+ resetState() {
806
+ this.clearTokens();
807
+ state.myTokensWithBalance = initialState.myTokensWithBalance;
808
+ state.tokensPriceMap = initialState.tokensPriceMap;
809
+ state.networkPrice = initialState.networkPrice;
810
+ state.networkTokenSymbol = initialState.networkTokenSymbol;
811
+ },
812
+
813
+ // -- Checks -------------------------------------------- //
814
+ hasInsufficientToken(sourceTokenAmount: string, sourceTokenAddress: string) {
815
+ const { balances } = ConnectionsController.state;
816
+ const networkToken = balances?.find(t => t.address === undefined);
817
+ const networkBalanceInUSD = networkToken
818
+ ? NumberUtil.multiply(networkToken.quantity?.numeric ?? '0', networkToken.price).toString()
819
+ : '0';
820
+
821
+ const isInsufficientSourceTokenForSwap = SwapCalculationUtil.isInsufficientSourceTokenForSwap(
822
+ sourceTokenAmount,
823
+ sourceTokenAddress,
824
+ state.myTokensWithBalance
825
+ );
826
+
827
+ let insufficientNetworkTokenForGas = true;
828
+ if (ConnectionsController.state.accountType === 'smartAccount') {
829
+ // Smart Accounts may pay gas in any ERC20 token
830
+ insufficientNetworkTokenForGas = false;
831
+ } else {
832
+ insufficientNetworkTokenForGas = SwapCalculationUtil.isInsufficientNetworkTokenForGas(
833
+ networkBalanceInUSD,
834
+ state.gasPriceInUSD
835
+ );
836
+ }
837
+
838
+ return insufficientNetworkTokenForGas || isInsufficientSourceTokenForSwap;
839
+ },
840
+
841
+ // -- Calculations -------------------------------------- //
842
+ setTransactionDetails() {
843
+ const { toTokenAddress, toTokenDecimals } = this.getParams();
844
+
845
+ if (!toTokenAddress || !toTokenDecimals) {
846
+ return;
847
+ }
848
+
849
+ state.gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(
850
+ state.networkPrice,
851
+ BigInt(state.gasFee),
852
+ BigInt(INITIAL_GAS_LIMIT)
853
+ );
854
+ state.priceImpact = SwapCalculationUtil.getPriceImpact({
855
+ sourceTokenAmount: state.sourceTokenAmount,
856
+ sourceTokenPriceInUSD: state.sourceTokenPriceInUSD,
857
+ toTokenPriceInUSD: state.toTokenPriceInUSD,
858
+ toTokenAmount: state.toTokenAmount
859
+ });
860
+ state.maxSlippage = SwapCalculationUtil.getMaxSlippage(state.slippage, state.toTokenAmount);
861
+ state.providerFee = SwapCalculationUtil.getProviderFee(state.sourceTokenAmount);
862
+ }
863
+ };