Skip to content

Commit

Permalink
🐛 safe: properly wait for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Oct 26, 2023
1 parent 8ebeb20 commit 215023a
Show file tree
Hide file tree
Showing 23 changed files with 109 additions and 21 deletions.
2 changes: 1 addition & 1 deletion components/ActiveStream/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from '@mui/material';
import React, { FC, useCallback, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { LoadingButton } from '@mui/lab';
import Image from 'next/image';
import formatNumber from 'utils/formatNumber';
Expand Down
2 changes: 1 addition & 1 deletion components/Allowances/RevokeButton.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { memo, useCallback, useState } from 'react';
import { LoadingButton } from '@mui/lab';
import { useNetwork, useSwitchNetwork } from 'wagmi';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { useTranslation } from 'react-i18next';
import { Allowance } from 'hooks/useAllowances';
import { useWeb3 } from 'hooks/useWeb3';
Expand Down
2 changes: 1 addition & 1 deletion components/StakingModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { MAX_UINT256, WEI_PER_ETHER } from 'utils/const';
import { LoadingButton } from '@mui/lab';
import { useWeb3 } from 'hooks/useWeb3';
import { useNetwork, useSwitchNetwork, useSignTypedData, useWalletClient } from 'wagmi';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { ModalBox, ModalBoxCell, ModalBoxRow } from 'components/common/modal/ModalBox';
import SocketAssetSelector from 'components/SocketAssetSelector';
import useSocketAssets from 'hooks/useSocketAssets';
Expand Down
2 changes: 1 addition & 1 deletion components/VestingInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { TransitionProps } from '@mui/material/transitions';
import dayjs from 'dayjs';
import { splitSignature } from '@ethersproject/bytes';
import { type Hex, formatEther, parseEther } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { escrowedExaABI } from 'types/abi';
import { AbiParametersToPrimitiveTypes, ExtractAbiFunction, ExtractAbiFunctionNames } from 'abitype';
import Draggable from 'react-draggable';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useCallback, useMemo, useState } from 'react';
import { CircularProgress, Tooltip, Typography } from '@mui/material';
import { useNetwork } from 'wagmi';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';

import StyledSwitch from 'components/Switch';
import parseHealthFactor from 'utils/parseHealthFactor';
Expand Down
3 changes: 2 additions & 1 deletion components/governance/Claimable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { FC, useMemo } from 'react';
import { Hex, formatEther, parseEther } from 'viem';
import Image from 'next/image';
import { useNetwork, useWaitForTransaction, useSwitchNetwork } from 'wagmi';
import { useNetwork, useSwitchNetwork } from 'wagmi';
import useWaitForTransaction from 'hooks/useWaitForTransaction';
import { Box, Button, Divider, Skeleton, Typography } from '@mui/material';
import { useTranslation } from 'react-i18next';
import { LoadingButton } from '@mui/lab';
Expand Down
3 changes: 2 additions & 1 deletion components/governance/Delegation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import { isAddress, zeroAddress } from 'viem';
import { formatWallet } from 'utils/utils';
import { useWeb3 } from 'hooks/useWeb3';
import { useDelegateRegistryClearDelegate, useDelegateRegistrySetDelegate } from 'types/abi';
import { mainnet, useEnsAvatar, useEnsName, useNetwork, useSwitchNetwork, useWaitForTransaction } from 'wagmi';
import { mainnet, useEnsAvatar, useEnsName, useNetwork, useSwitchNetwork } from 'wagmi';
import useWaitForTransaction from 'hooks/useWaitForTransaction';
import * as blockies from 'blockies-ts';
import { useDelegation, usePrepareClearDelegate, usePrepareDelegate } from 'hooks/useDelegateRegistry';
import formatNumber from 'utils/formatNumber';
Expand Down
3 changes: 2 additions & 1 deletion components/operations/Faucet/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useCallback, useState } from 'react';
import { useAccount, useContractWrite, useWaitForTransaction } from 'wagmi';
import { useAccount, useContractWrite } from 'wagmi';
import useWaitForTransaction from 'hooks/useWaitForTransaction';
import Image from 'next/image';
import imageToBase64 from 'utils/imageToBase64';
import { useTranslation } from 'react-i18next';
Expand Down
2 changes: 1 addition & 1 deletion components/operations/Repay/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ETH_ROUTER_SLIPPAGE, WEI_PER_ETHER } from 'utils/const';
import { CustomError } from 'types/Error';
import useEstimateGas from 'hooks/useEstimateGas';
import { formatUnits, parseUnits } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { gasLimit } from 'utils/gas';

