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

Improve metric numbers privacy #1289

Open
wants to merge 2 commits into
base: release-43
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/components/MarketSelector/MarketSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function MarketSelector({
useMissedCoinsSearch({
searchText: searchKeyword,
isEmpty: !filteredOptions.length && tab === "all",
isLoaded: marketsOptions.length > 0,
place: missedCoinsPlace,
skip: !missedCoinsPlace,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
1 change: 1 addition & 0 deletions src/components/TokenSelector/TokenSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export default function TokenSelector(props: Props) {
searchKeyword={searchKeyword}
place={missedCoinsPlace}
isEmpty={!filteredTokens.length}
isLoaded={Boolean(visibleTokens.length)}
/>
)}
<div className="TokenSelector-tokens">
Expand Down
4 changes: 2 additions & 2 deletions src/components/UserFeedbackModal/UserFeedbackModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
8 changes: 4 additions & 4 deletions src/domain/synthetics/userFeedback/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
});
Expand Down Expand Up @@ -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,
},
});
Expand Down
6 changes: 4 additions & 2 deletions src/domain/synthetics/userFeedback/useMissedCoinsSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,27 @@ import { MissedCoinsPlace } from "./types";
export function useMissedCoinsSearch({
searchText,
isEmpty,
isLoaded,
place,
skip,
}: {
searchText: string;
isEmpty: boolean;
isLoaded: boolean;
place?: MissedCoinsPlace;
skip?: boolean;
}) {
const lastMonthAccountStats = useSelector(selectLastMonthAccountStats);
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,
monthVolume: lastMonthAccountStats?.volume,
place,
});
}
}, [accountStats?.volume, isEmpty, lastMonthAccountStats?.volume, place, searchText, skip]);
}, [accountStats?.volume, isEmpty, isLoaded, lastMonthAccountStats?.volume, place, searchText, skip]);
}
8 changes: 6 additions & 2 deletions src/domain/synthetics/userFeedback/useNpsSurvey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
},
Expand Down
24 changes: 8 additions & 16 deletions src/lib/metrics/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down