@reown/appkit-core-react-native 1.2.4 → 2.0.0-alpha.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/ApiController.js +11 -11
- package/lib/commonjs/controllers/ApiController.js.map +1 -1
- package/lib/commonjs/controllers/BlockchainApiController.js +187 -14
- package/lib/commonjs/controllers/BlockchainApiController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionController.js +7 -1
- package/lib/commonjs/controllers/ConnectionController.js.map +1 -1
- package/lib/commonjs/controllers/ConnectionsController.js +205 -0
- package/lib/commonjs/controllers/ConnectionsController.js.map +1 -0
- package/lib/commonjs/controllers/ConnectorController.js +1 -0
- package/lib/commonjs/controllers/ConnectorController.js.map +1 -1
- package/lib/commonjs/controllers/OnRampController.js +421 -0
- package/lib/commonjs/controllers/OnRampController.js.map +1 -0
- package/lib/commonjs/controllers/OptionsController.js +11 -3
- package/lib/commonjs/controllers/OptionsController.js.map +1 -1
- package/lib/commonjs/controllers/RouterController.js +3 -2
- package/lib/commonjs/controllers/RouterController.js.map +1 -1
- package/lib/commonjs/controllers/SendController.js +5 -5
- package/lib/commonjs/controllers/SendController.js.map +1 -1
- package/lib/commonjs/controllers/SwapController.js +51 -40
- package/lib/commonjs/controllers/SwapController.js.map +1 -1
- package/lib/commonjs/controllers/ThemeController.js +9 -1
- package/lib/commonjs/controllers/ThemeController.js.map +1 -1
- package/lib/commonjs/controllers/TransactionsController.js +2 -2
- package/lib/commonjs/controllers/TransactionsController.js.map +1 -1
- package/lib/commonjs/index.js +10 -3
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/AssetUtil.js +5 -6
- package/lib/commonjs/utils/AssetUtil.js.map +1 -1
- package/lib/commonjs/utils/ConstantsUtil.js +599 -6
- package/lib/commonjs/utils/ConstantsUtil.js.map +1 -1
- package/lib/commonjs/utils/CoreHelperUtil.js +28 -0
- package/lib/commonjs/utils/CoreHelperUtil.js.map +1 -1
- package/lib/commonjs/utils/FetchUtil.js +15 -4
- package/lib/commonjs/utils/FetchUtil.js.map +1 -1
- package/lib/commonjs/utils/StorageUtil.js +218 -0
- package/lib/commonjs/utils/StorageUtil.js.map +1 -1
- package/lib/commonjs/utils/SwapApiUtil.js +19 -10
- package/lib/commonjs/utils/SwapApiUtil.js.map +1 -1
- package/lib/module/controllers/ApiController.js +11 -11
- package/lib/module/controllers/ApiController.js.map +1 -1
- package/lib/module/controllers/BlockchainApiController.js +187 -14
- package/lib/module/controllers/BlockchainApiController.js.map +1 -1
- package/lib/module/controllers/ConnectionController.js +7 -1
- package/lib/module/controllers/ConnectionController.js.map +1 -1
- package/lib/module/controllers/ConnectionsController.js +200 -0
- package/lib/module/controllers/ConnectionsController.js.map +1 -0
- package/lib/module/controllers/ConnectorController.js +1 -0
- package/lib/module/controllers/ConnectorController.js.map +1 -1
- package/lib/module/controllers/OnRampController.js +416 -0
- package/lib/module/controllers/OnRampController.js.map +1 -0
- package/lib/module/controllers/OptionsController.js +11 -3
- package/lib/module/controllers/OptionsController.js.map +1 -1
- package/lib/module/controllers/RouterController.js +3 -2
- package/lib/module/controllers/RouterController.js.map +1 -1
- package/lib/module/controllers/SendController.js +5 -5
- package/lib/module/controllers/SendController.js.map +1 -1
- package/lib/module/controllers/SwapController.js +51 -40
- package/lib/module/controllers/SwapController.js.map +1 -1
- package/lib/module/controllers/ThemeController.js +9 -1
- package/lib/module/controllers/ThemeController.js.map +1 -1
- package/lib/module/controllers/TransactionsController.js +2 -2
- package/lib/module/controllers/TransactionsController.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/AssetUtil.js +5 -6
- package/lib/module/utils/AssetUtil.js.map +1 -1
- package/lib/module/utils/ConstantsUtil.js +598 -5
- package/lib/module/utils/ConstantsUtil.js.map +1 -1
- package/lib/module/utils/CoreHelperUtil.js +26 -0
- package/lib/module/utils/CoreHelperUtil.js.map +1 -1
- package/lib/module/utils/FetchUtil.js +15 -4
- package/lib/module/utils/FetchUtil.js.map +1 -1
- package/lib/module/utils/StorageUtil.js +219 -0
- package/lib/module/utils/StorageUtil.js.map +1 -1
- package/lib/module/utils/SwapApiUtil.js +19 -10
- package/lib/module/utils/SwapApiUtil.js.map +1 -1
- package/lib/typescript/controllers/AccountController.d.ts +2 -2
- package/lib/typescript/controllers/AccountController.d.ts.map +1 -1
- package/lib/typescript/controllers/ApiController.d.ts +1 -1
- package/lib/typescript/controllers/ApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/BlockchainApiController.d.ts +27 -2
- package/lib/typescript/controllers/BlockchainApiController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionController.d.ts +2 -0
- package/lib/typescript/controllers/ConnectionController.d.ts.map +1 -1
- package/lib/typescript/controllers/ConnectionsController.d.ts +43 -0
- package/lib/typescript/controllers/ConnectionsController.d.ts.map +1 -0
- package/lib/typescript/controllers/ConnectorController.d.ts.map +1 -1
- package/lib/typescript/controllers/NetworkController.d.ts +1 -1
- package/lib/typescript/controllers/NetworkController.d.ts.map +1 -1
- package/lib/typescript/controllers/OnRampController.d.ts +54 -0
- package/lib/typescript/controllers/OnRampController.d.ts.map +1 -0
- package/lib/typescript/controllers/OptionsController.d.ts +7 -2
- package/lib/typescript/controllers/OptionsController.d.ts.map +1 -1
- package/lib/typescript/controllers/PublicStateController.d.ts +1 -1
- package/lib/typescript/controllers/PublicStateController.d.ts.map +1 -1
- package/lib/typescript/controllers/RouterController.d.ts +5 -3
- package/lib/typescript/controllers/RouterController.d.ts.map +1 -1
- package/lib/typescript/controllers/SwapController.d.ts +1 -1
- package/lib/typescript/controllers/SwapController.d.ts.map +1 -1
- package/lib/typescript/controllers/ThemeController.d.ts +1 -1
- package/lib/typescript/controllers/ThemeController.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +2 -1
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/AssetUtil.d.ts +2 -2
- package/lib/typescript/utils/AssetUtil.d.ts.map +1 -1
- package/lib/typescript/utils/ConstantsUtil.d.ts +474 -1
- package/lib/typescript/utils/ConstantsUtil.d.ts.map +1 -1
- package/lib/typescript/utils/CoreHelperUtil.d.ts +5 -2
- package/lib/typescript/utils/CoreHelperUtil.d.ts.map +1 -1
- package/lib/typescript/utils/FetchUtil.d.ts +5 -5
- package/lib/typescript/utils/FetchUtil.d.ts.map +1 -1
- package/lib/typescript/utils/NetworkUtil.d.ts +1 -1
- package/lib/typescript/utils/NetworkUtil.d.ts.map +1 -1
- package/lib/typescript/utils/StorageUtil.d.ts +26 -2
- package/lib/typescript/utils/StorageUtil.d.ts.map +1 -1
- package/lib/typescript/utils/SwapApiUtil.d.ts.map +1 -1
- package/lib/typescript/utils/TypeUtil.d.ts +164 -25
- package/lib/typescript/utils/TypeUtil.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/controllers/AccountController.ts +2 -2
- package/src/controllers/ApiController.ts +12 -7
- package/src/controllers/BlockchainApiController.ts +238 -15
- package/src/controllers/ConnectionController.ts +10 -2
- package/src/controllers/ConnectionsController.ts +291 -0
- package/src/controllers/ConnectorController.ts +1 -0
- package/src/controllers/NetworkController.ts +1 -1
- package/src/controllers/OnRampController.ts +585 -0
- package/src/controllers/OptionsController.ts +19 -13
- package/src/controllers/PublicStateController.ts +1 -1
- package/src/controllers/RouterController.ts +17 -3
- package/src/controllers/SendController.ts +5 -5
- package/src/controllers/SwapController.ts +58 -48
- package/src/controllers/ThemeController.ts +11 -2
- package/src/controllers/TransactionsController.ts +2 -2
- package/src/index.ts +6 -1
- package/src/utils/AssetUtil.ts +5 -7
- package/src/utils/ConstantsUtil.ts +584 -5
- package/src/utils/CoreHelperUtil.ts +38 -3
- package/src/utils/FetchUtil.ts +16 -9
- package/src/utils/NetworkUtil.ts +1 -1
- package/src/utils/StorageUtil.ts +267 -2
- package/src/utils/SwapApiUtil.ts +22 -10
- package/src/utils/TypeUtil.ts +185 -29
- package/lib/commonjs/utils/ConnectionUtil.js +0 -33
- package/lib/commonjs/utils/ConnectionUtil.js.map +0 -1
- package/lib/module/utils/ConnectionUtil.js +0 -27
- package/lib/module/utils/ConnectionUtil.js.map +0 -1
- package/lib/typescript/utils/ConnectionUtil.d.ts +0 -4
- package/lib/typescript/utils/ConnectionUtil.d.ts.map +0 -1
- package/src/utils/ConnectionUtil.ts +0 -27
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { proxy, ref } from 'valtio';
|
|
2
|
+
import { derive } from 'valtio/utils';
|
|
3
|
+
import { EVMAdapter } from '@reown/appkit-common-react-native';
|
|
4
|
+
import { StorageUtil } from '../utils/StorageUtil';
|
|
5
|
+
|
|
6
|
+
// -- Types --------------------------------------------- //
|
|
7
|
+
|
|
8
|
+
//TODO: balance could be elsewhere
|
|
9
|
+
|
|
10
|
+
// -- State --------------------------------------------- //
|
|
11
|
+
const baseState = proxy({
|
|
12
|
+
activeNamespace: undefined,
|
|
13
|
+
connections: new Map(),
|
|
14
|
+
networks: []
|
|
15
|
+
});
|
|
16
|
+
const derivedState = derive({
|
|
17
|
+
activeAddress: get => {
|
|
18
|
+
const snap = get(baseState);
|
|
19
|
+
if (!snap.activeNamespace) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const connection = snap.connections.get(snap.activeNamespace);
|
|
23
|
+
if (!connection || !connection.accounts || connection.accounts.length === 0) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//TODO: what happens if there are several accounts on the same chain?
|
|
28
|
+
const activeAccount = connection.accounts.find(account => account.startsWith(connection.caipNetwork));
|
|
29
|
+
return activeAccount;
|
|
30
|
+
},
|
|
31
|
+
activeBalance: get => {
|
|
32
|
+
const snap = get(baseState);
|
|
33
|
+
if (!snap.activeNamespace) return undefined;
|
|
34
|
+
const connection = snap.connections.get(snap.activeNamespace);
|
|
35
|
+
if (!connection || !connection.accounts || connection.accounts.length === 0) {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
const activeAccount = connection.accounts.find(account => account.startsWith(connection.caipNetwork));
|
|
39
|
+
if (!connection || !connection.balances || !activeAccount || Object.keys(connection.balances).length === 0) {
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
return connection.balances[activeAccount];
|
|
43
|
+
},
|
|
44
|
+
activeNetwork: get => {
|
|
45
|
+
const snap = get(baseState);
|
|
46
|
+
if (!snap.activeNamespace) return undefined;
|
|
47
|
+
const connection = snap.connections.get(snap.activeNamespace);
|
|
48
|
+
if (!connection) return undefined;
|
|
49
|
+
return snap.networks.find(network => network.chainNamespace === snap.activeNamespace && network.id?.toString() === connection.caipNetwork?.split(':')[1]);
|
|
50
|
+
},
|
|
51
|
+
activeCaipNetworkId: get => {
|
|
52
|
+
const snap = get(baseState);
|
|
53
|
+
if (!snap.activeNamespace) return undefined;
|
|
54
|
+
const connection = snap.connections.get(snap.activeNamespace);
|
|
55
|
+
if (!connection) return undefined;
|
|
56
|
+
return connection.caipNetwork;
|
|
57
|
+
},
|
|
58
|
+
walletInfo: get => {
|
|
59
|
+
const snap = get(baseState);
|
|
60
|
+
if (!snap.activeNamespace) return undefined;
|
|
61
|
+
return snap.connections.get(snap.activeNamespace)?.wallet;
|
|
62
|
+
}
|
|
63
|
+
}, {
|
|
64
|
+
proxy: baseState // Link derived proxy to the base state proxy
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
// -- Controller ---------------------------------------- //
|
|
68
|
+
export const ConnectionsController = {
|
|
69
|
+
state: derivedState,
|
|
70
|
+
setActiveNamespace(namespace) {
|
|
71
|
+
baseState.activeNamespace = namespace;
|
|
72
|
+
StorageUtil.setActiveNamespace(namespace);
|
|
73
|
+
},
|
|
74
|
+
storeConnection({
|
|
75
|
+
namespace,
|
|
76
|
+
adapter,
|
|
77
|
+
accounts,
|
|
78
|
+
chains,
|
|
79
|
+
wallet,
|
|
80
|
+
caipNetwork
|
|
81
|
+
}) {
|
|
82
|
+
const newConnectionEntry = {
|
|
83
|
+
balances: {},
|
|
84
|
+
caipNetwork: caipNetwork ?? chains[0],
|
|
85
|
+
adapter: ref(adapter),
|
|
86
|
+
accounts,
|
|
87
|
+
chains,
|
|
88
|
+
wallet
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
// Create a new Map to ensure Valtio detects the change
|
|
92
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
93
|
+
newConnectionsMap.set(namespace, newConnectionEntry);
|
|
94
|
+
baseState.connections = newConnectionsMap;
|
|
95
|
+
},
|
|
96
|
+
updateAccounts(namespace, accounts) {
|
|
97
|
+
const connection = baseState.connections.get(namespace);
|
|
98
|
+
if (!connection) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
102
|
+
const updatedConnection = {
|
|
103
|
+
...connection,
|
|
104
|
+
accounts
|
|
105
|
+
};
|
|
106
|
+
newConnectionsMap.set(namespace, updatedConnection);
|
|
107
|
+
baseState.connections = newConnectionsMap;
|
|
108
|
+
},
|
|
109
|
+
updateBalance(namespace, address, balance) {
|
|
110
|
+
const connection = baseState.connections.get(namespace);
|
|
111
|
+
if (!connection) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const newBalances = {
|
|
115
|
+
...connection.balances,
|
|
116
|
+
[address]: balance
|
|
117
|
+
};
|
|
118
|
+
const updatedConnection = {
|
|
119
|
+
...connection,
|
|
120
|
+
balances: newBalances
|
|
121
|
+
};
|
|
122
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
123
|
+
newConnectionsMap.set(namespace, updatedConnection);
|
|
124
|
+
baseState.connections = newConnectionsMap;
|
|
125
|
+
},
|
|
126
|
+
setActiveNetwork(namespace, networkId) {
|
|
127
|
+
const connection = baseState.connections.get(namespace);
|
|
128
|
+
if (!connection) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
baseState.connections.set(namespace, {
|
|
132
|
+
...connection,
|
|
133
|
+
caipNetwork: networkId
|
|
134
|
+
});
|
|
135
|
+
},
|
|
136
|
+
setNetworks(networks) {
|
|
137
|
+
baseState.networks = networks;
|
|
138
|
+
},
|
|
139
|
+
getConnectedNetworks() {
|
|
140
|
+
return baseState.networks.filter(network => baseState.connections.get(network.chainNamespace)?.accounts.some(account => account.startsWith(network.caipNetworkId)));
|
|
141
|
+
},
|
|
142
|
+
async disconnect(namespace, isInternal = true) {
|
|
143
|
+
const connection = baseState.connections.get(namespace);
|
|
144
|
+
if (!connection) return;
|
|
145
|
+
|
|
146
|
+
// Get the current connector from the adapter
|
|
147
|
+
const connector = connection.adapter.connector;
|
|
148
|
+
if (!connector) return;
|
|
149
|
+
|
|
150
|
+
// Find all namespaces that use the same connector
|
|
151
|
+
const namespacesUsingConnector = Array.from(baseState.connections.keys()).filter(ns => baseState.connections.get(ns)?.adapter.connector === connector);
|
|
152
|
+
|
|
153
|
+
// Unsubscribe all event listeners from the adapter
|
|
154
|
+
namespacesUsingConnector.forEach(ns => {
|
|
155
|
+
const _connection = baseState.connections.get(ns);
|
|
156
|
+
if (_connection?.adapter) {
|
|
157
|
+
_connection.adapter.removeAllListeners();
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
// Disconnect the adapter
|
|
162
|
+
if (isInternal) {
|
|
163
|
+
await connection.adapter.disconnect();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Remove all namespaces that used this connector
|
|
167
|
+
const newConnectionsMap = new Map(baseState.connections);
|
|
168
|
+
namespacesUsingConnector.forEach(ns => {
|
|
169
|
+
newConnectionsMap.delete(ns);
|
|
170
|
+
});
|
|
171
|
+
baseState.connections = newConnectionsMap;
|
|
172
|
+
|
|
173
|
+
// Remove activeNamespace if it is in the list of namespaces using the connector
|
|
174
|
+
if (baseState.activeNamespace && (baseState.activeNamespace === namespace || namespacesUsingConnector.includes(baseState.activeNamespace))) {
|
|
175
|
+
baseState.activeNamespace = undefined;
|
|
176
|
+
StorageUtil.setActiveNamespace(undefined);
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
parseUnits(value, decimals) {
|
|
180
|
+
if (!baseState.activeNamespace) return undefined;
|
|
181
|
+
return baseState.connections.get(baseState.activeNamespace)?.adapter.parseUnits(value, decimals);
|
|
182
|
+
},
|
|
183
|
+
async sendTransaction(args) {
|
|
184
|
+
if (!baseState.activeNamespace) return undefined;
|
|
185
|
+
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
186
|
+
if (adapter instanceof EVMAdapter) {
|
|
187
|
+
return adapter.sendTransaction(args);
|
|
188
|
+
}
|
|
189
|
+
return undefined;
|
|
190
|
+
},
|
|
191
|
+
async estimateGas(args) {
|
|
192
|
+
if (!baseState.activeNamespace) return undefined;
|
|
193
|
+
const adapter = baseState.connections.get(baseState.activeNamespace)?.adapter;
|
|
194
|
+
if (adapter instanceof EVMAdapter) {
|
|
195
|
+
return adapter.estimateGas(args);
|
|
196
|
+
}
|
|
197
|
+
return undefined;
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
//# sourceMappingURL=ConnectionsController.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["proxy","ref","derive","EVMAdapter","StorageUtil","baseState","activeNamespace","undefined","connections","Map","networks","derivedState","activeAddress","get","snap","connection","accounts","length","activeAccount","find","account","startsWith","caipNetwork","activeBalance","balances","Object","keys","activeNetwork","network","chainNamespace","id","toString","split","activeCaipNetworkId","walletInfo","wallet","ConnectionsController","state","setActiveNamespace","namespace","storeConnection","adapter","chains","newConnectionEntry","newConnectionsMap","set","updateAccounts","updatedConnection","updateBalance","address","balance","newBalances","setActiveNetwork","networkId","setNetworks","getConnectedNetworks","filter","some","caipNetworkId","disconnect","isInternal","connector","namespacesUsingConnector","Array","from","ns","forEach","_connection","removeAllListeners","delete","includes","parseUnits","value","decimals","sendTransaction","args","estimateGas"],"sourceRoot":"../../../src","sources":["controllers/ConnectionsController.ts"],"mappings":"AAAA,SAASA,KA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mC,WAAW,CAACD,IAAI,CAAC;IAClC;IAEA,OAAOpE,SAAS;EAClB;AACF,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["subscribeKey","subKey","proxy","ref","StorageUtil","state","connectors","ConnectorController","key","callback","setConnectors","map","c","addConnector","connector","getConnectors","getAuthConnector","find","type","setConnectedConnector","connectorType","saveStorage","connectedConnector","removeConnectedConnector","setAuthLoading","loading","authLoading"],"sourceRoot":"../../../src","sources":["controllers/ConnectorController.ts"],"mappings":"AACA,SAASA,YAAY,IAAIC,MAAM,QAAQ,cAAc;AACrD,SAASC,KAAK,EAAEC,GAAG,QAAQ,QAAQ;AAEnC,SAASC,WAAW,QAAQ,sBAAsB;;AAElD;;AASA;AACA,MAAMC,KAAK,GAAGH,KAAK,CAA2B;EAC5CI,UAAU,EAAE;AACd,CAAC,CAAC;;AAEF;AACA,OAAO,MAAMC,mBAAmB,GAAG;EACjCF,KAAK;EAELL,YAAYA,CAAqBQ,GAAM,EAAEC,QAAsD,EAAE;IAC/F,OAAOR,MAAM,CAACI,KAAK,EAAEG,GAAG,EAAEC,QAAQ,CAAC;EACrC,CAAC;EAEDC,aAAaA,CAACJ,UAAkD,EAAE;IAChED,KAAK,CAACC,UAAU,GAAGA,UAAU,CAACK,GAAG,CAACC,CAAC,IAAIT,GAAG,CAACS,CAAC,CAAC,CAAC;EAChD,CAAC;EAEDC,YAAYA,CAACC,SAAoB,EAAE;IACjCT,KAAK,CAACC,UAAU,GAAG,CAAC,GAAGD,KAAK,CAACC,UAAU,EAAEH,GAAG,CAACW,SAAS,CAAC,CAAC;EAC1D,CAAC;EAEDC,aAAaA,CAAA,EAAG;IACd,OAAOV,KAAK,CAACC,UAAU;EACzB,CAAC;EAEDU,gBAAgBA,CAAA,EAAG;IACjB,OAAOX,KAAK,CAACC,UAAU,CAACW,IAAI,CAACL,CAAC,IAAIA,CAAC,CAACM,IAAI,KAAK,MAAM,CAAC;EACtD,CAAC;EAEDC,qBAAqBA,CACnBC,aAA6D,EAC7DC,WAAW,GAAG,IAAI,EAClB;IACAhB,KAAK,CAACiB,kBAAkB,GAAGF,aAAa;IAExC,IAAIC,WAAW,EAAE;MACf,IAAID,aAAa,EAAE;
|
|
1
|
+
{"version":3,"names":["subscribeKey","subKey","proxy","ref","StorageUtil","state","connectors","ConnectorController","key","callback","setConnectors","map","c","addConnector","connector","getConnectors","getAuthConnector","find","type","setConnectedConnector","connectorType","saveStorage","connectedConnector","removeConnectedConnector","setAuthLoading","loading","authLoading"],"sourceRoot":"../../../src","sources":["controllers/ConnectorController.ts"],"mappings":"AACA,SAASA,YAAY,IAAIC,MAAM,QAAQ,cAAc;AACrD,SAASC,KAAK,EAAEC,GAAG,QAAQ,QAAQ;AAEnC,SAASC,WAAW,QAAQ,sBAAsB;;AAElD;;AASA;AACA,MAAMC,KAAK,GAAGH,KAAK,CAA2B;EAC5CI,UAAU,EAAE;AACd,CAAC,CAAC;;AAEF;AACA,OAAO,MAAMC,mBAAmB,GAAG;EACjCF,KAAK;EAELL,YAAYA,CAAqBQ,GAAM,EAAEC,QAAsD,EAAE;IAC/F,OAAOR,MAAM,CAACI,KAAK,EAAEG,GAAG,EAAEC,QAAQ,CAAC;EACrC,CAAC;EAEDC,aAAaA,CAACJ,UAAkD,EAAE;IAChED,KAAK,CAACC,UAAU,GAAGA,UAAU,CAACK,GAAG,CAACC,CAAC,IAAIT,GAAG,CAACS,CAAC,CAAC,CAAC;EAChD,CAAC;EAEDC,YAAYA,CAACC,SAAoB,EAAE;IACjCT,KAAK,CAACC,UAAU,GAAG,CAAC,GAAGD,KAAK,CAACC,UAAU,EAAEH,GAAG,CAACW,SAAS,CAAC,CAAC;EAC1D,CAAC;EAEDC,aAAaA,CAAA,EAAG;IACd,OAAOV,KAAK,CAACC,UAAU;EACzB,CAAC;EAEDU,gBAAgBA,CAAA,EAAG;IACjB,OAAOX,KAAK,CAACC,UAAU,CAACW,IAAI,CAACL,CAAC,IAAIA,CAAC,CAACM,IAAI,KAAK,MAAM,CAAC;EACtD,CAAC;EAEDC,qBAAqBA,CACnBC,aAA6D,EAC7DC,WAAW,GAAG,IAAI,EAClB;IACAhB,KAAK,CAACiB,kBAAkB,GAAGF,aAAa;IAExC,IAAIC,WAAW,EAAE;MACf,IAAID,aAAa,EAAE;QACjB;QACAhB,WAAW,CAACe,qBAAqB,CAACC,aAAa,CAAC;MAClD,CAAC,MAAM;QACLhB,WAAW,CAACmB,wBAAwB,CAAC,CAAC;MACxC;IACF;EACF,CAAC;EAEDC,cAAcA,CAACC,OAAgD,EAAE;IAC/DpB,KAAK,CAACqB,WAAW,GAAGD,OAAO;EAC7B;AACF,CAAC"}
|
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
import { subscribeKey as subKey } from 'valtio/vanilla/utils';
|
|
2
|
+
import { proxy, subscribe as sub } from 'valtio/vanilla';
|
|
3
|
+
import { CoreHelperUtil } from '../utils/CoreHelperUtil';
|
|
4
|
+
import { NetworkController } from './NetworkController';
|
|
5
|
+
import { AccountController } from './AccountController';
|
|
6
|
+
import { OptionsController } from './OptionsController';
|
|
7
|
+
import { ConstantsUtil, OnRampErrorType } from '../utils/ConstantsUtil';
|
|
8
|
+
import { StorageUtil } from '../utils/StorageUtil';
|
|
9
|
+
import { SnackController } from './SnackController';
|
|
10
|
+
import { EventsController } from './EventsController';
|
|
11
|
+
import { BlockchainApiController } from './BlockchainApiController';
|
|
12
|
+
|
|
13
|
+
// -- Helpers ------------------------------------------- //
|
|
14
|
+
|
|
15
|
+
let quotesAbortController = null;
|
|
16
|
+
|
|
17
|
+
// -- Utils --------------------------------------------- //
|
|
18
|
+
|
|
19
|
+
const mapErrorMessage = errorCode => {
|
|
20
|
+
const errorMap = {
|
|
21
|
+
[OnRampErrorType.AMOUNT_TOO_LOW]: {
|
|
22
|
+
type: OnRampErrorType.AMOUNT_TOO_LOW,
|
|
23
|
+
message: 'Amount is too low'
|
|
24
|
+
},
|
|
25
|
+
[OnRampErrorType.AMOUNT_TOO_HIGH]: {
|
|
26
|
+
type: OnRampErrorType.AMOUNT_TOO_HIGH,
|
|
27
|
+
message: 'Amount is too high'
|
|
28
|
+
},
|
|
29
|
+
[OnRampErrorType.INVALID_AMOUNT]: {
|
|
30
|
+
type: OnRampErrorType.INVALID_AMOUNT,
|
|
31
|
+
message: 'Please adjust amount'
|
|
32
|
+
},
|
|
33
|
+
[OnRampErrorType.INCOMPATIBLE_REQUEST]: {
|
|
34
|
+
type: OnRampErrorType.INCOMPATIBLE_REQUEST,
|
|
35
|
+
message: 'Try different amount or payment method'
|
|
36
|
+
},
|
|
37
|
+
[OnRampErrorType.BAD_REQUEST]: {
|
|
38
|
+
type: OnRampErrorType.BAD_REQUEST,
|
|
39
|
+
message: 'Try different amount or payment method'
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
return errorMap[errorCode] || {
|
|
43
|
+
type: OnRampErrorType.UNKNOWN,
|
|
44
|
+
message: 'Something went wrong. Please try again'
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// -- Types --------------------------------------------- //
|
|
49
|
+
|
|
50
|
+
const defaultState = {
|
|
51
|
+
quotesLoading: false,
|
|
52
|
+
countries: [],
|
|
53
|
+
paymentMethods: [],
|
|
54
|
+
serviceProviders: [],
|
|
55
|
+
paymentAmount: undefined
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// -- State --------------------------------------------- //
|
|
59
|
+
const state = proxy(defaultState);
|
|
60
|
+
|
|
61
|
+
// -- Controller ---------------------------------------- //
|
|
62
|
+
export const OnRampController = {
|
|
63
|
+
state,
|
|
64
|
+
subscribe(callback) {
|
|
65
|
+
return sub(state, () => callback(state));
|
|
66
|
+
},
|
|
67
|
+
subscribeKey(key, callback) {
|
|
68
|
+
return subKey(state, key, callback);
|
|
69
|
+
},
|
|
70
|
+
async setSelectedCountry(country, updateCurrency = true) {
|
|
71
|
+
state.selectedCountry = country;
|
|
72
|
+
state.loading = true;
|
|
73
|
+
if (updateCurrency) {
|
|
74
|
+
const currencyCode = ConstantsUtil.COUNTRY_CURRENCIES[country.countryCode] || 'USD';
|
|
75
|
+
const currency = state.paymentCurrencies?.find(c => c.currencyCode === currencyCode);
|
|
76
|
+
if (currency) {
|
|
77
|
+
this.setPaymentCurrency(currency);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
await Promise.all([this.fetchPaymentMethods(), this.fetchCryptoCurrencies()]);
|
|
81
|
+
state.loading = false;
|
|
82
|
+
StorageUtil.setOnRampPreferredCountry(country);
|
|
83
|
+
},
|
|
84
|
+
setSelectedPaymentMethod(paymentMethod) {
|
|
85
|
+
state.selectedPaymentMethod = paymentMethod;
|
|
86
|
+
this.clearQuotes();
|
|
87
|
+
},
|
|
88
|
+
setPurchaseCurrency(currency) {
|
|
89
|
+
state.purchaseCurrency = currency;
|
|
90
|
+
EventsController.sendEvent({
|
|
91
|
+
type: 'track',
|
|
92
|
+
event: 'SELECT_BUY_ASSET',
|
|
93
|
+
properties: {
|
|
94
|
+
asset: currency.currencyCode
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
this.clearQuotes();
|
|
98
|
+
},
|
|
99
|
+
setPaymentCurrency(currency, updateAmount = true) {
|
|
100
|
+
state.paymentCurrency = currency;
|
|
101
|
+
StorageUtil.setOnRampPreferredFiatCurrency(currency);
|
|
102
|
+
if (updateAmount) {
|
|
103
|
+
state.paymentAmount = undefined;
|
|
104
|
+
}
|
|
105
|
+
this.clearQuotes();
|
|
106
|
+
this.clearError();
|
|
107
|
+
},
|
|
108
|
+
setPaymentAmount(amount) {
|
|
109
|
+
state.paymentAmount = amount ? Number(amount) : undefined;
|
|
110
|
+
},
|
|
111
|
+
setSelectedQuote(quote) {
|
|
112
|
+
state.selectedQuote = quote;
|
|
113
|
+
},
|
|
114
|
+
updateSelectedPurchaseCurrency() {
|
|
115
|
+
let selectedCurrency;
|
|
116
|
+
if (NetworkController.state.caipNetwork?.id) {
|
|
117
|
+
const defaultCurrency = ConstantsUtil.NETWORK_DEFAULT_CURRENCIES[NetworkController.state.caipNetwork?.id];
|
|
118
|
+
selectedCurrency = state.purchaseCurrencies?.find(c => c.currencyCode === defaultCurrency);
|
|
119
|
+
}
|
|
120
|
+
state.purchaseCurrency = selectedCurrency || state.purchaseCurrencies?.[0] || undefined;
|
|
121
|
+
},
|
|
122
|
+
getServiceProviderImage(serviceProviderName) {
|
|
123
|
+
if (!serviceProviderName) return undefined;
|
|
124
|
+
const provider = state.serviceProviders.find(p => p.serviceProvider === serviceProviderName);
|
|
125
|
+
return provider?.logos?.lightShort;
|
|
126
|
+
},
|
|
127
|
+
getCurrencyLimit(currency) {
|
|
128
|
+
return state.paymentCurrenciesLimits?.find(l => l.currencyCode === currency.currencyCode);
|
|
129
|
+
},
|
|
130
|
+
async fetchCountries() {
|
|
131
|
+
try {
|
|
132
|
+
let countries = await StorageUtil.getOnRampCountries();
|
|
133
|
+
if (!countries.length) {
|
|
134
|
+
countries = (await BlockchainApiController.fetchOnRampCountries()) ?? [];
|
|
135
|
+
if (countries.length) {
|
|
136
|
+
StorageUtil.setOnRampCountries(countries);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
state.countries = countries;
|
|
140
|
+
const preferredCountry = await StorageUtil.getOnRampPreferredCountry();
|
|
141
|
+
if (preferredCountry) {
|
|
142
|
+
state.selectedCountry = preferredCountry;
|
|
143
|
+
} else {
|
|
144
|
+
const countryCode = CoreHelperUtil.getCountryFromTimezone();
|
|
145
|
+
state.selectedCountry = countries.find(c => c.countryCode === countryCode) || countries[0] || undefined;
|
|
146
|
+
}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
state.error = {
|
|
149
|
+
type: OnRampErrorType.FAILED_TO_LOAD_COUNTRIES,
|
|
150
|
+
message: 'Failed to load countries'
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
async fetchServiceProviders() {
|
|
155
|
+
try {
|
|
156
|
+
let serviceProviders = await StorageUtil.getOnRampServiceProviders();
|
|
157
|
+
if (!serviceProviders.length) {
|
|
158
|
+
serviceProviders = (await BlockchainApiController.fetchOnRampServiceProviders()) ?? [];
|
|
159
|
+
if (serviceProviders.length) {
|
|
160
|
+
StorageUtil.setOnRampServiceProviders(serviceProviders);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
state.serviceProviders = serviceProviders || [];
|
|
164
|
+
} catch (error) {
|
|
165
|
+
state.error = {
|
|
166
|
+
type: OnRampErrorType.FAILED_TO_LOAD_PROVIDERS,
|
|
167
|
+
message: 'Failed to load service providers'
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
},
|
|
171
|
+
async fetchPaymentMethods() {
|
|
172
|
+
try {
|
|
173
|
+
const paymentMethods = await BlockchainApiController.fetchOnRampPaymentMethods({
|
|
174
|
+
countries: state.selectedCountry?.countryCode
|
|
175
|
+
});
|
|
176
|
+
const defaultCountryPaymentMethods = ConstantsUtil.COUNTRY_DEFAULT_PAYMENT_METHOD[state.selectedCountry?.countryCode];
|
|
177
|
+
state.paymentMethods = paymentMethods?.sort((a, b) => {
|
|
178
|
+
const aIndex = defaultCountryPaymentMethods?.indexOf(a.paymentMethod);
|
|
179
|
+
const bIndex = defaultCountryPaymentMethods?.indexOf(b.paymentMethod);
|
|
180
|
+
if (aIndex === -1 && bIndex === -1) return 0;
|
|
181
|
+
if (aIndex === -1) return 1;
|
|
182
|
+
if (bIndex === -1) return -1;
|
|
183
|
+
return aIndex - bIndex;
|
|
184
|
+
}) || [];
|
|
185
|
+
state.selectedPaymentMethod = paymentMethods?.[0] || undefined;
|
|
186
|
+
this.clearQuotes();
|
|
187
|
+
} catch (error) {
|
|
188
|
+
state.error = {
|
|
189
|
+
type: OnRampErrorType.FAILED_TO_LOAD_METHODS,
|
|
190
|
+
message: 'Failed to load payment methods'
|
|
191
|
+
};
|
|
192
|
+
state.paymentMethods = [];
|
|
193
|
+
state.selectedPaymentMethod = undefined;
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
async fetchCryptoCurrencies() {
|
|
197
|
+
try {
|
|
198
|
+
const cryptoCurrencies = await BlockchainApiController.fetchOnRampCryptoCurrencies({
|
|
199
|
+
countries: state.selectedCountry?.countryCode
|
|
200
|
+
});
|
|
201
|
+
state.purchaseCurrencies = cryptoCurrencies || [];
|
|
202
|
+
let selectedCurrency;
|
|
203
|
+
if (NetworkController.state.caipNetwork?.id) {
|
|
204
|
+
const defaultCurrency = ConstantsUtil.NETWORK_DEFAULT_CURRENCIES[NetworkController.state.caipNetwork?.id] || 'ETH';
|
|
205
|
+
selectedCurrency = state.purchaseCurrencies?.find(c => c.currencyCode === defaultCurrency);
|
|
206
|
+
}
|
|
207
|
+
state.purchaseCurrency = selectedCurrency || cryptoCurrencies?.[0] || undefined;
|
|
208
|
+
} catch (error) {
|
|
209
|
+
state.error = {
|
|
210
|
+
type: OnRampErrorType.FAILED_TO_LOAD_CURRENCIES,
|
|
211
|
+
message: 'Failed to load crypto currencies'
|
|
212
|
+
};
|
|
213
|
+
state.purchaseCurrencies = [];
|
|
214
|
+
state.purchaseCurrency = undefined;
|
|
215
|
+
}
|
|
216
|
+
},
|
|
217
|
+
async fetchFiatCurrencies() {
|
|
218
|
+
try {
|
|
219
|
+
let fiatCurrencies = await StorageUtil.getOnRampFiatCurrencies();
|
|
220
|
+
let currencyCode = 'USD';
|
|
221
|
+
const countryCode = state.selectedCountry?.countryCode;
|
|
222
|
+
if (!fiatCurrencies.length) {
|
|
223
|
+
fiatCurrencies = (await BlockchainApiController.fetchOnRampFiatCurrencies()) ?? [];
|
|
224
|
+
if (fiatCurrencies.length) {
|
|
225
|
+
StorageUtil.setOnRampFiatCurrencies(fiatCurrencies);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
state.paymentCurrencies = fiatCurrencies || [];
|
|
229
|
+
if (countryCode) {
|
|
230
|
+
currencyCode = ConstantsUtil.COUNTRY_CURRENCIES[countryCode];
|
|
231
|
+
}
|
|
232
|
+
const preferredCurrency = await StorageUtil.getOnRampPreferredFiatCurrency();
|
|
233
|
+
const defaultCurrency = preferredCurrency || fiatCurrencies?.find(c => c.currencyCode === currencyCode) || fiatCurrencies?.[0] || undefined;
|
|
234
|
+
if (defaultCurrency) {
|
|
235
|
+
this.setPaymentCurrency(defaultCurrency);
|
|
236
|
+
}
|
|
237
|
+
} catch (error) {
|
|
238
|
+
state.error = {
|
|
239
|
+
type: OnRampErrorType.FAILED_TO_LOAD_CURRENCIES,
|
|
240
|
+
message: 'Failed to load fiat currencies'
|
|
241
|
+
};
|
|
242
|
+
state.paymentCurrencies = [];
|
|
243
|
+
state.paymentCurrency = undefined;
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
abortGetQuotes(clearState = true) {
|
|
247
|
+
if (quotesAbortController) {
|
|
248
|
+
quotesAbortController.abort();
|
|
249
|
+
quotesAbortController = null;
|
|
250
|
+
}
|
|
251
|
+
if (clearState) {
|
|
252
|
+
this.clearQuotes();
|
|
253
|
+
state.quotesLoading = false;
|
|
254
|
+
state.error = undefined;
|
|
255
|
+
}
|
|
256
|
+
},
|
|
257
|
+
getQuotesDebounced: CoreHelperUtil.debounce(function () {
|
|
258
|
+
OnRampController.getQuotes();
|
|
259
|
+
}, 500),
|
|
260
|
+
async getQuotes() {
|
|
261
|
+
if (!state.paymentAmount || state.paymentAmount <= 0) {
|
|
262
|
+
this.clearQuotes();
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
state.quotesLoading = true;
|
|
266
|
+
state.error = undefined;
|
|
267
|
+
this.abortGetQuotes(false);
|
|
268
|
+
quotesAbortController = new AbortController();
|
|
269
|
+
try {
|
|
270
|
+
const body = {
|
|
271
|
+
countryCode: state.selectedCountry?.countryCode,
|
|
272
|
+
paymentMethodType: state.selectedPaymentMethod?.paymentMethod,
|
|
273
|
+
destinationCurrencyCode: state.purchaseCurrency?.currencyCode,
|
|
274
|
+
sourceAmount: state.paymentAmount,
|
|
275
|
+
sourceCurrencyCode: state.paymentCurrency?.currencyCode,
|
|
276
|
+
walletAddress: AccountController.state.address
|
|
277
|
+
};
|
|
278
|
+
const response = await BlockchainApiController.getOnRampQuotes(body, quotesAbortController.signal);
|
|
279
|
+
if (!response || !response.length) {
|
|
280
|
+
throw new Error('No quotes available');
|
|
281
|
+
}
|
|
282
|
+
const quotes = response.sort((a, b) => b.customerScore - a.customerScore);
|
|
283
|
+
state.quotes = quotes;
|
|
284
|
+
state.selectedQuote = quotes[0];
|
|
285
|
+
state.selectedServiceProvider = state.serviceProviders.find(sp => sp.serviceProvider === quotes[0]?.serviceProvider);
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (error.name === 'AbortError') {
|
|
288
|
+
// Do nothing, another request was made
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
EventsController.sendEvent({
|
|
292
|
+
type: 'track',
|
|
293
|
+
event: 'BUY_FAIL',
|
|
294
|
+
properties: {
|
|
295
|
+
message: error?.message ?? error?.code ?? 'Error getting quotes'
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
this.clearQuotes();
|
|
299
|
+
state.error = mapErrorMessage(error?.code || 'UNKNOWN_ERROR');
|
|
300
|
+
} finally {
|
|
301
|
+
state.quotesLoading = false;
|
|
302
|
+
}
|
|
303
|
+
},
|
|
304
|
+
canGenerateQuote() {
|
|
305
|
+
return !!(state.selectedCountry?.countryCode && state.selectedPaymentMethod?.paymentMethod && state.purchaseCurrency?.currencyCode && state.paymentAmount && state.paymentAmount > 0 && state.paymentCurrency?.currencyCode && state.selectedCountry && !state.loading && AccountController.state.address);
|
|
306
|
+
},
|
|
307
|
+
async fetchFiatLimits() {
|
|
308
|
+
try {
|
|
309
|
+
let limits = await StorageUtil.getOnRampFiatLimits();
|
|
310
|
+
if (!limits.length) {
|
|
311
|
+
limits = (await BlockchainApiController.fetchOnRampFiatLimits()) ?? [];
|
|
312
|
+
if (limits.length) {
|
|
313
|
+
StorageUtil.setOnRampFiatLimits(limits);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
state.paymentCurrenciesLimits = limits;
|
|
317
|
+
} catch (error) {
|
|
318
|
+
state.error = {
|
|
319
|
+
type: OnRampErrorType.FAILED_TO_LOAD_LIMITS,
|
|
320
|
+
message: 'Failed to load fiat limits'
|
|
321
|
+
};
|
|
322
|
+
state.paymentCurrenciesLimits = [];
|
|
323
|
+
}
|
|
324
|
+
},
|
|
325
|
+
async generateWidget({
|
|
326
|
+
quote
|
|
327
|
+
}) {
|
|
328
|
+
const metadata = OptionsController.state.metadata;
|
|
329
|
+
const eventProperties = {
|
|
330
|
+
asset: quote.destinationCurrencyCode,
|
|
331
|
+
network: state.purchaseCurrency?.chainName ?? '',
|
|
332
|
+
amount: quote.destinationAmount.toString(),
|
|
333
|
+
currency: quote.destinationCurrencyCode,
|
|
334
|
+
paymentMethod: quote.paymentMethodType,
|
|
335
|
+
provider: 'MELD',
|
|
336
|
+
serviceProvider: quote.serviceProvider
|
|
337
|
+
};
|
|
338
|
+
try {
|
|
339
|
+
if (!quote) {
|
|
340
|
+
throw new Error('Invalid quote');
|
|
341
|
+
}
|
|
342
|
+
const widget = await BlockchainApiController.getOnRampWidget({
|
|
343
|
+
countryCode: quote.countryCode,
|
|
344
|
+
destinationCurrencyCode: quote.destinationCurrencyCode,
|
|
345
|
+
paymentMethodType: quote.paymentMethodType,
|
|
346
|
+
serviceProvider: quote.serviceProvider,
|
|
347
|
+
sourceAmount: quote.sourceAmount,
|
|
348
|
+
sourceCurrencyCode: quote.sourceCurrencyCode,
|
|
349
|
+
walletAddress: AccountController.state.address,
|
|
350
|
+
redirectUrl: metadata?.redirect?.universal ?? metadata?.redirect?.native
|
|
351
|
+
});
|
|
352
|
+
if (!widget || !widget.widgetUrl) {
|
|
353
|
+
throw new Error('Invalid widget response');
|
|
354
|
+
}
|
|
355
|
+
EventsController.sendEvent({
|
|
356
|
+
type: 'track',
|
|
357
|
+
event: 'BUY_SUBMITTED',
|
|
358
|
+
properties: eventProperties
|
|
359
|
+
});
|
|
360
|
+
state.widgetUrl = widget.widgetUrl;
|
|
361
|
+
return widget;
|
|
362
|
+
} catch (e) {
|
|
363
|
+
EventsController.sendEvent({
|
|
364
|
+
type: 'track',
|
|
365
|
+
event: 'BUY_FAIL',
|
|
366
|
+
properties: {
|
|
367
|
+
...eventProperties,
|
|
368
|
+
message: e?.message ?? e?.code ?? 'Error generating widget url'
|
|
369
|
+
}
|
|
370
|
+
});
|
|
371
|
+
state.error = mapErrorMessage(e?.code || 'UNKNOWN_ERROR');
|
|
372
|
+
SnackController.showInternalError({
|
|
373
|
+
shortMessage: 'Error creating purchase URL',
|
|
374
|
+
longMessage: e?.message ?? e?.code
|
|
375
|
+
});
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
},
|
|
379
|
+
clearError() {
|
|
380
|
+
state.error = undefined;
|
|
381
|
+
},
|
|
382
|
+
clearQuotes() {
|
|
383
|
+
state.quotes = [];
|
|
384
|
+
state.selectedQuote = undefined;
|
|
385
|
+
state.selectedServiceProvider = undefined;
|
|
386
|
+
},
|
|
387
|
+
async loadOnRampData() {
|
|
388
|
+
state.initialLoading = true;
|
|
389
|
+
state.error = undefined;
|
|
390
|
+
try {
|
|
391
|
+
await this.fetchCountries();
|
|
392
|
+
await this.fetchServiceProviders();
|
|
393
|
+
await Promise.all([this.fetchPaymentMethods(), this.fetchFiatLimits(), this.fetchCryptoCurrencies(), this.fetchFiatCurrencies()]);
|
|
394
|
+
} catch (error) {
|
|
395
|
+
if (!state.error) {
|
|
396
|
+
state.error = {
|
|
397
|
+
type: OnRampErrorType.FAILED_TO_LOAD,
|
|
398
|
+
message: 'Failed to load onramp data'
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
} finally {
|
|
402
|
+
state.initialLoading = false;
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
resetState() {
|
|
406
|
+
state.error = undefined;
|
|
407
|
+
state.quotesLoading = false;
|
|
408
|
+
state.quotes = [];
|
|
409
|
+
state.selectedQuote = undefined;
|
|
410
|
+
state.selectedServiceProvider = undefined;
|
|
411
|
+
state.widgetUrl = undefined;
|
|
412
|
+
state.paymentAmount = undefined;
|
|
413
|
+
this.updateSelectedPurchaseCurrency();
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
//# sourceMappingURL=OnRampController.js.map
|