-
Notifications
You must be signed in to change notification settings - Fork 2
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
27 changed files
with
452 additions
and
84 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
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,65 @@ | ||
import { UseMutateAsyncFunction, UseMutateFunction, UseMutationResult } from '@tanstack/react-query' | ||
import { ConfirmSafeOperationProps, SafeClientResult } from '@safe-global/sdk-starter-kit' | ||
import { ConfigParam, SafeConfigWithSigner } from '@/types/index.js' | ||
import { useSignerClientMutation } from '@/hooks/useSignerClientMutation.js' | ||
import { MutationKey, QueryKey } from '@/constants.js' | ||
import { invalidateQueries } from '@/queryClient.js' | ||
|
||
export type ConfirmSafeOperationVariables = ConfirmSafeOperationProps | ||
|
||
export type UseConfirmSafeOperationParams = ConfigParam<SafeConfigWithSigner> | ||
export type UseConfirmSafeOperationReturnType = Omit< | ||
UseMutationResult<SafeClientResult, Error, ConfirmSafeOperationVariables>, | ||
'mutate' | 'mutateAsync' | ||
> & { | ||
confirmSafeOperation: UseMutateFunction< | ||
SafeClientResult, | ||
Error, | ||
ConfirmSafeOperationVariables, | ||
unknown | ||
> | ||
confirmSafeOperationAsync: UseMutateAsyncFunction< | ||
SafeClientResult, | ||
Error, | ||
ConfirmSafeOperationVariables, | ||
unknown | ||
> | ||
} | ||
|
||
/** | ||
* Hook to confirm pending Safe Operations. | ||
* @param params Parameters to customize the hook behavior. | ||
* @param params.config SafeConfig to use instead of the one provided by `SafeProvider`. | ||
* @returns Object containing the mutation state and the confirmSafeOperation function. | ||
*/ | ||
export function useConfirmSafeOperation( | ||
params: UseConfirmSafeOperationParams = {} | ||
): UseConfirmSafeOperationReturnType { | ||
const { mutate, mutateAsync, ...result } = useSignerClientMutation< | ||
SafeClientResult, | ||
ConfirmSafeOperationVariables | ||
>({ | ||
...params, | ||
mutationKey: [MutationKey.ConfirmSafeOperation], | ||
mutationSafeClientFn: async (signerClient, { safeOperationHash }) => { | ||
if (!signerClient.confirmSafeOperation) | ||
throw new Error( | ||
'To use Safe Operations, you need to specify the safeOperationOptions in the SafeProvider configuration.' | ||
) | ||
|
||
const result = await signerClient.confirmSafeOperation({ | ||
safeOperationHash | ||
}) | ||
|
||
if (result.safeOperations?.userOperationHash) { | ||
invalidateQueries([QueryKey.SafeOperations, QueryKey.SafeInfo]) | ||
} else if (result.safeOperations?.safeOperationHash) { | ||
invalidateQueries([QueryKey.PendingSafeOperations]) | ||
} | ||
|
||
return result | ||
} | ||
}) | ||
|
||
return { ...result, confirmSafeOperation: mutate, confirmSafeOperationAsync: mutateAsync } | ||
} |
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,37 @@ | ||
import { type UseQueryResult } from '@tanstack/react-query' | ||
import { ListOptions } from '@safe-global/api-kit' | ||
import { usePublicClientQuery } from '@/hooks/usePublicClientQuery.js' | ||
import type { ConfigParam, SafeConfig } from '@/types/index.js' | ||
import { QueryKey } from '@/constants.js' | ||
import { ListResponse, SafeOperationResponse } from '@safe-global/types-kit' | ||
|
||
export type UsePendingSafeOperationsParams = ConfigParam<SafeConfig> & ListOptions | ||
export type UsePendingSafeOperationsReturnType = UseQueryResult<ListResponse<SafeOperationResponse>> | ||
|
||
/** | ||
* Hook to get all pending Safe Operations for the connected Safe. | ||
* @param params Parameters to customize the hook behavior. | ||
* @param params.config SafeConfig to use instead of the one provided by `SafeProvider`. | ||
* @returns Query result object containing the list of pending Safe Operations. | ||
*/ | ||
export function usePendingSafeOperations( | ||
params: UsePendingSafeOperationsParams = {} | ||
): UsePendingSafeOperationsReturnType { | ||
return usePublicClientQuery({ | ||
...params, | ||
querySafeClientFn: async (safeClient) => { | ||
if (!safeClient.getPendingSafeOperations) | ||
throw new Error( | ||
'To use Safe Operations, you need to specify the safeOperationOptions in the SafeProvider configuration.' | ||
) | ||
|
||
const pendingSafeOperations = await safeClient.getPendingSafeOperations({ | ||
limit: params.limit, | ||
offset: params.offset | ||
}) | ||
|
||
return pendingSafeOperations | ||
}, | ||
queryKey: [QueryKey.PendingSafeOperations] | ||
}) | ||
} |
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,38 @@ | ||
import { useCallback, useMemo } from 'react' | ||
import { Hash } from 'viem' | ||
import { useQuery, type UseQueryResult } from '@tanstack/react-query' | ||
import { useConfig } from '@/hooks/useConfig.js' | ||
import { SafeMultisigTransactionResponse } from '@safe-global/types-kit' | ||
import { usePublicClient } from '@/hooks/usePublicClient.js' | ||
import type { ConfigParam, SafeConfig } from '@/types/index.js' | ||
|
||
export type UseSafeOperationParams = ConfigParam<SafeConfig> & { safeOperationHash: Hash } | ||
export type UseSafeOperationReturnType = UseQueryResult<SafeMultisigTransactionResponse> | ||
|
||
/** | ||
* Hook to get the status of a specific Safe Operation. | ||
* @param params Parameters to customize the hook behavior. | ||
* @param params.config SafeConfig to use instead of the one provided by `SafeProvider`. | ||
* @param params.safeOperationHash Hash of Safe Operation to be fetched. | ||
* @returns Query result object containing the transaction object. | ||
*/ | ||
export function useSafeOperation(params: UseSafeOperationParams): UseSafeOperationReturnType { | ||
const [config] = useConfig({ config: params.config }) | ||
|
||
const safeClient = usePublicClient({ config }) | ||
|
||
const getSafeOperation = useCallback(async () => { | ||
if (!safeClient) { | ||
throw new Error('SafeClient not initialized') | ||
} | ||
|
||
return safeClient.apiKit.getSafeOperation(params.safeOperationHash) | ||
}, [safeClient]) | ||
|
||
const queryKey = useMemo( | ||
() => ['getSafeOperation', params.safeOperationHash], | ||
[params.safeOperationHash] | ||
) | ||
|
||
return useQuery({ queryKey, queryFn: getSafeOperation }) | ||
} |
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,43 @@ | ||
import { useCallback } from 'react' | ||
import { useQuery, type UseQueryResult } from '@tanstack/react-query' | ||
import { ListOptions } from '@safe-global/api-kit' | ||
import { useConfig } from '@/hooks/useConfig.js' | ||
import { usePublicClient } from '@/hooks/usePublicClient.js' | ||
import type { ConfigParam, SafeConfig } from '@/types/index.js' | ||
import { QueryKey } from '@/constants.js' | ||
import { useAddress } from '@/hooks/useSafeInfo/useAddress.js' | ||
import { ListResponse, SafeOperationResponse } from '@safe-global/types-kit' | ||
|
||
export type UseSafeOperationsParams = ConfigParam<SafeConfig> & ListOptions & { ordering?: string } | ||
export type UseSafeOperationsReturnType = UseQueryResult<ListResponse<SafeOperationResponse>> | ||
|
||
/**s | ||
* Hook to get all Safe Operations for the connected Safe. | ||
* @param params Parameters to customize the hook behavior. | ||
* @param params.config SafeConfig to use instead of the one provided by `SafeProvider`. | ||
* @returns Query result object containing the list of Safe Operations. | ||
*/ | ||
export function useSafeOperations( | ||
params: UseSafeOperationsParams = {} | ||
): UseSafeOperationsReturnType { | ||
const [config] = useConfig({ config: params.config }) | ||
const { data: address } = useAddress({ config }) | ||
const safeClient = usePublicClient({ config }) | ||
|
||
const getSafeOperations = useCallback(async () => { | ||
if (!safeClient || !address) { | ||
throw new Error('SafeClient not initialized') | ||
} | ||
|
||
const response = await safeClient.apiKit.getSafeOperationsByAddress({ | ||
safeAddress: address, | ||
limit: params.limit, | ||
offset: params.offset, | ||
ordering: params.ordering | ||
}) | ||
|
||
return response | ||
}, [safeClient, address]) | ||
|
||
return useQuery({ queryKey: [QueryKey.SafeOperations, config], queryFn: getSafeOperations }) | ||
} |
Oops, something went wrong.