vega-frontend-monorepo/libs/wallet/src/use-transaction-result.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
import { useApolloClient } from '@apollo/client';
import * as Schema from '@vegaprotocol/types';
Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
import { useCallback, useEffect, useRef } from 'react';
import type { Subscription } from 'zen-observable-ts';
import type {
TransactionEventSubscription,
TransactionEventSubscriptionVariables,
} from './__generated__/TransactionResult';
import { TransactionEventDocument } from './__generated__/TransactionResult';
Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
export interface TransactionResult {
partyId: string;
hash: string;
status: boolean;
error?: string | null;
__typename: 'TransactionResult';
}
type WaitFunc = (txHash: string, partyId: string) => Promise<TransactionResult>;
/**
* Returns a function that can be called to subscribe to a transaction
* result event and resolves when an event with a matching txhash is seen
*/
export const useTransactionResult = () => {
const client = useApolloClient();
const subRef = useRef<Subscription | null>(null);
const waitForTransactionResult = useCallback<WaitFunc>(
(txHash: string, partyId: string) => {
return new Promise((resolve) => {
subRef.current = client
.subscribe<
TransactionEventSubscription,
TransactionEventSubscriptionVariables
>({
query: TransactionEventDocument,
variables: { partyId },
})
.subscribe(({ data }) => {
if (!data?.busEvents?.length) {
return;
}
const matchingTransaction = data.busEvents.find((e) => {
if (
e.event.__typename !== Schema.BusEventType.TransactionResult
) {
Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
return false;
}
return e.event.hash.toLocaleLowerCase() === txHash.toLowerCase();
});
if (
matchingTransaction &&
matchingTransaction.event.__typename ===
Schema.BusEventType.TransactionResult
Feat/522 close position (#1762) * feat: use close position hook and dialog setup * chore: update wallet tx interface for batch market instruction * feat: add usage of data provider to show relevant order information * feat: render correctly formatted values in close position dialog * feat: make vega tx dialog more flexibly by allowing custom ui for every state of the tx * feat: adjust text alignment and spacing between active orders and order to close * feat: add unit tests * chore: remove stray log * chore: fix lint * chore: ignore ts error for formatter function of vesting chart * feat: split components up, memozie variables * feat: add shared loading state to prevent content popping in * feat: add time in force label * feat: move transaction result hook to wallet lib * feat: prevent being able to close vega tx dialog, must reject tx * chore: add test for useTransactionResult hook * chore: fix positiosn test after hook relocation * Revert "feat: prevent being able to close vega tx dialog, must reject tx" This reverts commit d1ecda69c3c55822bb042320f82b2e1c3833b99a. * chore: add check for order edge to be defined * chore: remove close callback * feat: use tx result state to determine dialog state * chore: update close position hook to check for transaction result * fix: readd types tif selection persistance * feat: convert order event func to be async, use it in close position for more result context * fix: rename utils * chore: adjust error language Co-authored-by: Madalina Raicu <madalina@raygroup.uk>
2022-10-24 18:16:15 +00:00
) {
resolve(matchingTransaction.event as TransactionResult);
subRef.current?.unsubscribe();
}
});
});
},
[client]
);
useEffect(() => {
return () => {
subRef.current?.unsubscribe();
};
}, []);
return waitForTransactionResult;
};