@reown/appkit-core-react-native 0.0.0-fix-email-login-20241010173711 → 0.0.0-fix-token-balance-20241017200828
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 +25 -1
- package/lib/commonjs/controllers/AccountController.js.map +1 -1
- package/lib/commonjs/controllers/ApiController.js +3 -3
- 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 +19 -0
- package/lib/commonjs/controllers/ConnectionController.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 +3 -0
- package/lib/commonjs/controllers/NetworkController.js.map +1 -1
- package/lib/commonjs/controllers/OptionsController.js +1 -1
- 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/SwapController.js +91 -0
- package/lib/commonjs/controllers/SwapController.js.map +1 -0
- package/lib/commonjs/controllers/TransactionsController.js +111 -0
- package/lib/commonjs/controllers/TransactionsController.js.map +1 -0
- package/lib/commonjs/index.js +35 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/ApiUtil.js +18 -0
- package/lib/commonjs/utils/ApiUtil.js.map +1 -0
- package/lib/commonjs/utils/ConstantsUtil.js +2 -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/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 +25 -1
- package/lib/module/controllers/AccountController.js.map +1 -1
- package/lib/module/controllers/ApiController.js +3 -3
- 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 +19 -0
- package/lib/module/controllers/ConnectionController.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 +3 -0
- package/lib/module/controllers/NetworkController.js.map +1 -1
- package/lib/module/controllers/OptionsController.js +1 -1
- 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/SwapController.js +86 -0
- package/lib/module/controllers/SwapController.js.map +1 -0
- package/lib/module/controllers/TransactionsController.js +106 -0
- package/lib/module/controllers/TransactionsController.js.map +1 -0
- package/lib/module/index.js +6 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/ApiUtil.js +12 -0
- package/lib/module/utils/ApiUtil.js.map +1 -0
- package/lib/module/utils/ConstantsUtil.js +2 -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/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 +4 -0
- package/lib/typescript/controllers/AccountController.d.ts.map +1 -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 +13 -1
- package/lib/typescript/controllers/ConnectionController.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 +1 -0
- package/lib/typescript/controllers/NetworkController.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/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/index.d.ts +18 -26
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/ApiUtil.d.ts +5 -0
- package/lib/typescript/utils/ApiUtil.d.ts.map +1 -0
- package/lib/typescript/utils/ConstantsUtil.d.ts +1 -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/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 +137 -0
- 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 +35 -1
- package/src/controllers/ApiController.ts +3 -7
- package/src/controllers/BlockchainApiController.ts +88 -1
- package/src/controllers/ConnectionController.ts +37 -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 +6 -0
- package/src/controllers/OptionsController.ts +1 -1
- package/src/controllers/RouterController.ts +52 -10
- package/src/controllers/SendController.ts +234 -0
- package/src/controllers/SwapController.ts +108 -0
- package/src/controllers/TransactionsController.ts +141 -0
- package/src/index.ts +41 -30
- package/src/utils/ApiUtil.ts +18 -0
- package/src/utils/ConstantsUtil.ts +3 -1
- package/src/utils/CoreHelperUtil.ts +38 -1
- package/src/utils/FetchUtil.ts +4 -0
- package/src/utils/SwapApiUtil.ts +19 -0
- package/src/utils/SwapCalculationUtil.ts +21 -0
- package/src/utils/TypeUtil.ts +169 -0
|
@@ -2,25 +2,41 @@ 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
|
-
| 'ConnectingWalletConnect'
|
|
10
20
|
| 'ConnectingExternal'
|
|
11
|
-
| '
|
|
12
|
-
| '
|
|
13
|
-
| 'AllWallets'
|
|
14
|
-
| 'WhatIsAWallet'
|
|
15
|
-
| 'WhatIsANetwork'
|
|
16
|
-
| 'GetWallet'
|
|
21
|
+
| 'ConnectingSiwe'
|
|
22
|
+
| 'ConnectingWalletConnect'
|
|
17
23
|
| 'EmailVerifyDevice'
|
|
18
24
|
| 'EmailVerifyOtp'
|
|
19
|
-
| '
|
|
25
|
+
| 'GetWallet'
|
|
26
|
+
| 'Networks'
|
|
27
|
+
| 'SwitchNetwork'
|
|
28
|
+
| 'Transactions'
|
|
20
29
|
| 'UpdateEmailPrimaryOtp'
|
|
21
30
|
| 'UpdateEmailSecondaryOtp'
|
|
31
|
+
| 'UpdateEmailWallet'
|
|
22
32
|
| 'UpgradeEmailWallet'
|
|
23
|
-
| '
|
|
33
|
+
| 'WalletCompatibleNetworks'
|
|
34
|
+
| 'WalletReceive'
|
|
35
|
+
| 'WalletSend'
|
|
36
|
+
| 'WalletSendPreview'
|
|
37
|
+
| 'WalletSendSelectToken'
|
|
38
|
+
| 'WhatIsANetwork'
|
|
39
|
+
| 'WhatIsAWallet';
|
|
24
40
|
history: RouterControllerState['view'][];
|
|
25
41
|
data?: {
|
|
26
42
|
connector?: Connector;
|
|
@@ -29,12 +45,14 @@ export interface RouterControllerState {
|
|
|
29
45
|
email?: string;
|
|
30
46
|
newEmail?: string;
|
|
31
47
|
};
|
|
48
|
+
transactionStack: TransactionAction[];
|
|
32
49
|
}
|
|
33
50
|
|
|
34
51
|
// -- State --------------------------------------------- //
|
|
35
52
|
const state = proxy<RouterControllerState>({
|
|
36
53
|
view: 'Connect',
|
|
37
|
-
history: ['Connect']
|
|
54
|
+
history: ['Connect'],
|
|
55
|
+
transactionStack: []
|
|
38
56
|
});
|
|
39
57
|
|
|
40
58
|
// -- Controller ---------------------------------------- //
|
|
@@ -49,6 +67,30 @@ export const RouterController = {
|
|
|
49
67
|
}
|
|
50
68
|
},
|
|
51
69
|
|
|
70
|
+
pushTransactionStack(action: TransactionAction) {
|
|
71
|
+
state.transactionStack.push(action);
|
|
72
|
+
},
|
|
73
|
+
|
|
74
|
+
popTransactionStack(cancel?: boolean) {
|
|
75
|
+
const action = state.transactionStack.pop();
|
|
76
|
+
|
|
77
|
+
if (!action) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (cancel) {
|
|
82
|
+
this.goBack();
|
|
83
|
+
action?.onCancel?.();
|
|
84
|
+
} else {
|
|
85
|
+
if (action.goBack) {
|
|
86
|
+
this.goBack();
|
|
87
|
+
} else if (action.view) {
|
|
88
|
+
this.reset(action.view);
|
|
89
|
+
}
|
|
90
|
+
action?.onSuccess?.();
|
|
91
|
+
}
|
|
92
|
+
},
|
|
93
|
+
|
|
52
94
|
reset(view: RouterControllerState['view']) {
|
|
53
95
|
state.view = view;
|
|
54
96
|
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: false,
|
|
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: false,
|
|
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: false,
|
|
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: false,
|
|
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
|
+
};
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import type { Transaction } from '@reown/appkit-common-react-native';
|
|
2
|
+
import { proxy, subscribe as sub } from 'valtio/vanilla';
|
|
3
|
+
import { OptionsController } from './OptionsController';
|
|
4
|
+
import { EventsController } from './EventsController';
|
|
5
|
+
import { SnackController } from './SnackController';
|
|
6
|
+
import { NetworkController } from './NetworkController';
|
|
7
|
+
import { BlockchainApiController } from './BlockchainApiController';
|
|
8
|
+
|
|
9
|
+
// -- Types --------------------------------------------- //
|
|
10
|
+
type TransactionByMonthMap = Record<string, Transaction[]>;
|
|
11
|
+
type TransactionByYearMap = Record<string, TransactionByMonthMap>;
|
|
12
|
+
|
|
13
|
+
export interface TransactionsControllerState {
|
|
14
|
+
transactions: Transaction[];
|
|
15
|
+
loading: boolean;
|
|
16
|
+
empty: boolean;
|
|
17
|
+
next: string | undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// -- State --------------------------------------------- //
|
|
21
|
+
const state = proxy<TransactionsControllerState>({
|
|
22
|
+
transactions: [],
|
|
23
|
+
loading: false,
|
|
24
|
+
empty: false,
|
|
25
|
+
next: undefined
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// -- Controller ---------------------------------------- //
|
|
29
|
+
export const TransactionsController = {
|
|
30
|
+
state,
|
|
31
|
+
|
|
32
|
+
subscribe(callback: (newState: TransactionsControllerState) => void) {
|
|
33
|
+
return sub(state, () => callback(state));
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
async fetchTransactions(accountAddress?: string, reset?: boolean) {
|
|
37
|
+
const { projectId } = OptionsController.state;
|
|
38
|
+
|
|
39
|
+
if (!projectId || !accountAddress) {
|
|
40
|
+
throw new Error("Transactions can't be fetched without a projectId and an accountAddress");
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
state.loading = true;
|
|
44
|
+
|
|
45
|
+
if (reset) {
|
|
46
|
+
state.next = undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
const response = await BlockchainApiController.fetchTransactions({
|
|
51
|
+
account: accountAddress,
|
|
52
|
+
projectId,
|
|
53
|
+
cursor: state.next
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const nonSpamTransactions = this.filterSpamTransactions(response?.data ?? []);
|
|
57
|
+
let filteredTransactions = [...state.transactions, ...nonSpamTransactions];
|
|
58
|
+
|
|
59
|
+
if (reset) {
|
|
60
|
+
filteredTransactions = nonSpamTransactions;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
state.loading = false;
|
|
64
|
+
|
|
65
|
+
state.transactions = filteredTransactions;
|
|
66
|
+
|
|
67
|
+
state.empty = nonSpamTransactions.length === 0;
|
|
68
|
+
state.next = response?.next ? response.next : undefined;
|
|
69
|
+
} catch (error) {
|
|
70
|
+
EventsController.sendEvent({
|
|
71
|
+
type: 'track',
|
|
72
|
+
event: 'ERROR_FETCH_TRANSACTIONS',
|
|
73
|
+
properties: {
|
|
74
|
+
address: accountAddress,
|
|
75
|
+
projectId,
|
|
76
|
+
cursor: state.next,
|
|
77
|
+
isSmartAccount: false
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
SnackController.showError('Failed to fetch transactions');
|
|
81
|
+
state.loading = false;
|
|
82
|
+
state.empty = true;
|
|
83
|
+
state.next = undefined;
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
getTransactionsByYearAndMonth(transactions: Transaction[]) {
|
|
88
|
+
const grouped: TransactionByYearMap = {};
|
|
89
|
+
let filteredTransactions = this.filterByConnectedChain(transactions);
|
|
90
|
+
|
|
91
|
+
filteredTransactions.forEach(transaction => {
|
|
92
|
+
const year = new Date(transaction.metadata.minedAt).getFullYear();
|
|
93
|
+
const month = new Date(transaction.metadata.minedAt).getMonth();
|
|
94
|
+
|
|
95
|
+
const yearTransactions = grouped[year] ?? {};
|
|
96
|
+
const monthTransactions = yearTransactions[month] ?? [];
|
|
97
|
+
|
|
98
|
+
// If there's a transaction with the same id, remove the old one
|
|
99
|
+
const newMonthTransactions = monthTransactions.filter(tx => tx.id !== transaction.id);
|
|
100
|
+
|
|
101
|
+
grouped[year] = {
|
|
102
|
+
...yearTransactions,
|
|
103
|
+
[month]: [...newMonthTransactions, transaction].sort(
|
|
104
|
+
(a, b) => new Date(b.metadata.minedAt).getTime() - new Date(a.metadata.minedAt).getTime()
|
|
105
|
+
)
|
|
106
|
+
};
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
return grouped;
|
|
110
|
+
},
|
|
111
|
+
|
|
112
|
+
filterSpamTransactions(transactions: Transaction[]) {
|
|
113
|
+
return transactions.filter(transaction => {
|
|
114
|
+
const isAllSpam = transaction.transfers.every(
|
|
115
|
+
transfer => transfer.nft_info?.flags.is_spam === true
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
return !isAllSpam;
|
|
119
|
+
});
|
|
120
|
+
},
|
|
121
|
+
|
|
122
|
+
filterByConnectedChain(transactions: Transaction[]) {
|
|
123
|
+
const chainId = NetworkController.state.caipNetwork?.id;
|
|
124
|
+
const filteredTransactions = transactions.filter(
|
|
125
|
+
transaction => transaction.metadata.chain === chainId
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return filteredTransactions;
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
clearCursor() {
|
|
132
|
+
state.next = undefined;
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
resetTransactions() {
|
|
136
|
+
state.transactions = [];
|
|
137
|
+
state.loading = false;
|
|
138
|
+
state.empty = false;
|
|
139
|
+
state.next = undefined;
|
|
140
|
+
}
|
|
141
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,52 +1,63 @@
|
|
|
1
1
|
// -- Controllers -------------------------------------------------------------
|
|
2
|
-
export {
|
|
3
|
-
|
|
2
|
+
export {
|
|
3
|
+
ModalController,
|
|
4
|
+
type ModalControllerArguments,
|
|
5
|
+
type ModalControllerState
|
|
6
|
+
} from './controllers/ModalController';
|
|
4
7
|
|
|
5
|
-
export { RouterController } from './controllers/RouterController';
|
|
6
|
-
export type { RouterControllerState } from './controllers/RouterController';
|
|
8
|
+
export { RouterController, type RouterControllerState } from './controllers/RouterController';
|
|
7
9
|
|
|
8
|
-
export { AccountController } from './controllers/AccountController';
|
|
9
|
-
export type { AccountControllerState } from './controllers/AccountController';
|
|
10
|
+
export { AccountController, type AccountControllerState } from './controllers/AccountController';
|
|
10
11
|
|
|
11
|
-
export {
|
|
12
|
-
|
|
13
|
-
NetworkControllerClient,
|
|
14
|
-
NetworkControllerState
|
|
12
|
+
export {
|
|
13
|
+
NetworkController,
|
|
14
|
+
type NetworkControllerClient,
|
|
15
|
+
type NetworkControllerState
|
|
15
16
|
} from './controllers/NetworkController';
|
|
16
17
|
|
|
17
|
-
export {
|
|
18
|
-
|
|
19
|
-
ConnectionControllerClient,
|
|
20
|
-
ConnectionControllerState
|
|
18
|
+
export {
|
|
19
|
+
ConnectionController,
|
|
20
|
+
type ConnectionControllerClient,
|
|
21
|
+
type ConnectionControllerState
|
|
21
22
|
} from './controllers/ConnectionController';
|
|
22
23
|
|
|
23
|
-
export {
|
|
24
|
-
|
|
24
|
+
export {
|
|
25
|
+
ConnectorController,
|
|
26
|
+
type ConnectorControllerState
|
|
27
|
+
} from './controllers/ConnectorController';
|
|
25
28
|
|
|
26
|
-
export { SnackController } from './controllers/SnackController';
|
|
27
|
-
export type { SnackControllerState } from './controllers/SnackController';
|
|
29
|
+
export { SnackController, type SnackControllerState } from './controllers/SnackController';
|
|
28
30
|
|
|
29
|
-
export { ApiController } from './controllers/ApiController';
|
|
30
|
-
export type { ApiControllerState } from './controllers/ApiController';
|
|
31
|
+
export { ApiController, type ApiControllerState } from './controllers/ApiController';
|
|
31
32
|
|
|
32
|
-
export { AssetController } from './controllers/AssetController';
|
|
33
|
-
export type { AssetControllerState } from './controllers/AssetController';
|
|
33
|
+
export { AssetController, type AssetControllerState } from './controllers/AssetController';
|
|
34
34
|
|
|
35
|
-
export { ThemeController } from './controllers/ThemeController';
|
|
36
|
-
export type { ThemeControllerState } from './controllers/ThemeController';
|
|
35
|
+
export { ThemeController, type ThemeControllerState } from './controllers/ThemeController';
|
|
37
36
|
|
|
38
|
-
export { OptionsController } from './controllers/OptionsController';
|
|
39
|
-
export type { OptionsControllerState } from './controllers/OptionsController';
|
|
37
|
+
export { OptionsController, type OptionsControllerState } from './controllers/OptionsController';
|
|
40
38
|
|
|
41
|
-
export {
|
|
42
|
-
|
|
39
|
+
export {
|
|
40
|
+
PublicStateController,
|
|
41
|
+
type PublicStateControllerState
|
|
42
|
+
} from './controllers/PublicStateController';
|
|
43
43
|
|
|
44
44
|
export { BlockchainApiController } from './controllers/BlockchainApiController';
|
|
45
45
|
|
|
46
|
-
export {
|
|
47
|
-
|
|
46
|
+
export { SwapController, type SwapControllerState } from './controllers/SwapController';
|
|
47
|
+
|
|
48
|
+
export { EventsController, type EventsControllerState } from './controllers/EventsController';
|
|
49
|
+
|
|
50
|
+
export { EnsController, type EnsControllerState } from './controllers/EnsController';
|
|
51
|
+
|
|
52
|
+
export {
|
|
53
|
+
TransactionsController,
|
|
54
|
+
type TransactionsControllerState
|
|
55
|
+
} from './controllers/TransactionsController';
|
|
56
|
+
|
|
57
|
+
export { SendController, type SendControllerState } from './controllers/SendController';
|
|
48
58
|
|
|
49
59
|
// -- Utils -------------------------------------------------------------------
|
|
60
|
+
export { ApiUtil } from './utils/ApiUtil';
|
|
50
61
|
export { AssetUtil } from './utils/AssetUtil';
|
|
51
62
|
export { ConstantsUtil } from './utils/ConstantsUtil';
|
|
52
63
|
export { CoreHelperUtil } from './utils/CoreHelperUtil';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import { CoreHelperUtil } from './CoreHelperUtil';
|
|
3
|
+
|
|
4
|
+
export const ApiUtil = {
|
|
5
|
+
getOrigin() {
|
|
6
|
+
return CoreHelperUtil.getBundleId();
|
|
7
|
+
},
|
|
8
|
+
|
|
9
|
+
getUserAgent() {
|
|
10
|
+
const reactNativeVersion = [
|
|
11
|
+
Platform.constants.reactNativeVersion.major,
|
|
12
|
+
Platform.constants.reactNativeVersion.minor,
|
|
13
|
+
Platform.constants.reactNativeVersion.patch
|
|
14
|
+
].join('.');
|
|
15
|
+
|
|
16
|
+
return `${Platform.OS}-${Platform.Version}@rn-${reactNativeVersion}`;
|
|
17
|
+
}
|
|
18
|
+
};
|