Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gmx-io/gmx-interface
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Sep 18, 2024
2 parents 0ba15fc + f3460de commit c423440
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/config/ab.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mapValues from "lodash/mapValues";
import { AB_FLAG_STORAGE_KEY } from "./localStorage";

type Flag = "testPrebuiltMarkets";
type Flag = "testRpcWindowFallback" | "testPrebuiltMarkets";

type AbFlag = {
enabled: boolean;
Expand All @@ -12,6 +12,7 @@ type AbStorage = {
};

const abFlagsConfig: Record<Flag, number> = {
testRpcWindowFallback: 0.5,
testPrebuiltMarkets: 0.5,
};

Expand Down
95 changes: 89 additions & 6 deletions src/domain/synthetics/markets/useMarkets.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import { useMemo } from "react";
import { ethers } from "ethers";

import { getContract } from "config/contracts";
import { isMarketEnabled } from "config/markets";
import { convertTokenAddress, getToken } from "config/tokens";
import { useMulticall } from "lib/multicall";
import { CONFIG_UPDATE_INTERVAL } from "lib/timeConstants";
import { getIsFlagEnabled } from "config/ab";

import { MarketsData } from "./types";
import { getMarketFullName } from "./utils";

import SyntheticsReader from "abis/SyntheticsReader.json";

import { MARKETS } from "config/markets";

export type MarketsResult = {
Expand All @@ -15,15 +21,20 @@ export type MarketsResult = {
error?: Error | undefined;
};

const MARKETS_COUNT = 100;

export function useMarkets(chainId: number): MarketsResult {
return useMemo(() => {
const markets = MARKETS[chainId];
const staticMarketData = useMemo(() => {
const enabledMarkets = MARKETS[chainId];

if (!enabledMarkets) {
// eslint-disable-next-line no-console
console.warn(`Static markets data for chain ${chainId} not found`);

if (!markets) {
throw new Error(`Static markets data for chain ${chainId} not found`);
return null;
}

return Object.values(markets).reduce(
return Object.values(enabledMarkets).reduce(
(acc: MarketsResult, enabledMarketConfig) => {
const market = enabledMarketConfig;

Expand Down Expand Up @@ -56,7 +67,79 @@ export function useMarkets(chainId: number): MarketsResult {

return acc;
},
{ marketsData: {}, marketsAddresses: [] }
{ marketsData: {}, marketsAddresses: [], error: undefined }
);
}, [chainId]);

const shouldUseStaticMarketKeys = staticMarketData && getIsFlagEnabled("testPrebuiltMarkets");

const freshData = useMarketsMulticall(chainId, { enabled: !shouldUseStaticMarketKeys });

return shouldUseStaticMarketKeys ? staticMarketData : freshData;
}

function useMarketsMulticall(chainId: number, { enabled = true } = {}): MarketsResult {
const { data, error } = useMulticall(chainId, "useMarketsData", {
key: enabled ? [] : null,

refreshInterval: CONFIG_UPDATE_INTERVAL,

request: () => ({
reader: {
contractAddress: getContract(chainId, "SyntheticsReader"),
abi: SyntheticsReader.abi,
calls: {
markets: {
methodName: "getMarkets",
params: [getContract(chainId, "DataStore"), 0, MARKETS_COUNT],
},
},
},
}),
parseResponse: (res) => {
return res.data.reader.markets.returnValues.reduce(
(acc: { marketsData: MarketsData; marketsAddresses: string[] }, marketValues) => {
if (!isMarketEnabled(chainId, marketValues.marketToken)) {
return acc;
}

try {
const indexToken = getToken(chainId, convertTokenAddress(chainId, marketValues.indexToken, "native"));
const longToken = getToken(chainId, marketValues.longToken);
const shortToken = getToken(chainId, marketValues.shortToken);

const isSameCollaterals = marketValues.longToken === marketValues.shortToken;
const isSpotOnly = marketValues.indexToken === ethers.ZeroAddress;

const name = getMarketFullName({ indexToken, longToken, shortToken, isSpotOnly });

acc.marketsData[marketValues.marketToken] = {
marketTokenAddress: marketValues.marketToken,
indexTokenAddress: marketValues.indexToken,
longTokenAddress: marketValues.longToken,
shortTokenAddress: marketValues.shortToken,
isSameCollaterals,
isSpotOnly,
name,
data: "",
};

acc.marketsAddresses.push(marketValues.marketToken);
} catch (e) {
// eslint-disable-next-line no-console
console.warn("unsupported market", e);
}

return acc;
},
{ marketsData: {}, marketsAddresses: [] }
);
},
});

return {
marketsData: data?.marketsData,
marketsAddresses: data?.marketsAddresses,
error,
};
}
4 changes: 2 additions & 2 deletions src/domain/synthetics/markets/useMarketsInfoRequest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export type MarketConfigMulticallRequestConfig = MulticallRequestConfig<{

export function useMarketsInfoRequest(chainId: number): MarketsInfoResult {
const { address: account } = useAccount();
const { marketsData, marketsAddresses } = useMarkets(chainId);
const { marketsData, marketsAddresses, error: marketsError } = useMarkets(chainId);
const { tokensData, pricesUpdatedAt, error: tokensDataError } = useTokensDataRequest(chainId);

const isDependenciesLoading = !marketsAddresses || !tokensData;
Expand Down Expand Up @@ -263,7 +263,7 @@ export function useMarketsInfoRequest(chainId: number): MarketsInfoResult {
return data as MarketsInfoData;
}, [marketsValues.data, marketsConfigs.data, marketsAddresses, marketsData, tokensData, chainId]);

const error = tokensDataError || marketsValues.error || marketsConfigs.error;
const error = marketsError || tokensDataError || marketsValues.error || marketsConfigs.error;

return {
marketsInfoData: isDependenciesLoading ? undefined : mergedData,
Expand Down
12 changes: 8 additions & 4 deletions src/lib/multicall/Multicall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class Multicall {
// eslint-disable-next-line no-console
console.groupEnd();

if (!isFallbackMode) {
if (!isFallbackMode && this.abFlags?.testRpcWindowFallback) {
this.fallbackRpcSwitcher?.trigger();
}

Expand Down Expand Up @@ -373,10 +373,14 @@ export class Multicall {
isAlchemy: isFallbackMode,
});

if (!isFallbackMode) {
this.fallbackRpcSwitcher?.trigger();
if (this.abFlags?.testRpcWindowFallback) {
if (!isFallbackMode) {
this.fallbackRpcSwitcher?.trigger();
}

return await fallbackMulticall(new Error("multicall fallback error")).then(processResponse);
}

return await fallbackMulticall(new Error("multicall fallback error")).then(processResponse);
return result;
}
}

0 comments on commit c423440

Please sign in to comment.