|
| 1 | +import { OrderStatus, SupportedChainId } from '@cowprotocol/cow-sdk'; |
| 2 | +import { Trans } from '@lingui/macro'; |
| 3 | +import { useQueryClient } from '@tanstack/react-query'; |
| 4 | +import { Interface } from 'ethers/lib/utils'; |
| 5 | +import { useIsWrongNetwork } from 'src/hooks/useIsWrongNetwork'; |
| 6 | +import { useModalContext } from 'src/hooks/useModal'; |
| 7 | +import { useWeb3Context } from 'src/libs/hooks/useWeb3Context'; |
| 8 | +import { |
| 9 | + ActionName, |
| 10 | + CowSwapSubset, |
| 11 | + isCowSwapSubset, |
| 12 | + SwapActionFields, |
| 13 | + TransactionHistoryItem, |
| 14 | +} from 'src/modules/history/types'; |
| 15 | +import { useRootStore } from 'src/store/root'; |
| 16 | +import { getErrorTextFromError, TxAction } from 'src/ui-config/errorMapping'; |
| 17 | +import { updateCowOrderStatus } from 'src/utils/swapAdapterHistory'; |
| 18 | + |
| 19 | +import { ADAPTER_FACTORY } from '../Swap/constants/cow.constants'; |
| 20 | +import { TxActionsWrapper } from '../TxActionsWrapper'; |
| 21 | + |
| 22 | +interface CancelAdapterOrderActionsProps { |
| 23 | + cowOrder: TransactionHistoryItem< |
| 24 | + | SwapActionFields[ActionName.DebtSwap] |
| 25 | + | SwapActionFields[ActionName.RepayWithCollateral] |
| 26 | + | SwapActionFields[ActionName.CollateralSwap] |
| 27 | + >; |
| 28 | + blocked: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +// ABI for cancelInstance function |
| 32 | +const ADAPTER_ABI = ['function cancelInstance(address instance) external']; |
| 33 | + |
| 34 | +export const CancelAdapterOrderActions = ({ |
| 35 | + cowOrder, |
| 36 | + blocked, |
| 37 | +}: CancelAdapterOrderActionsProps) => { |
| 38 | + const { isWrongNetwork } = useIsWrongNetwork(cowOrder.chainId); |
| 39 | + const { mainTxState, loadingTxns, setMainTxState, setTxError } = useModalContext(); |
| 40 | + const { sendTx } = useWeb3Context(); |
| 41 | + const queryClient = useQueryClient(); |
| 42 | + const account = useRootStore((state) => state.account); |
| 43 | + |
| 44 | + const action = async () => { |
| 45 | + try { |
| 46 | + setMainTxState({ ...mainTxState, loading: true }); |
| 47 | + |
| 48 | + // Type guard to ensure we have a CowSwapSubset with adapter fields |
| 49 | + if (!isCowSwapSubset(cowOrder)) { |
| 50 | + throw new Error('Order is not a CoW swap order'); |
| 51 | + } |
| 52 | + |
| 53 | + // At this point TypeScript knows cowOrder is CowSwapSubset, but we need to assert it has adapter fields |
| 54 | + const cowSwapOrder = cowOrder as CowSwapSubset; |
| 55 | + |
| 56 | + if (!cowSwapOrder.adapterInstanceAddress) { |
| 57 | + throw new Error('Adapter instance address not found'); |
| 58 | + } |
| 59 | + |
| 60 | + const adapterInterface = new Interface(ADAPTER_ABI); |
| 61 | + |
| 62 | + const factoryAddress = ADAPTER_FACTORY[cowOrder.chainId as SupportedChainId]; |
| 63 | + |
| 64 | + if (!factoryAddress) { |
| 65 | + throw new Error('Factory address not found for this chain'); |
| 66 | + } |
| 67 | + |
| 68 | + const data = adapterInterface.encodeFunctionData('cancelInstance', [ |
| 69 | + cowSwapOrder.adapterInstanceAddress, |
| 70 | + ]); |
| 71 | + |
| 72 | + const txResponse = await sendTx({ |
| 73 | + to: factoryAddress, |
| 74 | + data, |
| 75 | + chainId: cowOrder.chainId, |
| 76 | + }); |
| 77 | + |
| 78 | + await txResponse.wait(1); |
| 79 | + |
| 80 | + // Update order status to cancelled in local storage |
| 81 | + if (account && cowSwapOrder.orderId) { |
| 82 | + updateCowOrderStatus( |
| 83 | + cowOrder.chainId, |
| 84 | + account, |
| 85 | + cowSwapOrder.orderId, |
| 86 | + OrderStatus.CANCELLED |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + queryClient.invalidateQueries({ queryKey: 'transactionHistory' }); |
| 91 | + setMainTxState({ |
| 92 | + ...mainTxState, |
| 93 | + loading: false, |
| 94 | + success: true, |
| 95 | + txHash: txResponse.hash, |
| 96 | + }); |
| 97 | + } catch (error) { |
| 98 | + const parsedError = getErrorTextFromError(error, TxAction.MAIN_ACTION, false); |
| 99 | + setTxError(parsedError); |
| 100 | + setMainTxState({ |
| 101 | + txHash: undefined, |
| 102 | + loading: false, |
| 103 | + }); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + return ( |
| 108 | + <TxActionsWrapper |
| 109 | + isWrongNetwork={isWrongNetwork} |
| 110 | + handleAction={action} |
| 111 | + actionText={<Trans>Cancel order</Trans>} |
| 112 | + actionInProgressText={<Trans>Cancelling order...</Trans>} |
| 113 | + blocked={blocked} |
| 114 | + mainTxState={mainTxState} |
| 115 | + requiresApproval={false} |
| 116 | + preparingTransactions={loadingTxns} |
| 117 | + /> |
| 118 | + ); |
| 119 | +}; |
0 commit comments