diff --git a/modules/pool/lib/pool-gql-loader.service.ts b/modules/pool/lib/pool-gql-loader.service.ts index 71dc8cc40..1e90acefc 100644 --- a/modules/pool/lib/pool-gql-loader.service.ts +++ b/modules/pool/lib/pool-gql-loader.service.ts @@ -11,7 +11,6 @@ import { import { GqlBalancePoolAprItem, GqlBalancePoolAprSubItem, - GqlChain, GqlPoolDynamicData, GqlPoolFeaturedPoolGroup, GqlPoolInvestConfig, @@ -27,6 +26,7 @@ import { GqlPoolTokenExpanded, GqlPoolTokenUnion, GqlPoolUnion, + GqlPoolUserBalance, GqlPoolWithdrawConfig, GqlPoolWithdrawOption, QueryPoolGetPoolsArgs, @@ -34,11 +34,13 @@ import { import { isSameAddress } from '@balancer-labs/sdk'; import _ from 'lodash'; import { prisma } from '../../../prisma/prisma-client'; -import { Chain, Prisma, PrismaPoolAprType } from '@prisma/client'; +import { Chain, Prisma, PrismaPoolAprType, PrismaUserStakedBalance, PrismaUserWalletBalance } from '@prisma/client'; import { isWeightedPoolV2 } from './pool-utils'; import { oldBnum } from '../../big-number/old-big-number'; import { networkContext } from '../../network/network-context.service'; import { fixedNumber } from '../../view-helpers/fixed-number'; +import { parseUnits } from 'ethers/lib/utils'; +import { formatFixed } from '@ethersproject/bignumber'; export class PoolGqlLoaderService { public async getPool(id: string, chain: Chain): Promise { @@ -59,6 +61,39 @@ export class PoolGqlLoaderService { } public async getPools(args: QueryPoolGetPoolsArgs): Promise { + // only include wallet and staked balances if the query requests it + // this makes sure that we don't load ALL user balances when we don't filter on userAddress + if (args.where?.userAddress) { + const pools = await prisma.prismaPool.findMany({ + ...this.mapQueryArgsToPoolQuery(args), + include: { + ...prismaPoolMinimal.include, + userWalletBalances: { + where: { + userAddress: { + equals: args.where?.userAddress, + mode: 'insensitive' as const, + }, + balanceNum: { gt: 0 }, + }, + }, + userStakedBalances: { + where: { + userAddress: { + equals: args.where?.userAddress, + mode: 'insensitive' as const, + }, + balanceNum: { gt: 0 }, + }, + }, + }, + }); + + return pools.map((pool) => + this.mapToMinimalGqlPool(pool, pool.userWalletBalances, pool.userStakedBalances), + ); + } + const pools = await prisma.prismaPool.findMany({ ...this.mapQueryArgsToPoolQuery(args), include: prismaPoolMinimal.include, @@ -77,7 +112,11 @@ export class PoolGqlLoaderService { return pools.map((pool) => this.mapPoolToGqlPool(pool)) as GqlPoolLinear[]; } - public mapToMinimalGqlPool(pool: PrismaPoolMinimal): GqlPoolMinimal { + public mapToMinimalGqlPool( + pool: PrismaPoolMinimal, + userWalletbalances: PrismaUserWalletBalance[] = [], + userStakedBalances: PrismaUserStakedBalance[] = [], + ): GqlPoolMinimal { return { ...pool, decimals: 18, @@ -85,6 +124,7 @@ export class PoolGqlLoaderService { allTokens: this.mapAllTokens(pool), displayTokens: this.mapDisplayTokens(pool), staking: this.getStakingData(pool), + userBalance: this.getUserBalance(userWalletbalances, userStakedBalances), }; } @@ -570,6 +610,20 @@ export class PoolGqlLoaderService { }; } + private getUserBalance( + userWalletBalances: PrismaUserWalletBalance[], + userStakedBalances: PrismaUserStakedBalance[], + ): GqlPoolUserBalance { + const stakedNum = parseUnits(userWalletBalances.at(0)?.balance || '0', 18); + const walletNum = parseUnits(userStakedBalances.at(0)?.balance || '0', 18); + + return { + walletBalance: userWalletBalances.at(0)?.balance || '0', + stakedBalance: userStakedBalances.at(0)?.balance || '0', + totalBalance: formatFixed(stakedNum.add(walletNum), 18), + }; + } + private getPoolDynamicData(pool: PrismaPoolMinimal): GqlPoolDynamicData { const { fees24h, diff --git a/modules/pool/pool.gql b/modules/pool/pool.gql index 219dea792..f6d231cf3 100644 --- a/modules/pool/pool.gql +++ b/modules/pool/pool.gql @@ -75,6 +75,13 @@ type GqlPoolMinimal { staking: GqlPoolStaking type: GqlPoolMinimalType! version: Int! + userBalance: GqlPoolUserBalance +} + +type GqlPoolUserBalance { + totalBalance: AmountHumanReadable! + walletBalance: AmountHumanReadable! + stakedBalance: AmountHumanReadable! } enum GqlPoolMinimalType {