/** * A hook that polls for the user's balance to increase, fetching the new position when * it does. This is an unfortunate alternative to listening for the Transfer event to confirm the * position is opened as we seem to occasionally miss this event - see issues raised in Ethers.js */ import { useUserPositions } from "../useUserPositions/useUserPositions"; import { wait } from "../../../utils"; import { UserPosition } from "../../../state"; type PositionDidChange = (position: UserPosition) => boolean; export const usePollForPositionChange = (positionId: number) => { const { fetchUserPosition } = useUserPositions(); const poll = async ( positionDidChange: PositionDidChange ): Promise => { const position = await fetchUserPosition(positionId); if (position && positionDidChange(position)) { return position; } await wait(3000); return poll(positionDidChange); }; return poll; };