-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 safe: properly wait for transactions
- Loading branch information
1 parent
8ebeb20
commit 215023a
Showing
23 changed files
with
109 additions
and
21 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
2 changes: 1 addition & 1 deletion
2
...hboardContent/FloatingPoolDashboard/FloatingPoolDashboardTable/SwitchCollateral/index.tsx
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
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
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 @@ | ||
// https://github.com/wagmi-dev/wagmi/blob/wagmi@1.4.5/packages/react/src/hooks/transactions/useWaitForTransaction.ts | ||
import type { WaitForTransactionArgs, WaitForTransactionResult } from '@wagmi/core'; | ||
import type { QueryFunctionContext, UseQueryOptions } from '@tanstack/react-query'; | ||
import { useChainId, useQuery } from 'wagmi'; | ||
import waitForTransaction from 'utils/waitForTransaction'; | ||
|
||
export type UseWaitForTransactionArgs = Partial<WaitForTransactionArgs>; | ||
export type UseWaitForTransactionConfig = QueryConfig<WaitForTransactionResult, Error>; | ||
|
||
type QueryKeyArgs = Partial<UseWaitForTransactionArgs>; | ||
type QueryKeyConfig = Pick<UseWaitForTransactionConfig, 'scopeKey'>; | ||
|
||
function queryKey({ confirmations, chainId, hash, scopeKey, timeout }: QueryKeyArgs & QueryKeyConfig) { | ||
return [{ entity: 'waitForTransaction', confirmations, chainId, hash, scopeKey, timeout }] as const; | ||
} | ||
|
||
function queryFn({ onReplaced }: { onReplaced?: WaitForTransactionArgs['onReplaced'] }) { | ||
return ({ queryKey: [{ chainId, confirmations, hash, timeout }] }: QueryFunctionArgs<typeof queryKey>) => { | ||
if (!hash) throw new Error('hash is required'); | ||
return waitForTransaction({ chainId, confirmations, hash, onReplaced, timeout }); | ||
}; | ||
} | ||
|
||
export default function useWaitForTransaction({ | ||
chainId: chainId_, | ||
confirmations, | ||
hash, | ||
timeout, | ||
cacheTime, | ||
enabled = true, | ||
scopeKey, | ||
staleTime, | ||
suspense, | ||
onError, | ||
onReplaced, | ||
onSettled, | ||
onSuccess, | ||
}: UseWaitForTransactionArgs & UseWaitForTransactionConfig = {}) { | ||
const chainId = useChainId({ chainId: chainId_ }); | ||
|
||
return useQuery(queryKey({ chainId, confirmations, hash, scopeKey, timeout }), queryFn({ onReplaced }), { | ||
cacheTime, | ||
enabled: Boolean(enabled && hash), | ||
staleTime, | ||
suspense, | ||
onError, | ||
onSettled, | ||
onSuccess, | ||
}); | ||
} | ||
|
||
type QueryFunctionArgs<T extends (...args: any) => any> = QueryFunctionContext<ReturnType<T>>; // eslint-disable-line @typescript-eslint/no-explicit-any | ||
|
||
type QueryConfig<TData, TError, TSelectData = TData> = Pick< | ||
UseQueryOptions<TData, TError, TSelectData>, | ||
| 'cacheTime' | ||
| 'enabled' | ||
| 'isDataEqual' | ||
| 'staleTime' | ||
| 'structuralSharing' | ||
| 'suspense' | ||
| 'onError' | ||
| 'onSettled' | ||
| 'onSuccess' | ||
> & { scopeKey?: string }; |
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,20 @@ | ||
import SafeAppsSDK, { TransactionStatus } from '@safe-global/safe-apps-sdk'; | ||
import { getConfig, waitForTransaction as wait, type WaitForTransactionArgs } from '@wagmi/core'; | ||
import type { Hash } from 'viem'; | ||
|
||
export default async function waitForTransaction(args: WaitForTransactionArgs) { | ||
if (getConfig().connector?.id !== 'safe') return wait(args); | ||
|
||
const { txs } = new SafeAppsSDK(); | ||
const hash = await new Promise<Hash>(function get(resolve, reject) { | ||
txs | ||
.getBySafeTxHash(args.hash) | ||
.then(({ txStatus, txHash }) => { | ||
if (txStatus === TransactionStatus.SUCCESS && txHash) resolve(txHash as Hash); | ||
else if (txStatus === TransactionStatus.FAILED || txStatus === TransactionStatus.CANCELLED) reject(txStatus); | ||
else setTimeout(() => get(resolve, reject), 3_333); | ||
}) | ||
.catch(reject); | ||
}); | ||
return wait({ ...args, hash }); | ||
} |