Skip to content

Commit

Permalink
feat: alarm sync chain
Browse files Browse the repository at this point in the history
  • Loading branch information
cs1707 committed Mar 28, 2024
1 parent 04d0299 commit 4cc3530
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 20 deletions.
2 changes: 1 addition & 1 deletion scripts/sync-support-chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 4 additions & 19 deletions src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<string | number, any> = {};

Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -97,6 +98,7 @@ async function restoreAppState() {

appStoreLoaded = true;

syncChainService.roll();
transactionWatchService.roll();
transactionBroadcastWatchService.roll();
startEnableUser();
Expand Down
65 changes: 65 additions & 0 deletions src/background/service/syncChain.ts
Original file line number Diff line number Diff line change
@@ -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<typeof setInterval> | 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();
1 change: 1 addition & 0 deletions src/background/utils/alarms.ts
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit 4cc3530

Please sign in to comment.