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

[DO NOT MERGE] Test more markets #1418

Open
wants to merge 4 commits into
base: master
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/config/ab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type AbStorage = {

const abFlagsConfig = {
// testExampleAb: 0.5,
testMoreMarkets: 0.5,
};

export type AbFlag = string; //keyof typeof abFlagsConfig;
Expand Down
895 changes: 895 additions & 0 deletions src/config/static/markets.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/context/SyntheticsEvents/SyntheticsEventsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function SyntheticsEventsProvider({ children }: { children: ReactNode })
...glvData,
};

const marketTokensAddressesString = Object.keys(glvAndGmMarketsData).join("-");
const marketTokensAddressesString = Object.keys(glvAndGmMarketsData).join(":");

return {
glvAndGmMarketsData,
Expand Down Expand Up @@ -814,7 +814,7 @@ export function SyntheticsEventsProvider({ children }: { children: ReactNode })
chainId,
wsProvider,
currentAccount,
marketTokensAddressesString.split("-"),
marketTokensAddressesString.split(":").filter((s) => !s.includes("1-")),
(tokenAddress, amount) => {
setTokensBalancesUpdates((old) => {
const oldDiff = old[tokenAddress]?.diff || 0n;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export function useClaimableFundingDataRequest(chainId: number) {
}

return marketsAddresses.reduce((request, marketAddress) => {
// eslint-disable-next-line es-x/no-string-prototype-replaceall
const fixedAddress = marketAddress.replaceAll("1-", "");
const market = getByKey(marketsData, marketAddress);

if (!market) {
Expand All @@ -39,11 +41,11 @@ export function useClaimableFundingDataRequest(chainId: number) {
const keys = hashDataMap({
claimableFundingAmountLong: [
["bytes32", "address", "address", "address"],
[CLAIMABLE_FUNDING_AMOUNT, marketAddress, market.longTokenAddress, account as string],
[CLAIMABLE_FUNDING_AMOUNT, fixedAddress, market.longTokenAddress, account as string],
],
claimableFundingAmountShort: [
["bytes32", "address", "address", "address"],
[CLAIMABLE_FUNDING_AMOUNT, marketAddress, market.shortTokenAddress, account as string],
[CLAIMABLE_FUNDING_AMOUNT, fixedAddress, market.shortTokenAddress, account as string],
],
});

Expand Down
5 changes: 4 additions & 1 deletion src/domain/synthetics/markets/useGmMarketsApy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ type SwrResult = {

function useMarketAddresses(marketsInfoData: GlvAndGmMarketsInfoData | undefined) {
return useMemo(
() => Object.keys(marketsInfoData || {}).filter((address) => !marketsInfoData![address].isDisabled),
() =>
Object.keys(marketsInfoData || {}).filter(
(address) => !marketsInfoData![address].isDisabled && !address.includes("1-")
),
[marketsInfoData]
);
}
Expand Down
8 changes: 5 additions & 3 deletions src/domain/synthetics/markets/useMarketTokensData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ export function useMarketTokensDataRequest(

request: () =>
marketsAddresses!.reduce((requests, marketAddress) => {
const market = getByKey(marketsData, marketAddress)!;
// eslint-disable-next-line es-x/no-string-prototype-replaceall
const fixedAddress = marketAddress.replaceAll("1-", "");
const market = getByKey(marketsData, fixedAddress)!;
const marketPrices = getContractMarketPrices(tokensData!, market);

if (marketPrices) {
const marketProps = {
marketToken: market.marketTokenAddress,
marketToken: fixedAddress,
longToken: market.longTokenAddress,
shortToken: market.shortTokenAddress,
indexToken: market.indexTokenAddress,
Expand Down Expand Up @@ -111,7 +113,7 @@ export function useMarketTokensDataRequest(
}

requests[`${marketAddress}-tokenData`] = {
contractAddress: marketAddress,
contractAddress: fixedAddress,
abi: TokenAbi.abi,
calls: {
totalSupply: {
Expand Down
17 changes: 8 additions & 9 deletions src/domain/synthetics/markets/useMarkets.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useMemo } from "react";
import { ethers } from "ethers";
import { useMemo } from "react";

import { isMarketEnabled } from "config/markets";
import { convertTokenAddress, getToken } from "config/tokens";

import { MarketsData } from "./types";
Expand All @@ -23,13 +22,13 @@ export function useMarkets(chainId: number): MarketsResult {
throw new Error(`Static markets data for chain ${chainId} not found`);
}

return Object.values(markets).reduce(
(acc: MarketsResult, enabledMarketConfig) => {
return Object.entries(markets).reduce(
(acc: MarketsResult, [marketAddress, enabledMarketConfig]) => {
const market = enabledMarketConfig;

if (!isMarketEnabled(chainId, market.marketTokenAddress)) {
return acc;
}
// if (!isMarketEnabled(chainId, marketAddress)) {
// return acc;
// }

try {
const indexToken = getToken(chainId, convertTokenAddress(chainId, market.indexTokenAddress, "native"));
Expand All @@ -41,8 +40,8 @@ export function useMarkets(chainId: number): MarketsResult {

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

acc.marketsAddresses!.push(market.marketTokenAddress);
acc.marketsData![market.marketTokenAddress] = {
acc.marketsAddresses!.push(marketAddress);
acc.marketsData![marketAddress] = {
...market,
isSameCollaterals,
isSpotOnly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ export async function buildMarketsConfigsRequest(
) {
const request: MarketConfigMulticallRequestConfig = {};
for (const marketAddress of marketsAddresses || []) {
const prebuiltHashedKeys = HASHED_MARKET_CONFIG_KEYS[chainId]?.[marketAddress];
// eslint-disable-next-line es-x/no-string-prototype-replaceall
const fixedAddress = marketAddress.replaceAll("1-", "");

const prebuiltHashedKeys = HASHED_MARKET_CONFIG_KEYS[chainId]?.[fixedAddress];

if (!prebuiltHashedKeys) {
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export async function buildMarketsValuesRequest(
const request: MarketValuesMulticallRequestConfig = {};

for (const marketAddress of marketsAddresses || []) {
// eslint-disable-next-line es-x/no-string-prototype-replaceall
const fixedAddress = marketAddress.replaceAll("1-", "");

const market = getByKey(marketsData, marketAddress)!;
const marketPrices = getContractMarketPrices(tokensData!, market)!;

Expand All @@ -41,7 +44,7 @@ export async function buildMarketsValuesRequest(
}

const marketProps = {
marketToken: market.marketTokenAddress,
marketToken: fixedAddress,
indexToken: market.indexTokenAddress,
longToken: market.longTokenAddress,
shortToken: market.shortTokenAddress,
Expand All @@ -53,7 +56,7 @@ export async function buildMarketsValuesRequest(
calls: {
marketInfo: {
methodName: "getMarketInfo",
params: [dataStoreAddress, marketPrices, marketAddress],
params: [dataStoreAddress, marketPrices, fixedAddress],
},
marketTokenPriceMax: {
methodName: "getMarketTokenPrice",
Expand Down Expand Up @@ -82,11 +85,11 @@ export async function buildMarketsValuesRequest(
},
};

const prebuiltHashedKeys = HASHED_MARKET_VALUES_KEYS[chainId]?.[marketAddress];
const prebuiltHashedKeys = HASHED_MARKET_VALUES_KEYS[chainId]?.[fixedAddress];

if (!prebuiltHashedKeys) {
throw new Error(
`No pre-built hashed market keys found for the market ${marketAddress}. Run \`yarn prebuild\` to generate them.`
`No pre-built hashed market keys found for the market ${fixedAddress}. Run \`yarn prebuild\` to generate them.`
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/domain/synthetics/markets/useMarketsInfoRequest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ function useMarketsValuesRequest({
syntheticsReaderAddress,
}),
parseResponse: (res) => {
console.log("PARSE MARKETS INFO", res);

const result = marketsAddresses!.reduce(
(acc, marketAddress) => {
const readerErrors = res.errors[`${marketAddress}-reader`];
Expand Down Expand Up @@ -408,6 +410,8 @@ function useMarketsValuesRequest({
}
);

console.log("MARKETS ADDRESSES", marketsAddresses?.length);

return result;
},
});
Expand Down
33 changes: 31 additions & 2 deletions src/domain/synthetics/trade/utils/swapRouting.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import { MarketInfo, MarketsInfoData } from "domain/synthetics/markets";
import { getAvailableUsdLiquidityForCollateral, MarketInfo, MarketsInfoData } from "domain/synthetics/markets";
import { MarketEdge, MarketsGraph, SwapEstimator, SwapRoute } from "../types";
import { getMaxSwapPathLiquidity, getSwapStats } from "./swapStats";

const MAX_MARKETS_PER_TOKEN = 5;

export function getMarketsGraph(markets: MarketInfo[]): MarketsGraph {
const graph: MarketsGraph = {
abjacencyList: {},
edges: [],
};

for (const market of markets) {
const sortedMarkets = markets.sort((a, b) => {
const liquidityA = getAvailableUsdLiquidityForCollateral(a, true) + getAvailableUsdLiquidityForCollateral(a, false);
const liquidityB = getAvailableUsdLiquidityForCollateral(b, true) + getAvailableUsdLiquidityForCollateral(b, false);

return Number(liquidityB) - Number(liquidityA);
});

const sortedMarketsByTokens: { [token: string]: MarketInfo[] } = {};

for (const market of sortedMarkets) {
const { longTokenAddress, shortTokenAddress } = market;

sortedMarketsByTokens[longTokenAddress] = sortedMarketsByTokens[longTokenAddress] || [];

if (sortedMarketsByTokens[longTokenAddress].length < MAX_MARKETS_PER_TOKEN) {
sortedMarketsByTokens[longTokenAddress].push(market);
}

sortedMarketsByTokens[shortTokenAddress] = sortedMarketsByTokens[shortTokenAddress] || [];

if (sortedMarketsByTokens[shortTokenAddress].length < MAX_MARKETS_PER_TOKEN) {
sortedMarketsByTokens[shortTokenAddress].push(market);
}
}

const marketsForGraph = Object.values(sortedMarketsByTokens).flat();

for (const market of marketsForGraph) {
const { longTokenAddress, shortTokenAddress, marketTokenAddress, isSameCollaterals, isDisabled } = market;

if (isSameCollaterals || isDisabled) {
Expand Down
8 changes: 6 additions & 2 deletions src/lib/multicall/executeMulticall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ async function executeChainMulticall(chainId: number, calls: MulticallFetcherCon

const URGENT_WINDOW_MS = 50;
const BACKGROUND_WINDOW_MS = FREQUENT_UPDATE_INTERVAL - FREQUENT_MULTICALL_REFRESH_INTERVAL;
let numberOfCalls = 0;

const throttledExecuteUrgentChainsMulticalls = throttle(executeChainsMulticalls, URGENT_WINDOW_MS, {
leading: false,
Expand Down Expand Up @@ -159,7 +160,7 @@ export function executeMulticall<TConfig extends MulticallRequestConfig<any>>(

const callResultHandler: CallResultHandler = (destination, callResult, callError) => {
resolvedCallsCount++;

numberOfCalls++;
const { callGroupName, callName } = destination;

if (callResult) {
Expand All @@ -174,6 +175,7 @@ export function executeMulticall<TConfig extends MulticallRequestConfig<any>>(
}

if (resolvedCallsCount === requestCallsCount) {
console.log("batches number of calls", numberOfCalls);
return resolve(requestResult);
}
};
Expand All @@ -190,7 +192,7 @@ export function executeMulticall<TConfig extends MulticallRequestConfig<any>>(
// There are two main reasons for this:
// 1. Single token backed pools have many pairs with the same method signatures
// 2. The majority of pools have USDC as the short token, which means they all have some common calls
const callId = getCallId(callGroup.contractAddress, call.methodName, call.params);
const callId = getCallId(callGroupName, call.methodName, call.params);

if (!store.current[chainId]) {
store.current[chainId] = {};
Expand Down Expand Up @@ -249,6 +251,8 @@ export function executeMulticall<TConfig extends MulticallRequestConfig<any>>(
return promise.then((result) => {
const duration = performance.now() - durationStart;

console.log("batches timing", duration);

if (result.success) {
emitMetricTiming<MulticallBatchedTiming>({
event: "multicall.batched.timing",
Expand Down