Skip to content

Commit

Permalink
Fix/staking model (#140)
Browse files Browse the repository at this point in the history
* wip

* only include staked balance of active/preferred gauge
  • Loading branch information
franzns authored Feb 29, 2024
1 parent 4acc856 commit c30fcc9
Showing 1 changed file with 78 additions and 31 deletions.
109 changes: 78 additions & 31 deletions modules/pool/lib/pool-gql-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ import {
import { isSameAddress } from '@balancer-labs/sdk';
import _ from 'lodash';
import { prisma } from '../../../prisma/prisma-client';
import { Chain, Prisma, PrismaPoolAprType, PrismaUserStakedBalance, PrismaUserWalletBalance } from '@prisma/client';
import {
Chain,
Prisma,
PrismaPoolAprType,
PrismaPoolStaking,
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';
Expand All @@ -48,15 +55,15 @@ import { BeethovenChainIds, chainToIdMap } from '../../network/network-config';
import { GithubContentService } from '../../content/github-content.service';
import { SanityContentService } from '../../content/sanity-content.service';
import { ElementData, FxData, GyroData, LinearData, StableData } from '../subgraph-mapper';
import { PoolUsdDataService } from './pool-usd-data.service';

export class PoolGqlLoaderService {
public async getPool(id: string, chain: Chain, userAddress?: string): Promise<GqlPoolUnion> {
let pool = undefined;
pool = await prisma.prismaPool.findUnique({
where: { id_chain: { id, chain: chain } },
include: {
...prismaPoolWithExpandedNesting.include,
...this.getUserBalancesInclude(userAddress),
...this.getPoolInclude(userAddress),
},
});

Expand All @@ -68,7 +75,11 @@ export class PoolGqlLoaderService {
throw new Error('Pool exists, but has an unknown type');
}

return this.mapPoolToGqlPool(pool, pool.userWalletBalances, pool.userStakedBalances);
return this.mapPoolToGqlPool(
pool,
pool.userWalletBalances,
pool.staking.map((staking) => staking.userStakedBalances).flat(),
);
}

public async getPools(args: QueryPoolGetPoolsArgs): Promise<GqlPoolMinimal[]> {
Expand All @@ -83,16 +94,20 @@ export class PoolGqlLoaderService {
args.first = undefined;
args.skip = undefined;
}
// const includeQuery = args.where.userAddress ? prismaPoolMinimal.include.staking.include.
const pools = await prisma.prismaPool.findMany({
...this.mapQueryArgsToPoolQuery(args),
include: {
...prismaPoolMinimal.include,
...this.getUserBalancesInclude(args.where.userAddress),
...this.getPoolInclude(args.where.userAddress),
},
});

const gqlPools = pools.map((pool) =>
this.mapToMinimalGqlPool(pool, pool.userWalletBalances, pool.userStakedBalances),
this.mapToMinimalGqlPool(
pool,
pool.userWalletBalances,
pool.staking.map((staking) => staking.userStakedBalances).flat(),
),
);

if (args.orderBy === 'userbalanceUsd') {
Expand Down Expand Up @@ -458,9 +473,11 @@ export class PoolGqlLoaderService {

const bpt = pool.tokens.find((token) => token.address === pool.address);

const stakingData = this.getStakingData(pool);

const mappedData = {
decimals: 18,
staking: this.getStakingData(pool),
staking: stakingData,
dynamicData: this.getPoolDynamicData(pool),
investConfig: this.getPoolInvestConfig(pool),
withdrawConfig: this.getPoolWithdrawConfig(pool),
Expand Down Expand Up @@ -647,7 +664,21 @@ export class PoolGqlLoaderService {
}
}

const sorted = _.sortBy(pool.staking, (staking) => {
const sorted = this.getSortedGauges(pool);

return {
...sorted[0],
gauge: {
...sorted[0].gauge!,
otherGauges: sorted.slice(1).map((item) => item.gauge!),
},
farm: null,
reliquary: null,
};
}

private getSortedGauges(pool: PrismaPoolMinimal) {
return _.sortBy(pool.staking, (staking) => {
if (staking.gauge) {
switch (staking.gauge.status) {
case 'PREFERRED':
Expand All @@ -661,16 +692,6 @@ export class PoolGqlLoaderService {

return 100;
}).filter((staking) => staking.gauge);

return {
...sorted[0],
gauge: {
...sorted[0].gauge!,
otherGauges: sorted.slice(1).map((item) => item.gauge!),
},
farm: null,
reliquary: null,
};
}

private getUserBalance(
Expand All @@ -683,14 +704,24 @@ export class PoolGqlLoaderService {
bptPrice = pool.dynamicData.totalLiquidity / parseFloat(pool.dynamicData.totalShares);
}

let activeStakingId = userStakedBalances.at(0)?.id;
if (pool.staking.length > 1) {
const sortedGauges = this.getSortedGauges(pool);
activeStakingId = sortedGauges[0].id;
}

const activeUserStakedBalance = userStakedBalances.find(
(userStakedBalance) => userStakedBalance.id === activeStakingId,
);

const walletBalance = parseUnits(userWalletBalances.at(0)?.balance || '0', 18);
const stakedBalance = parseUnits(userStakedBalances.at(0)?.balance || '0', 18);
const stakedBalance = parseUnits(activeUserStakedBalance?.balance || '0', 18);
const walletBalanceNum = userWalletBalances.at(0)?.balanceNum || 0;
const stakedBalanceNum = userStakedBalances.at(0)?.balanceNum || 0;
const stakedBalanceNum = activeUserStakedBalance?.balanceNum || 0;

return {
walletBalance: userWalletBalances.at(0)?.balance || '0',
stakedBalance: userStakedBalances.at(0)?.balance || '0',
stakedBalance: activeUserStakedBalance?.balance || '0',
totalBalance: formatFixed(stakedBalance.add(walletBalance), 18),
walletBalanceUsd: walletBalanceNum * bptPrice,
stakedBalanceUsd: stakedBalanceNum * bptPrice,
Expand Down Expand Up @@ -1244,21 +1275,37 @@ export class PoolGqlLoaderService {
};
}

private getUserBalancesInclude(userAddress?: string) {
private getPoolInclude(userAddress?: string) {
if (!userAddress) {
return {};
return {
...prismaPoolWithExpandedNesting.include,
staking: {
include: {
...prismaPoolWithExpandedNesting.include.staking.include,
userStakedBalances: false,
},
},
userWalletBalances: false,
};
}

return {
userWalletBalances: {
where: {
userAddress: {
equals: userAddress,
mode: 'insensitive' as const,
...prismaPoolWithExpandedNesting.include,
staking: {
include: {
...prismaPoolWithExpandedNesting.include.staking.include,
userStakedBalances: {
where: {
userAddress: {
equals: userAddress,
mode: 'insensitive' as const,
},
balanceNum: { gt: 0 },
},
},
balanceNum: { gt: 0 },
},
},
userStakedBalances: {
userWalletBalances: {
where: {
userAddress: {
equals: userAddress,
Expand Down

0 comments on commit c30fcc9

Please sign in to comment.