Skip to content

Commit

Permalink
Merge pull request #546 from beethovenxfi/feature/userbalances-on-pools
Browse files Browse the repository at this point in the history
Feature/userbalances on pools
  • Loading branch information
franzns authored Dec 1, 2023
2 parents 97589a1 + fbb73de commit 9b84298
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
60 changes: 57 additions & 3 deletions modules/pool/lib/pool-gql-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
import {
GqlBalancePoolAprItem,
GqlBalancePoolAprSubItem,
GqlChain,
GqlPoolDynamicData,
GqlPoolFeaturedPoolGroup,
GqlPoolInvestConfig,
Expand All @@ -27,18 +26,21 @@ import {
GqlPoolTokenExpanded,
GqlPoolTokenUnion,
GqlPoolUnion,
GqlPoolUserBalance,
GqlPoolWithdrawConfig,
GqlPoolWithdrawOption,
QueryPoolGetPoolsArgs,
} from '../../../schema';
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<GqlPoolUnion> {
Expand All @@ -59,6 +61,39 @@ export class PoolGqlLoaderService {
}

public async getPools(args: QueryPoolGetPoolsArgs): Promise<GqlPoolMinimal[]> {
// 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,
Expand All @@ -77,14 +112,19 @@ 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,
dynamicData: this.getPoolDynamicData(pool),
allTokens: this.mapAllTokens(pool),
displayTokens: this.mapDisplayTokens(pool),
staking: this.getStakingData(pool),
userBalance: this.getUserBalance(userWalletbalances, userStakedBalances),
};
}

Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions modules/pool/pool.gql
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ type GqlPoolMinimal {
staking: GqlPoolStaking
type: GqlPoolMinimalType!
version: Int!
userBalance: GqlPoolUserBalance
}

type GqlPoolUserBalance {
totalBalance: AmountHumanReadable!
walletBalance: AmountHumanReadable!
stakedBalance: AmountHumanReadable!
}

enum GqlPoolMinimalType {
Expand Down

0 comments on commit 9b84298

Please sign in to comment.