-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathuseAssetLookup.ts
More file actions
398 lines (361 loc) · 11.7 KB
/
Copy pathuseAssetLookup.ts
File metadata and controls
398 lines (361 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { useReducer } from "react";
import { useSelector } from "react-redux";
import { captureException } from "@sentry/browser";
import { getTokenDetails } from "@shared/api/internal";
import { isSacContractExecutable } from "@shared/helpers/soroban/token";
import { INDEXER_URL } from "@shared/constants/mercury";
import { BlockAidScanAssetResult } from "@shared/api/types";
import {
getVerifiedTokens,
getNativeContractDetails,
searchAsset,
VerifiedTokenRecord,
} from "popup/helpers/searchAsset";
import { splitVerifiedAssetCurrency } from "popup/helpers/assetList";
import { isContractId } from "popup/helpers/soroban";
import { initialState, reducer } from "helpers/request";
import { RequestState } from "constants/request";
import { isAssetSuspicious, scanAsset } from "popup/helpers/blockaid";
import { settingsSelector } from "popup/ducks/settings";
import { ManageAssetCurrency } from "popup/components/manageAssets/ManageAssetRows";
import { NetworkDetails } from "@shared/constants/stellar";
import { getCombinedAssetListData } from "@shared/api/helpers/token-list";
import { getIconFromTokenLists } from "@shared/api/helpers/getIconFromTokenList";
interface AssetRecord {
asset: string;
domain?: string;
tomlInfo?: { image: string };
}
interface AssetLookupDetails {
isVerifiedToken: boolean;
isVerificationInfoShowing: boolean;
verifiedAssetRows: ManageAssetCurrency[];
unverifiedAssetRows: ManageAssetCurrency[];
verifiedLists: string[];
blockaidScanResults: {
[key: string]: BlockAidScanAssetResult;
};
}
const DEFAULT_PAYLOAD: AssetLookupDetails = {
isVerifiedToken: false,
isVerificationInfoShowing: false,
verifiedAssetRows: [],
unverifiedAssetRows: [],
verifiedLists: [],
blockaidScanResults: {},
};
const useAssetLookup = () => {
let isVerifiedToken = false;
let isVerificationInfoShowing = false;
let verifiedLists = [] as string[];
const { assetsLists } = useSelector(settingsSelector);
const MAX_ASSETS_TO_SCAN = 10;
const [state, dispatch] = useReducer(
reducer<AssetLookupDetails, unknown>,
initialState,
);
const addBlockaidScanResults = async ({
assetRows,
payload,
isVerifiedList,
}: {
assetRows: ManageAssetCurrency[];
payload: AssetLookupDetails;
isVerifiedList: boolean;
}) => {
// scan the first few assets to see if they are suspicious
// due to the length of time it takes to scan, we'll do it in consecutive chunks
for (let i = 0; i < assetRows.length; i += MAX_ASSETS_TO_SCAN) {
const url = new URL(`${INDEXER_URL}/scan-asset-bulk`);
const chunk = assetRows.slice(i, i + MAX_ASSETS_TO_SCAN);
chunk.forEach((record) => {
if (record.code && record.issuer) {
url.searchParams.append(
"asset_ids",
`${record.code}-${record.issuer}`,
);
}
});
const response = await fetch(url.href);
const data = await response.json();
const blockaidScanChunkResults: {
[key: string]: BlockAidScanAssetResult;
} = data.data.results;
// take our scanned assets and update the assetRows with the new isSuspicious values
const assetRowsAddendum = assetRows
.slice(i, i + MAX_ASSETS_TO_SCAN)
.map((row) => {
const assetId = `${row.code}-${row.issuer}`;
return {
...row,
isSuspicious: blockaidScanChunkResults[assetId]
? isAssetSuspicious(blockaidScanChunkResults[assetId])
: row.isSuspicious,
};
});
// insert our newly scanned rows into the existing data
assetRows = [
...assetRows.slice(0, i),
...assetRowsAddendum,
...assetRows.slice(i + MAX_ASSETS_TO_SCAN),
];
const rowsToUpdate = isVerifiedList
? "verifiedAssetRows"
: "unverifiedAssetRows";
dispatch({
type: "FETCH_DATA_SUCCESS",
payload: { ...payload, [rowsToUpdate]: assetRows },
});
}
};
/*
* Fetches token data for the given contract ID.
* We have multiple ways to get token information, but this function
* always returns an array of an ManageAssetCurrency object.
* This array will always have one object in it because we'll always only have one result with a token lookup.
*
* @param {string} contractId - The contract ID to look up.
* @returns {Promise<ManageAssetCurrency[]>}
*/
const fetchTokenData = async (
publicKey: string,
contractId: string,
isAllowListVerificationEnabled: boolean,
networkDetails: NetworkDetails,
) => {
let assetRows = [] as ManageAssetCurrency[];
const nativeContractDetails = getNativeContractDetails(networkDetails);
let verifiedTokens = [] as VerifiedTokenRecord[];
// we already have native contract info, so just load it statically
if (nativeContractDetails.contract === contractId) {
// override our rules for verification for XLM
isVerificationInfoShowing = false;
assetRows = [
{
code: nativeContractDetails.code,
issuer: nativeContractDetails.issuer,
contract: contractId,
domain: nativeContractDetails.domain,
name: `${nativeContractDetails.code}:${nativeContractDetails.issuer}`,
},
];
return assetRows;
}
const tokenLookup = async () => {
// lookup contract
isVerifiedToken = false;
const tokenDetailsResponse = await getTokenDetails({
contractId,
publicKey,
networkDetails,
});
const isSacContract = await isSacContractExecutable(
contractId,
networkDetails,
);
if (!tokenDetailsResponse) {
assetRows = [];
} else {
const issuer = isSacContract
? tokenDetailsResponse.name.split(":")[1] || ""
: contractId; // get the issuer name, if applicable ,
const scannedAsset = await scanAsset(
`${tokenDetailsResponse.symbol}-${issuer}`,
networkDetails,
);
assetRows = [
{
code: tokenDetailsResponse.symbol,
contract: contractId,
issuer,
domain: "",
name: tokenDetailsResponse.name,
isSuspicious: isAssetSuspicious(scannedAsset),
},
];
}
};
// If we're using asset lists, we can retrieve asset info from the list
if (isAllowListVerificationEnabled) {
verifiedTokens = await getVerifiedTokens({
networkDetails,
contractId,
assetsLists,
});
try {
if (verifiedTokens.length) {
isVerifiedToken = true;
verifiedLists = verifiedTokens[0].verifiedLists;
assetRows = verifiedTokens.map((record: VerifiedTokenRecord) => ({
code: record.code || record.contract,
issuer: record.issuer || record.contract,
image: record.icon,
domain: record.domain,
contract: record.contract,
}));
} else {
// token not found on asset list, look up the details manually using RPC
await tokenLookup();
}
} catch (e) {
captureException(
`Failed to fetch token details - ${JSON.stringify(e)}`,
);
console.error(e);
return [];
}
} else {
// Futurenet token lookup
await tokenLookup();
}
isVerificationInfoShowing = isAllowListVerificationEnabled;
return assetRows;
};
/*
* Fetches data from Stellar Expert for the given asset.
* It returns an array of ManageAssetCurrency objects.
*
* @param {string} asset - The asset to look up.
* @returns {Promise<ManageAssetCurrency[]>}
*/
const fetchStellarExpertData = async ({
asset,
networkDetails,
}: {
asset: string;
networkDetails: NetworkDetails;
}): Promise<ManageAssetCurrency[]> => {
const resJson = await searchAsset({
asset,
networkDetails,
});
return resJson._embedded.records.map((record: AssetRecord) => ({
code: record.asset.split("-")[0],
issuer: record.asset.split("-")[1],
domain: record.domain,
image: record.tomlInfo?.image,
isSuspicious: false,
}));
};
/*
* Fetches data for the asset lookup.
* If the asset is a contract ID, it fetches token data.
* Otherwise, it fetches Stellar Expert data.
* It also scans assets using BlockAid if enabled.
*
* @param {string} asset - The asset to look up.
* @param {boolean} isBlockaidEnabled - Whether BlockAid is enabled.
*/
const fetchData = async ({
asset,
isBlockaidEnabled,
publicKey,
isAllowListVerificationEnabled,
networkDetails,
}: {
asset: string;
isBlockaidEnabled: boolean;
publicKey: string;
isAllowListVerificationEnabled: boolean;
networkDetails: NetworkDetails;
}) => {
dispatch({ type: "FETCH_DATA_START" });
if (!asset) {
dispatch({
type: "FETCH_DATA_SUCCESS",
payload: DEFAULT_PAYLOAD,
});
return;
}
let assetRows: ManageAssetCurrency[] = [];
const blockaidScanResults: { [key: string]: BlockAidScanAssetResult } = {};
if (isContractId(asset)) {
// for custom tokens, try to go down the the custom token flow
try {
assetRows = await fetchTokenData(
publicKey,
asset,
isAllowListVerificationEnabled,
networkDetails,
);
} catch (e) {
captureException(
`Failed to fetch token details - ${JSON.stringify(e)}`,
);
console.error(e);
dispatch({ type: "FETCH_DATA_ERROR", payload: e });
return;
}
} else {
// otherwise, try to use stellar.expert to search for a classic asset
try {
assetRows = await fetchStellarExpertData({ asset, networkDetails });
} catch (error) {
captureException(
`Failed to fetch StellarExpert data - ${JSON.stringify(error)}`,
);
console.error(error);
dispatch({ type: "FETCH_DATA_ERROR", payload: DEFAULT_PAYLOAD });
return;
}
}
const assetsListsData = await getCombinedAssetListData({
networkDetails,
assetsLists,
});
for (const assetRow of assetRows) {
const key = assetRow.issuer!;
const code = assetRow.code!;
if (!assetRow.image) {
const tokenListIcon = await getIconFromTokenLists({
networkDetails,
issuerId: key,
contractId: assetRow.contract,
code,
assetsListsData,
});
if (tokenListIcon.icon && tokenListIcon.canonicalAsset) {
assetRow.image = tokenListIcon.icon;
}
}
}
const { verifiedAssets, unverifiedAssets } =
await splitVerifiedAssetCurrency({
networkDetails,
assets: assetRows,
assetsListsDetails: assetsLists,
});
const payload = {
verifiedAssetRows: verifiedAssets,
unverifiedAssetRows: unverifiedAssets,
isVerifiedToken,
isVerificationInfoShowing,
verifiedLists,
blockaidScanResults,
};
dispatch({ type: "FETCH_DATA_SUCCESS", payload });
// If we want to gather Blockaid data about our assets, we'll fetch the data and then combine with our
// asset rows so we have all of our information in one object
if (isBlockaidEnabled && !isContractId(asset)) {
if (verifiedAssets.length) {
addBlockaidScanResults({
assetRows: verifiedAssets,
payload,
isVerifiedList: true,
});
}
if (unverifiedAssets.length) {
addBlockaidScanResults({
assetRows: unverifiedAssets,
payload,
isVerifiedList: false,
});
}
}
};
return {
fetchData,
state,
};
};
export { useAssetLookup, RequestState };