vega-frontend-monorepo/libs/withdraws/src/lib/use-complete-withdraw.ts
Matthew Russell 5eb06254de
feat (#896): large withdraws (#1180)
* feat: add deposits table to deposits tab on portfolio page

* feat: refactor use-withdraw hook to not invoke eth tx

* feat: rename hook for clarity

* feat: fix withdrawal creation test

* feat: update withdraw-manager and withrawals-table tests

* chore: fix lint

* feat: remove web3 input to avoid double dialog

* chore: use renderHook from testing-library/react

* chore: update to use non deprecated fields

* chore: remove usage of all bridge contract

* feat: correctly merge cache update in withdrawals table

* feat: changes to support token app withdrawals

* chore: add height to ag grid table wrapping element

* feat: add txhash col to withdraw table

* feat: provide better ui if withdrawal is not ready to be completed

* feat: use separate dialogs for txs

* feat: allow user to immediately complete withdrawal if delay not triggered

* feat: add withdraw store to tidy up state management

* chore: fix tests

* chore: convert callback to promises, fix tests, delete withdraw page

* chore: fix lint errors

* fix: withdrawals link in nav

* feat: style changes after design update

* fix: proposal form test

* chore: tidy error ui logic

* feat: review comments

* chore: lint

* feat: add better typing for tables

* chore: put withdrawals tab at the end

* chore: update i18n

* fix: dialog in positions manager due to rename

* chore: increase spacing in withdrawal form

* chore: update tests

* chore: lint

* chore: use new assetsConnection and update cy test

* fix: incorrect shape of withdrawal generate function

* feat: delete withdrawals page now that its shown on the portfolio page

* chore: update tests to check for withdrawals page

* chore: fix tests again

* fix: page title test
2022-09-05 18:30:13 -07:00

84 lines
2.3 KiB
TypeScript

import { gql, useApolloClient } from '@apollo/client';
import { captureException } from '@sentry/react';
import type { CollateralBridge } from '@vegaprotocol/smart-contracts';
import {
EthTxStatus,
useBridgeContract,
useEthereumTransaction,
} from '@vegaprotocol/web3';
import { useCallback, useEffect, useState } from 'react';
import { ERC20_APPROVAL_QUERY } from './queries';
import type {
Erc20Approval,
Erc20ApprovalVariables,
} from './__generated__/Erc20Approval';
import type { PendingWithdrawal } from './__generated__/PendingWithdrawal';
export const PENDING_WITHDRAWAL_FRAGMMENT = gql`
fragment PendingWithdrawal on Withdrawal {
pendingOnForeignChain @client
txHash
}
`;
export const useCompleteWithdraw = () => {
const { query, cache } = useApolloClient();
const contract = useBridgeContract();
const [id, setId] = useState('');
const { transaction, perform, reset, Dialog } = useEthereumTransaction<
CollateralBridge,
'withdraw_asset'
>(contract, 'withdraw_asset');
const submit = useCallback(
async (withdrawalId: string) => {
setId(withdrawalId);
try {
if (!contract) {
return;
}
const res = await query<Erc20Approval, Erc20ApprovalVariables>({
query: ERC20_APPROVAL_QUERY,
variables: { withdrawalId },
});
const approval = res.data.erc20WithdrawalApproval;
if (!approval) {
throw new Error('Could not retrieve withdrawal approval');
}
perform(
approval.assetSource,
approval.amount,
approval.targetAddress,
approval.creation,
approval.nonce,
approval.signatures
);
} catch (err) {
captureException(err);
}
},
[contract, query, perform]
);
useEffect(() => {
if (id && transaction.txHash) {
cache.writeFragment<PendingWithdrawal>({
id: `Withdrawal:${id}`,
fragment: PENDING_WITHDRAWAL_FRAGMMENT,
data: {
__typename: 'Withdrawal',
pendingOnForeignChain:
transaction.status === EthTxStatus.Pending ? true : false,
txHash: transaction.txHash,
},
});
}
}, [cache, transaction.status, transaction.txHash, id]);
return { transaction, reset, Dialog, submit, withdrawalId: id };
};