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