diff --git a/scripts/sync-support-chain.js b/scripts/sync-support-chain.js index cd88f191a6b..237c89dc064 100644 --- a/scripts/sync-support-chain.js +++ b/scripts/sync-support-chain.js @@ -10,7 +10,7 @@ const targetPath = path.join( ); const res = child_process.execSync( - 'curl -s https://api.rabby.io/v1/wallet/supported_chains' + 'curl -s https://static.debank.com/supported_chains.json' ); let supported_chains_list = []; try { diff --git a/src/background/controller/wallet.ts b/src/background/controller/wallet.ts index 20c8910d541..477c4946710 100644 --- a/src/background/controller/wallet.ts +++ b/src/background/controller/wallet.ts @@ -52,6 +52,7 @@ import { ERC20ABI } from 'consts/abi'; import { Account, IHighlightedAddress } from '../service/preference'; import { ConnectedSite } from '../service/permission'; import openapi, { + SupportedChain, TokenItem, Tx, testnetOpenapiService, @@ -111,6 +112,8 @@ import HdKeyring from '@rabby-wallet/eth-hd-keyring'; import CoinbaseKeyring from '@rabby-wallet/eth-coinbase-keyring/dist/coinbase-keyring'; import { customTestnetService } from '../service/customTestnet'; import { getKeyringBridge, hasBridge } from '../service/keyring/bridge'; +import { http } from '../utils/http'; +import { syncChainService } from '../service/syncChain'; const stashKeyrings: Record = {}; @@ -3746,25 +3749,7 @@ export class WalletController extends BaseController { return res; }; - syncMainnetChainList = async () => { - try { - const chains = await openapi.getSupportedChains(); - const list: Chain[] = chains - .filter((item) => !item.is_disabled) - .map((item) => { - const chain: Chain = supportedChainToChain(item); - return chain; - }); - updateChainStore({ - mainnetList: list, - }); - chrome.storage.local.set({ - rabbyMainnetChainList: list, - }); - } catch (e) { - console.error('fetch chain list error: ', e); - } - }; + syncMainnetChainList = syncChainService.syncMainnetChainList; } const wallet = new WalletController(); diff --git a/src/background/index.ts b/src/background/index.ts index 322e374059b..87d2aa874c3 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -43,6 +43,7 @@ import fetchAdapter from '@vespaiach/axios-fetch-adapter'; import Safe from '@rabby-wallet/gnosis-sdk'; import { customTestnetService } from './service/customTestnet'; import { findChain } from '@/utils/chain'; +import { syncChainService } from './service/syncChain'; Safe.adapter = fetchAdapter as any; @@ -97,6 +98,7 @@ async function restoreAppState() { appStoreLoaded = true; + syncChainService.roll(); transactionWatchService.roll(); transactionBroadcastWatchService.roll(); startEnableUser(); diff --git a/src/background/service/syncChain.ts b/src/background/service/syncChain.ts new file mode 100644 index 00000000000..e8e71b8e634 --- /dev/null +++ b/src/background/service/syncChain.ts @@ -0,0 +1,65 @@ +import { supportedChainToChain, updateChainStore } from '@/utils/chain'; +import { isManifestV3 } from '@/utils/env'; +import { Chain } from '@debank/common'; +import browser from 'webextension-polyfill'; +import { ALARMS_SYNC_CHAINS } from '../utils/alarms'; +import { http } from '../utils/http'; +import { SupportedChain } from './openapi'; + +class SyncChainService { + timer: ReturnType | null = null; + + syncMainnetChainList = async () => { + try { + const chains = await http + .get('https://static.debank.com/supported_chains.json') + .then((res) => { + return res.data as SupportedChain[]; + }); + const list: Chain[] = chains + .filter((item) => !item.is_disabled) + .map((item) => { + const chain: Chain = supportedChainToChain(item); + return chain; + }); + updateChainStore({ + mainnetList: list, + }); + chrome.storage.local.set({ + rabbyMainnetChainList: list, + }); + } catch (e) { + console.error('fetch chain list error: ', e); + } + }; + + resetTimer = () => { + const periodInMinutes = 60; + if (this.timer) { + clearTimeout(this.timer); + } else if (isManifestV3) { + browser.alarms.clear(ALARMS_SYNC_CHAINS); + } + + if (isManifestV3) { + browser.alarms.create(ALARMS_SYNC_CHAINS, { + delayInMinutes: periodInMinutes, + periodInMinutes: periodInMinutes, + }); + browser.alarms.onAlarm.addListener((alarm) => { + if (alarm.name === ALARMS_SYNC_CHAINS) { + this.syncMainnetChainList(); + } + }); + } else { + this.timer = setInterval(() => { + this.syncMainnetChainList(); + }, periodInMinutes * 60 * 1000); + } + }; + roll = () => { + this.resetTimer(); + }; +} + +export const syncChainService = new SyncChainService(); diff --git a/src/background/utils/alarms.ts b/src/background/utils/alarms.ts index 9deeb50727d..ec59beca4da 100644 --- a/src/background/utils/alarms.ts +++ b/src/background/utils/alarms.ts @@ -1,3 +1,4 @@ export const ALARMS_AUTO_LOCK = 'ALARMS_AUTO_LOCK'; export const ALARMS_RPC_CACHE = 'ALARMS_RPC_CACHE'; export const ALARMS_RELOAD_TX = 'ALARMS_RELOAD_TX'; +export const ALARMS_SYNC_CHAINS = 'ALARMS_SYNC_CHAINS';