From 06c2a0ff993e4af92d79fcb1f9ef7bd0f54c43b6 Mon Sep 17 00:00:00 2001 From: franz Date: Mon, 27 Nov 2023 15:53:37 +0100 Subject: [PATCH 1/2] start user balances for pools --- modules/pool/lib/pool-gql-loader.service.ts | 36 +++++++++++++++++++-- modules/pool/pool.gql | 7 ++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/modules/pool/lib/pool-gql-loader.service.ts b/modules/pool/lib/pool-gql-loader.service.ts index 71dc8cc40..3d46b8439 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,7 +34,7 @@ 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'; @@ -59,6 +59,24 @@ 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: true, + userStakedBalances: true, + }, + }); + + 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 +95,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 +107,7 @@ export class PoolGqlLoaderService { allTokens: this.mapAllTokens(pool), displayTokens: this.mapDisplayTokens(pool), staking: this.getStakingData(pool), + userBalance: this.getUserBalance(userWalletbalances, userStakedBalances), }; } @@ -570,6 +593,13 @@ export class PoolGqlLoaderService { }; } + private getUserBalance( + userWalletBalances: PrismaUserWalletBalance[], + userStakedBalances: PrismaUserStakedBalance[], + ): GqlPoolUserBalance | null { + throw new Error('Method not implemented.'); + } + 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 { From fbb73de74d490cd0abccbdb8a4b8d560238c9a60 Mon Sep 17 00:00:00 2001 From: franz Date: Fri, 1 Dec 2023 10:14:11 +0100 Subject: [PATCH 2/2] return balances --- modules/pool/lib/pool-gql-loader.service.ts | 34 ++++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/modules/pool/lib/pool-gql-loader.service.ts b/modules/pool/lib/pool-gql-loader.service.ts index 3d46b8439..1e90acefc 100644 --- a/modules/pool/lib/pool-gql-loader.service.ts +++ b/modules/pool/lib/pool-gql-loader.service.ts @@ -39,6 +39,8 @@ 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 { @@ -61,14 +63,29 @@ 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: true, - userStakedBalances: true, + 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 }, + }, + }, }, }); @@ -596,8 +613,15 @@ export class PoolGqlLoaderService { private getUserBalance( userWalletBalances: PrismaUserWalletBalance[], userStakedBalances: PrismaUserStakedBalance[], - ): GqlPoolUserBalance | null { - throw new Error('Method not implemented.'); + ): 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 {