Skip to content

Commit

Permalink
Cleanup unnecessary fetching for ratio txn history which is causing e…
Browse files Browse the repository at this point in the history
…rrors in Sentry
  • Loading branch information
jinchung committed Oct 10, 2023
1 parent d86260f commit 6b460c6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 149 deletions.
139 changes: 3 additions & 136 deletions src/redux/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
StaticJsonRpcProvider,
TransactionResponse,
} from '@ethersproject/providers';
import { isEmpty, isNil, mapValues, partition, cloneDeep } from 'lodash';
import { isEmpty, isNil, mapValues, partition } from 'lodash';
import { Dispatch } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
import {
Expand Down Expand Up @@ -47,13 +47,9 @@ import {
getTransactionSocketStatus,
} from '@/handlers/transactions';
import { SwapType } from '@rainbow-me/swaps';
import { FiatProviderName } from '@/entities/f2c';
import { logger as loggr, RainbowError } from '@/logger';
import { analyticsV2 } from '@/analytics';
import { logger as loggr } from '@/logger';
import { queryClient } from '@/react-query';
import { nftsQueryKey } from '@/resources/nfts';
import { QueryClient } from '@tanstack/react-query';
import { ratioGetUserActivityItem } from '@/resources/f2c';

const BACKUP_SHEET_DELAY_MS = android ? 10000 : 3000;

Expand Down Expand Up @@ -618,11 +614,8 @@ export const transactionsReceived = (
}, 60000);
}

const maybeUpdatedPendingTransactions = await maybeFetchF2CHashForPendingTransactions(
cloneDeep(pendingTransactions)
);
const txHashes = parsedTransactions.map(tx => ethereumUtils.getHash(tx));
const updatedPendingTransactions = maybeUpdatedPendingTransactions.filter(
const updatedPendingTransactions = pendingTransactions.filter(
tx => !txHashes.includes(ethereumUtils.getHash(tx))
);

Expand Down Expand Up @@ -658,132 +651,6 @@ export const transactionsReceived = (
}
};

/**
* Maps over every pendingTransaction, and if it's a F2C transaction, fetches
* the transaction has and updates the pendingTransaction with its hash.
*
* This method returns all pendingTransactions that were passed in, so 5 go in,
* 5 come out, but they might have a tx hash added.
*/
export const maybeFetchF2CHashForPendingTransactions = async (
pendingTransactions: RainbowTransaction[]
) => {
loggr.debug(
`maybeFetchF2CHashForPendingTransactions`,
{},
loggr.DebugContext.f2c
);

/**
* A CUSTOM query client used for this query only. We don't need to store tx
* data on this device.
*/
const queryClient = new QueryClient();

return Promise.all(
pendingTransactions.map(async tx => {
// If not from a F2C provider, return the original tx
if (!tx.fiatProvider) return tx;
if (tx.hash) {
/**
* Sometimes `transactionsReceived` gets called more than once in quick
* succession, which can result it fetching order data more than once.
*
* So if we already have a tx hash, then we don't need to fetch
* anything else.
*/
return tx;
}

// If it is from an F2C provider, see if we can add the tx hash to it
switch (tx.fiatProvider?.name) {
// handle Ratio case
case FiatProviderName.Ratio: {
loggr.debug(
`maybeFetchF2CHashForPendingTransactions`,
{ provider: tx.fiatProvider?.name },
loggr.DebugContext.f2c
);

const { userId, orderId } = tx.fiatProvider;

loggr.debug(
`maybeFetchF2CHashForPendingTransactions: fetching order`
);

try {
const data = await queryClient.fetchQuery({
queryKey: ['f2c', 'ratio', 'pending_tx_check'],
staleTime: 10_000, // only fetch AT MOST once every 10 seconds
async queryFn() {
const { data, error } = await ratioGetUserActivityItem({
userId,
orderId,
});

if (!data || error) {
const [{ message }] = error.errors;

if (error) {
throw new Error(message);
} else {
throw new Error(
'Ratio API returned no data for this transaction'
);
}
}

return data;
},
});

loggr.debug(
`maybeFetchF2CHashForPendingTransactions: fetched order`,
{
hasData: Boolean(data),
hasHash: Boolean(data?.crypto?.transactionHash),
}
);

if (data.crypto.transactionHash) {
tx.hash = data.crypto.transactionHash;

analyticsV2.track(analyticsV2.event.f2cTransactionReceived, {
provider: FiatProviderName.Ratio,
sessionId: tx.fiatProvider.analyticsSessionId,
});

loggr.debug(
`maybeFetchF2CHashForPendingTransactions: fetched order and updated hash on transaction`
);
} else {
loggr.info(
`maybeFetchF2CHashForPendingTransactions: fetcher returned no transaction data`
);
}
} catch (e: any) {
loggr.error(
new RainbowError(
`maybeFetchF2CHashForPendingTransactions: failed to fetch transaction data`
),
{
message: e.message,
provider: tx.fiatProvider.name,
}
);
}

break;
}

// handle other cases here once we have more providers
}

return tx;
})
);
};

const callbacksOnAssetReceived: {
[address: string]: ((asset: ParsedAddressAsset) => unknown) | undefined;
} = {};
Expand Down
13 changes: 0 additions & 13 deletions src/resources/f2c/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { create } from 'gretchen';
import { ActivityItem } from '@ratio.me/ratio-react-native-library';
import qs from 'query-string';

import { IS_PROD } from '@/env';
Expand Down Expand Up @@ -39,18 +38,6 @@ export function ratioGetClientSession({
).json();
}

export function ratioGetUserActivityItem({
userId,
orderId,
}: {
userId: string;
orderId: string;
}) {
return gretch<ActivityItem, ErrorResponse>(
`/v1/providers/ratio/users/${userId}/activity/${orderId}`
).json();
}

export function coinbaseGetWidgetURL({
depositAddress,
}: {
Expand Down

0 comments on commit 6b460c6

Please sign in to comment.