Skip to content

Commit

Permalink
manual sort
Browse files Browse the repository at this point in the history
  • Loading branch information
franzns committed Dec 8, 2023
1 parent 93ed17c commit cdf5f9d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
30 changes: 25 additions & 5 deletions modules/pool/lib/pool-gql-loader.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ export class PoolGqlLoaderService {
},
});

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

if (args.orderBy === 'userbalanceUsd') {
if (args.orderDirection === 'asc') {
return gqlPools.sort((a, b) => a.userBalance!.totalBalanceUsd - b.userBalance!.totalBalanceUsd);
}
return gqlPools.sort((a, b) => b.userBalance!.totalBalanceUsd - a.userBalance!.totalBalanceUsd);
}

return gqlPools;
}

const pools = await prisma.prismaPool.findMany({
Expand Down Expand Up @@ -124,7 +133,7 @@ export class PoolGqlLoaderService {
allTokens: this.mapAllTokens(pool),
displayTokens: this.mapDisplayTokens(pool),
staking: this.getStakingData(pool),
userBalance: this.getUserBalance(userWalletbalances, userStakedBalances),
userBalance: this.getUserBalance(pool, userWalletbalances, userStakedBalances),
};
}

Expand Down Expand Up @@ -611,16 +620,27 @@ export class PoolGqlLoaderService {
}

private getUserBalance(
pool: PrismaPoolMinimal,
userWalletBalances: PrismaUserWalletBalance[],
userStakedBalances: PrismaUserStakedBalance[],
): GqlPoolUserBalance {
const stakedNum = parseUnits(userWalletBalances.at(0)?.balance || '0', 18);
const walletNum = parseUnits(userStakedBalances.at(0)?.balance || '0', 18);
let bptPrice = 0;
if (pool.dynamicData && pool.dynamicData.totalLiquidity > 0 && parseFloat(pool.dynamicData.totalShares) > 0) {
bptPrice = pool.dynamicData.totalLiquidity / parseFloat(pool.dynamicData.totalShares);
}

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

return {
walletBalance: userWalletBalances.at(0)?.balance || '0',
stakedBalance: userStakedBalances.at(0)?.balance || '0',
totalBalance: formatFixed(stakedNum.add(walletNum), 18),
totalBalance: formatFixed(stakedBalance.add(walletBalance), 18),
walletBalanceUsd: walletBalanceNum * bptPrice,
stakedBalanceUsd: stakedBalanceNum * bptPrice,
totalBalanceUsd: (walletBalanceNum + stakedBalanceNum) * bptPrice,
};
}

Expand Down
4 changes: 4 additions & 0 deletions modules/pool/pool.gql
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ type GqlPoolMinimal {

type GqlPoolUserBalance {
totalBalance: AmountHumanReadable!
totalBalanceUsd: Float!
walletBalance: AmountHumanReadable!
walletBalanceUsd: Float!
stakedBalance: AmountHumanReadable!
stakedBalanceUsd: Float!
}

enum GqlPoolMinimalType {
Expand Down Expand Up @@ -602,6 +605,7 @@ enum GqlPoolOrderBy {
volume24h
fees24h
apr
userbalanceUsd
}

enum GqlPoolOrderDirection {
Expand Down

0 comments on commit cdf5f9d

Please sign in to comment.