Skip to content

Commit

Permalink
[Issue-3960] Create migration job to disable zero-balance tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
dungnguyen-art committed Jan 7, 2025
1 parent 2741ac5 commit 93d7267
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 5 deletions.
8 changes: 4 additions & 4 deletions packages/extension-base/src/services/chain-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2030,17 +2030,17 @@ export class ChainService {
public async updatePopularAssetsByChain (chainSlug: string, visible: boolean) {
const currentAssetSettings = await this.getAssetSettings();
const assetsByChain = this.getFungibleTokensByChain(chainSlug);
const popularTokensMap = this.popularTokensSubject.value || {};
const priorityTokensMap = this.priorityTokensSubject.value || {};

const popularTokensList = Object.values(popularTokensMap).flatMap((tokenData) =>
Object.keys(tokenData.tokens)
const priorityTokensList = Object.values(priorityTokensMap).flatMap((tokenData) =>
Object.keys(tokenData.priorityTokens)
);

let popularTokenFound = false;

for (const asset of Object.values(assetsByChain)) {
if (visible) {
const isPopularToken = popularTokensList.includes(asset.slug);
const isPopularToken = priorityTokensList.includes(asset.slug);

if (isPopularToken) {
currentAssetSettings[asset.slug] = { visible: true };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2019-2022 @subwallet/extension-koni authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { TokenPriorityDetails } from '@subwallet/extension-base/background/KoniTypes';
import { _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
import BaseMigrationJob from '@subwallet/extension-base/services/migration-service/Base';
import { fetchStaticData } from '@subwallet/extension-base/utils';

// Usage:
// 1. Disable tokens with a balance of 0, except for the native token and priorityToken.

export default class DisableZeroBalanceTokens extends BaseMigrationJob {
public override async run (): Promise<void> {
const state = this.state;

try {
const rawBalanceMap = await state.dbService.getStoredBalance();
const tokensList = await state.chainService.getAssetSettings();

const balanceNonZero = rawBalanceMap.filter((item) => {
return (BigInt(item.free) + BigInt(item.locked) > 0);
});

const priorityTokensMap = await fetchStaticData<Record<string, TokenPriorityDetails>>('chain-assets/priority-tokens') || [];
const priorityTokensList = Object.values(priorityTokensMap).flatMap((tokenData) =>
Object.keys(tokenData.priorityTokens)
);
// Extract the slugs of tokens with balance > 0
const allowedSlugs = new Set(balanceNonZero.map((item) => item.tokenSlug));
const updatedSettings = { ...tokensList };

Object.keys(tokensList).forEach((slug) => {
const originAsset = state.chainService.getAssetBySlug(slug);

// Check if it is a native token
const isNativeToken = originAsset && _isNativeToken(originAsset);

// Check if it is a popular token
const isPopularToken = priorityTokensList.includes(slug);

if (!isNativeToken && !isPopularToken && !allowedSlugs.has(slug)) {
updatedSettings[slug] = {
visible: false
};
}
});

state.chainService.setAssetSettings(updatedSettings);
} catch (error) {
console.error(error);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import AutoEnableChainsTokens from './AutoEnableChainsTokens';
import DeleteChain from './DeleteChain';
import DeleteChainStaking from './DeleteChainStaking';
import DeleteEarningData from './DeleteEarningData';
import DisableZeroBalanceTokens from './DisableZeroBalanceTokens';
import EnableVaraChain from './EnableVaraChain';
import MigrateAuthUrls from './MigrateAuthUrls';
import MigrateAutoLock from './MigrateAutoLock';
Expand Down Expand Up @@ -64,7 +65,8 @@ export default <Record<string, typeof BaseMigrationJob>>{
'1.2.13-01': ReloadMetadata,
'1.2.14-01': ClearMetadataDatabase,
'1.2.32-01': MigratePairData,
'1.3.6-01': MigrateTransactionHistoryBridge
'1.3.6-01': MigrateTransactionHistoryBridge,
'1.3.12': DisableZeroBalanceTokens
// [`${EVERYTIME}-1.1.42-02`]: MigrateTransactionHistoryBySymbol
// [`${EVERYTIME}-1`]: AutoEnableChainsTokens
};

0 comments on commit 93d7267

Please sign in to comment.