Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amplitude: prevent outlier value usd logs #3825

Merged
merged 5 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/web/components/bridge/amount-and-review-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useMemo, useState } from "react";
import { getAddress } from "viem";

import { Screen, useScreenManager } from "~/components/screen-manager";
import { EventName } from "~/config";
import { EventName, OUTLIER_USD_VALUE_THRESHOLD } from "~/config";
import { useAmplitudeAnalytics } from "~/hooks";
import { BridgeScreen } from "~/hooks/bridge";
import { useEvmWalletAccount } from "~/hooks/evm-wallet";
Expand Down Expand Up @@ -342,6 +342,18 @@ export const AmountAndReviewScreen = observer(
? fromChain.chainName
: toChain.chainName;

let valueUsd = Number(
q.input.fiatValue.toDec().toString()
);
// Protect our data from outliers
// Perhaps from upstream issues with price data providers
if (
isNaN(valueUsd) ||
valueUsd > OUTLIER_USD_VALUE_THRESHOLD
) {
valueUsd = 0;
}

logEvent([
EventName.DepositWithdraw.started,
{
Expand All @@ -352,9 +364,7 @@ export const AmountAndReviewScreen = observer(
isRecommendedVariant,
network: networkName,
transferDirection: direction,
valueUsd: Number(
q.input.fiatValue.toDec().toString()
),
valueUsd,
walletName,
},
]);
Expand Down
16 changes: 12 additions & 4 deletions packages/web/components/swap-tool/alt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { tError } from "~/components/localization";
import { TradeDetails } from "~/components/swap-tool/trade-details";
import { GenericDisclaimer } from "~/components/tooltip/generic-disclaimer";
import { Button } from "~/components/ui/button";
import { EventName, EventPage } from "~/config";
import { EventName, EventPage, OUTLIER_USD_VALUE_THRESHOLD } from "~/config";
import {
useAmplitudeAnalytics,
useDisclosure,
Expand Down Expand Up @@ -214,6 +214,16 @@ export const AltSwapTool: FunctionComponent<SwapToolProps> = observer(
const sendSwapTx = () => {
if (!swapState.inAmountInput.amount) return;

let valueUsd = Number(
swapState.inAmountInput.fiatValue?.toDec().toString() ?? "0"
);

// Protect our data from outliers
// Perhaps from upstream issues with price data providers
if (isNaN(valueUsd) || valueUsd > OUTLIER_USD_VALUE_THRESHOLD) {
valueUsd = 0;
}

const baseEvent = {
fromToken: swapState.fromAsset?.coinDenom,
tokenAmount: Number(swapState.inAmountInput.amount.toDec().toString()),
Expand All @@ -223,9 +233,7 @@ export const AltSwapTool: FunctionComponent<SwapToolProps> = observer(
({ pools }) => pools.length !== 1
),
isMultiRoute: (swapState.quote?.split.length ?? 0) > 1,
valueUsd: Number(
swapState.inAmountInput.fiatValue?.toDec().toString() ?? "0"
),
valueUsd,
feeValueUsd: Number(swapState.totalFee?.toString() ?? "0"),
page,
quoteTimeMilliseconds: swapState.quote?.timeMs,
Expand Down
16 changes: 12 additions & 4 deletions packages/web/components/swap-tool/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { Popover } from "~/components/popover";
import { SplitRoute } from "~/components/swap-tool/split-route";
import { InfoTooltip, Tooltip } from "~/components/tooltip";
import { Button } from "~/components/ui/button";
import { EventName, EventPage } from "~/config";
import { EventName, EventPage, OUTLIER_USD_VALUE_THRESHOLD } from "~/config";
import {
useAmplitudeAnalytics,
useDisclosure,
Expand Down Expand Up @@ -186,6 +186,16 @@ export const SwapTool: FunctionComponent<SwapToolProps> = observer(

if (!swapState.inAmountInput.amount) return;

let valueUsd = Number(
swapState.inAmountInput.fiatValue?.toDec().toString() ?? "0"
);

// Protect our data from outliers
// Perhaps from upstream issues with price data providers
if (isNaN(valueUsd) || valueUsd > OUTLIER_USD_VALUE_THRESHOLD) {
valueUsd = 0;
}

const baseEvent = {
fromToken: swapState.fromAsset?.coinDenom,
tokenAmount: Number(swapState.inAmountInput.amount.toDec().toString()),
Expand All @@ -195,9 +205,7 @@ export const SwapTool: FunctionComponent<SwapToolProps> = observer(
({ pools }) => pools.length !== 1
),
isMultiRoute: (swapState.quote?.split.length ?? 0) > 1,
valueUsd: Number(
swapState.inAmountInput.fiatValue?.toDec().toString() ?? "0"
),
valueUsd,
feeValueUsd: Number(swapState.totalFee?.toString() ?? "0"),
page,
quoteTimeMilliseconds: swapState.quote?.timeMs,
Expand Down
6 changes: 3 additions & 3 deletions packages/web/config/analytics-events.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/** # User Events Constants
* Logged to Amplitude at https://analytics.amplitude.com/osmosis-zone/
*/

import { AllocationOptions } from "~/components/complex/portfolio/types";

// Should be in sync with: https://docs.google.com/spreadsheets/d/18w8VwJmmRdb_E-XkE1UjkqhLxCyhqVVhWlzDgTtbRWo/edit?usp=sharing
// For maintainability - all event logs should be in high level component
/** Max value of USD event to check against to prevent
* outliers from corrupting dashboards. */
export const OUTLIER_USD_VALUE_THRESHOLD = 1_500_000;

export type AmountDefault = "half" | "max" | "input";

Expand Down
25 changes: 20 additions & 5 deletions packages/web/hooks/limit-orders/use-place-limit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useCallback, useEffect, useMemo, useState } from "react";
import { useAsync } from "react-use";

import { tError } from "~/components/localization";
import { EventName, EventPage } from "~/config";
import { EventName, EventPage, OUTLIER_USD_VALUE_THRESHOLD } from "~/config";
import {
isValidNumericalRawInput,
useAmountInput,
Expand Down Expand Up @@ -283,6 +283,16 @@ export const usePlaceLimit = ({
}

if (isMarket) {
let valueUsd = Number(
marketState.inAmountInput.fiatValue?.toDec().toString() ?? "0"
);

// Protect our data from outliers
// Perhaps from upstream issues with price data providers
if (isNaN(valueUsd) || valueUsd > OUTLIER_USD_VALUE_THRESHOLD) {
valueUsd = 0;
}

const baseEvent = {
fromToken: marketState.fromAsset?.coinDenom,
tokenAmount: Number(
Expand All @@ -294,9 +304,7 @@ export const usePlaceLimit = ({
({ pools }) => pools.length !== 1
),
isMultiRoute: (marketState.quote?.split.length ?? 0) > 1,
valueUsd: Number(
marketState.inAmountInput.fiatValue?.toDec().toString() ?? "0"
),
valueUsd,
feeValueUsd: Number(marketState.totalFee?.toString() ?? "0"),
page,
quoteTimeMilliseconds: marketState.quote?.timeMs,
Expand Down Expand Up @@ -328,12 +336,19 @@ export const usePlaceLimit = ({

const paymentDenom = paymentTokenValue?.toCoin().denom ?? "";

let valueUsd = Number(paymentFiatValue?.toDec().toString() ?? "0");
// Protect our data from outliers
// Perhaps from upstream issues with price data providers
if (isNaN(valueUsd) || valueUsd > OUTLIER_USD_VALUE_THRESHOLD) {
valueUsd = 0;
}

const baseEvent = {
type: orderDirection === "bid" ? "buy" : "sell",
fromToken: paymentDenom,
toToken:
orderDirection === "bid" ? baseAsset?.coinDenom : quoteAsset?.coinDenom,
valueUsd: Number(paymentFiatValue?.toDec().toString() ?? "0"),
valueUsd,
tokenAmount: Number(quantity),
page,
isOnHomePage: page === "Swap Page",
Expand Down
Loading