fix(trading,web3): prevent deposit toasts reappearing (#4797)
Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
This commit is contained in:
parent
6d35f2b39d
commit
ba39720f05
@ -15,6 +15,7 @@ const requestedTransactionUpdate = {
|
|||||||
status: EthTxStatus.Requested,
|
status: EthTxStatus.Requested,
|
||||||
error: null,
|
error: null,
|
||||||
confirmations: 0,
|
confirmations: 0,
|
||||||
|
notify: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
const mockDepositAsset = jest.fn();
|
const mockDepositAsset = jest.fn();
|
||||||
@ -57,6 +58,7 @@ const createTransaction = (
|
|||||||
dialogOpen: false,
|
dialogOpen: false,
|
||||||
txHash: null,
|
txHash: null,
|
||||||
receipt: null,
|
receipt: null,
|
||||||
|
notify: true,
|
||||||
...transaction,
|
...transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -158,6 +160,7 @@ describe('useVegaTransactionManager', () => {
|
|||||||
expect(update.mock.calls[1][1]).toEqual({
|
expect(update.mock.calls[1][1]).toEqual({
|
||||||
status: EthTxStatus.Pending,
|
status: EthTxStatus.Pending,
|
||||||
txHash,
|
txHash,
|
||||||
|
notify: true,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -197,6 +200,7 @@ describe('useVegaTransactionManager', () => {
|
|||||||
expect(update.mock.calls[3][1]).toEqual({
|
expect(update.mock.calls[3][1]).toEqual({
|
||||||
status: EthTxStatus.Confirmed,
|
status: EthTxStatus.Confirmed,
|
||||||
receipt,
|
receipt,
|
||||||
|
notify: true,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ export const useEthTransactionManager = () => {
|
|||||||
status: EthTxStatus.Requested,
|
status: EthTxStatus.Requested,
|
||||||
error: null,
|
error: null,
|
||||||
confirmations: 0,
|
confirmations: 0,
|
||||||
|
notify: true,
|
||||||
});
|
});
|
||||||
const {
|
const {
|
||||||
contract,
|
contract,
|
||||||
@ -48,6 +49,7 @@ export const useEthTransactionManager = () => {
|
|||||||
update(transaction.id, {
|
update(transaction.id, {
|
||||||
status: EthTxStatus.Error,
|
status: EthTxStatus.Error,
|
||||||
error: err as EthereumError,
|
error: err as EthereumError,
|
||||||
|
notify: true,
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -61,6 +63,7 @@ export const useEthTransactionManager = () => {
|
|||||||
update(transaction.id, {
|
update(transaction.id, {
|
||||||
status: EthTxStatus.Pending,
|
status: EthTxStatus.Pending,
|
||||||
txHash: tx.hash,
|
txHash: tx.hash,
|
||||||
|
notify: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
for (let i = 1; i <= requiredConfirmations; i++) {
|
for (let i = 1; i <= requiredConfirmations; i++) {
|
||||||
@ -77,19 +80,31 @@ export const useEthTransactionManager = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (requiresConfirmation) {
|
if (requiresConfirmation) {
|
||||||
update(transaction.id, { status: EthTxStatus.Complete, receipt });
|
update(transaction.id, {
|
||||||
|
status: EthTxStatus.Complete,
|
||||||
|
receipt,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
update(transaction.id, { status: EthTxStatus.Confirmed, receipt });
|
update(transaction.id, {
|
||||||
|
status: EthTxStatus.Confirmed,
|
||||||
|
receipt,
|
||||||
|
notify: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof Error || isEthereumError(err)) {
|
if (err instanceof Error || isEthereumError(err)) {
|
||||||
if (!isExpectedEthereumError(err)) {
|
if (!isExpectedEthereumError(err)) {
|
||||||
update(transaction.id, { status: EthTxStatus.Error, error: err });
|
update(transaction.id, {
|
||||||
|
status: EthTxStatus.Error,
|
||||||
|
error: err,
|
||||||
|
notify: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
update(transaction.id, {
|
update(transaction.id, {
|
||||||
status: EthTxStatus.Error,
|
status: EthTxStatus.Error,
|
||||||
error: new Error('Something went wrong'),
|
error: new Error('Something went wrong'),
|
||||||
|
notify: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
@ -27,7 +27,13 @@ export interface EthStoredTxState extends EthTxState {
|
|||||||
methodName: ContractMethod;
|
methodName: ContractMethod;
|
||||||
args: string[];
|
args: string[];
|
||||||
requiredConfirmations: number;
|
requiredConfirmations: number;
|
||||||
requiresConfirmation: boolean; // whether or not the tx needs external confirmation (IE from a subscription even)
|
// whether or not the tx needs external confirmation (IE from a subscription even)
|
||||||
|
requiresConfirmation: boolean;
|
||||||
|
// whether or not to notify via toast
|
||||||
|
// true = force open toast
|
||||||
|
// false = force close toast
|
||||||
|
// undefined = leave alone
|
||||||
|
notify: boolean | undefined;
|
||||||
assetId?: string;
|
assetId?: string;
|
||||||
deposit?: DepositBusEventFieldsFragment;
|
deposit?: DepositBusEventFieldsFragment;
|
||||||
withdrawal?: WithdrawalBusEventFieldsFragment;
|
withdrawal?: WithdrawalBusEventFieldsFragment;
|
||||||
@ -49,7 +55,7 @@ export interface EthTransactionStore {
|
|||||||
update?: Partial<
|
update?: Partial<
|
||||||
Pick<
|
Pick<
|
||||||
EthStoredTxState,
|
EthStoredTxState,
|
||||||
'status' | 'error' | 'receipt' | 'confirmations' | 'txHash'
|
'status' | 'error' | 'receipt' | 'confirmations' | 'txHash' | 'notify'
|
||||||
>
|
>
|
||||||
>
|
>
|
||||||
) => void;
|
) => void;
|
||||||
@ -89,6 +95,7 @@ export const useEthTransactionStore = create<EthTransactionStore>()(
|
|||||||
requiresConfirmation,
|
requiresConfirmation,
|
||||||
assetId,
|
assetId,
|
||||||
withdrawal,
|
withdrawal,
|
||||||
|
notify: true,
|
||||||
};
|
};
|
||||||
set({ transactions: transactions.concat(transaction) });
|
set({ transactions: transactions.concat(transaction) });
|
||||||
return transaction.id;
|
return transaction.id;
|
||||||
@ -135,6 +142,7 @@ export const useEthTransactionStore = create<EthTransactionStore>()(
|
|||||||
transaction.deposit = deposit;
|
transaction.deposit = deposit;
|
||||||
transaction.dialogOpen = true;
|
transaction.dialogOpen = true;
|
||||||
transaction.updatedAt = new Date();
|
transaction.updatedAt = new Date();
|
||||||
|
transaction.notify = true;
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import type { ReactNode } from 'react';
|
import type { ReactNode } from 'react';
|
||||||
|
import { useEffect } from 'react';
|
||||||
import { useAssetsDataProvider } from '@vegaprotocol/assets';
|
import { useAssetsDataProvider } from '@vegaprotocol/assets';
|
||||||
import { EtherscanLink } from '@vegaprotocol/environment';
|
import { EtherscanLink } from '@vegaprotocol/environment';
|
||||||
import { formatNumber, toBigNum } from '@vegaprotocol/utils';
|
import { formatNumber, toBigNum } from '@vegaprotocol/utils';
|
||||||
@ -169,11 +170,13 @@ export const useEthereumTransactionToasts = () => {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
const dismissTx = useEthTransactionStore((state) => state.dismiss);
|
const dismissTx = useEthTransactionStore((state) => state.dismiss);
|
||||||
|
const updateTx = useEthTransactionStore((state) => state.update);
|
||||||
|
|
||||||
const onClose = useCallback(
|
const onClose = useCallback(
|
||||||
(tx: EthStoredTxState) => () => {
|
(tx: EthStoredTxState) => () => {
|
||||||
dismissTx(tx.id);
|
dismissTx(tx.id);
|
||||||
removeToast(`eth-${tx.id}`);
|
removeToast(`eth-${tx.id}`);
|
||||||
|
updateTx(tx.id, { notify: false });
|
||||||
// closes related "Funds released" toast after successful withdrawal
|
// closes related "Funds released" toast after successful withdrawal
|
||||||
if (
|
if (
|
||||||
isWithdrawTransaction(tx) &&
|
isWithdrawTransaction(tx) &&
|
||||||
@ -183,7 +186,7 @@ export const useEthereumTransactionToasts = () => {
|
|||||||
closeToastBy({ withdrawalId: tx.withdrawal.id });
|
closeToastBy({ withdrawalId: tx.withdrawal.id });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[closeToastBy, dismissTx, removeToast]
|
[closeToastBy, dismissTx, removeToast, updateTx]
|
||||||
);
|
);
|
||||||
|
|
||||||
const fromEthTransaction = useCallback(
|
const fromEthTransaction = useCallback(
|
||||||
@ -213,12 +216,15 @@ export const useEthereumTransactionToasts = () => {
|
|||||||
loader: [EthTxStatus.Pending, EthTxStatus.Complete].includes(tx.status),
|
loader: [EthTxStatus.Pending, EthTxStatus.Complete].includes(tx.status),
|
||||||
content,
|
content,
|
||||||
closeAfter,
|
closeAfter,
|
||||||
|
hidden: !tx.notify,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
[onClose]
|
[onClose]
|
||||||
);
|
);
|
||||||
|
|
||||||
useEthTransactionStore.subscribe(
|
// Only register a subscription once
|
||||||
|
useEffect(() => {
|
||||||
|
const unsubscribe = useEthTransactionStore.subscribe(
|
||||||
(state) => compact(state.transactions.filter((tx) => tx?.dialogOpen)),
|
(state) => compact(state.transactions.filter((tx) => tx?.dialogOpen)),
|
||||||
(txs) => {
|
(txs) => {
|
||||||
txs.forEach((tx) => {
|
txs.forEach((tx) => {
|
||||||
@ -226,4 +232,9 @@ export const useEthereumTransactionToasts = () => {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unsubscribe();
|
||||||
|
};
|
||||||
|
}, [fromEthTransaction, setToast]);
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user