function Repay() {
Expand Down
2 changes: 1 addition & 1 deletion components/operations/RepayAtMaturity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { CustomError } from 'types/Error';
import useEstimateGas from 'hooks/useEstimateGas';
import { formatUnits, parseUnits, zeroAddress } from 'viem';
import dayjs from 'dayjs';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { gasLimit } from 'utils/gas';

type RepayWithDiscount = {
Expand Down
2 changes: 1 addition & 1 deletion components/operations/Withdraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { useTranslation } from 'react-i18next';
import useTranslateOperation from 'hooks/useTranslateOperation';
import useEstimateGas from 'hooks/useEstimateGas';
import { formatUnits, parseUnits } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { gasLimit } from 'utils/gas';

const Withdraw: FC = () => {
Expand Down
2 changes: 1 addition & 1 deletion components/operations/WithdrawAtMaturity/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import useTranslateOperation from 'hooks/useTranslateOperation';
import { WEI_PER_ETHER } from 'utils/const';
import useEstimateGas from 'hooks/useEstimateGas';
import { formatUnits, parseUnits, zeroAddress } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { gasLimit } from 'utils/gas';

const WithdrawAtMaturity: FC = () => {
Expand Down
2 changes: 1 addition & 1 deletion contexts/DebtManagerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, {
useReducer,
} from 'react';
import { useWalletClient } from 'wagmi';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';

import type { ErrorData } from 'types/Error';
import type { PopulatedTransaction, Transaction } from 'types/Transaction';
Expand Down
2 changes: 1 addition & 1 deletion contexts/GetEXAContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import useIsContract from 'hooks/useIsContract';
import useIsPermit from 'hooks/useIsPermit';
import usePermit2 from 'hooks/usePermit2';
import { MAX_UINT256, WEI_PER_ETHER } from 'utils/const';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import dayjs from 'dayjs';
import { splitSignature } from '@ethersproject/bytes';
import useDelayedEffect from 'hooks/useDelayedEffect';
Expand Down
2 changes: 1 addition & 1 deletion contexts/LeveragerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import React, {
useMemo,
} from 'react';
import { useSignTypedData, usePublicClient } from 'wagmi';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import {
formatEther,
formatUnits,
Expand Down
2 changes: 1 addition & 1 deletion hooks/useApprove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useWeb3 } from './useWeb3';
import { useOperationContext } from 'contexts/OperationContext';
import useAccountData from './useAccountData';
import handleOperationError from 'utils/handleOperationError';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { useTranslation } from 'react-i18next';

import { MAX_UINT256 } from 'utils/const';
Expand Down
2 changes: 1 addition & 1 deletion hooks/useBorrow.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';

import { useOperationContext } from 'contexts/OperationContext';
import useAccountData from 'hooks/useAccountData';
Expand Down
2 changes: 1 addition & 1 deletion hooks/useBorrowAtMaturity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import useAnalytics from './useAnalytics';
import { WEI_PER_ETHER } from 'utils/const';
import useEstimateGas from './useEstimateGas';
import { formatUnits, parseUnits } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import dayjs from 'dayjs';
import { gasLimit } from 'utils/gas';

Expand Down
2 changes: 1 addition & 1 deletion hooks/useDeposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import useAnalytics from './useAnalytics';
import { CustomError } from 'types/Error';
import useEstimateGas from './useEstimateGas';
import { parseUnits } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import { gasLimit } from 'utils/gas';

type Deposit = {
Expand Down
2 changes: 1 addition & 1 deletion hooks/useDepositAtMaturity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { WEI_PER_ETHER } from 'utils/const';
import { CustomError } from 'types/Error';
import useEstimateGas from './useEstimateGas';
import { parseUnits } from 'viem';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import dayjs from 'dayjs';
import { gasLimit } from 'utils/gas';

Expand Down
65 changes: 65 additions & 0 deletions hooks/useWaitForTransaction.ts
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 };
2 changes: 1 addition & 1 deletion pages/vesting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useUpdateStreams, useEscrowedEXA, useEscrowedEXAReserveRatio } from 'ho
import { useWeb3 } from 'hooks/useWeb3';
import { useNetwork, useSwitchNetwork } from 'wagmi';
import { LoadingButton } from '@mui/lab';
import { waitForTransaction } from '@wagmi/core';
import waitForTransaction from 'utils/waitForTransaction';
import useRewards from 'hooks/useRewards';
import { useModal } from 'contexts/ModalContext';
import formatNumber from 'utils/formatNumber';
Expand Down
20 changes: 20 additions & 0 deletions utils/waitForTransaction.ts
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 });
}

0 comments on commit 215023a

Please sign in to comment.