@ledgerhq/live-common 26.1.0-next.1 → 27.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/account/importAccounts.d.ts +35 -2
- package/lib/account/importAccounts.d.ts.map +1 -1
- package/lib/account/importAccounts.integration.test.d.ts +2 -0
- package/lib/account/importAccounts.integration.test.d.ts.map +1 -0
- package/lib/account/importAccounts.integration.test.js +219 -0
- package/lib/account/importAccounts.integration.test.js.map +1 -0
- package/lib/account/importAccounts.js +130 -12
- package/lib/account/importAccounts.js.map +1 -1
- package/lib/account/importAccounts.test.d.ts +2 -0
- package/lib/account/importAccounts.test.d.ts.map +1 -0
- package/lib/{__tests__/accounts/importAccounts.js → account/importAccounts.test.js} +44 -64
- package/lib/account/importAccounts.test.js.map +1 -0
- package/package.json +17 -17
- package/src/account/importAccounts.integration.test.ts +193 -0
- package/src/{__tests__/accounts/importAccounts.ts → account/importAccounts.test.ts} +39 -59
- package/src/account/importAccounts.ts +103 -12
- package/lib/__tests__/accounts/importAccounts.d.ts +0 -2
- package/lib/__tests__/accounts/importAccounts.d.ts.map +0 -1
- package/lib/__tests__/accounts/importAccounts.js.map +0 -1
|
@@ -5,6 +5,13 @@ import { findAccountMigration, checkAccountSupported } from "./support";
|
|
|
5
5
|
import joinSwapHistories from "../exchange/swap/joinSwapHistories";
|
|
6
6
|
import isEqual from "lodash/isEqual";
|
|
7
7
|
import type { Account } from "@ledgerhq/types-live";
|
|
8
|
+
import { BridgeCacheSystem } from "../bridge/cache";
|
|
9
|
+
import { getAccountBridge } from "../bridge";
|
|
10
|
+
import { promiseAllBatched } from "../promise";
|
|
11
|
+
import { getEnv } from "../env";
|
|
12
|
+
import { Observable } from "rxjs";
|
|
13
|
+
import { reduce } from "rxjs/operators";
|
|
14
|
+
|
|
8
15
|
const itemModeDisplaySort = {
|
|
9
16
|
create: 1,
|
|
10
17
|
update: 2,
|
|
@@ -103,31 +110,115 @@ export const importAccountsMakeItems = ({
|
|
|
103
110
|
itemModeDisplaySort[(a as ImportItem).mode] -
|
|
104
111
|
itemModeDisplaySort[(b as ImportItem).mode]
|
|
105
112
|
) as ImportItem[];
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* SyncNewAccountsOutput
|
|
116
|
+
* - synchronized: a map of account id to account. the account id are kept as originally set in the items[].account before a possible "remap" of the ids after a sync happened
|
|
117
|
+
* - failed: a map of account id to errors for all possible fails that happened.
|
|
118
|
+
*/
|
|
119
|
+
export type SyncNewAccountsOutput = {
|
|
120
|
+
synchronized: Record<string, Account>;
|
|
121
|
+
failed: Record<string, Error>;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export type SyncNewAccountsInput = {
|
|
125
|
+
items: ImportItem[];
|
|
126
|
+
selectedAccounts: string[];
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* @param items: result of importAccountsMakeItems
|
|
132
|
+
* @param selectedAccounts: user's selected accounts
|
|
133
|
+
* @param bridgeCache: the bridge cache created on user land to prepare currency for a sync
|
|
134
|
+
* @param blacklistedTokenIds: the list of blacklisted token ids to ignore sync with
|
|
135
|
+
* @returns SyncNewAccountsOutput
|
|
136
|
+
*/
|
|
137
|
+
export const syncNewAccountsToImport = async (
|
|
138
|
+
{ items, selectedAccounts }: SyncNewAccountsInput,
|
|
139
|
+
bridgeCache: BridgeCacheSystem,
|
|
140
|
+
blacklistedTokenIds?: string[]
|
|
141
|
+
): Promise<SyncNewAccountsOutput> => {
|
|
142
|
+
const selectedItems = items.filter((item) =>
|
|
143
|
+
selectedAccounts.includes(item.account.id)
|
|
144
|
+
);
|
|
145
|
+
const synchronized = {};
|
|
146
|
+
const failed = {};
|
|
147
|
+
await promiseAllBatched(
|
|
148
|
+
getEnv("SYNC_MAX_CONCURRENT"),
|
|
149
|
+
selectedItems,
|
|
150
|
+
async ({ account }) => {
|
|
151
|
+
try {
|
|
152
|
+
const bridge = getAccountBridge(account);
|
|
153
|
+
await bridgeCache.prepareCurrency(account.currency);
|
|
154
|
+
const syncConfig = {
|
|
155
|
+
paginationConfig: {},
|
|
156
|
+
blacklistedTokenIds,
|
|
157
|
+
};
|
|
158
|
+
const observable = bridge.sync(account, syncConfig);
|
|
159
|
+
const reduced: Observable<Account> = observable.pipe(
|
|
160
|
+
reduce((a, f: (_: Account) => Account) => f(a), account)
|
|
161
|
+
);
|
|
162
|
+
const synced = await reduced.toPromise();
|
|
163
|
+
synchronized[account.id] = synced;
|
|
164
|
+
} catch (e) {
|
|
165
|
+
failed[account.id] = e;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
);
|
|
169
|
+
return { synchronized, failed };
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
export type ImportAccountsReduceInput = {
|
|
173
|
+
items: ImportItem[];
|
|
174
|
+
selectedAccounts: string[];
|
|
175
|
+
syncResult: SyncNewAccountsOutput;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
*
|
|
180
|
+
* @param existingAccounts
|
|
181
|
+
* @param items: value resulting of importAccountsMakeItems
|
|
182
|
+
* @params selectedAccounts: array of all account ids selected by user for import
|
|
183
|
+
* @param syncedNewAccounts: accounts resulting of syncNewAccountsToImport(). all accounts that would be missing from that list would be ignored to be imported (fresh sync is needed to make sure the accounts are "ready")
|
|
184
|
+
* @returns all accounts
|
|
185
|
+
*/
|
|
106
186
|
export const importAccountsReduce = (
|
|
107
187
|
existingAccounts: Account[],
|
|
108
|
-
{
|
|
109
|
-
items,
|
|
110
|
-
selectedAccounts,
|
|
111
|
-
}: {
|
|
112
|
-
items: ImportItem[];
|
|
113
|
-
selectedAccounts: string[];
|
|
114
|
-
}
|
|
188
|
+
{ items, selectedAccounts, syncResult }: ImportAccountsReduceInput
|
|
115
189
|
): Account[] => {
|
|
116
190
|
const accounts = existingAccounts.slice(0);
|
|
117
191
|
const selectedItems = items.filter((item) =>
|
|
118
192
|
selectedAccounts.includes(item.account.id)
|
|
119
193
|
);
|
|
120
194
|
|
|
121
|
-
for (const {
|
|
195
|
+
for (const {
|
|
196
|
+
mode,
|
|
197
|
+
account: { id },
|
|
198
|
+
initialAccountId,
|
|
199
|
+
} of selectedItems) {
|
|
200
|
+
const account = syncResult.synchronized[id];
|
|
201
|
+
if (!account) continue;
|
|
122
202
|
switch (mode) {
|
|
123
|
-
case "create":
|
|
124
|
-
accounts.
|
|
203
|
+
case "create": {
|
|
204
|
+
const exists = accounts.find(
|
|
205
|
+
(a) => a.id === initialAccountId || a.id === id
|
|
206
|
+
);
|
|
207
|
+
if (!exists) {
|
|
208
|
+
// prevent duplicate cases to happen (in case of race condition)
|
|
209
|
+
accounts.push(account);
|
|
210
|
+
}
|
|
125
211
|
break;
|
|
212
|
+
}
|
|
126
213
|
|
|
127
214
|
case "update": {
|
|
128
|
-
const item = accounts.find(
|
|
215
|
+
const item = accounts.find(
|
|
216
|
+
(a) => a.id === initialAccountId || a.id === id
|
|
217
|
+
);
|
|
129
218
|
const i = accounts.indexOf(item as Account);
|
|
130
|
-
|
|
219
|
+
if (i !== -1) {
|
|
220
|
+
accounts[i] = account;
|
|
221
|
+
}
|
|
131
222
|
break;
|
|
132
223
|
}
|
|
133
224
|
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"importAccounts.d.ts","sourceRoot":"","sources":["../../../src/__tests__/accounts/importAccounts.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"importAccounts.js","sourceRoot":"","sources":["../../../src/__tests__/accounts/importAccounts.ts"],"names":[],"mappings":";;AACA,yCAIuB;AACvB,+CAA0D;AAC1D,kDAA4D;AAE5D,IAAA,4BAAkB,EAAC,OAAO,CAAC,CAAC;AAE5B,IAAA,mCAAsB,EAAC,CAAC,UAAU,CAAC,CAAC,CAAC;AACrC,QAAQ,CAAC,yBAAyB,EAAE;IAClC,iHAAiH;IACjH,IAAI,CAAC,IAAI,C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