Skip to content

redo wallet/staked balance query #53

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

Merged
merged 3 commits into from
Jan 22, 2024
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
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