forked from polkadot-js/extension
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Issue-3960] Create migration job to disable zero-balance tokens
- Loading branch information
1 parent
2741ac5
commit 93d7267
Showing
3 changed files
with
60 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/extension-base/src/services/migration-service/scripts/DisableZeroBalanceTokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters