@reown/appkit-core-react-native 1.0.2 → 1.1.1
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/lib/commonjs/controllers/AccountController.js +35 -1
- package/lib/commonjs/controllers/AccountController.js.map +1 -1
- package/lib/commonjs/controllers/ApiController.js +21 -9
- package/lib/commonjs/controllers/ApiController.js.map +1 -1
- package/lib/commonjs/controllers/BlockchainApiController.js +84 -0
- package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionController.js +33 -7
- package/lib/commonjs/controllers/ConnectionController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectorController.js +9 -1
- package/lib/commonjs/controllers/ConnectorController.js.map +1 -1
- package/lib/commonjs/controllers/EnsController.js +35 -0
- package/lib/commonjs/controllers/EnsController.js.map +1 -0
- package/lib/commonjs/controllers/EventsController.js +1 -15
- package/lib/commonjs/controllers/EventsController.js.map +1 -1
- package/lib/commonjs/controllers/ModalController.js +3 -1
- package/lib/commonjs/controllers/ModalController.js.map +1 -1
- package/lib/commonjs/controllers/NetworkController.js +20 -1
- package/lib/commonjs/controllers/NetworkController.js.map +1 -1
- package/lib/commonjs/controllers/OptionsController.js +14 -2
- package/lib/commonjs/controllers/OptionsController.js.map +1 -1
- package/lib/commonjs/controllers/RouterController.js +22 -1
- package/lib/commonjs/controllers/RouterController.js.map +1 -1
- package/lib/commonjs/controllers/SendController.js +176 -0
- package/lib/commonjs/controllers/SendController.js.map +1 -0
- package/lib/commonjs/controllers/SnackController.js +21 -1
- package/lib/commonjs/controllers/SnackController.js.map +1 -1
- package/lib/commonjs/controllers/SwapController.js +91 -0
- package/lib/commonjs/controllers/SwapController.js.map +1 -0
- package/lib/commonjs/controllers/TransactionsController.js +112 -0
- package/lib/commonjs/controllers/TransactionsController.js.map +1 -0
- package/lib/commonjs/controllers/WebviewController.js +52 -0
- package/lib/commonjs/controllers/WebviewController.js.map +1 -0
- package/lib/commonjs/index.js +42 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/ApiUtil.js +25 -0
- package/lib/commonjs/utils/ApiUtil.js.map +1 -0
- package/lib/commonjs/utils/ConstantsUtil.js +8 -1
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/CoreHelperUtil.js +33 -0
- package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
- package/lib/commonjs/utils/FetchUtil.js.map +1 -1
- package/lib/commonjs/utils/StorageUtil.js +24 -0
- package/lib/commonjs/utils/StorageUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapApiUtil.js +23 -0
- package/lib/commonjs/utils/SwapApiUtil.js.map +1 -0
- package/lib/commonjs/utils/SwapCalculationUtil.js +24 -0
- package/lib/commonjs/utils/SwapCalculationUtil.js.map +1 -0
- package/lib/commonjs/utils/TypeUtil.js +4 -0
- package/lib/module/controllers/AccountController.js +35 -1
- package/lib/module/controllers/AccountController.js.map +1 -1
- package/lib/module/controllers/ApiController.js +21 -9
- package/lib/module/controllers/ApiController.js.map +1 -1
- package/lib/module/controllers/BlockchainApiController.js +84 -0
- package/lib/module/controllers/BlockchainApiController.js.map +1 -1
- package/lib/module/controllers/ConnectionController.js +33 -7
- package/lib/module/controllers/ConnectionController.js.map +1 -1
- package/lib/module/controllers/ConnectorController.js +9 -1
- package/lib/module/controllers/ConnectorController.js.map +1 -1
- package/lib/module/controllers/EnsController.js +30 -0
- package/lib/module/controllers/EnsController.js.map +1 -0
- package/lib/module/controllers/EventsController.js +1 -15
- package/lib/module/controllers/EventsController.js.map +1 -1
- package/lib/module/controllers/ModalController.js +3 -1
- package/lib/module/controllers/ModalController.js.map +1 -1
- package/lib/module/controllers/NetworkController.js +20 -1
- package/lib/module/controllers/NetworkController.js.map +1 -1
- package/lib/module/controllers/OptionsController.js +14 -2
- package/lib/module/controllers/OptionsController.js.map +1 -1
- package/lib/module/controllers/RouterController.js +22 -1
- package/lib/module/controllers/RouterController.js.map +1 -1
- package/lib/module/controllers/SendController.js +171 -0
- package/lib/module/controllers/SendController.js.map +1 -0
- package/lib/module/controllers/SnackController.js +21 -1
- package/lib/module/controllers/SnackController.js.map +1 -1
- package/lib/module/controllers/SwapController.js +86 -0
- package/lib/module/controllers/SwapController.js.map +1 -0
- package/lib/module/controllers/TransactionsController.js +107 -0
- package/lib/module/controllers/TransactionsController.js.map +1 -0
- package/lib/module/controllers/WebviewController.js +47 -0
- package/lib/module/controllers/WebviewController.js.map +1 -0
- package/lib/module/index.js +7 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/ApiUtil.js +19 -0
- package/lib/module/utils/ApiUtil.js.map +1 -0
- package/lib/module/utils/ConstantsUtil.js +8 -1
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/CoreHelperUtil.js +33 -0
- package/lib/module/utils/CoreHelperUtil.js.map +1 -1
- package/lib/module/utils/FetchUtil.js.map +1 -1
- package/lib/module/utils/StorageUtil.js +24 -0
- package/lib/module/utils/StorageUtil.js.map +1 -1
- package/lib/module/utils/SwapApiUtil.js +17 -0
- package/lib/module/utils/SwapApiUtil.js.map +1 -0
- package/lib/module/utils/SwapCalculationUtil.js +19 -0
- package/lib/module/utils/SwapCalculationUtil.js.map +1 -0
- package/lib/module/utils/TypeUtil.js +1 -1
- package/lib/typescript/controllers/AccountController.d.ts +9 -1
- package/lib/typescript/controllers/AccountController.d.ts.map +1 -1
- package/lib/typescript/controllers/ApiController.d.ts +3 -1
- package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts +6 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionController.d.ts +18 -1
- package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectorController.d.ts +1 -1
- package/lib/typescript/controllers/ConnectorController.d.ts.map +1 -1
- package/lib/typescript/controllers/EnsController.d.ts +10 -0
- package/lib/typescript/controllers/EnsController.d.ts.map +1 -0
- package/lib/typescript/controllers/EventsController.d.ts +0 -6
- package/lib/typescript/controllers/EventsController.d.ts.map +1 -1
- package/lib/typescript/controllers/ModalController.d.ts.map +1 -1
- package/lib/typescript/controllers/NetworkController.d.ts +5 -0
- package/lib/typescript/controllers/NetworkController.d.ts.map +1 -1
- package/lib/typescript/controllers/OptionsController.d.ts +6 -2
- package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/RouterController.d.ts +13 -1
- package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
- package/lib/typescript/controllers/SendController.d.ts +41 -0
- package/lib/typescript/controllers/SendController.d.ts.map +1 -0
- package/lib/typescript/controllers/SnackController.d.ts +7 -0
- package/lib/typescript/controllers/SnackController.d.ts.map +1 -1
- package/lib/typescript/controllers/SwapController.d.ts +29 -0
- package/lib/typescript/controllers/SwapController.d.ts.map +1 -0
- package/lib/typescript/controllers/TransactionsController.d.ts +21 -0
- package/lib/typescript/controllers/TransactionsController.d.ts.map +1 -0
- package/lib/typescript/controllers/WebviewController.d.ts +21 -0
- package/lib/typescript/controllers/WebviewController.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +19 -26
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/ApiUtil.d.ts +6 -0
- package/lib/typescript/utils/ApiUtil.d.ts.map +1 -0
- package/lib/typescript/utils/ConstantsUtil.d.ts +3 -0
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/CoreHelperUtil.d.ts +6 -0
- package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
- package/lib/typescript/utils/FetchUtil.d.ts +3 -0
- package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
- package/lib/typescript/utils/StorageUtil.d.ts +5 -1
- package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapApiUtil.d.ts +4 -0
- package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -0
- package/lib/typescript/utils/SwapCalculationUtil.d.ts +5 -0
- package/lib/typescript/utils/SwapCalculationUtil.d.ts.map +1 -0
- package/lib/typescript/utils/TypeUtil.d.ts +257 -4
- package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
- package/package.json +5 -5
- package/readme.md +1 -1
- package/src/controllers/AccountController.ts +50 -2
- package/src/controllers/ApiController.ts +34 -23
- package/src/controllers/BlockchainApiController.ts +88 -1
- package/src/controllers/ConnectionController.ts +57 -8
- package/src/controllers/ConnectorController.ts +13 -1
- package/src/controllers/EnsController.ts +39 -0
- package/src/controllers/EventsController.ts +1 -13
- package/src/controllers/ModalController.ts +3 -1
- package/src/controllers/NetworkController.ts +34 -1
- package/src/controllers/OptionsController.ts +25 -4
- package/src/controllers/RouterController.ts +57 -10
- package/src/controllers/SendController.ts +234 -0
- package/src/controllers/SnackController.ts +28 -1
- package/src/controllers/SwapController.ts +108 -0
- package/src/controllers/TransactionsController.ts +142 -0
- package/src/controllers/WebviewController.ts +63 -0
- package/src/index.ts +43 -30
- package/src/utils/ApiUtil.ts +26 -0
- package/src/utils/ConstantsUtil.ts +13 -1
- package/src/utils/CoreHelperUtil.ts +38 -1
- package/src/utils/FetchUtil.ts +4 -0
- package/src/utils/StorageUtil.ts +31 -1
- package/src/utils/SwapApiUtil.ts +19 -0
- package/src/utils/SwapCalculationUtil.ts +21 -0
- package/src/utils/TypeUtil.ts +290 -4
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { proxy, ref } from 'valtio';
|
|
2
2
|
import type { CaipNetwork, CaipNetworkId } from '../utils/TypeUtil';
|
|
3
3
|
import { PublicStateController } from './PublicStateController';
|
|
4
|
+
import { NetworkUtil } from '@reown/appkit-common-react-native';
|
|
4
5
|
|
|
5
6
|
// -- Types --------------------------------------------- //
|
|
6
7
|
export interface NetworkControllerClient {
|
|
@@ -18,12 +19,14 @@ export interface NetworkControllerState {
|
|
|
18
19
|
caipNetwork?: CaipNetwork;
|
|
19
20
|
requestedCaipNetworks?: CaipNetwork[];
|
|
20
21
|
approvedCaipNetworkIds?: CaipNetworkId[];
|
|
22
|
+
smartAccountEnabledNetworks: number[];
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
// -- State --------------------------------------------- //
|
|
24
26
|
const state = proxy<NetworkControllerState>({
|
|
25
27
|
supportsAllNetworks: true,
|
|
26
|
-
isDefaultCaipNetwork: false
|
|
28
|
+
isDefaultCaipNetwork: false,
|
|
29
|
+
smartAccountEnabledNetworks: []
|
|
27
30
|
});
|
|
28
31
|
|
|
29
32
|
// -- Controller ---------------------------------------- //
|
|
@@ -57,12 +60,41 @@ export const NetworkController = {
|
|
|
57
60
|
state.requestedCaipNetworks = requestedNetworks;
|
|
58
61
|
},
|
|
59
62
|
|
|
63
|
+
setSmartAccountEnabledNetworks(
|
|
64
|
+
smartAccountEnabledNetworks: NetworkControllerState['smartAccountEnabledNetworks']
|
|
65
|
+
) {
|
|
66
|
+
state.smartAccountEnabledNetworks = smartAccountEnabledNetworks;
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
checkIfSmartAccountEnabled() {
|
|
70
|
+
const networkId = NetworkUtil.caipNetworkIdToNumber(state.caipNetwork?.id);
|
|
71
|
+
|
|
72
|
+
if (!networkId) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return Boolean(state.smartAccountEnabledNetworks?.includes(Number(networkId)));
|
|
77
|
+
},
|
|
78
|
+
|
|
60
79
|
async getApprovedCaipNetworksData() {
|
|
61
80
|
const data = await this._getClient().getApprovedCaipNetworksData();
|
|
62
81
|
state.supportsAllNetworks = data.supportsAllNetworks;
|
|
63
82
|
state.approvedCaipNetworkIds = data.approvedCaipNetworkIds;
|
|
64
83
|
},
|
|
65
84
|
|
|
85
|
+
getApprovedCaipNetworks() {
|
|
86
|
+
return state.approvedCaipNetworkIds
|
|
87
|
+
?.map(id => state.requestedCaipNetworks?.find(network => network.id === id))
|
|
88
|
+
.filter(Boolean) as CaipNetwork[];
|
|
89
|
+
},
|
|
90
|
+
|
|
91
|
+
getSmartAccountEnabledNetworks() {
|
|
92
|
+
return this.getApprovedCaipNetworks().filter(
|
|
93
|
+
network =>
|
|
94
|
+
state.smartAccountEnabledNetworks?.find(networkId => network.id === `eip155:${networkId}`)
|
|
95
|
+
);
|
|
96
|
+
},
|
|
97
|
+
|
|
66
98
|
async switchActiveNetwork(network: NetworkControllerState['caipNetwork']) {
|
|
67
99
|
await this._getClient().switchCaipNetwork(network);
|
|
68
100
|
state.caipNetwork = network;
|
|
@@ -75,5 +107,6 @@ export const NetworkController = {
|
|
|
75
107
|
}
|
|
76
108
|
state.approvedCaipNetworkIds = undefined;
|
|
77
109
|
state.supportsAllNetworks = true;
|
|
110
|
+
state.smartAccountEnabledNetworks = [];
|
|
78
111
|
}
|
|
79
112
|
};
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import { proxy, ref } from 'valtio';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
CustomWallet,
|
|
4
|
+
Features,
|
|
5
|
+
Metadata,
|
|
6
|
+
ProjectId,
|
|
7
|
+
SdkType,
|
|
8
|
+
SdkVersion,
|
|
9
|
+
Tokens
|
|
10
|
+
} from '../utils/TypeUtil';
|
|
11
|
+
import { ConstantsUtil } from '../utils/ConstantsUtil';
|
|
3
12
|
|
|
4
13
|
// -- Types --------------------------------------------- //
|
|
5
14
|
export interface ClipboardClient {
|
|
@@ -15,17 +24,21 @@ export interface OptionsControllerState {
|
|
|
15
24
|
customWallets?: CustomWallet[];
|
|
16
25
|
tokens?: Tokens;
|
|
17
26
|
enableAnalytics?: boolean;
|
|
18
|
-
sdkType:
|
|
27
|
+
sdkType: SdkType;
|
|
19
28
|
sdkVersion: SdkVersion;
|
|
20
29
|
metadata?: Metadata;
|
|
21
30
|
isSiweEnabled?: boolean;
|
|
31
|
+
features?: Features;
|
|
32
|
+
debug?: boolean;
|
|
22
33
|
}
|
|
23
34
|
|
|
24
35
|
// -- State --------------------------------------------- //
|
|
25
36
|
const state = proxy<OptionsControllerState>({
|
|
26
37
|
projectId: '',
|
|
27
38
|
sdkType: 'appkit',
|
|
28
|
-
sdkVersion: 'react-native-wagmi-undefined'
|
|
39
|
+
sdkVersion: 'react-native-wagmi-undefined',
|
|
40
|
+
features: ConstantsUtil.DEFAULT_FEATURES,
|
|
41
|
+
debug: false
|
|
29
42
|
});
|
|
30
43
|
|
|
31
44
|
// -- Controller ---------------------------------------- //
|
|
@@ -76,6 +89,14 @@ export const OptionsController = {
|
|
|
76
89
|
state.isSiweEnabled = isSiweEnabled;
|
|
77
90
|
},
|
|
78
91
|
|
|
92
|
+
setFeatures(features: OptionsControllerState['features']) {
|
|
93
|
+
state.features = { ...ConstantsUtil.DEFAULT_FEATURES, ...features };
|
|
94
|
+
},
|
|
95
|
+
|
|
96
|
+
setDebug(debug: OptionsControllerState['debug']) {
|
|
97
|
+
state.debug = debug;
|
|
98
|
+
},
|
|
99
|
+
|
|
79
100
|
isClipboardAvailable() {
|
|
80
101
|
return !!state._clipboardClient;
|
|
81
102
|
},
|
|
@@ -83,7 +104,7 @@ export const OptionsController = {
|
|
|
83
104
|
copyToClipboard(value: string) {
|
|
84
105
|
const client = state._clipboardClient;
|
|
85
106
|
if (client) {
|
|
86
|
-
client
|
|
107
|
+
client?.setString(value);
|
|
87
108
|
}
|
|
88
109
|
}
|
|
89
110
|
};
|
|
@@ -2,25 +2,46 @@ import { proxy } from 'valtio';
|
|
|
2
2
|
import type { WcWallet, CaipNetwork, Connector } from '../utils/TypeUtil';
|
|
3
3
|
|
|
4
4
|
// -- Types --------------------------------------------- //
|
|
5
|
+
type TransactionAction = {
|
|
6
|
+
goBack: boolean;
|
|
7
|
+
view: RouterControllerState['view'] | null;
|
|
8
|
+
close?: boolean;
|
|
9
|
+
replace?: boolean;
|
|
10
|
+
onSuccess?: () => void;
|
|
11
|
+
onCancel?: () => void;
|
|
12
|
+
};
|
|
13
|
+
|
|
5
14
|
export interface RouterControllerState {
|
|
6
15
|
view:
|
|
7
16
|
| 'Account'
|
|
17
|
+
| 'AccountDefault'
|
|
18
|
+
| 'AllWallets'
|
|
8
19
|
| 'Connect'
|
|
9
|
-
| '
|
|
20
|
+
| 'ConnectSocials'
|
|
10
21
|
| 'ConnectingExternal'
|
|
11
|
-
| '
|
|
12
|
-
| '
|
|
13
|
-
| '
|
|
14
|
-
| '
|
|
15
|
-
| '
|
|
16
|
-
| 'GetWallet'
|
|
22
|
+
| 'ConnectingSiwe'
|
|
23
|
+
| 'ConnectingSocial'
|
|
24
|
+
| 'ConnectingFarcaster'
|
|
25
|
+
| 'ConnectingWalletConnect'
|
|
26
|
+
| 'Create'
|
|
17
27
|
| 'EmailVerifyDevice'
|
|
18
28
|
| 'EmailVerifyOtp'
|
|
19
|
-
| '
|
|
29
|
+
| 'GetWallet'
|
|
30
|
+
| 'Networks'
|
|
31
|
+
| 'SwitchNetwork'
|
|
32
|
+
| 'Transactions'
|
|
20
33
|
| 'UpdateEmailPrimaryOtp'
|
|
21
34
|
| 'UpdateEmailSecondaryOtp'
|
|
35
|
+
| 'UpdateEmailWallet'
|
|
22
36
|
| 'UpgradeEmailWallet'
|
|
23
|
-
| '
|
|
37
|
+
| 'UpgradeToSmartAccount'
|
|
38
|
+
| 'WalletCompatibleNetworks'
|
|
39
|
+
| 'WalletReceive'
|
|
40
|
+
| 'WalletSend'
|
|
41
|
+
| 'WalletSendPreview'
|
|
42
|
+
| 'WalletSendSelectToken'
|
|
43
|
+
| 'WhatIsANetwork'
|
|
44
|
+
| 'WhatIsAWallet';
|
|
24
45
|
history: RouterControllerState['view'][];
|
|
25
46
|
data?: {
|
|
26
47
|
connector?: Connector;
|
|
@@ -29,12 +50,14 @@ export interface RouterControllerState {
|
|
|
29
50
|
email?: string;
|
|
30
51
|
newEmail?: string;
|
|
31
52
|
};
|
|
53
|
+
transactionStack: TransactionAction[];
|
|
32
54
|
}
|
|
33
55
|
|
|
34
56
|
// -- State --------------------------------------------- //
|
|
35
57
|
const state = proxy<RouterControllerState>({
|
|
36
58
|
view: 'Connect',
|
|
37
|
-
history: ['Connect']
|
|
59
|
+
history: ['Connect'],
|
|
60
|
+
transactionStack: []
|
|
38
61
|
});
|
|
39
62
|
|
|
40
63
|
// -- Controller ---------------------------------------- //
|
|
@@ -49,6 +72,30 @@ export const RouterController = {
|
|
|
49
72
|
}
|
|
50
73
|
},
|
|
51
74
|
|
|
75
|
+
pushTransactionStack(action: TransactionAction) {
|
|
76
|
+
state.transactionStack.push(action);
|
|
77
|
+
},
|
|
78
|
+
|
|
79
|
+
popTransactionStack(cancel?: boolean) {
|
|
80
|
+
const action = state.transactionStack.pop();
|
|
81
|
+
|
|
82
|
+
if (!action) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (cancel) {
|
|
87
|
+
this.goBack();
|
|
88
|
+
action?.onCancel?.();
|
|
89
|
+
} else {
|
|
90
|
+
if (action.goBack) {
|
|
91
|
+
this.goBack();
|
|
92
|
+
} else if (action.view) {
|
|
93
|
+
this.reset(action.view);
|
|
94
|
+
}
|
|
95
|
+
action?.onSuccess?.();
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
|
|
52
99
|
reset(view: RouterControllerState['view']) {
|
|
53
100
|
state.view = view;
|
|
54
101
|
state.history = [view];
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { subscribeKey as subKey } from 'valtio/vanilla/utils';
|
|
2
|
+
import { proxy, ref, subscribe as sub } from 'valtio/vanilla';
|
|
3
|
+
import { ContractUtil, type Balance } from '@reown/appkit-common-react-native';
|
|
4
|
+
import { AccountController } from './AccountController';
|
|
5
|
+
import { ConnectionController } from './ConnectionController';
|
|
6
|
+
import { SnackController } from './SnackController';
|
|
7
|
+
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
|
|
8
|
+
import { EventsController } from './EventsController';
|
|
9
|
+
import { NetworkController } from './NetworkController';
|
|
10
|
+
import { RouterController } from './RouterController';
|
|
11
|
+
|
|
12
|
+
// -- Types --------------------------------------------- //
|
|
13
|
+
export interface TxParams {
|
|
14
|
+
receiverAddress: string;
|
|
15
|
+
sendTokenAmount: number;
|
|
16
|
+
gasPrice: bigint;
|
|
17
|
+
decimals: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface ContractWriteParams {
|
|
21
|
+
receiverAddress: string;
|
|
22
|
+
tokenAddress: string;
|
|
23
|
+
sendTokenAmount: number;
|
|
24
|
+
decimals: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface SendControllerState {
|
|
28
|
+
token?: Balance;
|
|
29
|
+
sendTokenAmount?: number;
|
|
30
|
+
receiverAddress?: string;
|
|
31
|
+
receiverProfileName?: string;
|
|
32
|
+
receiverProfileImageUrl?: string;
|
|
33
|
+
gasPrice?: bigint;
|
|
34
|
+
gasPriceInUSD?: number;
|
|
35
|
+
loading: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type StateKey = keyof SendControllerState;
|
|
39
|
+
|
|
40
|
+
// -- State --------------------------------------------- //
|
|
41
|
+
const state = proxy<SendControllerState>({
|
|
42
|
+
loading: false
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
// -- Controller ---------------------------------------- //
|
|
46
|
+
export const SendController = {
|
|
47
|
+
state,
|
|
48
|
+
|
|
49
|
+
subscribe(callback: (newState: SendControllerState) => void) {
|
|
50
|
+
return sub(state, () => callback(state));
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
subscribeKey<K extends StateKey>(key: K, callback: (value: SendControllerState[K]) => void) {
|
|
54
|
+
return subKey(state, key, callback);
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
setToken(token: SendControllerState['token']) {
|
|
58
|
+
if (token) {
|
|
59
|
+
state.token = ref(token);
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
setTokenAmount(sendTokenAmount: SendControllerState['sendTokenAmount']) {
|
|
64
|
+
state.sendTokenAmount = sendTokenAmount;
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
setReceiverAddress(receiverAddress: SendControllerState['receiverAddress']) {
|
|
68
|
+
state.receiverAddress = receiverAddress;
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
setReceiverProfileImageUrl(
|
|
72
|
+
receiverProfileImageUrl: SendControllerState['receiverProfileImageUrl']
|
|
73
|
+
) {
|
|
74
|
+
state.receiverProfileImageUrl = receiverProfileImageUrl;
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
setReceiverProfileName(receiverProfileName: SendControllerState['receiverProfileName']) {
|
|
78
|
+
state.receiverProfileName = receiverProfileName;
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
setGasPrice(gasPrice: SendControllerState['gasPrice']) {
|
|
82
|
+
state.gasPrice = gasPrice;
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
setGasPriceInUsd(gasPriceInUSD: SendControllerState['gasPriceInUSD']) {
|
|
86
|
+
state.gasPriceInUSD = gasPriceInUSD;
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
setLoading(loading: SendControllerState['loading']) {
|
|
90
|
+
state.loading = loading;
|
|
91
|
+
},
|
|
92
|
+
|
|
93
|
+
sendToken() {
|
|
94
|
+
if (this.state.token?.address && this.state.sendTokenAmount && this.state.receiverAddress) {
|
|
95
|
+
state.loading = true;
|
|
96
|
+
EventsController.sendEvent({
|
|
97
|
+
type: 'track',
|
|
98
|
+
event: 'SEND_INITIATED',
|
|
99
|
+
properties: {
|
|
100
|
+
isSmartAccount: AccountController.state.preferredAccountType === 'smartAccount',
|
|
101
|
+
token: this.state.token.address,
|
|
102
|
+
amount: this.state.sendTokenAmount,
|
|
103
|
+
network: NetworkController.state.caipNetwork?.id || ''
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
this.sendERC20Token({
|
|
107
|
+
receiverAddress: this.state.receiverAddress,
|
|
108
|
+
tokenAddress: this.state.token.address,
|
|
109
|
+
sendTokenAmount: this.state.sendTokenAmount,
|
|
110
|
+
decimals: this.state.token.quantity.decimals
|
|
111
|
+
});
|
|
112
|
+
} else if (
|
|
113
|
+
this.state.receiverAddress &&
|
|
114
|
+
this.state.sendTokenAmount &&
|
|
115
|
+
this.state.gasPrice &&
|
|
116
|
+
this.state.token?.quantity.decimals
|
|
117
|
+
) {
|
|
118
|
+
state.loading = true;
|
|
119
|
+
EventsController.sendEvent({
|
|
120
|
+
type: 'track',
|
|
121
|
+
event: 'SEND_INITIATED',
|
|
122
|
+
properties: {
|
|
123
|
+
isSmartAccount: AccountController.state.preferredAccountType === 'smartAccount',
|
|
124
|
+
token: this.state.token?.symbol,
|
|
125
|
+
amount: this.state.sendTokenAmount,
|
|
126
|
+
network: NetworkController.state.caipNetwork?.id || ''
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
this.sendNativeToken({
|
|
130
|
+
receiverAddress: this.state.receiverAddress,
|
|
131
|
+
sendTokenAmount: this.state.sendTokenAmount,
|
|
132
|
+
gasPrice: this.state.gasPrice,
|
|
133
|
+
decimals: this.state.token.quantity.decimals
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
async sendNativeToken(params: TxParams) {
|
|
139
|
+
RouterController.pushTransactionStack({
|
|
140
|
+
view: 'Account',
|
|
141
|
+
goBack: false
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const to = params.receiverAddress as `0x${string}`;
|
|
145
|
+
const address = AccountController.state.address as `0x${string}`;
|
|
146
|
+
const value = ConnectionController.parseUnits(
|
|
147
|
+
params.sendTokenAmount.toString(),
|
|
148
|
+
Number(params.decimals)
|
|
149
|
+
);
|
|
150
|
+
const data = '0x';
|
|
151
|
+
|
|
152
|
+
try {
|
|
153
|
+
await ConnectionController.sendTransaction({
|
|
154
|
+
to,
|
|
155
|
+
address,
|
|
156
|
+
data,
|
|
157
|
+
value,
|
|
158
|
+
gasPrice: params.gasPrice
|
|
159
|
+
});
|
|
160
|
+
SnackController.showSuccess('Transaction started');
|
|
161
|
+
EventsController.sendEvent({
|
|
162
|
+
type: 'track',
|
|
163
|
+
event: 'SEND_SUCCESS',
|
|
164
|
+
properties: {
|
|
165
|
+
isSmartAccount: AccountController.state.preferredAccountType === 'smartAccount',
|
|
166
|
+
token: this.state.token?.symbol || '',
|
|
167
|
+
amount: params.sendTokenAmount,
|
|
168
|
+
network: NetworkController.state.caipNetwork?.id || ''
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
this.resetSend();
|
|
172
|
+
} catch (error) {
|
|
173
|
+
state.loading = false;
|
|
174
|
+
EventsController.sendEvent({
|
|
175
|
+
type: 'track',
|
|
176
|
+
event: 'SEND_ERROR',
|
|
177
|
+
properties: {
|
|
178
|
+
isSmartAccount: AccountController.state.preferredAccountType === 'smartAccount',
|
|
179
|
+
token: this.state.token?.symbol || '',
|
|
180
|
+
amount: params.sendTokenAmount,
|
|
181
|
+
network: NetworkController.state.caipNetwork?.id || ''
|
|
182
|
+
}
|
|
183
|
+
});
|
|
184
|
+
SnackController.showError('Something went wrong');
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
|
|
188
|
+
async sendERC20Token(params: ContractWriteParams) {
|
|
189
|
+
RouterController.pushTransactionStack({
|
|
190
|
+
view: 'Account',
|
|
191
|
+
goBack: false
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
const amount = ConnectionController.parseUnits(
|
|
195
|
+
params.sendTokenAmount.toString(),
|
|
196
|
+
Number(params.decimals)
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
try {
|
|
200
|
+
if (
|
|
201
|
+
AccountController.state.address &&
|
|
202
|
+
params.sendTokenAmount &&
|
|
203
|
+
params.receiverAddress &&
|
|
204
|
+
params.tokenAddress
|
|
205
|
+
) {
|
|
206
|
+
const tokenAddress = CoreHelperUtil.getPlainAddress(
|
|
207
|
+
params.tokenAddress as `${string}:${string}:${string}`
|
|
208
|
+
) as `0x${string}`;
|
|
209
|
+
await ConnectionController.writeContract({
|
|
210
|
+
fromAddress: AccountController.state.address as `0x${string}`,
|
|
211
|
+
tokenAddress,
|
|
212
|
+
receiverAddress: params.receiverAddress as `0x${string}`,
|
|
213
|
+
tokenAmount: amount,
|
|
214
|
+
method: 'transfer',
|
|
215
|
+
abi: ContractUtil.getERC20Abi(tokenAddress)
|
|
216
|
+
});
|
|
217
|
+
SnackController.showSuccess('Transaction started');
|
|
218
|
+
this.resetSend();
|
|
219
|
+
}
|
|
220
|
+
} catch (error) {
|
|
221
|
+
state.loading = false;
|
|
222
|
+
SnackController.showError('Something went wrong');
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
|
|
226
|
+
resetSend() {
|
|
227
|
+
state.token = undefined;
|
|
228
|
+
state.sendTokenAmount = undefined;
|
|
229
|
+
state.receiverAddress = undefined;
|
|
230
|
+
state.receiverProfileImageUrl = undefined;
|
|
231
|
+
state.receiverProfileName = undefined;
|
|
232
|
+
state.loading = false;
|
|
233
|
+
}
|
|
234
|
+
};
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import { proxy } from 'valtio';
|
|
2
|
+
import { OptionsController } from './OptionsController';
|
|
2
3
|
|
|
3
4
|
// -- Types --------------------------------------------- //
|
|
5
|
+
interface Message {
|
|
6
|
+
shortMessage: string;
|
|
7
|
+
longMessage?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
export interface SnackControllerState {
|
|
5
11
|
message: string;
|
|
6
12
|
variant: 'error' | 'success';
|
|
7
13
|
open: boolean;
|
|
14
|
+
long: boolean;
|
|
8
15
|
}
|
|
9
16
|
|
|
10
17
|
// -- State --------------------------------------------- //
|
|
11
18
|
const state = proxy<SnackControllerState>({
|
|
12
19
|
message: '',
|
|
13
20
|
variant: 'success',
|
|
14
|
-
open: false
|
|
21
|
+
open: false,
|
|
22
|
+
long: false
|
|
15
23
|
});
|
|
16
24
|
|
|
17
25
|
// -- Controller ---------------------------------------- //
|
|
@@ -30,7 +38,26 @@ export const SnackController = {
|
|
|
30
38
|
state.open = true;
|
|
31
39
|
},
|
|
32
40
|
|
|
41
|
+
showInternalError(error: Message) {
|
|
42
|
+
const { debug } = OptionsController.state;
|
|
43
|
+
|
|
44
|
+
if (debug) {
|
|
45
|
+
state.message = error.shortMessage;
|
|
46
|
+
state.variant = 'error';
|
|
47
|
+
state.open = true;
|
|
48
|
+
state.long = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (error.longMessage) {
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
console.error(error.longMessage);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
|
|
33
57
|
hide() {
|
|
34
58
|
state.open = false;
|
|
59
|
+
state.long = false;
|
|
60
|
+
state.message = '';
|
|
61
|
+
state.variant = 'success';
|
|
35
62
|
}
|
|
36
63
|
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { subscribeKey as subKey } from 'valtio/utils';
|
|
2
|
+
import { proxy, subscribe as sub } from 'valtio';
|
|
3
|
+
|
|
4
|
+
import { ConstantsUtil } from '../utils/ConstantsUtil';
|
|
5
|
+
import { SwapApiUtil } from '../utils/SwapApiUtil';
|
|
6
|
+
import { NetworkController } from './NetworkController';
|
|
7
|
+
import { BlockchainApiController } from './BlockchainApiController';
|
|
8
|
+
import { OptionsController } from './OptionsController';
|
|
9
|
+
import { SwapCalculationUtil } from '../utils/SwapCalculationUtil';
|
|
10
|
+
|
|
11
|
+
// -- Constants ---------------------------------------- //
|
|
12
|
+
export const INITIAL_GAS_LIMIT = 150000;
|
|
13
|
+
export const TO_AMOUNT_DECIMALS = 6;
|
|
14
|
+
|
|
15
|
+
// -- Types --------------------------------------------- //
|
|
16
|
+
|
|
17
|
+
export interface SwapControllerState {
|
|
18
|
+
// Input values
|
|
19
|
+
networkPrice: string;
|
|
20
|
+
networkTokenSymbol: string;
|
|
21
|
+
|
|
22
|
+
// Tokens
|
|
23
|
+
tokensPriceMap: Record<string, number>;
|
|
24
|
+
|
|
25
|
+
// Calculations
|
|
26
|
+
gasFee: string;
|
|
27
|
+
gasPriceInUSD?: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
type StateKey = keyof SwapControllerState;
|
|
31
|
+
|
|
32
|
+
// -- State --------------------------------------------- //
|
|
33
|
+
const initialState: SwapControllerState = {
|
|
34
|
+
// Input values
|
|
35
|
+
networkPrice: '0',
|
|
36
|
+
networkTokenSymbol: '',
|
|
37
|
+
|
|
38
|
+
// Tokens
|
|
39
|
+
tokensPriceMap: {},
|
|
40
|
+
|
|
41
|
+
// Calculations
|
|
42
|
+
gasFee: '0',
|
|
43
|
+
gasPriceInUSD: 0
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const state = proxy<SwapControllerState>(initialState);
|
|
47
|
+
|
|
48
|
+
// -- Controller ---------------------------------------- //
|
|
49
|
+
export const SwapController = {
|
|
50
|
+
state,
|
|
51
|
+
|
|
52
|
+
subscribe(callback: (newState: SwapControllerState) => void) {
|
|
53
|
+
return sub(state, () => callback(state));
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
subscribeKey<K extends StateKey>(key: K, callback: (value: SwapControllerState[K]) => void) {
|
|
57
|
+
return subKey(state, key, callback);
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
getParams() {
|
|
61
|
+
const caipNetwork = NetworkController.state.caipNetwork;
|
|
62
|
+
const networkAddress = `${caipNetwork?.id}:${ConstantsUtil.NATIVE_TOKEN_ADDRESS}`;
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
networkAddress
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
resetState() {
|
|
70
|
+
state.tokensPriceMap = initialState.tokensPriceMap;
|
|
71
|
+
state.networkPrice = initialState.networkPrice;
|
|
72
|
+
state.networkTokenSymbol = initialState.networkTokenSymbol;
|
|
73
|
+
},
|
|
74
|
+
|
|
75
|
+
//this
|
|
76
|
+
async getNetworkTokenPrice() {
|
|
77
|
+
const { networkAddress } = this.getParams();
|
|
78
|
+
|
|
79
|
+
const response = await BlockchainApiController.fetchTokenPrice({
|
|
80
|
+
projectId: OptionsController.state.projectId,
|
|
81
|
+
addresses: [networkAddress]
|
|
82
|
+
});
|
|
83
|
+
const token = response?.fungibles?.[0];
|
|
84
|
+
const price = token?.price.toString() || '0';
|
|
85
|
+
state.tokensPriceMap[networkAddress] = parseFloat(price);
|
|
86
|
+
state.networkTokenSymbol = token?.symbol || '';
|
|
87
|
+
state.networkPrice = price;
|
|
88
|
+
},
|
|
89
|
+
|
|
90
|
+
//this
|
|
91
|
+
async getInitialGasPrice() {
|
|
92
|
+
const res = await SwapApiUtil.fetchGasPrice();
|
|
93
|
+
|
|
94
|
+
if (!res) {
|
|
95
|
+
return { gasPrice: null, gasPriceInUsd: null };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const value = res.standard;
|
|
99
|
+
const gasFee = BigInt(value);
|
|
100
|
+
const gasLimit = BigInt(INITIAL_GAS_LIMIT);
|
|
101
|
+
const gasPrice = SwapCalculationUtil.getGasPriceInUSD(state.networkPrice, gasLimit, gasFee);
|
|
102
|
+
|
|
103
|
+
state.gasFee = value;
|
|
104
|
+
state.gasPriceInUSD = gasPrice;
|
|
105
|
+
|
|
106
|
+
return { gasPrice: gasFee, gasPriceInUSD: state.gasPriceInUSD };
|
|
107
|
+
}
|
|
108
|
+
};
|