import { Link, Dialog, Icon, Intent, Loader } from '@vegaprotocol/ui-toolkit';
import { useEnvironment } from '@vegaprotocol/environment';
import type { VegaTxState } from '@vegaprotocol/wallet';
import { VegaTxStatus } from '@vegaprotocol/wallet';
import type { ReactNode } from 'react';
import type { EthTxState } from '@vegaprotocol/web3';
import { isEthereumError } from '@vegaprotocol/web3';
import { EthTxStatus } from '@vegaprotocol/web3';
import { t } from '@vegaprotocol/react-helpers';
import type { Erc20Approval_erc20WithdrawalApproval } from './__generated__/Erc20Approval';
export interface WithdrawDialogProps {
vegaTx: VegaTxState;
ethTx: EthTxState;
approval: Erc20Approval_erc20WithdrawalApproval | null;
dialogOpen: boolean;
onDialogChange: (isOpen: boolean) => void;
}
export const WithdrawDialog = ({
vegaTx,
ethTx,
approval,
dialogOpen,
onDialogChange,
}: WithdrawDialogProps) => {
const { ETHERSCAN_URL } = useEnvironment();
const { intent, title, icon, children } = getProps(
approval,
vegaTx,
ethTx,
ETHERSCAN_URL
);
return (
);
};
interface StepProps {
children: ReactNode;
}
const Step = ({ children }: StepProps) => {
return (
{children}
);
};
interface DialogProps {
title: string;
icon: ReactNode;
children: ReactNode;
intent?: Intent;
}
const getProps = (
approval: Erc20Approval_erc20WithdrawalApproval | null,
vegaTx: VegaTxState,
ethTx: EthTxState,
ethUrl: string
) => {
const renderVegaTxError = () => {
if (vegaTx.error) {
if ('errors' in vegaTx.error) {
return vegaTx.error.errors['*'].map((e) => {e}
);
} else if ('error' in vegaTx.error) {
return {vegaTx.error.error}
;
} else {
return {t('Something went wrong')}
;
}
}
return null;
};
const vegaTxPropsMap: Record = {
[VegaTxStatus.Default]: {
title: '',
icon: null,
intent: undefined,
children: null,
},
[VegaTxStatus.Error]: {
title: t('Withdrawal transaction failed'),
icon: ,
intent: Intent.Danger,
children: {renderVegaTxError()},
},
[VegaTxStatus.Requested]: {
title: t('Confirm withdrawal'),
icon: ,
intent: Intent.Warning,
children: Confirm withdrawal in Vega wallet,
},
[VegaTxStatus.Pending]: {
title: t('Withdrawal transaction pending'),
icon: ,
intent: Intent.None,
children: Awaiting transaction,
},
[VegaTxStatus.Complete]: {
title: t('Withdrawal transaction complete'),
icon: ,
intent: Intent.Success,
children: Withdrawal created,
},
};
const completeProps = {
title: t('Withdrawal complete'),
icon: ,
intent: Intent.Success,
children: (
{t('Ethereum transaction complete')}
{t('View on Etherscan')}
),
};
const ethTxPropsMap: Record = {
[EthTxStatus.Default]: {
title: '',
icon: null,
intent: undefined,
children: null,
},
[EthTxStatus.Error]: {
title: t('Ethereum transaction failed'),
icon: ,
intent: Intent.Danger,
children: (
{isEthereumError(ethTx.error)
? `Error: ${ethTx.error.reason}`
: ethTx.error instanceof Error
? t(`Error: ${ethTx.error.message}`)
: t('Something went wrong')}
),
},
[EthTxStatus.Requested]: {
title: t('Confirm transaction'),
icon: ,
intent: Intent.Warning,
children: {t('Confirm transaction in wallet')},
},
[EthTxStatus.Pending]: {
title: t('Ethereum transaction pending'),
icon: ,
intent: Intent.None,
children: (
{t(
`Awaiting Ethereum transaction ${ethTx.confirmations}/1 confirmations...`
)}
{t('View on Etherscan')}
),
},
[EthTxStatus.Complete]: completeProps,
[EthTxStatus.Confirmed]: completeProps,
};
return approval ? ethTxPropsMap[ethTx.status] : vegaTxPropsMap[vegaTx.status];
};