Skip to content

Commit

Permalink
redo wallet/staked balance query (#53)
Browse files Browse the repository at this point in the history
* replace queries, no join but two queries

* adding indexes on user addresses in balance tables

* filter non-zero balances manually

---------

Co-authored-by: gmbronco <[email protected]>
  • Loading branch information
franzns and gmbronco authored Jan 22, 2024
1 parent cc6eef8 commit 38646bc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 22 deletions.
71 changes: 49 additions & 22 deletions modules/user/lib/user-balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,48 @@ export class UserBalanceService {
constructor() {}

public async getUserPoolBalances(address: string, chains: Chain[]): Promise<UserPoolBalance[]> {
const user = await prisma.prismaUser.findUnique({
where: { address: address.toLowerCase() },
include: {
walletBalances: {
where: { chain: { in: chains }, poolId: { not: null }, balanceNum: { gt: 0 } },
},
stakedBalances: {
where: { chain: { in: chains }, poolId: { not: null }, balanceNum: { gt: 0 } },
},
// this seems to cause heavy load
// const user = await prisma.prismaUser.findUnique({
// where: { address: address.toLowerCase() },
// include: {
// walletBalances: {
// where: { chain: { in: chains }, poolId: { not: null }, balanceNum: { gt: 0 } },
// },
// stakedBalances: {
// where: { chain: { in: chains }, poolId: { not: null }, balanceNum: { gt: 0 } },
// },
// },
// });

const userWalletBalances = await prisma.prismaUserWalletBalance.findMany({
where: {
userAddress: address.toLowerCase(),
chain: { in: chains },
},
});

const userStakedBalances = await prisma.prismaUserStakedBalance.findMany({
where: {
userAddress: address.toLowerCase(),
chain: { in: chains },
},
});

if (!user) {
const nonZeroUserWalletBalances = userWalletBalances.filter((balance) => balance.balanceNum > 0);
const nonZeroUserStakedBalances = userStakedBalances.filter((balance) => balance.balanceNum > 0);

if (nonZeroUserWalletBalances.length === 0 && nonZeroUserStakedBalances.length === 0) {
return [];
}

const poolIds = _.uniq([
...user.stakedBalances.map((balance) => balance.poolId),
...user.walletBalances.map((balance) => balance.poolId),
...nonZeroUserWalletBalances.map((balance) => balance.poolId),
...nonZeroUserStakedBalances.map((balance) => balance.poolId),
]) as string[];

return poolIds.map((poolId) => {
const stakedBalance = user.stakedBalances.find((balance) => balance.poolId === poolId);
const walletBalance = user.walletBalances.find((balance) => balance.poolId === poolId);
const walletBalance = nonZeroUserWalletBalances.find((balance) => balance.poolId === poolId);
const stakedBalance = nonZeroUserStakedBalances.find((balance) => balance.poolId === poolId);
const stakedNum = parseUnits(stakedBalance?.balance || '0', 18);
const walletNum = parseUnits(walletBalance?.balance || '0', 18);

Expand All @@ -52,16 +70,25 @@ export class UserBalanceService {
public async getUserFbeetsBalance(address: string): Promise<Omit<UserPoolBalance, 'poolId'>> {
const fbeetsAddress = networkContext.data.fbeets?.address || '';

const user = await prisma.prismaUser.findUnique({
where: { address: address.toLowerCase() },
include: {
walletBalances: { where: { chain: networkContext.chain, tokenAddress: fbeetsAddress } },
stakedBalances: { where: { chain: networkContext.chain, tokenAddress: fbeetsAddress } },
},
const userWalletBalances = await prisma.prismaUserWalletBalance.findMany({
where: { userAddress: address.toLowerCase(), chain: networkContext.chain, tokenAddress: fbeetsAddress },
});

const userStakedBalances = await prisma.prismaUserStakedBalance.findMany({
where: { userAddress: address.toLowerCase(), chain: networkContext.chain, tokenAddress: fbeetsAddress },
});

const stakedBalance = user?.stakedBalances[0];
const walletBalance = user?.walletBalances[0];
// this seems to cause heavy load
// const user = await prisma.prismaUser.findUnique({
// where: { address: address.toLowerCase() },
// include: {
// walletBalances: { where: { chain: networkContext.chain, tokenAddress: fbeetsAddress } },
// stakedBalances: { where: { chain: networkContext.chain, tokenAddress: fbeetsAddress } },
// },
// });

const stakedBalance = userWalletBalances[0];
const walletBalance = userStakedBalances[0];
const stakedNum = parseUnits(stakedBalance?.balance || '0', 18);
const walletNum = parseUnits(walletBalance?.balance || '0', 18);

Expand Down
2 changes: 2 additions & 0 deletions modules/user/user.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ model PrismaUser {

model PrismaUserWalletBalance {
@@id([id, chain])
@@index(userAddress)
id String
chain Chain
Expand All @@ -35,6 +36,7 @@ model PrismaUserWalletBalance {

model PrismaUserStakedBalance {
@@id([id, chain])
@@index(userAddress)
id String
chain Chain
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- CreateIndex
CREATE INDEX "PrismaUserStakedBalance_userAddress_idx" ON "PrismaUserStakedBalance"("userAddress");

-- CreateIndex
CREATE INDEX "PrismaUserWalletBalance_userAddress_idx" ON "PrismaUserWalletBalance"("userAddress");
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ model PrismaUser {

model PrismaUserWalletBalance {
@@id([id, chain])
@@index(userAddress)
id String
chain Chain
Expand All @@ -797,6 +798,7 @@ model PrismaUserWalletBalance {

model PrismaUserStakedBalance {
@@id([id, chain])
@@index(userAddress)
id String
chain Chain
Expand Down

0 comments on commit 38646bc

Please sign in to comment.