diff --git a/packages/extension/src/libs/tokens-state/index.ts b/packages/extension/src/libs/tokens-state/index.ts index 919e28fed..f62e84d4f 100644 --- a/packages/extension/src/libs/tokens-state/index.ts +++ b/packages/extension/src/libs/tokens-state/index.ts @@ -35,7 +35,7 @@ export class TokensState { if ( t.type === TokenType.ERC20 && (t as CustomErc20Token).address.toLowerCase() === - token.address.toLowerCase() + token.address.toLowerCase() ) { return false; } @@ -54,6 +54,44 @@ export class TokensState { return true; } + /** + * Remove a custom ERC20 token from a given network. + * Returns `true` if the token was removed and false otherwise. + * @param {NetworkNames} chainName - The name of the network the token is being removed from. + * @param {string} address - The address of the token being removed. + */ + async removeErc20Token( + chainName: NetworkNames, + address: string, + ): Promise { + const state: IState | null = await this.storage.get(StorageKeys.customTokens); + + if (state && state[chainName]) { + const tokens = state[chainName]; + + for (let i = 0; i < tokens!.length; i++) { + const token = tokens![i]; + + if ( + token.type === TokenType.ERC20 && + (token as CustomErc20Token).address.toLowerCase() === + address.toLowerCase() + ) { + tokens!.splice(i, 1); + + if (tokens!.length === 0) { + delete state[chainName]; + } + + await this.storage.set(StorageKeys.customTokens, state); + return true; + } + } + } + + return false; + } + async getTokensByNetwork(chainName: NetworkNames): Promise { const state: IState | null = await this.storage.get( StorageKeys.customTokens, diff --git a/packages/extension/src/providers/ethereum/types/evm-network.ts b/packages/extension/src/providers/ethereum/types/evm-network.ts index c4897a4e3..10a4158f2 100644 --- a/packages/extension/src/providers/ethereum/types/evm-network.ts +++ b/packages/extension/src/providers/ethereum/types/evm-network.ts @@ -157,7 +157,7 @@ export class EvmNetwork extends BaseNetwork { decimals: this.decimals, sparkline: nativeMarketData ? new Sparkline(nativeMarketData.sparkline_in_24h.price, 25) - .dataValues + .dataValues : '', priceChangePercentage: nativeMarketData?.price_change_percentage_24h_in_currency ?? 0, @@ -197,7 +197,6 @@ export class EvmNetwork extends BaseNetwork { assets = [nativeAsset, ...assetInfos]; } - const customTokens = await tokensState .getTokensByNetwork(this.name) .then(tokens => { @@ -211,7 +210,7 @@ export class EvmNetwork extends BaseNetwork { a.contract && (token as CustomErc20Token).address && a.contract.toLowerCase() === - (token as CustomErc20Token).address.toLowerCase() + (token as CustomErc20Token).address.toLowerCase() ) { return false; } diff --git a/packages/extension/src/ui/action/views/asset-detail-view/index.vue b/packages/extension/src/ui/action/views/asset-detail-view/index.vue index 20c6aa868..1e484a44b 100644 --- a/packages/extension/src/ui/action/views/asset-detail-view/index.vue +++ b/packages/extension/src/ui/action/views/asset-detail-view/index.vue @@ -52,12 +52,24 @@

${{ token.balanceUSDf }}

+
+
+ + *Deleting custom token deletes it for the whole network. +