@metamask-previews/assets-controller 9.1.0-preview-aabb3f2e3 → 9.1.0-preview-ba933fc1b

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.
@@ -1396,30 +1396,44 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1396
1396
  const metadata = state.assetsInfo;
1397
1397
  const balances = state.assetsBalance;
1398
1398
  const prices = state.assetsPrice;
1399
- if (normalizedResponse.assetsInfo && mode !== 'update') {
1400
- for (const [key, value] of Object.entries(normalizedResponse.assetsInfo)) {
1401
- if (!(0, lodash_1.isEqual)(previousState.assetsInfo[key], value)) {
1399
+ if (normalizedResponse.assetsInfo) {
1400
+ if (mode === 'update') {
1401
+ // Balance-only refresh: patch amounts without overwriting metadata,
1402
+ // but seed entries that are missing so RPC-only chains (e.g.
1403
+ // Avalanche) can render native balances on first fetch.
1404
+ for (const [key, value] of Object.entries(normalizedResponse.assetsInfo)) {
1405
+ if (metadata[key]) {
1406
+ continue;
1407
+ }
1408
+ metadata[key] = value;
1402
1409
  changedMetadata.push(key);
1403
1410
  }
1404
- const existing = metadata[key];
1405
- const incoming = value;
1406
- // When the API returns no symbol or name for this token, it has no
1407
- // real knowledge of it (e.g. a user-deployed token not indexed by
1408
- // the API). Preserve richer metadata already in state (e.g. from
1409
- // pendingMetadata set by addCustomAsset) so that the correct
1410
- // decimals/symbol/name/image are not overwritten with empty values.
1411
- if (existing && !incoming.symbol && !incoming.name) {
1412
- metadata[key] = {
1413
- ...existing,
1414
- ...incoming,
1415
- symbol: existing.symbol,
1416
- name: existing.name,
1417
- decimals: existing.decimals ?? incoming.decimals,
1418
- image: existing.image ?? incoming.image,
1419
- };
1420
- }
1421
- else {
1422
- metadata[key] = value;
1411
+ }
1412
+ else {
1413
+ for (const [key, value] of Object.entries(normalizedResponse.assetsInfo)) {
1414
+ if (!(0, lodash_1.isEqual)(previousState.assetsInfo[key], value)) {
1415
+ changedMetadata.push(key);
1416
+ }
1417
+ const existing = metadata[key];
1418
+ const incoming = value;
1419
+ // When the API returns no symbol or name for this token, it has no
1420
+ // real knowledge of it (e.g. a user-deployed token not indexed by
1421
+ // the API). Preserve richer metadata already in state (e.g. from
1422
+ // pendingMetadata set by addCustomAsset) so that the correct
1423
+ // decimals/symbol/name/image are not overwritten with empty values.
1424
+ if (existing && !incoming.symbol && !incoming.name) {
1425
+ metadata[key] = {
1426
+ ...existing,
1427
+ ...incoming,
1428
+ symbol: existing.symbol,
1429
+ name: existing.name,
1430
+ decimals: existing.decimals ?? incoming.decimals,
1431
+ image: existing.image ?? incoming.image,
1432
+ };
1433
+ }
1434
+ else {
1435
+ metadata[key] = value;
1436
+ }
1423
1437
  }
1424
1438
  }
1425
1439
  }
@@ -1515,10 +1529,21 @@ async function _AssetsController_executeMiddlewares(sources, request, initialRes
1515
1529
  balances[accountId] = effective;
1516
1530
  }
1517
1531
  }
1518
- // Update prices in state (skipped for balance-only update mode)
1519
- if (normalizedResponse.assetsPrice && mode !== 'update') {
1520
- for (const [key, value] of Object.entries(normalizedResponse.assetsPrice)) {
1521
- prices[key] = value;
1532
+ // Update prices in state. Update mode only seeds missing entries so
1533
+ // balance-only refreshes do not clobber existing price data.
1534
+ if (normalizedResponse.assetsPrice) {
1535
+ if (mode === 'update') {
1536
+ for (const [key, value] of Object.entries(normalizedResponse.assetsPrice)) {
1537
+ if (prices[key]) {
1538
+ continue;
1539
+ }
1540
+ prices[key] = value;
1541
+ }
1542
+ }
1543
+ else {
1544
+ for (const [key, value] of Object.entries(normalizedResponse.assetsPrice)) {
1545
+ prices[key] = value;
1546
+ }
1522
1547
  }
1523
1548
  }
1524
1549
  });