-
Notifications
You must be signed in to change notification settings - Fork 308
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add balance & blocknumber notifier
- Loading branch information
1 parent
df90e51
commit 5d31864
Showing
11 changed files
with
99 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Emitter } from '@servie/events'; | ||
import type { BalanceEvent, BalanceNotifierState as Web3BalanceNotifierState } from '@masknet/web3-shared-base' | ||
|
||
export class BalanceNotifierState<ChainId> implements Web3BalanceNotifierState<ChainId> { | ||
emitter: Emitter<BalanceEvent<ChainId>> = new Emitter() | ||
} |
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,6 @@ | ||
import { Emitter } from '@servie/events'; | ||
import type { BlockNumberEvent, BlockNumberNotifierState as Web3BlockNumberNotifierState } from '@masknet/web3-shared-base' | ||
|
||
export class BlockNumberNotifierState<ChainId> implements Web3BlockNumberNotifierState<ChainId> { | ||
emitter: Emitter<BlockNumberEvent<ChainId>> = new Emitter() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,34 @@ | ||
import { useEffect } from 'react' | ||
import { useAsyncRetry } from 'react-use' | ||
import { noop } from 'lodash-unified' | ||
import type { NetworkPluginID } from '@masknet/web3-shared-base' | ||
import type { Web3Helper } from '../web3-helpers' | ||
import { useAccount } from './useAccount' | ||
import { useWeb3Connection } from './useWeb3Connection' | ||
import { useWeb3State } from '../entry-web3' | ||
|
||
export function useBalance<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>( | ||
pluginID?: T, | ||
options?: Web3Helper.Web3ConnectionOptionsScope<S, T>, | ||
) { | ||
const account = useAccount(pluginID, options?.account) | ||
const connection = useWeb3Connection(pluginID, options) | ||
const { BalanceNotifier, Others } = useWeb3State(pluginID) | ||
|
||
return useAsyncRetry(async () => { | ||
const asyncResult = useAsyncRetry(async () => { | ||
if (!account || !connection) return '0' | ||
return connection.getBalance(account) | ||
}, [account, connection]) | ||
|
||
useEffect(() => { | ||
return ( | ||
BalanceNotifier?.emitter.on('update', (ev) => { | ||
if (Others?.isSameAddress(account, ev.account)) { | ||
asyncResult.retry() | ||
} | ||
}) ?? noop | ||
) | ||
}, [account, asyncResult.retry, Others]) | ||
|
||
return asyncResult | ||
} |
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 |
---|---|---|
@@ -1,18 +1,34 @@ | ||
import { useEffect } from 'react' | ||
import { useAsyncRetry } from 'react-use' | ||
import { noop } from 'lodash-unified' | ||
import type { NetworkPluginID } from '@masknet/web3-shared-base' | ||
import type { Web3Helper } from '../web3-helpers' | ||
import { useChainId } from './useChainId' | ||
import { useWeb3Connection } from './useWeb3Connection' | ||
import { useWeb3State } from '../entry-web3' | ||
|
||
export function useBlockNumber<S extends 'all' | void = void, T extends NetworkPluginID = NetworkPluginID>( | ||
pluginID?: T, | ||
options?: Web3Helper.Web3ConnectionOptionsScope<S, T>, | ||
) { | ||
const chainId = useChainId(pluginID, options?.chainId) | ||
const connection = useWeb3Connection(pluginID, options) | ||
const { BlockNumberNotifier } = useWeb3State(pluginID) | ||
|
||
return useAsyncRetry(async () => { | ||
const asyncRetry = useAsyncRetry(async () => { | ||
if (!connection) return 0 | ||
return connection.getBlockNumber() | ||
}, [chainId, connection]) | ||
|
||
useEffect(() => { | ||
return ( | ||
BlockNumberNotifier?.emitter.on('update', (actualChainId) => { | ||
if (actualChainId === chainId) { | ||
asyncRetry.retry() | ||
} | ||
}) ?? noop | ||
) | ||
}, [chainId, asyncRetry]) | ||
|
||
return asyncRetry | ||
} |
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,8 @@ | ||
import { BalanceNotifierState } from '@masknet/plugin-infra/web3' | ||
import type { ChainId } from '@masknet/web3-shared-evm' | ||
|
||
export class BalanceNotifier extends BalanceNotifierState<ChainId> { | ||
constructor() { | ||
super() | ||
} | ||
} |
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,8 @@ | ||
import { BlockNumberNotifierState } from '@masknet/plugin-infra/web3' | ||
import type { ChainId } from '@masknet/web3-shared-evm' | ||
|
||
export class BlockNumberNotifier extends BlockNumberNotifierState<ChainId> { | ||
constructor() { | ||
super() | ||
} | ||
} |
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