From a7da8c54f3904fcb1aec5df8ee43555668c10a46 Mon Sep 17 00:00:00 2001 From: midas-myth Date: Wed, 2 Oct 2024 17:09:16 +0000 Subject: [PATCH 1/2] Improve metric numbers privacy --- .../UserFeedbackModal/UserFeedbackModal.tsx | 4 ++-- .../synthetics/userFeedback/requests.ts | 8 +++---- .../synthetics/userFeedback/useNpsSurvey.tsx | 8 +++++-- src/lib/metrics/utils.ts | 24 +++++++------------ src/lib/numbers.ts | 4 ++-- 5 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/components/UserFeedbackModal/UserFeedbackModal.tsx b/src/components/UserFeedbackModal/UserFeedbackModal.tsx index bd369be89b..ae990f1871 100644 --- a/src/components/UserFeedbackModal/UserFeedbackModal.tsx +++ b/src/components/UserFeedbackModal/UserFeedbackModal.tsx @@ -60,8 +60,8 @@ export function UserFeedbackModal({ isVisible, setIsVisible }: Props) { account: "", rating: undefined, isGeneralFeedback: true, - monthVolume: formatAmountForMetrics(lastMonthAccountStats?.volume || 0n, USD_DECIMALS, "toInt")!, - totalVolume: formatAmountForMetrics(accountStats?.volume || 0n, USD_DECIMALS, "toInt")!, + monthVolume: formatAmountForMetrics(lastMonthAccountStats?.volume || 0n, USD_DECIMALS, "toSecondOrderInt")!, + totalVolume: formatAmountForMetrics(accountStats?.volume || 0n, USD_DECIMALS, "toSecondOrderInt")!, answers: formatAnswersByQuestionType([ { questionType: QuestionType.generalFeedback, diff --git a/src/domain/synthetics/userFeedback/requests.ts b/src/domain/synthetics/userFeedback/requests.ts index 113d597dcc..2e30f47375 100644 --- a/src/domain/synthetics/userFeedback/requests.ts +++ b/src/domain/synthetics/userFeedback/requests.ts @@ -21,8 +21,8 @@ export function sendMissedCoinsFeedback({ isError: false, data: { coin, - totalVolume: formatAmountForMetrics(totalVolume ? totalVolume : 0n, USD_DECIMALS, "toInt"), - monthVolume: formatAmountForMetrics(monthVolume ? monthVolume : 0n, USD_DECIMALS, "toInt"), + totalVolume: formatAmountForMetrics(totalVolume ? totalVolume : 0n, USD_DECIMALS, "toSecondOrderInt"), + monthVolume: formatAmountForMetrics(monthVolume ? monthVolume : 0n, USD_DECIMALS, "toSecondOrderInt"), place, }, }); @@ -53,8 +53,8 @@ export const sendMissedCoinSearchDebounced = debounce( isError: false, data: { coin, - totalVolume: formatAmountForMetrics(totalVolume ? totalVolume : 0n, USD_DECIMALS, "toInt"), - monthVolume: formatAmountForMetrics(monthVolume ? monthVolume : 0n, USD_DECIMALS, "toInt"), + totalVolume: formatAmountForMetrics(totalVolume ? totalVolume : 0n, USD_DECIMALS, "toSecondOrderInt"), + monthVolume: formatAmountForMetrics(monthVolume ? monthVolume : 0n, USD_DECIMALS, "toSecondOrderInt"), place, }, }); diff --git a/src/domain/synthetics/userFeedback/useNpsSurvey.tsx b/src/domain/synthetics/userFeedback/useNpsSurvey.tsx index cfba2ed276..9fcc676fb8 100644 --- a/src/domain/synthetics/userFeedback/useNpsSurvey.tsx +++ b/src/domain/synthetics/userFeedback/useNpsSurvey.tsx @@ -64,8 +64,12 @@ export function useNpsSurvey() { account: "", rating, isGeneralFeedback: false, - monthVolume: formatAmountForMetrics(lastMonthAccountStats?.volume || 0n, USD_DECIMALS, "toInt")!, - totalVolume: formatAmountForMetrics(accountStats?.volume || 0n, USD_DECIMALS, "toInt")!, + monthVolume: formatAmountForMetrics( + lastMonthAccountStats?.volume || 0n, + USD_DECIMALS, + "toSecondOrderInt" + )!, + totalVolume: formatAmountForMetrics(accountStats?.volume || 0n, USD_DECIMALS, "toSecondOrderInt")!, answers: formatAnswersByQuestionType(answers), }, }, diff --git a/src/lib/metrics/utils.ts b/src/lib/metrics/utils.ts index 3d98c3eeae..5314ce0c40 100644 --- a/src/lib/metrics/utils.ts +++ b/src/lib/metrics/utils.ts @@ -7,7 +7,7 @@ import { OrderType } from "domain/synthetics/orders"; import { TokenData } from "domain/synthetics/tokens"; import { DecreasePositionAmounts, IncreasePositionAmounts, SwapAmounts } from "domain/synthetics/trade"; import { TxError } from "lib/contracts/transactionErrors"; -import { formatTokenAmount, roundToOrder } from "lib/numbers"; +import { bigintToNumber, roundToOrder } from "lib/numbers"; import { metrics, SubmittedOrderEvent } from "."; import { prepareErrorMetricData } from "./errorReporting"; import { @@ -566,27 +566,19 @@ export function sendOrderCancelledMetric(metricId: OrderMetricId, eventData: Eve export function formatAmountForMetrics( amount?: bigint, decimals = USD_DECIMALS, - round: "toOrder" | "toInt" | false = "toOrder" -) { + round: "toOrder" | "toSecondOrderInt" | false = "toOrder" +): number | undefined { if (amount === undefined) { return undefined; } - const value = round === "toOrder" ? roundToOrder(amount) : amount; - - const str = formatTokenAmount(value, decimals); - - if (!str) { - return undefined; - } - - let result = parseFloat(str); - - if (round === "toInt") { - result = Math.round(result); + if (round === "toOrder") { + return bigintToNumber(roundToOrder(amount), decimals); + } else if (round === "toSecondOrderInt") { + return Math.round(bigintToNumber(roundToOrder(amount, 2), decimals)); } - return result; + return bigintToNumber(amount, decimals); } export function getRequestId() { diff --git a/src/lib/numbers.ts b/src/lib/numbers.ts index 97e2262180..c8acc24487 100644 --- a/src/lib/numbers.ts +++ b/src/lib/numbers.ts @@ -394,8 +394,8 @@ export function roundToTwoDecimals(n: number) { return Math.round(n * 100) / 100; } -export function roundToOrder(n: bigint) { - const decimals = n.toString().length - 1; +export function roundToOrder(n: bigint, significantDigits = 1) { + const decimals = Math.max(n.toString().length - significantDigits, 0); return (n / expandDecimals(1, decimals)) * expandDecimals(1, decimals); } From 7d9cb8f35513cd1824ab2f0f32aabe0bde535c83 Mon Sep 17 00:00:00 2001 From: midas-myth Date: Thu, 3 Oct 2024 11:08:22 +0000 Subject: [PATCH 2/2] Await for options before sending missing tokens --- src/components/MarketSelector/MarketSelector.tsx | 1 + .../Synthetics/ChartTokenSelector/ChartTokenSelector.tsx | 3 ++- src/components/TokenSelector/TokenSelector.tsx | 1 + .../WithMissedCoinsSearch/WithMissedCoinsSearch.tsx | 3 +++ src/domain/synthetics/userFeedback/useMissedCoinsSearch.ts | 6 ++++-- 5 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/components/MarketSelector/MarketSelector.tsx b/src/components/MarketSelector/MarketSelector.tsx index ab575caee3..43a81ebe78 100644 --- a/src/components/MarketSelector/MarketSelector.tsx +++ b/src/components/MarketSelector/MarketSelector.tsx @@ -132,6 +132,7 @@ export function MarketSelector({ useMissedCoinsSearch({ searchText: searchKeyword, isEmpty: !filteredOptions.length && tab === "all", + isLoaded: marketsOptions.length > 0, place: missedCoinsPlace, skip: !missedCoinsPlace, }); diff --git a/src/components/Synthetics/ChartTokenSelector/ChartTokenSelector.tsx b/src/components/Synthetics/ChartTokenSelector/ChartTokenSelector.tsx index 37665e44fd..481d98cbda 100644 --- a/src/components/Synthetics/ChartTokenSelector/ChartTokenSelector.tsx +++ b/src/components/Synthetics/ChartTokenSelector/ChartTokenSelector.tsx @@ -123,7 +123,8 @@ function MarketsList(props: { options: Token[] | undefined }) { useMissedCoinsSearch({ searchText: searchKeyword, - isEmpty: !sortedTokens?.length && tab == "all", + isEmpty: !sortedTokens?.length && tab === "all", + isLoaded: Boolean(options?.length), place: MissedCoinsPlace.marketDropdown, }); diff --git a/src/components/TokenSelector/TokenSelector.tsx b/src/components/TokenSelector/TokenSelector.tsx index 4574194e5e..9fe914b41e 100644 --- a/src/components/TokenSelector/TokenSelector.tsx +++ b/src/components/TokenSelector/TokenSelector.tsx @@ -216,6 +216,7 @@ export default function TokenSelector(props: Props) { searchKeyword={searchKeyword} place={missedCoinsPlace} isEmpty={!filteredTokens.length} + isLoaded={Boolean(visibleTokens.length)} /> )}
diff --git a/src/components/WithMissedCoinsSearch/WithMissedCoinsSearch.tsx b/src/components/WithMissedCoinsSearch/WithMissedCoinsSearch.tsx index 5e5063c56a..796acbda28 100644 --- a/src/components/WithMissedCoinsSearch/WithMissedCoinsSearch.tsx +++ b/src/components/WithMissedCoinsSearch/WithMissedCoinsSearch.tsx @@ -4,17 +4,20 @@ import { useMissedCoinsSearch } from "domain/synthetics/userFeedback/useMissedCo export function WithMissedCoinsSearch({ searchKeyword, isEmpty, + isLoaded, place, skip, }: { searchKeyword: string; isEmpty: boolean; + isLoaded: boolean; place?: MissedCoinsPlace; skip?: boolean; }) { useMissedCoinsSearch({ searchText: searchKeyword, isEmpty, + isLoaded, place, skip, }); diff --git a/src/domain/synthetics/userFeedback/useMissedCoinsSearch.ts b/src/domain/synthetics/userFeedback/useMissedCoinsSearch.ts index 1bf53d2447..556457c46c 100644 --- a/src/domain/synthetics/userFeedback/useMissedCoinsSearch.ts +++ b/src/domain/synthetics/userFeedback/useMissedCoinsSearch.ts @@ -10,11 +10,13 @@ import { MissedCoinsPlace } from "./types"; export function useMissedCoinsSearch({ searchText, isEmpty, + isLoaded, place, skip, }: { searchText: string; isEmpty: boolean; + isLoaded: boolean; place?: MissedCoinsPlace; skip?: boolean; }) { @@ -22,7 +24,7 @@ export function useMissedCoinsSearch({ const accountStats = useSelector(selectAccountStats); useEffect(() => { - if (!skip && searchText.length > 2 && isEmpty && place) { + if (!skip && searchText.length > 2 && isEmpty && place && isLoaded) { sendMissedCoinSearchDebounced({ searchText, totalVolume: accountStats?.volume, @@ -30,5 +32,5 @@ export function useMissedCoinsSearch({ place, }); } - }, [accountStats?.volume, isEmpty, lastMonthAccountStats?.volume, place, searchText, skip]); + }, [accountStats?.volume, isEmpty, isLoaded, lastMonthAccountStats?.volume, place, searchText, skip]); }