@reown/appkit-core 1.7.0-rc.3.0 → 1.7.0
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.
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/package.json +2 -2
- package/dist/esm/exports/modal.js +0 -2
- package/dist/esm/exports/modal.js.map +0 -1
- package/dist/esm/src/controllers/AccountController.js +0 -160
- package/dist/esm/src/controllers/AccountController.js.map +0 -1
- package/dist/esm/src/controllers/AlertController.js +0 -35
- package/dist/esm/src/controllers/AlertController.js.map +0 -1
- package/dist/esm/src/controllers/ApiController.js +0 -246
- package/dist/esm/src/controllers/ApiController.js.map +0 -1
- package/dist/esm/src/controllers/AssetController.js +0 -43
- package/dist/esm/src/controllers/AssetController.js.map +0 -1
- package/dist/esm/src/controllers/BlockchainApiController.js +0 -472
- package/dist/esm/src/controllers/BlockchainApiController.js.map +0 -1
- package/dist/esm/src/controllers/ChainController.js +0 -486
- package/dist/esm/src/controllers/ChainController.js.map +0 -1
- package/dist/esm/src/controllers/ConnectionController.js +0 -195
- package/dist/esm/src/controllers/ConnectionController.js.map +0 -1
- package/dist/esm/src/controllers/ConnectorController.js +0 -218
- package/dist/esm/src/controllers/ConnectorController.js.map +0 -1
- package/dist/esm/src/controllers/EnsController.js +0 -144
- package/dist/esm/src/controllers/EnsController.js.map +0 -1
- package/dist/esm/src/controllers/EventsController.js +0 -76
- package/dist/esm/src/controllers/EventsController.js.map +0 -1
- package/dist/esm/src/controllers/ModalController.js +0 -102
- package/dist/esm/src/controllers/ModalController.js.map +0 -1
- package/dist/esm/src/controllers/OnRampController.js +0 -132
- package/dist/esm/src/controllers/OnRampController.js.map +0 -1
- package/dist/esm/src/controllers/OptionsController.js +0 -146
- package/dist/esm/src/controllers/OptionsController.js.map +0 -1
- package/dist/esm/src/controllers/PublicStateController.js +0 -20
- package/dist/esm/src/controllers/PublicStateController.js.map +0 -1
- package/dist/esm/src/controllers/RouterController.js +0 -125
- package/dist/esm/src/controllers/RouterController.js.map +0 -1
- package/dist/esm/src/controllers/SendController.js +0 -305
- package/dist/esm/src/controllers/SendController.js.map +0 -1
- package/dist/esm/src/controllers/SnackController.js +0 -62
- package/dist/esm/src/controllers/SnackController.js.map +0 -1
- package/dist/esm/src/controllers/SwapController.js +0 -674
- package/dist/esm/src/controllers/SwapController.js.map +0 -1
- package/dist/esm/src/controllers/ThemeController.js +0 -55
- package/dist/esm/src/controllers/ThemeController.js.map +0 -1
- package/dist/esm/src/controllers/TooltipController.js +0 -41
- package/dist/esm/src/controllers/TooltipController.js.map +0 -1
- package/dist/esm/src/controllers/TransactionsController.js +0 -113
- package/dist/esm/src/controllers/TransactionsController.js.map +0 -1
- package/dist/esm/src/utils/AssetUtil.js +0 -85
- package/dist/esm/src/utils/AssetUtil.js.map +0 -1
- package/dist/esm/src/utils/ConstantsUtil.js +0 -237
- package/dist/esm/src/utils/ConstantsUtil.js.map +0 -1
- package/dist/esm/src/utils/CoreHelperUtil.js +0 -354
- package/dist/esm/src/utils/CoreHelperUtil.js.map +0 -1
- package/dist/esm/src/utils/ERC7811Util.js +0 -111
- package/dist/esm/src/utils/ERC7811Util.js.map +0 -1
- package/dist/esm/src/utils/EnsUtil.js +0 -10
- package/dist/esm/src/utils/EnsUtil.js.map +0 -1
- package/dist/esm/src/utils/FetchUtil.js +0 -73
- package/dist/esm/src/utils/FetchUtil.js.map +0 -1
- package/dist/esm/src/utils/MobileWallet.js +0 -31
- package/dist/esm/src/utils/MobileWallet.js.map +0 -1
- package/dist/esm/src/utils/OptionsUtil.js +0 -29
- package/dist/esm/src/utils/OptionsUtil.js.map +0 -1
- package/dist/esm/src/utils/RouterUtil.js +0 -23
- package/dist/esm/src/utils/RouterUtil.js.map +0 -1
- package/dist/esm/src/utils/SIWXUtil.js +0 -258
- package/dist/esm/src/utils/SIWXUtil.js.map +0 -1
- package/dist/esm/src/utils/SendApiUtil.js +0 -65
- package/dist/esm/src/utils/SendApiUtil.js.map +0 -1
- package/dist/esm/src/utils/StorageUtil.js +0 -485
- package/dist/esm/src/utils/StorageUtil.js.map +0 -1
- package/dist/esm/src/utils/SwapApiUtil.js +0 -89
- package/dist/esm/src/utils/SwapApiUtil.js.map +0 -1
- package/dist/esm/src/utils/SwapCalculationUtil.js +0 -74
- package/dist/esm/src/utils/SwapCalculationUtil.js.map +0 -1
- package/dist/esm/src/utils/TypeUtil.js +0 -2
- package/dist/esm/src/utils/TypeUtil.js.map +0 -1
- package/dist/esm/tests/constants/OnrampTransactions.js +0 -151
- package/dist/esm/tests/constants/OnrampTransactions.js.map +0 -1
- package/dist/esm/tests/controllers/AccountController.test.js +0 -186
- package/dist/esm/tests/controllers/AccountController.test.js.map +0 -1
- package/dist/esm/tests/controllers/AlertController.test.js +0 -48
- package/dist/esm/tests/controllers/AlertController.test.js.map +0 -1
- package/dist/esm/tests/controllers/ApiController.test.js +0 -594
- package/dist/esm/tests/controllers/ApiController.test.js.map +0 -1
- package/dist/esm/tests/controllers/AssetController.test.js +0 -92
- package/dist/esm/tests/controllers/AssetController.test.js.map +0 -1
- package/dist/esm/tests/controllers/BlockchainApiController.test.js +0 -38
- package/dist/esm/tests/controllers/BlockchainApiController.test.js.map +0 -1
- package/dist/esm/tests/controllers/ChainController.test.js +0 -385
- package/dist/esm/tests/controllers/ChainController.test.js.map +0 -1
- package/dist/esm/tests/controllers/ConnectionController.test.js +0 -151
- package/dist/esm/tests/controllers/ConnectionController.test.js.map +0 -1
- package/dist/esm/tests/controllers/ConnectorController.test.js +0 -282
- package/dist/esm/tests/controllers/ConnectorController.test.js.map +0 -1
- package/dist/esm/tests/controllers/EnsController.test.js +0 -186
- package/dist/esm/tests/controllers/EnsController.test.js.map +0 -1
- package/dist/esm/tests/controllers/EventsController.test.js +0 -47
- package/dist/esm/tests/controllers/EventsController.test.js.map +0 -1
- package/dist/esm/tests/controllers/ModalController.test.js +0 -32
- package/dist/esm/tests/controllers/ModalController.test.js.map +0 -1
- package/dist/esm/tests/controllers/OnRampController.test.js +0 -118
- package/dist/esm/tests/controllers/OnRampController.test.js.map +0 -1
- package/dist/esm/tests/controllers/OptionsController.test.js +0 -42
- package/dist/esm/tests/controllers/OptionsController.test.js.map +0 -1
- package/dist/esm/tests/controllers/PublicStateController.test.js +0 -33
- package/dist/esm/tests/controllers/PublicStateController.test.js.map +0 -1
- package/dist/esm/tests/controllers/RouterController.test.js +0 -103
- package/dist/esm/tests/controllers/RouterController.test.js.map +0 -1
- package/dist/esm/tests/controllers/SendController.test.js +0 -136
- package/dist/esm/tests/controllers/SendController.test.js.map +0 -1
- package/dist/esm/tests/controllers/SnackController.test.js +0 -86
- package/dist/esm/tests/controllers/SnackController.test.js.map +0 -1
- package/dist/esm/tests/controllers/SwapController.test.js +0 -105
- package/dist/esm/tests/controllers/SwapController.test.js.map +0 -1
- package/dist/esm/tests/controllers/ThemeController.test.js +0 -26
- package/dist/esm/tests/controllers/ThemeController.test.js.map +0 -1
- package/dist/esm/tests/controllers/TooltipController.test.js +0 -40
- package/dist/esm/tests/controllers/TooltipController.test.js.map +0 -1
- package/dist/esm/tests/controllers/TransactionsController.test.js +0 -239
- package/dist/esm/tests/controllers/TransactionsController.test.js.map +0 -1
- package/dist/esm/tests/hooks/react.test.js +0 -149
- package/dist/esm/tests/hooks/react.test.js.map +0 -1
- package/dist/esm/tests/hooks/vue.test.js +0 -56
- package/dist/esm/tests/hooks/vue.test.js.map +0 -1
- package/dist/esm/tests/mocks/ChainController.js +0 -24
- package/dist/esm/tests/mocks/ChainController.js.map +0 -1
- package/dist/esm/tests/mocks/SwapController.js +0 -118
- package/dist/esm/tests/mocks/SwapController.js.map +0 -1
- package/dist/esm/tests/mocks/useAppKitAccount.js +0 -41
- package/dist/esm/tests/mocks/useAppKitAccount.js.map +0 -1
- package/dist/esm/tests/utils/AssetUtil.test.js +0 -108
- package/dist/esm/tests/utils/AssetUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/CoreHelperUtil.test.js +0 -67
- package/dist/esm/tests/utils/CoreHelperUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/ERC7811.test.js +0 -268
- package/dist/esm/tests/utils/ERC7811.test.js.map +0 -1
- package/dist/esm/tests/utils/EnsUtil.test.js +0 -23
- package/dist/esm/tests/utils/EnsUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/FetchUtil.test.js +0 -175
- package/dist/esm/tests/utils/FetchUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/MobileWallet.test.js +0 -72
- package/dist/esm/tests/utils/MobileWallet.test.js.map +0 -1
- package/dist/esm/tests/utils/OptionsUtil.test.js +0 -20
- package/dist/esm/tests/utils/OptionsUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/RouterUtil.test.js +0 -62
- package/dist/esm/tests/utils/RouterUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/SendApiUtil.test.js +0 -340
- package/dist/esm/tests/utils/SendApiUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/StorageUtil.test.js +0 -162
- package/dist/esm/tests/utils/StorageUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/SwapApiUtil.test.js +0 -213
- package/dist/esm/tests/utils/SwapApiUtil.test.js.map +0 -1
- package/dist/esm/tests/utils/SwapCalculationUtil.test.js +0 -80
- package/dist/esm/tests/utils/SwapCalculationUtil.test.js.map +0 -1
- package/dist/types/exports/modal.d.ts +0 -2
- package/dist/types/src/controllers/AccountController.d.ts +0 -56
- package/dist/types/src/controllers/AlertController.d.ts +0 -17
- package/dist/types/src/controllers/ApiController.d.ts +0 -51
- package/dist/types/src/controllers/AssetController.d.ts +0 -22
- package/dist/types/src/controllers/BlockchainApiController.d.ts +0 -76
- package/dist/types/src/controllers/ChainController.d.ts +0 -65
- package/dist/types/src/controllers/ConnectionController.d.ts +0 -86
- package/dist/types/src/controllers/ConnectorController.d.ts +0 -49
- package/dist/types/src/controllers/EnsController.d.ts +0 -26
- package/dist/types/src/controllers/EventsController.d.ts +0 -17
- package/dist/types/src/controllers/ModalController.d.ts +0 -22
- package/dist/types/src/controllers/OnRampController.d.ts +0 -55
- package/dist/types/src/controllers/OptionsController.d.ts +0 -364
- package/dist/types/src/controllers/PublicStateController.d.ts +0 -33
- package/dist/types/src/controllers/RouterController.d.ts +0 -56
- package/dist/types/src/controllers/SendController.d.ts +0 -52
- package/dist/types/src/controllers/SnackController.d.ts +0 -29
- package/dist/types/src/controllers/SwapController.d.ts +0 -127
- package/dist/types/src/controllers/ThemeController.d.ts +0 -30
- package/dist/types/src/controllers/TooltipController.d.ts +0 -25
- package/dist/types/src/controllers/TransactionsController.d.ts +0 -25
- package/dist/types/src/utils/AssetUtil.d.ts +0 -15
- package/dist/types/src/utils/ConstantsUtil.d.ts +0 -85
- package/dist/types/src/utils/CoreHelperUtil.d.ts +0 -56
- package/dist/types/src/utils/ERC7811Util.d.ts +0 -67
- package/dist/types/src/utils/EnsUtil.d.ts +0 -3
- package/dist/types/src/utils/FetchUtil.d.ts +0 -26
- package/dist/types/src/utils/MobileWallet.d.ts +0 -9
- package/dist/types/src/utils/OptionsUtil.d.ts +0 -5
- package/dist/types/src/utils/RouterUtil.d.ts +0 -4
- package/dist/types/src/utils/SIWXUtil.d.ts +0 -183
- package/dist/types/src/utils/SendApiUtil.d.ts +0 -13
- package/dist/types/src/utils/StorageUtil.d.ts +0 -99
- package/dist/types/src/utils/SwapApiUtil.d.ts +0 -27
- package/dist/types/src/utils/SwapCalculationUtil.d.ts +0 -22
- package/dist/types/src/utils/TypeUtil.d.ts +0 -981
- package/dist/types/tests/constants/OnrampTransactions.d.ts +0 -138
- package/dist/types/tests/controllers/AccountController.test.d.ts +0 -1
- package/dist/types/tests/controllers/AlertController.test.d.ts +0 -1
- package/dist/types/tests/controllers/ApiController.test.d.ts +0 -1
- package/dist/types/tests/controllers/AssetController.test.d.ts +0 -1
- package/dist/types/tests/controllers/BlockchainApiController.test.d.ts +0 -1
- package/dist/types/tests/controllers/ChainController.test.d.ts +0 -1
- package/dist/types/tests/controllers/ConnectionController.test.d.ts +0 -1
- package/dist/types/tests/controllers/ConnectorController.test.d.ts +0 -1
- package/dist/types/tests/controllers/EnsController.test.d.ts +0 -1
- package/dist/types/tests/controllers/EventsController.test.d.ts +0 -1
- package/dist/types/tests/controllers/ModalController.test.d.ts +0 -1
- package/dist/types/tests/controllers/OnRampController.test.d.ts +0 -1
- package/dist/types/tests/controllers/OptionsController.test.d.ts +0 -1
- package/dist/types/tests/controllers/PublicStateController.test.d.ts +0 -1
- package/dist/types/tests/controllers/RouterController.test.d.ts +0 -1
- package/dist/types/tests/controllers/SendController.test.d.ts +0 -1
- package/dist/types/tests/controllers/SnackController.test.d.ts +0 -1
- package/dist/types/tests/controllers/SwapController.test.d.ts +0 -1
- package/dist/types/tests/controllers/ThemeController.test.d.ts +0 -1
- package/dist/types/tests/controllers/TooltipController.test.d.ts +0 -1
- package/dist/types/tests/controllers/TransactionsController.test.d.ts +0 -1
- package/dist/types/tests/hooks/react.test.d.ts +0 -1
- package/dist/types/tests/hooks/vue.test.d.ts +0 -2
- package/dist/types/tests/mocks/ChainController.d.ts +0 -2
- package/dist/types/tests/mocks/SwapController.d.ts +0 -75
- package/dist/types/tests/mocks/useAppKitAccount.d.ts +0 -40
- package/dist/types/tests/utils/AssetUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/CoreHelperUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/ERC7811.test.d.ts +0 -1
- package/dist/types/tests/utils/EnsUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/FetchUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/MobileWallet.test.d.ts +0 -1
- package/dist/types/tests/utils/OptionsUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/RouterUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/SendApiUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/StorageUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/SwapApiUtil.test.d.ts +0 -1
- package/dist/types/tests/utils/SwapCalculationUtil.test.d.ts +0 -1
|
@@ -1,674 +0,0 @@
|
|
|
1
|
-
import { proxy, subscribe as sub } from 'valtio/vanilla';
|
|
2
|
-
import { subscribeKey as subKey } from 'valtio/vanilla/utils';
|
|
3
|
-
import { NumberUtil } from '@reown/appkit-common';
|
|
4
|
-
import { ConstantsUtil as CommonConstantsUtil } from '@reown/appkit-common';
|
|
5
|
-
import { W3mFrameRpcConstants } from '@reown/appkit-wallet/utils';
|
|
6
|
-
import { ConstantsUtil } from '../utils/ConstantsUtil.js';
|
|
7
|
-
import { CoreHelperUtil } from '../utils/CoreHelperUtil.js';
|
|
8
|
-
import { StorageUtil } from '../utils/StorageUtil.js';
|
|
9
|
-
import { SwapApiUtil } from '../utils/SwapApiUtil.js';
|
|
10
|
-
import { SwapCalculationUtil } from '../utils/SwapCalculationUtil.js';
|
|
11
|
-
import { AccountController } from './AccountController.js';
|
|
12
|
-
import { AlertController } from './AlertController.js';
|
|
13
|
-
import { BlockchainApiController } from './BlockchainApiController.js';
|
|
14
|
-
import { ChainController } from './ChainController.js';
|
|
15
|
-
import { ConnectionController } from './ConnectionController.js';
|
|
16
|
-
import { EventsController } from './EventsController.js';
|
|
17
|
-
import { RouterController } from './RouterController.js';
|
|
18
|
-
import { SnackController } from './SnackController.js';
|
|
19
|
-
// -- Constants ---------------------------------------- //
|
|
20
|
-
export const INITIAL_GAS_LIMIT = 150000;
|
|
21
|
-
export const TO_AMOUNT_DECIMALS = 6;
|
|
22
|
-
class TransactionError extends Error {
|
|
23
|
-
constructor(message, shortMessage) {
|
|
24
|
-
super(message);
|
|
25
|
-
this.name = 'TransactionError';
|
|
26
|
-
this.shortMessage = shortMessage;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
// -- State --------------------------------------------- //
|
|
30
|
-
const initialState = {
|
|
31
|
-
// Loading states
|
|
32
|
-
initializing: false,
|
|
33
|
-
initialized: false,
|
|
34
|
-
loadingPrices: false,
|
|
35
|
-
loadingQuote: false,
|
|
36
|
-
loadingApprovalTransaction: false,
|
|
37
|
-
loadingBuildTransaction: false,
|
|
38
|
-
loadingTransaction: false,
|
|
39
|
-
// Error states
|
|
40
|
-
fetchError: false,
|
|
41
|
-
// Approval & Swap transaction states
|
|
42
|
-
approvalTransaction: undefined,
|
|
43
|
-
swapTransaction: undefined,
|
|
44
|
-
transactionError: undefined,
|
|
45
|
-
// Input values
|
|
46
|
-
sourceToken: undefined,
|
|
47
|
-
sourceTokenAmount: '',
|
|
48
|
-
sourceTokenPriceInUSD: 0,
|
|
49
|
-
toToken: undefined,
|
|
50
|
-
toTokenAmount: '',
|
|
51
|
-
toTokenPriceInUSD: 0,
|
|
52
|
-
networkPrice: '0',
|
|
53
|
-
networkBalanceInUSD: '0',
|
|
54
|
-
networkTokenSymbol: '',
|
|
55
|
-
inputError: undefined,
|
|
56
|
-
// Request values
|
|
57
|
-
slippage: ConstantsUtil.CONVERT_SLIPPAGE_TOLERANCE,
|
|
58
|
-
// Tokens
|
|
59
|
-
tokens: undefined,
|
|
60
|
-
popularTokens: undefined,
|
|
61
|
-
suggestedTokens: undefined,
|
|
62
|
-
foundTokens: undefined,
|
|
63
|
-
myTokensWithBalance: undefined,
|
|
64
|
-
tokensPriceMap: {},
|
|
65
|
-
// Calculations
|
|
66
|
-
gasFee: '0',
|
|
67
|
-
gasPriceInUSD: 0,
|
|
68
|
-
priceImpact: undefined,
|
|
69
|
-
maxSlippage: undefined,
|
|
70
|
-
providerFee: undefined
|
|
71
|
-
};
|
|
72
|
-
const state = proxy(initialState);
|
|
73
|
-
// -- Controller ---------------------------------------- //
|
|
74
|
-
export const SwapController = {
|
|
75
|
-
state,
|
|
76
|
-
subscribe(callback) {
|
|
77
|
-
return sub(state, () => callback(state));
|
|
78
|
-
},
|
|
79
|
-
subscribeKey(key, callback) {
|
|
80
|
-
return subKey(state, key, callback);
|
|
81
|
-
},
|
|
82
|
-
getParams() {
|
|
83
|
-
const caipAddress = ChainController.state.activeCaipAddress;
|
|
84
|
-
const namespace = ChainController.state.activeChain;
|
|
85
|
-
const address = CoreHelperUtil.getPlainAddress(caipAddress);
|
|
86
|
-
const networkAddress = ChainController.getActiveNetworkTokenAddress();
|
|
87
|
-
const connectorId = StorageUtil.getConnectedConnectorId(namespace);
|
|
88
|
-
if (!address) {
|
|
89
|
-
throw new Error('No address found to swap the tokens from.');
|
|
90
|
-
}
|
|
91
|
-
const invalidToToken = !state.toToken?.address || !state.toToken?.decimals;
|
|
92
|
-
const invalidSourceToken = !state.sourceToken?.address ||
|
|
93
|
-
!state.sourceToken?.decimals ||
|
|
94
|
-
!NumberUtil.bigNumber(state.sourceTokenAmount).gt(0);
|
|
95
|
-
const invalidSourceTokenAmount = !state.sourceTokenAmount;
|
|
96
|
-
return {
|
|
97
|
-
networkAddress,
|
|
98
|
-
fromAddress: address,
|
|
99
|
-
fromCaipAddress: caipAddress,
|
|
100
|
-
sourceTokenAddress: state.sourceToken?.address,
|
|
101
|
-
toTokenAddress: state.toToken?.address,
|
|
102
|
-
toTokenAmount: state.toTokenAmount,
|
|
103
|
-
toTokenDecimals: state.toToken?.decimals,
|
|
104
|
-
sourceTokenAmount: state.sourceTokenAmount,
|
|
105
|
-
sourceTokenDecimals: state.sourceToken?.decimals,
|
|
106
|
-
invalidToToken,
|
|
107
|
-
invalidSourceToken,
|
|
108
|
-
invalidSourceTokenAmount,
|
|
109
|
-
availableToSwap: caipAddress && !invalidToToken && !invalidSourceToken && !invalidSourceTokenAmount,
|
|
110
|
-
isAuthConnector: connectorId === CommonConstantsUtil.CONNECTOR_ID.AUTH
|
|
111
|
-
};
|
|
112
|
-
},
|
|
113
|
-
setSourceToken(sourceToken) {
|
|
114
|
-
if (!sourceToken) {
|
|
115
|
-
state.sourceToken = sourceToken;
|
|
116
|
-
state.sourceTokenAmount = '';
|
|
117
|
-
state.sourceTokenPriceInUSD = 0;
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
state.sourceToken = sourceToken;
|
|
121
|
-
this.setTokenPrice(sourceToken.address, 'sourceToken');
|
|
122
|
-
},
|
|
123
|
-
setSourceTokenAmount(amount) {
|
|
124
|
-
state.sourceTokenAmount = amount;
|
|
125
|
-
},
|
|
126
|
-
setToToken(toToken) {
|
|
127
|
-
if (!toToken) {
|
|
128
|
-
state.toToken = toToken;
|
|
129
|
-
state.toTokenAmount = '';
|
|
130
|
-
state.toTokenPriceInUSD = 0;
|
|
131
|
-
return;
|
|
132
|
-
}
|
|
133
|
-
state.toToken = toToken;
|
|
134
|
-
this.setTokenPrice(toToken.address, 'toToken');
|
|
135
|
-
},
|
|
136
|
-
setToTokenAmount(amount) {
|
|
137
|
-
state.toTokenAmount = amount
|
|
138
|
-
? NumberUtil.formatNumberToLocalString(amount, TO_AMOUNT_DECIMALS)
|
|
139
|
-
: '';
|
|
140
|
-
},
|
|
141
|
-
async setTokenPrice(address, target) {
|
|
142
|
-
let price = state.tokensPriceMap[address] || 0;
|
|
143
|
-
if (!price) {
|
|
144
|
-
state.loadingPrices = true;
|
|
145
|
-
price = await this.getAddressPrice(address);
|
|
146
|
-
}
|
|
147
|
-
if (target === 'sourceToken') {
|
|
148
|
-
state.sourceTokenPriceInUSD = price;
|
|
149
|
-
}
|
|
150
|
-
else if (target === 'toToken') {
|
|
151
|
-
state.toTokenPriceInUSD = price;
|
|
152
|
-
}
|
|
153
|
-
if (state.loadingPrices) {
|
|
154
|
-
state.loadingPrices = false;
|
|
155
|
-
}
|
|
156
|
-
if (this.getParams().availableToSwap) {
|
|
157
|
-
this.swapTokens();
|
|
158
|
-
}
|
|
159
|
-
},
|
|
160
|
-
switchTokens() {
|
|
161
|
-
if (state.initializing || !state.initialized) {
|
|
162
|
-
return;
|
|
163
|
-
}
|
|
164
|
-
const newSourceToken = state.toToken ? { ...state.toToken } : undefined;
|
|
165
|
-
const newToToken = state.sourceToken ? { ...state.sourceToken } : undefined;
|
|
166
|
-
const newSourceTokenAmount = newSourceToken && state.toTokenAmount === '' ? '1' : state.toTokenAmount;
|
|
167
|
-
this.setSourceToken(newSourceToken);
|
|
168
|
-
this.setToToken(newToToken);
|
|
169
|
-
this.setSourceTokenAmount(newSourceTokenAmount);
|
|
170
|
-
this.setToTokenAmount('');
|
|
171
|
-
this.swapTokens();
|
|
172
|
-
},
|
|
173
|
-
resetState() {
|
|
174
|
-
state.myTokensWithBalance = initialState.myTokensWithBalance;
|
|
175
|
-
state.tokensPriceMap = initialState.tokensPriceMap;
|
|
176
|
-
state.initialized = initialState.initialized;
|
|
177
|
-
state.sourceToken = initialState.sourceToken;
|
|
178
|
-
state.sourceTokenAmount = initialState.sourceTokenAmount;
|
|
179
|
-
state.sourceTokenPriceInUSD = initialState.sourceTokenPriceInUSD;
|
|
180
|
-
state.toToken = initialState.toToken;
|
|
181
|
-
state.toTokenAmount = initialState.toTokenAmount;
|
|
182
|
-
state.toTokenPriceInUSD = initialState.toTokenPriceInUSD;
|
|
183
|
-
state.networkPrice = initialState.networkPrice;
|
|
184
|
-
state.networkTokenSymbol = initialState.networkTokenSymbol;
|
|
185
|
-
state.networkBalanceInUSD = initialState.networkBalanceInUSD;
|
|
186
|
-
state.inputError = initialState.inputError;
|
|
187
|
-
},
|
|
188
|
-
resetValues() {
|
|
189
|
-
const { networkAddress } = this.getParams();
|
|
190
|
-
const networkToken = state.tokens?.find(token => token.address === networkAddress);
|
|
191
|
-
this.setSourceToken(networkToken);
|
|
192
|
-
this.setToToken(undefined);
|
|
193
|
-
},
|
|
194
|
-
getApprovalLoadingState() {
|
|
195
|
-
return state.loadingApprovalTransaction;
|
|
196
|
-
},
|
|
197
|
-
clearError() {
|
|
198
|
-
state.transactionError = undefined;
|
|
199
|
-
},
|
|
200
|
-
async initializeState() {
|
|
201
|
-
if (state.initializing) {
|
|
202
|
-
return;
|
|
203
|
-
}
|
|
204
|
-
state.initializing = true;
|
|
205
|
-
if (!state.initialized) {
|
|
206
|
-
try {
|
|
207
|
-
await this.fetchTokens();
|
|
208
|
-
state.initialized = true;
|
|
209
|
-
}
|
|
210
|
-
catch (error) {
|
|
211
|
-
state.initialized = false;
|
|
212
|
-
SnackController.showError('Failed to initialize swap');
|
|
213
|
-
RouterController.goBack();
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
state.initializing = false;
|
|
217
|
-
},
|
|
218
|
-
async fetchTokens() {
|
|
219
|
-
const { networkAddress } = this.getParams();
|
|
220
|
-
await this.getTokenList();
|
|
221
|
-
await this.getNetworkTokenPrice();
|
|
222
|
-
await this.getMyTokensWithBalance();
|
|
223
|
-
const networkToken = state.tokens?.find(token => token.address === networkAddress);
|
|
224
|
-
if (networkToken) {
|
|
225
|
-
state.networkTokenSymbol = networkToken.symbol;
|
|
226
|
-
this.setSourceToken(networkToken);
|
|
227
|
-
this.setSourceTokenAmount('1');
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
async getTokenList() {
|
|
231
|
-
const tokens = await SwapApiUtil.getTokenList();
|
|
232
|
-
state.tokens = tokens;
|
|
233
|
-
state.popularTokens = tokens.sort((aTokenInfo, bTokenInfo) => {
|
|
234
|
-
if (aTokenInfo.symbol < bTokenInfo.symbol) {
|
|
235
|
-
return -1;
|
|
236
|
-
}
|
|
237
|
-
if (aTokenInfo.symbol > bTokenInfo.symbol) {
|
|
238
|
-
return 1;
|
|
239
|
-
}
|
|
240
|
-
return 0;
|
|
241
|
-
});
|
|
242
|
-
state.suggestedTokens = tokens.filter(token => {
|
|
243
|
-
if (ConstantsUtil.SWAP_SUGGESTED_TOKENS.includes(token.symbol)) {
|
|
244
|
-
return true;
|
|
245
|
-
}
|
|
246
|
-
return false;
|
|
247
|
-
}, {});
|
|
248
|
-
},
|
|
249
|
-
async getAddressPrice(address) {
|
|
250
|
-
const existPrice = state.tokensPriceMap[address];
|
|
251
|
-
if (existPrice) {
|
|
252
|
-
return existPrice;
|
|
253
|
-
}
|
|
254
|
-
const response = await BlockchainApiController.fetchTokenPrice({
|
|
255
|
-
addresses: [address]
|
|
256
|
-
});
|
|
257
|
-
const fungibles = response?.fungibles || [];
|
|
258
|
-
const allTokens = [...(state.tokens || []), ...(state.myTokensWithBalance || [])];
|
|
259
|
-
const symbol = allTokens?.find(token => token.address === address)?.symbol;
|
|
260
|
-
const price = fungibles.find(p => p.symbol.toLowerCase() === symbol?.toLowerCase())?.price || 0;
|
|
261
|
-
const priceAsFloat = parseFloat(price.toString());
|
|
262
|
-
state.tokensPriceMap[address] = priceAsFloat;
|
|
263
|
-
return priceAsFloat;
|
|
264
|
-
},
|
|
265
|
-
async getNetworkTokenPrice() {
|
|
266
|
-
const { networkAddress } = this.getParams();
|
|
267
|
-
const response = await BlockchainApiController.fetchTokenPrice({
|
|
268
|
-
addresses: [networkAddress]
|
|
269
|
-
}).catch(() => {
|
|
270
|
-
SnackController.showError('Failed to fetch network token price');
|
|
271
|
-
return { fungibles: [] };
|
|
272
|
-
});
|
|
273
|
-
const token = response.fungibles?.[0];
|
|
274
|
-
const price = token?.price.toString() || '0';
|
|
275
|
-
state.tokensPriceMap[networkAddress] = parseFloat(price);
|
|
276
|
-
state.networkTokenSymbol = token?.symbol || '';
|
|
277
|
-
state.networkPrice = price;
|
|
278
|
-
},
|
|
279
|
-
async getMyTokensWithBalance(forceUpdate) {
|
|
280
|
-
const balances = await SwapApiUtil.getMyTokensWithBalance(forceUpdate);
|
|
281
|
-
if (!balances) {
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
284
|
-
await this.getInitialGasPrice();
|
|
285
|
-
this.setBalances(balances);
|
|
286
|
-
},
|
|
287
|
-
setBalances(balances) {
|
|
288
|
-
const { networkAddress } = this.getParams();
|
|
289
|
-
const caipNetwork = ChainController.state.activeCaipNetwork;
|
|
290
|
-
if (!caipNetwork) {
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
const networkToken = balances.find(token => token.address === networkAddress);
|
|
294
|
-
balances.forEach(token => {
|
|
295
|
-
state.tokensPriceMap[token.address] = token.price || 0;
|
|
296
|
-
});
|
|
297
|
-
state.myTokensWithBalance = balances.filter(token => token.address.startsWith(caipNetwork.caipNetworkId));
|
|
298
|
-
state.networkBalanceInUSD = networkToken
|
|
299
|
-
? NumberUtil.multiply(networkToken.quantity.numeric, networkToken.price).toString()
|
|
300
|
-
: '0';
|
|
301
|
-
},
|
|
302
|
-
async getInitialGasPrice() {
|
|
303
|
-
const res = await SwapApiUtil.fetchGasPrice();
|
|
304
|
-
if (!res) {
|
|
305
|
-
return { gasPrice: null, gasPriceInUSD: null };
|
|
306
|
-
}
|
|
307
|
-
switch (ChainController.state?.activeCaipNetwork?.chainNamespace) {
|
|
308
|
-
case 'solana':
|
|
309
|
-
state.gasFee = res.standard ?? '0';
|
|
310
|
-
state.gasPriceInUSD = NumberUtil.multiply(res.standard, state.networkPrice)
|
|
311
|
-
.div(1e9)
|
|
312
|
-
.toNumber();
|
|
313
|
-
return {
|
|
314
|
-
gasPrice: BigInt(state.gasFee),
|
|
315
|
-
gasPriceInUSD: Number(state.gasPriceInUSD)
|
|
316
|
-
};
|
|
317
|
-
case 'eip155':
|
|
318
|
-
default:
|
|
319
|
-
// eslint-disable-next-line no-case-declarations
|
|
320
|
-
const value = res.standard ?? '0';
|
|
321
|
-
// eslint-disable-next-line no-case-declarations
|
|
322
|
-
const gasFee = BigInt(value);
|
|
323
|
-
// eslint-disable-next-line no-case-declarations
|
|
324
|
-
const gasLimit = BigInt(INITIAL_GAS_LIMIT);
|
|
325
|
-
// eslint-disable-next-line no-case-declarations
|
|
326
|
-
const gasPrice = SwapCalculationUtil.getGasPriceInUSD(state.networkPrice, gasLimit, gasFee);
|
|
327
|
-
state.gasFee = value;
|
|
328
|
-
state.gasPriceInUSD = gasPrice;
|
|
329
|
-
return { gasPrice: gasFee, gasPriceInUSD: gasPrice };
|
|
330
|
-
}
|
|
331
|
-
},
|
|
332
|
-
// -- Swap -------------------------------------- //
|
|
333
|
-
async swapTokens() {
|
|
334
|
-
const address = AccountController.state.address;
|
|
335
|
-
const sourceToken = state.sourceToken;
|
|
336
|
-
const toToken = state.toToken;
|
|
337
|
-
const haveSourceTokenAmount = NumberUtil.bigNumber(state.sourceTokenAmount).gt(0);
|
|
338
|
-
if (!haveSourceTokenAmount) {
|
|
339
|
-
this.setToTokenAmount('');
|
|
340
|
-
}
|
|
341
|
-
if (!toToken || !sourceToken || state.loadingPrices || !haveSourceTokenAmount) {
|
|
342
|
-
return;
|
|
343
|
-
}
|
|
344
|
-
state.loadingQuote = true;
|
|
345
|
-
const amountDecimal = NumberUtil.bigNumber(state.sourceTokenAmount)
|
|
346
|
-
.times(10 ** sourceToken.decimals)
|
|
347
|
-
.round(0);
|
|
348
|
-
try {
|
|
349
|
-
const quoteResponse = await BlockchainApiController.fetchSwapQuote({
|
|
350
|
-
userAddress: address,
|
|
351
|
-
from: sourceToken.address,
|
|
352
|
-
to: toToken.address,
|
|
353
|
-
gasPrice: state.gasFee,
|
|
354
|
-
amount: amountDecimal.toString()
|
|
355
|
-
});
|
|
356
|
-
state.loadingQuote = false;
|
|
357
|
-
const quoteToAmount = quoteResponse?.quotes?.[0]?.toAmount;
|
|
358
|
-
if (!quoteToAmount) {
|
|
359
|
-
AlertController.open({
|
|
360
|
-
shortMessage: 'Incorrect amount',
|
|
361
|
-
longMessage: 'Please enter a valid amount'
|
|
362
|
-
}, 'error');
|
|
363
|
-
return;
|
|
364
|
-
}
|
|
365
|
-
const toTokenAmount = NumberUtil.bigNumber(quoteToAmount)
|
|
366
|
-
.div(10 ** toToken.decimals)
|
|
367
|
-
.toString();
|
|
368
|
-
this.setToTokenAmount(toTokenAmount);
|
|
369
|
-
const isInsufficientToken = this.hasInsufficientToken(state.sourceTokenAmount, sourceToken.address);
|
|
370
|
-
if (isInsufficientToken) {
|
|
371
|
-
state.inputError = 'Insufficient balance';
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
state.inputError = undefined;
|
|
375
|
-
this.setTransactionDetails();
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
catch (error) {
|
|
379
|
-
state.loadingQuote = false;
|
|
380
|
-
state.inputError = 'Insufficient balance';
|
|
381
|
-
}
|
|
382
|
-
},
|
|
383
|
-
// -- Create Transactions -------------------------------------- //
|
|
384
|
-
async getTransaction() {
|
|
385
|
-
const { fromCaipAddress, availableToSwap } = this.getParams();
|
|
386
|
-
const sourceToken = state.sourceToken;
|
|
387
|
-
const toToken = state.toToken;
|
|
388
|
-
if (!fromCaipAddress || !availableToSwap || !sourceToken || !toToken || state.loadingQuote) {
|
|
389
|
-
return undefined;
|
|
390
|
-
}
|
|
391
|
-
try {
|
|
392
|
-
state.loadingBuildTransaction = true;
|
|
393
|
-
const hasAllowance = await SwapApiUtil.fetchSwapAllowance({
|
|
394
|
-
userAddress: fromCaipAddress,
|
|
395
|
-
tokenAddress: sourceToken.address,
|
|
396
|
-
sourceTokenAmount: state.sourceTokenAmount,
|
|
397
|
-
sourceTokenDecimals: sourceToken.decimals
|
|
398
|
-
});
|
|
399
|
-
let transaction = undefined;
|
|
400
|
-
if (hasAllowance) {
|
|
401
|
-
transaction = await this.createSwapTransaction();
|
|
402
|
-
}
|
|
403
|
-
else {
|
|
404
|
-
transaction = await this.createAllowanceTransaction();
|
|
405
|
-
}
|
|
406
|
-
state.loadingBuildTransaction = false;
|
|
407
|
-
state.fetchError = false;
|
|
408
|
-
return transaction;
|
|
409
|
-
}
|
|
410
|
-
catch (error) {
|
|
411
|
-
RouterController.goBack();
|
|
412
|
-
SnackController.showError('Failed to check allowance');
|
|
413
|
-
state.loadingBuildTransaction = false;
|
|
414
|
-
state.approvalTransaction = undefined;
|
|
415
|
-
state.swapTransaction = undefined;
|
|
416
|
-
state.fetchError = true;
|
|
417
|
-
return undefined;
|
|
418
|
-
}
|
|
419
|
-
},
|
|
420
|
-
async createAllowanceTransaction() {
|
|
421
|
-
const { fromCaipAddress, fromAddress, sourceTokenAddress, toTokenAddress } = this.getParams();
|
|
422
|
-
if (!fromCaipAddress || !toTokenAddress) {
|
|
423
|
-
return undefined;
|
|
424
|
-
}
|
|
425
|
-
if (!sourceTokenAddress) {
|
|
426
|
-
throw new Error('createAllowanceTransaction - No source token address found.');
|
|
427
|
-
}
|
|
428
|
-
try {
|
|
429
|
-
const response = await BlockchainApiController.generateApproveCalldata({
|
|
430
|
-
from: sourceTokenAddress,
|
|
431
|
-
to: toTokenAddress,
|
|
432
|
-
userAddress: fromCaipAddress
|
|
433
|
-
});
|
|
434
|
-
const gasLimit = await ConnectionController.estimateGas({
|
|
435
|
-
address: fromAddress,
|
|
436
|
-
to: CoreHelperUtil.getPlainAddress(response.tx.to),
|
|
437
|
-
data: response.tx.data
|
|
438
|
-
});
|
|
439
|
-
const transaction = {
|
|
440
|
-
data: response.tx.data,
|
|
441
|
-
to: CoreHelperUtil.getPlainAddress(response.tx.from),
|
|
442
|
-
gas: gasLimit,
|
|
443
|
-
gasPrice: BigInt(response.tx.eip155.gasPrice),
|
|
444
|
-
value: BigInt(response.tx.value),
|
|
445
|
-
toAmount: state.toTokenAmount
|
|
446
|
-
};
|
|
447
|
-
state.swapTransaction = undefined;
|
|
448
|
-
state.approvalTransaction = {
|
|
449
|
-
data: transaction.data,
|
|
450
|
-
to: transaction.to,
|
|
451
|
-
gas: transaction.gas ?? BigInt(0),
|
|
452
|
-
gasPrice: transaction.gasPrice,
|
|
453
|
-
value: transaction.value,
|
|
454
|
-
toAmount: transaction.toAmount
|
|
455
|
-
};
|
|
456
|
-
return {
|
|
457
|
-
data: transaction.data,
|
|
458
|
-
to: transaction.to,
|
|
459
|
-
gas: transaction.gas ?? BigInt(0),
|
|
460
|
-
gasPrice: transaction.gasPrice,
|
|
461
|
-
value: transaction.value,
|
|
462
|
-
toAmount: transaction.toAmount
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
catch (error) {
|
|
466
|
-
RouterController.goBack();
|
|
467
|
-
SnackController.showError('Failed to create approval transaction');
|
|
468
|
-
state.approvalTransaction = undefined;
|
|
469
|
-
state.swapTransaction = undefined;
|
|
470
|
-
state.fetchError = true;
|
|
471
|
-
return undefined;
|
|
472
|
-
}
|
|
473
|
-
},
|
|
474
|
-
async createSwapTransaction() {
|
|
475
|
-
const { networkAddress, fromCaipAddress, sourceTokenAmount } = this.getParams();
|
|
476
|
-
const sourceToken = state.sourceToken;
|
|
477
|
-
const toToken = state.toToken;
|
|
478
|
-
if (!fromCaipAddress || !sourceTokenAmount || !sourceToken || !toToken) {
|
|
479
|
-
return undefined;
|
|
480
|
-
}
|
|
481
|
-
const amount = ConnectionController.parseUnits(sourceTokenAmount, sourceToken.decimals)?.toString();
|
|
482
|
-
try {
|
|
483
|
-
const response = await BlockchainApiController.generateSwapCalldata({
|
|
484
|
-
userAddress: fromCaipAddress,
|
|
485
|
-
from: sourceToken.address,
|
|
486
|
-
to: toToken.address,
|
|
487
|
-
amount: amount
|
|
488
|
-
});
|
|
489
|
-
const isSourceTokenIsNetworkToken = sourceToken.address === networkAddress;
|
|
490
|
-
const gas = BigInt(response.tx.eip155.gas);
|
|
491
|
-
const gasPrice = BigInt(response.tx.eip155.gasPrice);
|
|
492
|
-
const transaction = {
|
|
493
|
-
data: response.tx.data,
|
|
494
|
-
to: CoreHelperUtil.getPlainAddress(response.tx.to),
|
|
495
|
-
gas,
|
|
496
|
-
gasPrice,
|
|
497
|
-
value: isSourceTokenIsNetworkToken ? BigInt(amount ?? '0') : BigInt('0'),
|
|
498
|
-
toAmount: state.toTokenAmount
|
|
499
|
-
};
|
|
500
|
-
state.gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(state.networkPrice, gas, gasPrice);
|
|
501
|
-
state.approvalTransaction = undefined;
|
|
502
|
-
state.swapTransaction = transaction;
|
|
503
|
-
return transaction;
|
|
504
|
-
}
|
|
505
|
-
catch (error) {
|
|
506
|
-
RouterController.goBack();
|
|
507
|
-
SnackController.showError('Failed to create transaction');
|
|
508
|
-
state.approvalTransaction = undefined;
|
|
509
|
-
state.swapTransaction = undefined;
|
|
510
|
-
state.fetchError = true;
|
|
511
|
-
return undefined;
|
|
512
|
-
}
|
|
513
|
-
},
|
|
514
|
-
// -- Send Transactions --------------------------------- //
|
|
515
|
-
async sendTransactionForApproval(data) {
|
|
516
|
-
const { fromAddress, isAuthConnector } = this.getParams();
|
|
517
|
-
state.loadingApprovalTransaction = true;
|
|
518
|
-
const approveLimitMessage = `Approve limit increase in your wallet`;
|
|
519
|
-
if (isAuthConnector) {
|
|
520
|
-
RouterController.pushTransactionStack({
|
|
521
|
-
view: null,
|
|
522
|
-
goBack: true,
|
|
523
|
-
onSuccess() {
|
|
524
|
-
SnackController.showLoading(approveLimitMessage);
|
|
525
|
-
}
|
|
526
|
-
});
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
SnackController.showLoading(approveLimitMessage);
|
|
530
|
-
}
|
|
531
|
-
try {
|
|
532
|
-
await ConnectionController.sendTransaction({
|
|
533
|
-
address: fromAddress,
|
|
534
|
-
to: data.to,
|
|
535
|
-
data: data.data,
|
|
536
|
-
gas: data.gas,
|
|
537
|
-
gasPrice: BigInt(data.gasPrice),
|
|
538
|
-
value: data.value,
|
|
539
|
-
chainNamespace: 'eip155'
|
|
540
|
-
});
|
|
541
|
-
await this.swapTokens();
|
|
542
|
-
await this.getTransaction();
|
|
543
|
-
state.approvalTransaction = undefined;
|
|
544
|
-
state.loadingApprovalTransaction = false;
|
|
545
|
-
}
|
|
546
|
-
catch (err) {
|
|
547
|
-
const error = err;
|
|
548
|
-
state.transactionError = error?.shortMessage;
|
|
549
|
-
state.loadingApprovalTransaction = false;
|
|
550
|
-
SnackController.showError(error?.shortMessage || 'Transaction error');
|
|
551
|
-
EventsController.sendEvent({
|
|
552
|
-
type: 'track',
|
|
553
|
-
event: 'SWAP_APPROVAL_ERROR',
|
|
554
|
-
properties: {
|
|
555
|
-
message: error?.shortMessage || error?.message || 'Unknown',
|
|
556
|
-
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
|
|
557
|
-
swapFromToken: this.state.sourceToken?.symbol || '',
|
|
558
|
-
swapToToken: this.state.toToken?.symbol || '',
|
|
559
|
-
swapFromAmount: this.state.sourceTokenAmount || '',
|
|
560
|
-
swapToAmount: this.state.toTokenAmount || '',
|
|
561
|
-
isSmartAccount: AccountController.state.preferredAccountType ===
|
|
562
|
-
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
|
|
563
|
-
}
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
},
|
|
567
|
-
async sendTransactionForSwap(data) {
|
|
568
|
-
if (!data) {
|
|
569
|
-
return undefined;
|
|
570
|
-
}
|
|
571
|
-
const { fromAddress, toTokenAmount, isAuthConnector } = this.getParams();
|
|
572
|
-
state.loadingTransaction = true;
|
|
573
|
-
const snackbarPendingMessage = `Swapping ${state.sourceToken?.symbol} to ${NumberUtil.formatNumberToLocalString(toTokenAmount, 3)} ${state.toToken?.symbol}`;
|
|
574
|
-
const snackbarSuccessMessage = `Swapped ${state.sourceToken?.symbol} to ${NumberUtil.formatNumberToLocalString(toTokenAmount, 3)} ${state.toToken?.symbol}`;
|
|
575
|
-
if (isAuthConnector) {
|
|
576
|
-
RouterController.pushTransactionStack({
|
|
577
|
-
view: 'Account',
|
|
578
|
-
goBack: false,
|
|
579
|
-
onSuccess() {
|
|
580
|
-
SnackController.showLoading(snackbarPendingMessage);
|
|
581
|
-
SwapController.resetState();
|
|
582
|
-
}
|
|
583
|
-
});
|
|
584
|
-
}
|
|
585
|
-
else {
|
|
586
|
-
SnackController.showLoading('Confirm transaction in your wallet');
|
|
587
|
-
}
|
|
588
|
-
try {
|
|
589
|
-
const forceUpdateAddresses = [state.sourceToken?.address, state.toToken?.address].join(',');
|
|
590
|
-
const transactionHash = await ConnectionController.sendTransaction({
|
|
591
|
-
address: fromAddress,
|
|
592
|
-
to: data.to,
|
|
593
|
-
data: data.data,
|
|
594
|
-
gas: data.gas,
|
|
595
|
-
gasPrice: BigInt(data.gasPrice),
|
|
596
|
-
value: data.value,
|
|
597
|
-
chainNamespace: 'eip155'
|
|
598
|
-
});
|
|
599
|
-
state.loadingTransaction = false;
|
|
600
|
-
SnackController.showSuccess(snackbarSuccessMessage);
|
|
601
|
-
EventsController.sendEvent({
|
|
602
|
-
type: 'track',
|
|
603
|
-
event: 'SWAP_SUCCESS',
|
|
604
|
-
properties: {
|
|
605
|
-
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
|
|
606
|
-
swapFromToken: this.state.sourceToken?.symbol || '',
|
|
607
|
-
swapToToken: this.state.toToken?.symbol || '',
|
|
608
|
-
swapFromAmount: this.state.sourceTokenAmount || '',
|
|
609
|
-
swapToAmount: this.state.toTokenAmount || '',
|
|
610
|
-
isSmartAccount: AccountController.state.preferredAccountType ===
|
|
611
|
-
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
|
|
612
|
-
}
|
|
613
|
-
});
|
|
614
|
-
SwapController.resetState();
|
|
615
|
-
if (!isAuthConnector) {
|
|
616
|
-
RouterController.replace('Account');
|
|
617
|
-
}
|
|
618
|
-
SwapController.getMyTokensWithBalance(forceUpdateAddresses);
|
|
619
|
-
return transactionHash;
|
|
620
|
-
}
|
|
621
|
-
catch (err) {
|
|
622
|
-
const error = err;
|
|
623
|
-
state.transactionError = error?.shortMessage;
|
|
624
|
-
state.loadingTransaction = false;
|
|
625
|
-
SnackController.showError(error?.shortMessage || 'Transaction error');
|
|
626
|
-
EventsController.sendEvent({
|
|
627
|
-
type: 'track',
|
|
628
|
-
event: 'SWAP_ERROR',
|
|
629
|
-
properties: {
|
|
630
|
-
message: error?.shortMessage || error?.message || 'Unknown',
|
|
631
|
-
network: ChainController.state.activeCaipNetwork?.caipNetworkId || '',
|
|
632
|
-
swapFromToken: this.state.sourceToken?.symbol || '',
|
|
633
|
-
swapToToken: this.state.toToken?.symbol || '',
|
|
634
|
-
swapFromAmount: this.state.sourceTokenAmount || '',
|
|
635
|
-
swapToAmount: this.state.toTokenAmount || '',
|
|
636
|
-
isSmartAccount: AccountController.state.preferredAccountType ===
|
|
637
|
-
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
return undefined;
|
|
641
|
-
}
|
|
642
|
-
},
|
|
643
|
-
// -- Checks -------------------------------------------- //
|
|
644
|
-
hasInsufficientToken(sourceTokenAmount, sourceTokenAddress) {
|
|
645
|
-
const isInsufficientSourceTokenForSwap = SwapCalculationUtil.isInsufficientSourceTokenForSwap(sourceTokenAmount, sourceTokenAddress, state.myTokensWithBalance);
|
|
646
|
-
let insufficientNetworkTokenForGas = true;
|
|
647
|
-
if (AccountController.state.preferredAccountType ===
|
|
648
|
-
W3mFrameRpcConstants.ACCOUNT_TYPES.SMART_ACCOUNT) {
|
|
649
|
-
// Smart Accounts may pay gas in any ERC20 token
|
|
650
|
-
insufficientNetworkTokenForGas = false;
|
|
651
|
-
}
|
|
652
|
-
else {
|
|
653
|
-
insufficientNetworkTokenForGas = SwapCalculationUtil.isInsufficientNetworkTokenForGas(state.networkBalanceInUSD, state.gasPriceInUSD);
|
|
654
|
-
}
|
|
655
|
-
return insufficientNetworkTokenForGas || isInsufficientSourceTokenForSwap;
|
|
656
|
-
},
|
|
657
|
-
// -- Calculations -------------------------------------- //
|
|
658
|
-
setTransactionDetails() {
|
|
659
|
-
const { toTokenAddress, toTokenDecimals } = this.getParams();
|
|
660
|
-
if (!toTokenAddress || !toTokenDecimals) {
|
|
661
|
-
return;
|
|
662
|
-
}
|
|
663
|
-
state.gasPriceInUSD = SwapCalculationUtil.getGasPriceInUSD(state.networkPrice, BigInt(state.gasFee), BigInt(INITIAL_GAS_LIMIT));
|
|
664
|
-
state.priceImpact = SwapCalculationUtil.getPriceImpact({
|
|
665
|
-
sourceTokenAmount: state.sourceTokenAmount,
|
|
666
|
-
sourceTokenPriceInUSD: state.sourceTokenPriceInUSD,
|
|
667
|
-
toTokenPriceInUSD: state.toTokenPriceInUSD,
|
|
668
|
-
toTokenAmount: state.toTokenAmount
|
|
669
|
-
});
|
|
670
|
-
state.maxSlippage = SwapCalculationUtil.getMaxSlippage(state.slippage, state.toTokenAmount);
|
|
671
|
-
state.providerFee = SwapCalculationUtil.getProviderFee(state.sourceTokenAmount);
|
|
672
|
-
}
|
|
673
|
-
};
|
|
674
|
-
//# sourceMappingURL=SwapController.js.map
|