-
Notifications
You must be signed in to change notification settings - Fork 306
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: reduce block number queries (#3511)
* chore: update weth address * refactor: move chain state to wallet plugin * chore: add useStartWatchChainState * fix: fetch balance if wallet is just created * refactor: batch requests * refactor: remove unused deps Co-authored-by: Jack Works <[email protected]> * refactor: reply reviews * fix: chain name Co-authored-by: Jack Works <[email protected]>
- Loading branch information
1 parent
c74a884
commit ed441b6
Showing
15 changed files
with
145 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
packages/maskbook/src/extension/background-script/EthereumServices/chainState.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/maskbook/src/plugins/Wallet/hooks/useStartWatchChainState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { useEffect } from 'react' | ||
import { pollingTask } from '../../../utils' | ||
import { UPDATE_CHAIN_STATE_DELAY } from '../constants' | ||
import { WalletRPC } from '../messages' | ||
|
||
const task = pollingTask( | ||
async () => { | ||
await WalletRPC.kickToUpdateChainState() | ||
return false | ||
}, | ||
{ | ||
autoStart: false, | ||
delay: UPDATE_CHAIN_STATE_DELAY, | ||
}, | ||
) | ||
|
||
export function useStartWatchChainState() { | ||
useEffect(() => { | ||
// emit an updating request immediately | ||
WalletRPC.updateChainState() | ||
}, []) | ||
return useEffect(() => { | ||
// start the polling task | ||
task.reset() | ||
return () => task.cancel() | ||
}, []) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { ChainId, getNetworkTypeFromChainId, ProviderType } from '@dimensiondev/web3-shared' | ||
import { getBalance, getBlockNumber, resetAllNonce } from '../../../extension/background-script/EthereumService' | ||
import { pollingTask, startEffects } from '../../../utils' | ||
import { | ||
currentAccountSettings, | ||
currentBalanceSettings, | ||
currentBlockNumberSettings, | ||
currentChainIdSettings, | ||
currentNetworkSettings, | ||
currentProviderSettings, | ||
} from '../settings' | ||
import { UPDATE_CHAIN_STATE_DELAY } from '../constants' | ||
import { getWallet } from './wallet' | ||
|
||
const beats: true[] = [] | ||
|
||
export async function kickToUpdateChainState() { | ||
beats.push(true) | ||
} | ||
|
||
export async function updateChainState(chainId?: ChainId) { | ||
// reset the polling task cause it will be called from service call | ||
resetPoolTask() | ||
|
||
// forget those passed beats | ||
beats.length = 0 | ||
|
||
// update network type | ||
if (chainId) currentNetworkSettings.value = getNetworkTypeFromChainId(chainId) | ||
|
||
// update chain state | ||
try { | ||
const wallet = await getWallet() | ||
;[currentBlockNumberSettings.value, currentBalanceSettings.value] = await Promise.all([ | ||
getBlockNumber(), | ||
wallet ? getBalance(wallet.address) : currentBalanceSettings.value, | ||
]) | ||
} catch (error) { | ||
// do nothing | ||
} finally { | ||
// reset the polling if chain state updated successfully | ||
resetPoolTask() | ||
} | ||
} | ||
|
||
let resetPoolTask: () => void = () => {} | ||
|
||
const effect = startEffects(import.meta.webpackHot) | ||
|
||
// poll the newest chain state | ||
effect(() => { | ||
const { reset, cancel } = pollingTask( | ||
async () => { | ||
if (beats.length <= 0) return false | ||
await updateChainState() | ||
return false | ||
}, | ||
{ | ||
delay: UPDATE_CHAIN_STATE_DELAY, | ||
}, | ||
) | ||
resetPoolTask = reset | ||
return cancel | ||
}) | ||
|
||
// revalidate chain state if the chainId of current provider was changed | ||
effect(() => | ||
currentChainIdSettings.addListener((chainId) => { | ||
updateChainState(chainId) | ||
if (currentProviderSettings.value === ProviderType.Maskbook) resetAllNonce() | ||
}), | ||
) | ||
|
||
// revalidate chain state if the current wallet was changed | ||
effect(() => currentAccountSettings.addListener(() => updateChainState())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters