Skip to content

Commit

Permalink
[Issue-3132] fix: apply chain state for new chain
Browse files Browse the repository at this point in the history
  • Loading branch information
bluezdot committed Nov 5, 2024
1 parent a47c54d commit 93c0939
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions packages/extension-base/src/koni/background/handlers/State.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ export default class KoniState {

await this.startSubscription();

this.chainService.checkLatestData();
this.chainOnlineService.checkLatestData();
this.chainService.checkLatestData();
}

public async initMantaPay (password: string) {
Expand Down Expand Up @@ -1645,8 +1645,8 @@ export default class KoniState {
await this.chainService.init();
this.afterChainServiceInit();

this.chainService.checkLatestData();
this.chainOnlineService.checkLatestData();
this.chainService.checkLatestData();
}

public async enableMantaPay (updateStore: boolean, address: string, password: string, seedPhrase?: string) {
Expand Down
31 changes: 27 additions & 4 deletions packages/extension-base/src/services/chain-online-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MultiChainAssetMap } from '@subwallet/chain-list';
import { _ChainAsset, _ChainInfo, _MultiChainAsset } from '@subwallet/chain-list/types';
import { ChainService, filterAssetInfoMap } from '@subwallet/extension-base/services/chain-service';
import { LATEST_CHAIN_DATA_FETCHING_INTERVAL } from '@subwallet/extension-base/services/chain-service/constants';
import { _ChainState } from '@subwallet/extension-base/services/chain-service/types';
import { fetchPatchData, PatchInfo } from '@subwallet/extension-base/services/chain-service/utils';
import { EventService } from '@subwallet/extension-base/services/event-service';
import { Md5 } from 'ts-md5';
Expand Down Expand Up @@ -90,19 +91,37 @@ export class ChainOnlineService {
// 1. validate fetch data with its hash
const isSafePatch = this.validatePatchWithHash(latestPatch);
const { ChainAsset: latestAssetInfo, ChainInfo: latestChainInfo, MultiChainAsset: latestMultiChainAsset, patchVersion } = latestPatch;
let chainInfoMap = {};
let assetRegistry = {};
let multiChainAssetMap = {};
let chainInfoMap: Record<string, _ChainInfo> = {};
let assetRegistry: Record<string, _ChainAsset> = {};
let multiChainAssetMap: Record<string, _MultiChainAsset> = {};
let currentChainState: Record<string, _ChainState> = {};

let addedChain: string[] = [];
// todo: AssetLogoMap, ChainLogoMap

if (isSafePatch && this.patchVersion !== patchVersion) {
// 2. merge data map
if (latestChainInfo && Object.keys(latestChainInfo).length > 0) {
chainInfoMap = Object.assign({}, this.chainService.getChainInfoMap(), latestChainInfo);

currentChainState = this.chainService.getChainStateMap();
const currentChainStateKey = Object.keys(currentChainState);
const newChainStateKey = Object.keys(chainInfoMap);

addedChain = newChainStateKey.filter((chain) => !currentChainStateKey.includes(chain));

addedChain.forEach((key) => {
currentChainState[key] = {
active: false,
currentProvider: Object.keys(chainInfoMap[key].providers)[0],
manualTurnOff: false,
slug: key
};
});
}

if (latestAssetInfo && Object.keys(latestAssetInfo).length > 0) {
assetRegistry = filterAssetInfoMap(this.chainService.getChainInfoMap(), Object.assign({}, this.chainService.getAssetRegistry(), latestAssetInfo));
assetRegistry = filterAssetInfoMap(this.chainService.getChainInfoMap(), Object.assign({}, this.chainService.getAssetRegistry(), latestAssetInfo), addedChain);
}

if (latestMultiChainAsset && Object.keys(latestMultiChainAsset).length > 0) {
Expand All @@ -126,6 +145,10 @@ export class ChainOnlineService {
.catch(console.error);

this.chainService.subscribeMultiChainAssetMap().next(multiChainAssetMap);

this.chainService.setChainStateMap(currentChainState);
this.chainService.subscribeChainStateMap().next(currentChainState);

this.patchVersion = patchVersion;
}
}
Expand Down
8 changes: 6 additions & 2 deletions packages/extension-base/src/services/chain-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ const ignoredList = [
'storyPartner_testnet'
];

export const filterAssetInfoMap = (chainInfo: Record<string, _ChainInfo>, assets: Record<string, _ChainAsset>): Record<string, _ChainAsset> => {
export const filterAssetInfoMap = (chainInfo: Record<string, _ChainInfo>, assets: Record<string, _ChainAsset>, addedChains?: string[]): Record<string, _ChainAsset> => {
return Object.fromEntries(
Object.entries(assets)
.filter(([, info]) => chainInfo[info.originChain])
.filter(([, info]) => chainInfo[info.originChain] || addedChains?.includes(info.originChain))
);
};

Expand Down Expand Up @@ -359,6 +359,10 @@ export class ChainService {
return this.dataMap.chainStateMap;
}

public setChainStateMap (chainStateMap: Record<string, _ChainState>) {
this.dataMap.chainStateMap = chainStateMap;
}

public getChainStateByKey (key: string) {
return this.dataMap.chainStateMap[key];
}
Expand Down

0 comments on commit 93c0939

Please sign in to comment.