Skip to content

Commit

Permalink
wip QuotesReceived event
Browse files Browse the repository at this point in the history
  • Loading branch information
micaelae committed Nov 26, 2024
1 parent 07163b7 commit 857539e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 30 deletions.
82 changes: 55 additions & 27 deletions ui/hooks/bridge/events/useQuoteFetchEvents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable camelcase */
import { useEffect } from 'react';
import { useEffect, useMemo } from 'react';
import { useSelector } from 'react-redux';
import { MetaMetricsEventName } from '../../../../shared/constants/metametrics';
import {
Expand All @@ -14,6 +14,8 @@ import { Numeric } from '../../../../shared/modules/Numeric';
import { fetchUsdExchangeRates } from './utils';
import { useRequestMetadataProperties } from './useRequestMetadataProperties';
import { useRequestProperties } from './useRequestProperties';
import { useQuoteProperties } from './useQuoteProperties';
import { useTradeProperties } from './useTradeProperties';

// This hook is used to track cross chain swaps events related to quote-fetching
export const useQuoteFetchEvents = () => {
Expand All @@ -28,44 +30,70 @@ export const useQuoteFetchEvents = () => {

const { quoteRequestProperties } = useRequestProperties();
const requestMetadataProperties = useRequestMetadataProperties();
const quoteListProperties = useQuoteProperties();
const tradeProperties = useTradeProperties();

const has_sufficient_funds = !insufficientBal;

const usdConversionRatePromise = useMemo(() => {
return (async () =>
srcTokenAddress &&
(await fetchUsdExchangeRates(currency, srcTokenAddress, chainId)))();
}, [currency, srcTokenAddress, chainId]);

useEffect(() => {
const isInitialFetch = isLoading && quotesRefreshCount === 0;
if (
quoteRequestProperties &&
fromTokenInputValue &&
srcTokenAddress &&
(isInitialFetch || quoteFetchError)
) {

if (quoteRequestProperties && fromTokenInputValue && srcTokenAddress) {
(async () => {
const usdConversionRate = await fetchUsdExchangeRates(
currency,
srcTokenAddress,
chainId,
);
const usdConversionRate = await usdConversionRatePromise;
const usd_amount_source = usdConversionRate
? new Numeric(fromTokenInputValue, 10)
.applyConversionRate(usdConversionRate)
.toNumber()
: fromAmountInFiat.toNumber();

trackCrossChainSwapsEvent({
event: quoteFetchError
? // Emitted when an error is caught during fetch
MetaMetricsEventName.QuoteError
: // Emitted when quotes are fetched for the first time for a given request
MetaMetricsEventName.CrossChainSwapsQuotesRequested,
properties: {
...quoteRequestProperties,
...requestMetadataProperties,
has_sufficient_funds,
usd_amount_source,
error_message: quoteFetchError,
},
});
if (isInitialFetch || quoteFetchError) {
trackCrossChainSwapsEvent({
event: quoteFetchError
? // Emitted when an error is caught during fetch
MetaMetricsEventName.QuoteError
: // Emitted when quotes are fetched for the first time for a given request
MetaMetricsEventName.CrossChainSwapsQuotesRequested,
properties: {
...quoteRequestProperties,
...requestMetadataProperties,
has_sufficient_funds,
usd_amount_source,
error_message: quoteFetchError,
},
});
} else if (
quotesRefreshCount >= 0 &&
quoteListProperties &&
tradeProperties
) {
// Emitted after each time quotes are fetched successfully
trackCrossChainSwapsEvent({
event: MetaMetricsEventName.QuotesReceived,
properties: {
...quoteRequestProperties,
...requestMetadataProperties,
...quoteListProperties,
...tradeProperties,
refresh_count: quotesRefreshCount,
usd_amount_source,
warnings: [], // TODO populate from validations
},
});
}
})();
}
}, [isLoading, quoteFetchError]);
}, [
isLoading,
quoteFetchError,
quotesRefreshCount,
quoteListProperties,
tradeProperties,
]);
};
6 changes: 5 additions & 1 deletion ui/hooks/bridge/events/useQuoteProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const useQuoteProperties = () => {
? (Date.now() - quotesInitialLoadTimeMs) / (MILLISECOND * MINUTE)
: undefined;

if (!isLoading && initial_load_time_all_quotes !== undefined) {
if (
!isLoading &&
initial_load_time_all_quotes !== undefined &&
can_submit !== undefined
) {
return {
can_submit,
best_quote_provider: formatProviderLabel(recommendedQuote),
Expand Down
2 changes: 0 additions & 2 deletions ui/hooks/bridge/events/useTradeProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,3 @@ export const useTradeProperties = () => {

return;
};

// TODO make all property hooks async to avoid race conditions

0 comments on commit 857539e

Please sign in to comment.