Skip to content

Commit

Permalink
[Issue-3960] Update enable native token when turn on token
Browse files Browse the repository at this point in the history
  • Loading branch information
dungnguyen-art committed Jan 8, 2025
1 parent fd15c4d commit 902496f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ export default class KoniState {
const tokenSlug = this.chainService.upsertCustomToken(data);

if (_isAssetFungibleToken(data)) {
await this.chainService.updateAssetSetting(tokenSlug, { visible: true });
await this.chainService.updateAssetSetting(tokenSlug, { visible: true }, true);
this.eventService.emit('asset.updateState', tokenSlug);
} else {
this.eventService.emit('asset.updateState', tokenSlug);
Expand Down
4 changes: 2 additions & 2 deletions packages/extension-base/src/services/chain-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SubstrateChainHandler } from '@subwallet/extension-base/services/chain-
import { TonChainHandler } from '@subwallet/extension-base/services/chain-service/handler/TonChainHandler';
import { _CHAIN_VALIDATION_ERROR } from '@subwallet/extension-base/services/chain-service/handler/types';
import { _ChainApiStatus, _ChainConnectionStatus, _ChainState, _CUSTOM_PREFIX, _DataMap, _EvmApi, _NetworkUpsertParams, _NFT_CONTRACT_STANDARDS, _SMART_CONTRACT_STANDARDS, _SmartContractTokenInfo, _SubstrateApi, _ValidateCustomAssetRequest, _ValidateCustomAssetResponse } from '@subwallet/extension-base/services/chain-service/types';
import { _isAssetAutoEnable, _isAssetCanPayTxFee, _isAssetFungibleToken, _isChainEnabled, _isCustomAsset, _isCustomChain, _isCustomProvider, _isEqualContractAddress, _isEqualSmartContractAsset, _isLocalToken, _isMantaZkAsset, _isNativeToken, _isPureEvmChain, _isPureSubstrateChain, _parseAssetRefKey, randomizeProvider, updateLatestChainInfo } from '@subwallet/extension-base/services/chain-service/utils';
import { _isAssetAutoEnable, _isAssetCanPayTxFee, _isAssetFungibleToken, _isChainEnabled, _isCustomAsset, _isCustomChain, _isCustomProvider, _isEqualContractAddress, _isEqualSmartContractAsset, _isLocalToken, _isMantaZkAsset, _isNativeToken, _isNativeTokenBySlug, _isPureEvmChain, _isPureSubstrateChain, _parseAssetRefKey, randomizeProvider, updateLatestChainInfo } from '@subwallet/extension-base/services/chain-service/utils';
import { EventService } from '@subwallet/extension-base/services/event-service';
import { IChain, IMetadataItem } from '@subwallet/extension-base/services/storage-service/databases';
import DatabaseService from '@subwallet/extension-base/services/storage-service/DatabaseService';
Expand Down Expand Up @@ -760,7 +760,7 @@ export class ChainService {

if (!assetState) { // If this asset not has asset setting, this token is not enabled before (not turned off before)
if (!chainState || !chainState.manualTurnOff) {
await this.updateAssetSetting(assetSlug, { visible: true });
await this.updateAssetSetting(assetSlug, { visible: true }, true);
}
} else {
if (originChain === 'avail_mainnet') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@

import { _ChainAsset } from '@subwallet/chain-list/types';
import { AssetSetting } from '@subwallet/extension-base/background/KoniTypes';
import { _isNativeToken } from '@subwallet/extension-base/services/chain-service/utils';
import useNotification from '@subwallet/extension-koni-ui/hooks/common/useNotification';
import useTranslation from '@subwallet/extension-koni-ui/hooks/common/useTranslation';
import { updateAssetSetting } from '@subwallet/extension-koni-ui/messaging';
import { ThemeProps } from '@subwallet/extension-koni-ui/types';
import { Button, Icon, Switch } from '@subwallet/react-ui';
import { PencilSimpleLine } from 'phosphor-react';
import React, { useCallback, useState } from 'react';
import React, { useCallback, useMemo, useState } from 'react';
import { NavigateFunction } from 'react-router';
import styled from 'styled-components';

Expand All @@ -25,6 +26,10 @@ function Component ({ assetSetting, className = '', navigate, tokenInfo }: Props

const [loading, setLoading] = useState(false);

const isNativeToken = useMemo(() => {
return _isNativeToken(tokenInfo);
}, [tokenInfo]);

const onSwitchTokenVisible = useCallback((checked: boolean, event: React.MouseEvent<HTMLButtonElement>) => {
if (!loading) {
setLoading(true);
Expand All @@ -33,7 +38,8 @@ function Component ({ assetSetting, className = '', navigate, tokenInfo }: Props
tokenSlug: tokenInfo.slug,
assetSetting: {
visible: checked
}
},
autoEnableNativeToken: !isNativeToken
})
.then((result) => {
if (!result) {
Expand All @@ -54,7 +60,7 @@ function Component ({ assetSetting, className = '', navigate, tokenInfo }: Props
});
}, 300);
}
}, [loading, showNotification, t, tokenInfo.slug]);
}, [isNativeToken, loading, showNotification, t, tokenInfo.slug]);

const onClick = useCallback(() => {
navigate('/settings/tokens/detail', { state: tokenInfo.slug });
Expand Down

0 comments on commit 902496f

Please sign in to comment.