Skip to content

Commit 029f5d3

Browse files
committed
Update high leverage warning logic
1 parent 7aca697 commit 029f5d3

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

src/components/Synthetics/TradeBox/hooks/useShowHighLeverageWarning.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
import { useCallback, useEffect, useState } from "react";
22

3-
import { AB_HIGH_LEVERAGE_WARNING_GROUP, AB_HIGH_LEVERAGE_WARNING_PROBABILITY } from "config/ab";
3+
import {
4+
AB_HIGH_LEVERAGE_WARNING_ALTCOIN_LEVERAGE,
5+
AB_HIGH_LEVERAGE_WARNING_GROUP,
6+
AB_HIGH_LEVERAGE_WARNING_MAJOR_TOKEN_LEVERAGE,
7+
AB_HIGH_LEVERAGE_WARNING_PROBABILITY,
8+
} from "config/ab";
49
import { ARBITRUM, AVALANCHE, BOTANIX } from "config/chains";
5-
import { BASIS_POINTS_DIVISOR_BIGINT } from "config/factors";
610
import { getHighLeverageWarningDismissedTimestampKey } from "config/localStorage";
711
import { selectAccount, selectChainId } from "context/SyntheticsStateContext/selectors/globalSelectors";
12+
import { selectIsLeverageSliderEnabled } from "context/SyntheticsStateContext/selectors/settingsSelectors";
813
import { selectTradeboxToTokenAddress } from "context/SyntheticsStateContext/selectors/shared/baseSelectors";
9-
import { selectTradeboxLeverage } from "context/SyntheticsStateContext/selectors/tradeboxSelectors";
14+
import {
15+
selectTradeboxIncreasePositionAmounts,
16+
selectTradeboxLeverage,
17+
} from "context/SyntheticsStateContext/selectors/tradeboxSelectors";
1018
import { useSelector } from "context/SyntheticsStateContext/utils";
1119
import { useIsFreshAccountForHighLeverageTrading } from "domain/synthetics/accountStats/useIsFreshAccountForHighLeverageTrading";
1220
import { useIsAddressInGroup } from "lib/userAnalytics/getIsAddressInGroup";
@@ -18,8 +26,6 @@ const IS_MAJOR_TOKEN_MAP: Record<number, string[]> = {
1826
[BOTANIX]: ["BTC"],
1927
};
2028

21-
const MAX_MAJOR_TOKEN_LEVERAGE = 15n * BASIS_POINTS_DIVISOR_BIGINT;
22-
const MAX_ALTCOIN_LEVERAGE = 10n * BASIS_POINTS_DIVISOR_BIGINT;
2329
const WAIVE_DISMISSAL_PERIOD_MS = 24 * 60 * 60 * 1000; // 24 hours
2430
const DISMISSAL_POLL_INTERVAL_MS = 5000;
2531

@@ -50,9 +56,14 @@ export function useShowHighLeverageWarning(): {
5056
const toTokenAddress = useSelector(selectTradeboxToTokenAddress);
5157
const toTokenSymbol = toTokenAddress ? getToken(chainId, toTokenAddress).symbol : undefined;
5258
const isMajorToken = toTokenSymbol ? IS_MAJOR_TOKEN_MAP[chainId].includes(toTokenSymbol) : false;
53-
const leverage = useSelector(selectTradeboxLeverage);
54-
55-
const isHighLeverage = isMajorToken ? leverage >= MAX_MAJOR_TOKEN_LEVERAGE : leverage >= MAX_ALTCOIN_LEVERAGE;
59+
const isLeverageSliderEnabled = useSelector(selectIsLeverageSliderEnabled);
60+
const leverageSliderLeverage = useSelector(selectTradeboxLeverage);
61+
const amounts = useSelector(selectTradeboxIncreasePositionAmounts);
62+
const leverage = isLeverageSliderEnabled ? leverageSliderLeverage : amounts?.estimatedLeverage ?? 0n;
63+
64+
const isHighLeverage = isMajorToken
65+
? leverage >= AB_HIGH_LEVERAGE_WARNING_MAJOR_TOKEN_LEVERAGE
66+
: leverage >= AB_HIGH_LEVERAGE_WARNING_ALTCOIN_LEVERAGE;
5667

5768
const [dismissedTimestamp, setDismissedTimestamp] = useState(() => {
5869
if (!account) {

src/config/ab.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import mapValues from "lodash/mapValues";
22

3+
import { BASIS_POINTS_DIVISOR_BIGINT } from "./factors";
34
import { AB_FLAG_STORAGE_KEY } from "./localStorage";
45

56
type AbFlagValue = {
@@ -102,4 +103,6 @@ export function getAbFlagUrlParams(): string {
102103
// Config for deterministic ab flags based on address
103104

104105
export const AB_HIGH_LEVERAGE_WARNING_GROUP = "alert-high-leverage";
105-
export const AB_HIGH_LEVERAGE_WARNING_PROBABILITY = 50n;
106+
export const AB_HIGH_LEVERAGE_WARNING_PROBABILITY = 0.5;
107+
export const AB_HIGH_LEVERAGE_WARNING_MAJOR_TOKEN_LEVERAGE = 15n * BASIS_POINTS_DIVISOR_BIGINT;
108+
export const AB_HIGH_LEVERAGE_WARNING_ALTCOIN_LEVERAGE = 10n * BASIS_POINTS_DIVISOR_BIGINT;

src/lib/userAnalytics/getIsAddressInGroup.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@ import { getIsAddressInGroup } from "./getIsAddressInGroup";
44

55
describe("getIsAddressInGroup", () => {
66
it("it should be roughly in expected probability", () => {
7-
const prefferedProbabilities = [1, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
7+
const prefferedProbabilities = [0.01, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0];
88
for (const probability of prefferedProbabilities) {
99
const count = 50_000;
10-
let yesses = 0;
10+
let positiveCount = 0;
1111
for (let i = 0; i < count; i++) {
1212
const isInGroup = getIsAddressInGroup({
1313
address: i.toString(),
14-
experimentGroupProbability: BigInt(probability),
14+
experimentGroupProbability: probability,
1515
grouping: "test",
1616
});
1717
if (isInGroup) {
18-
yesses++;
18+
positiveCount++;
1919
}
2020
}
2121

22-
expect(yesses / count).toBeCloseTo(probability / 100);
22+
expect(positiveCount / count).toBeCloseTo(probability);
2323
}
2424
});
2525
});

src/lib/userAnalytics/getIsAddressInGroup.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export function getIsAddressInGroup({
88
}: {
99
address: string;
1010
/**
11-
* 0n-100n meaning 0% - 100%
11+
* 0-1 meaning 0% - 100%
1212
*/
13-
experimentGroupProbability: bigint;
13+
experimentGroupProbability: number;
1414
grouping: string;
1515
}): boolean {
1616
const hash = keccak256(stringToHex(address.toLowerCase() + (salt || "")));
1717
const twoDigits = BigInt(hash) % 100n;
18-
const isInGroup = twoDigits < probability;
18+
const isInGroup = twoDigits < BigInt(Math.trunc(probability * 100));
1919
return isInGroup;
2020
}
2121

@@ -25,7 +25,7 @@ export function useIsAddressInGroup({
2525
grouping: salt,
2626
}: {
2727
address: string | undefined;
28-
experimentGroupProbability: bigint;
28+
experimentGroupProbability: number;
2929
grouping: string;
3030
}) {
3131
const isInGroup = useMemo(

0 commit comments

Comments
 (0)