-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
97 additions
and
24 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
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,54 @@ | ||
import { SENTINEL_ADDRESS } from '@safe-global/safe-core-sdk/dist/src/utils/constants' | ||
import type { Delay } from '@gnosis.pm/zodiac' | ||
|
||
import { getDelayModifiers, MODULE_PAGE_SIZE } from '@/services/recovery/delay-modifier' | ||
import useAsync from '../useAsync' | ||
import useSafeInfo from '../useSafeInfo' | ||
import { useWeb3ReadOnly } from '../wallets/web3' | ||
import type { AsyncResult } from '../useAsync' | ||
import type { RecoveryState } from '@/store/recoverySlice' | ||
|
||
const getRecoveryState = async (delayModifier: Delay): Promise<RecoveryState[number]> => { | ||
const transactionAddedFilter = delayModifier.filters.TransactionAdded() | ||
|
||
const [[modules], txNonce, transactionsAdded] = await Promise.all([ | ||
delayModifier.getModulesPaginated(SENTINEL_ADDRESS, MODULE_PAGE_SIZE), | ||
delayModifier.txNonce(), | ||
delayModifier.queryFilter(transactionAddedFilter), | ||
]) | ||
|
||
return { | ||
address: delayModifier.address, | ||
modules, | ||
txNonce: txNonce.toString(), | ||
transactionsAdded: transactionsAdded.filter(({ args }) => args.queueNonce.gte(txNonce)), | ||
} | ||
} | ||
|
||
// TODO: Polling | ||
const useLoadRecovery = (): AsyncResult<RecoveryState> => { | ||
const { safe } = useSafeInfo() | ||
const web3ReadOnly = useWeb3ReadOnly() | ||
|
||
const [delayModifiers, delayModifiersError, delayModifiersLoading] = useAsync<Array<Delay>>(() => { | ||
if (!web3ReadOnly || safe.modules?.length === 0) { | ||
return | ||
} | ||
|
||
return getDelayModifiers(safe.chainId, safe.modules, web3ReadOnly) | ||
// Need to check length of modules array to prevent new request every time Safe info polls | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [safe.chainId, safe.modules?.length, web3ReadOnly]) | ||
|
||
const [recoveryState, recoveryStateError, recoveryStateLoading] = useAsync<RecoveryState>(() => { | ||
if (!delayModifiers || delayModifiers.length === 0) { | ||
return | ||
} | ||
|
||
return Promise.all(delayModifiers.map(getRecoveryState)) | ||
}, [delayModifiers]) | ||
|
||
return [recoveryState, delayModifiersError || recoveryStateError, delayModifiersLoading || recoveryStateLoading] | ||
} | ||
|
||
export default useLoadRecovery |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createSelector } from '@reduxjs/toolkit' | ||
import type { TransactionAddedEvent } from '@gnosis.pm/zodiac/dist/cjs/types/Delay' | ||
|
||
import { makeLoadableSlice } from './common' | ||
|
||
export type RecoveryState = Array<{ | ||
address: string | ||
modules: Array<string> | ||
txNonce: string | ||
transactionsAdded: Array<TransactionAddedEvent> | ||
}> | ||
|
||
const initialState: RecoveryState = [] | ||
|
||
const { slice, selector } = makeLoadableSlice('recovery', initialState) | ||
|
||
export const recoverySlice = slice | ||
|
||
// TODO: Multiple Delay Modifiers | ||
export const selectRecovery = createSelector(selector, (recovery) => recovery.data[0]) | ||
export const selectRecoveryLoading = createSelector(selector, (recovery) => recovery.loading) |