Skip to content

Commit

Permalink
feat(ramp): add sell order polling when has txhash (#7963)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachunei authored Dec 5, 2023
1 parent 89eb8cd commit cbaa4bc
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { OrderOrderTypeEnum } from '@consensys/on-ramp-sdk/dist/API';
import Routes from '../../../../../../constants/navigation/Routes';
import { RampSDK } from '../../sdk';
import { PROVIDER_LINKS } from '../../types';
import AppConstants from '../../../../../../core/AppConstants';

const mockNavigate = jest.fn();
const mockGoBack = jest.fn();
Expand Down Expand Up @@ -335,6 +336,29 @@ describe('OrderDetails', () => {
expect(screen.toJSON()).toMatchSnapshot();
});

it('polls transacted orders', async () => {
jest.useFakeTimers();
const createdOrder = {
...mockOrder,
orderType: OrderOrderTypeEnum.Sell,
state: FIAT_ORDER_STATES.CREATED,
sellTxHash: '0x123',
};

const intervalCount = 3;

await waitFor(() => render(OrderDetails, [createdOrder]));
act(() => {
jest.advanceTimersByTime(
AppConstants.FIAT_ORDERS.POLLING_FREQUENCY * intervalCount,
);
jest.clearAllTimers();
jest.useRealTimers();
});
// processFiatOrder is called on load and then 3 times by the interval
expect(processFiatOrder).toHaveBeenCalledTimes(1 + intervalCount);
});

it('renders non-transacted orders', async () => {
const createdOrder = {
...mockOrder,
Expand Down
65 changes: 46 additions & 19 deletions app/components/UI/Ramp/common/Views/OrderDetails/OrderDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import {
import { RootState } from '../../../../../../reducers';
import { FIAT_ORDER_STATES } from '../../../../../../constants/on-ramp';
import ErrorView from '../../components/ErrorView';
import useInterval from '../../../../../hooks/useInterval';
import AppConstants from '../../../../../../core/AppConstants';

interface OrderDetailsParams {
orderId?: string;
Expand Down Expand Up @@ -59,6 +61,7 @@ const OrderDetails = () => {
const dispatchThunk = useThunkDispatch();

const [isRefreshing, setIsRefreshing] = useState(false);
const [isRefreshingInterval, setIsRefreshingInterval] = useState(false);

useEffect(() => {
navigation.setOptions(
Expand Down Expand Up @@ -139,25 +142,36 @@ const OrderDetails = () => {
[dispatch],
);

const handleOnRefresh = useCallback(async () => {
if (!order) return;
try {
setError(null);
setIsRefreshing(true);
await processFiatOrder(order, dispatchUpdateFiatOrder, dispatchThunk, {
forced: true,
});
} catch (fetchError) {
Logger.error(fetchError as Error, {
message: 'FiatOrders::OrderDetails error while processing order',
order,
});
setError((fetchError as Error).message || 'An error as occurred');
} finally {
setIsLoading(false);
setIsRefreshing(false);
}
}, [dispatchThunk, dispatchUpdateFiatOrder, order]);
const handleOnRefresh = useCallback(
async ({ fromInterval }: { fromInterval?: boolean } = {}) => {
if (!order) return;
try {
setError(null);
if (fromInterval) {
setIsRefreshingInterval(true);
} else {
setIsRefreshing(true);
}
await processFiatOrder(order, dispatchUpdateFiatOrder, dispatchThunk, {
forced: true,
});
} catch (fetchError) {
Logger.error(fetchError as Error, {
message: 'FiatOrders::OrderDetails error while processing order',
order,
});
setError((fetchError as Error).message || 'An error as occurred');
} finally {
if (fromInterval) {
setIsRefreshingInterval(false);
} else {
setIsLoading(false);
setIsRefreshing(false);
}
}
},
[dispatchThunk, dispatchUpdateFiatOrder, order],
);

useEffect(() => {
if (order?.state === FIAT_ORDER_STATES.CREATED) {
Expand All @@ -176,6 +190,19 @@ const OrderDetails = () => {
);
}, [navigation, order?.orderType]);

useInterval(
() => {
handleOnRefresh({ fromInterval: true });
},
!isLoading &&
!isRefreshingInterval &&
order &&
order.state === FIAT_ORDER_STATES.CREATED &&
order.sellTxHash
? AppConstants.FIAT_ORDERS.POLLING_FREQUENCY
: null,
);

if (!order) {
return <ScreenLayout />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ describe('SendTransaction View', () => {
]
`);

expect(mockNavigate).toHaveBeenCalledWith(Routes.WALLET_VIEW);
expect(mockGoBack).toHaveBeenCalled();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import { NATIVE_ADDRESS } from '../../../../../../constants/on-ramp';
import { safeToChecksumAddress } from '../../../../../../util/address';
import { generateTransferData } from '../../../../../../util/transactions';
import useAnalytics from '../../hooks/useAnalytics';
import Routes from '../../../../../../constants/navigation/Routes';

interface SendTransactionParams {
orderId?: string;
Expand Down Expand Up @@ -158,7 +157,6 @@ function SendTransaction() {
'OFFRAMP_SEND_TRANSACTION_CONFIRMED',
transactionAnalyticsPayload,
);
navigation.navigate(Routes.WALLET_VIEW);
}
} catch (error) {
trackEvent(
Expand Down

0 comments on commit cbaa4bc

Please sign in to comment.