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

Feature/userbalances on pools #546

Merged
merged 3 commits into from
Dec 1, 2023
Merged
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
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