vega-frontend-monorepo/libs/wallet/src/use-transaction-result.ts
botond 2211abbd72
chore: cleanup type gen (#2362)
* chore: remove apollo type gen and clean up types lib

* fix: format

* fix: tests

* fix: format

* fix: hammer token types til sh*t turns green

* fix: format

* fix: apparently format again

* fix: lint

* fix: build-specs

* fix: format

* chore: regen types

* chore: regenerate types again

* fix: format
2022-12-08 21:13:30 +00:00

77 lines
2.2 KiB
TypeScript

import { useApolloClient } from '@apollo/client';
import * as Schema from '@vegaprotocol/types';
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';
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
) {
return false;
}
return e.event.hash.toLocaleLowerCase() === txHash.toLowerCase();
});
if (
matchingTransaction &&
matchingTransaction.event.__typename ===
Schema.BusEventType.TransactionResult
) {
resolve(matchingTransaction.event as TransactionResult);
subRef.current?.unsubscribe();
}
});
});
},
[client]
);
useEffect(() => {
return () => {
subRef.current?.unsubscribe();
};
}, []);
return waitForTransactionResult;
};