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

Break up the GetUserPools query #3064

Open
ValarDragon opened this issue Apr 9, 2024 · 1 comment
Open

Break up the GetUserPools query #3064

ValarDragon opened this issue Apr 9, 2024 · 1 comment

Comments

@ValarDragon
Copy link
Member

The getUserPools query takes >5s on my laptop.

This seems attributable to this function doing way too much and having a sub-optimal caching + server side vs client side strategy.

export async function getUserPools(params: {
assetLists: AssetList[];
chainList: Chain[];
bech32Address: string;
}) {
const { assetLists } = params;
const [accountPositions, poolIncentives, superfluidPoolIds] =
await Promise.all([
timeout(
() =>
queryAccountPositions(params)
.then(({ positions }) => positions)
.catch(() => [] as LiquidityPosition[]),
10_000, // 10 seconds
"queryCLPositions"
)().catch(() => [] as LiquidityPosition[]),
timeout(
() =>
getCachedPoolIncentivesMap().catch(
() => new Map<string, PoolIncentives>()
),
10_000, // 10 seconds
"getCachedPoolIncentivesMap"
)().catch(() => new Map<string, PoolIncentives>()),
timeout(
() => getSuperfluidPoolIds(params).catch(() => [] as string[]),
10_000, // 10 seconds
"getSuperfluidPoolIds"
)().catch(() => [] as string[]),
]);
const { locked: lockedShares, poolIds } = await getUserShareRawCoins(params);
const userUniquePoolIds = new Set(poolIds);
accountPositions
.map(({ position: { pool_id } }) => pool_id)
.forEach((poolId) => userUniquePoolIds.add(poolId));
const eventualPools = await timeout(
() => getPools({ ...params, poolIds: Array.from(userUniquePoolIds) }),
10_000, // 10 seconds
"getPools"
)();
return await Promise.all(
eventualPools.map(async (pool) => {
const { id, reserveCoins, totalFiatValueLocked, type } = pool;
let userValue: PricePretty = new PricePretty(
DEFAULT_VS_CURRENCY,
new Dec(0)
);
if (type === "concentrated") {
const positions = accountPositions.filter(
({ position: { pool_id } }) => pool_id === id
);
if (positions.length === 0) {
throw new Error(
`Positions for pool id ${id} not found. It should exist if the pool id is available in the userPoolIds set.`
);
}
const aggregatedRawCoins = aggregateRawCoinsByDenom(
positions.flatMap(({ asset0, asset1 }) => [asset0, asset1])
);
const coinsToCalculateValue = mapRawCoinToPretty(
assetLists,
aggregatedRawCoins
);
userValue = new PricePretty(
DEFAULT_VS_CURRENCY,
await calcSumCoinsValue({ ...params, coins: coinsToCalculateValue })
);
} else if (type === "weighted" || type === "stable") {
const totalShareAmount = new Dec(
(
pool.raw as WeightedPoolRawResponse | StablePoolRawResponse
).total_shares.amount
);
const rawShare = aggregateRawCoinsByDenom(
lockedShares
.filter((coin) => coin.denom === `gamm/pool/${id}`)
.concat()
)[0];
if (rawShare) {
const userValueAmount = totalShareAmount.isZero()
? new Dec(0)
: totalFiatValueLocked
.mul(
new IntPretty(new Dec(rawShare.amount).quo(totalShareAmount))
)
.trim(true);
userValue = new PricePretty(DEFAULT_VS_CURRENCY, userValueAmount);
}
}
return {
id,
type,
reserveCoins,
apr: poolIncentives.get(id)?.aprBreakdown?.total,
poolLiquidity: totalFiatValueLocked,
isSuperfluid: superfluidPoolIds.some(
(superfluidPoolId) => superfluidPoolId === id
),
/** Note: if it's a share pool it is just locked shares value. */
userValue,
};
})
);
}

We can get each of the pool types independently. Furthermore some of this data already is known locally, e.g. queryAccountPositions

@jonator
Copy link
Member

jonator commented Oct 8, 2024

I think migration to SQS pools query, which includes a list of pool IDs as input, greatly improves response time since we no longer have to filter over all pools

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants