Skip to content

Commit

Permalink
Add support for fetching user balances for multiple chains
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmkm committed Oct 16, 2023
1 parent 725d93f commit 1fb4c7b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions modules/user/lib/user-balance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export class UserBalanceService {
where: { address: address.toLowerCase() },
include: {
walletBalances: {
where: { chain: networkContext.chain, poolId: { not: null }, balanceNum: { gt: 0 } },
where: { poolId: { not: null }, balanceNum: { gt: 0 } },
},
stakedBalances: {
where: { chain: networkContext.chain, poolId: { not: null }, balanceNum: { gt: 0 } },
where: { poolId: { not: null }, balanceNum: { gt: 0 } },
},
},
});
Expand All @@ -43,6 +43,8 @@ export class UserBalanceService {
totalBalance: formatFixed(stakedNum.add(walletNum), 18),
stakedBalance: stakedBalance?.balance || '0',
walletBalance: walletBalance?.balance || '0',
// the prisma query above ensures that one of these balances exists
chain: (stakedBalance?.chain || walletBalance?.chain)!,
};
});
}
Expand All @@ -68,6 +70,7 @@ export class UserBalanceService {
totalBalance: formatFixed(stakedNum.add(walletNum), 18),
stakedBalance: stakedBalance?.balance || '0',
walletBalance: walletBalance?.balance || '0',
chain: networkContext.chain,
};
}

Expand Down
3 changes: 2 additions & 1 deletion modules/user/user-types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AmountHumanReadable } from '../common/global-types';
import { PrismaPoolStaking, PrismaPoolStakingType } from '@prisma/client';
import { Chain, PrismaPoolStaking, PrismaPoolStakingType } from '@prisma/client';
import { Relic } from '../subgraphs/reliquary-subgraph/generated/reliquary-subgraph-types';

export interface UserStakedBalanceService {
Expand All @@ -14,6 +14,7 @@ export interface UserPoolBalance {
totalBalance: AmountHumanReadable;
walletBalance: AmountHumanReadable;
stakedBalance: AmountHumanReadable;
chain: Chain;
}

export interface UserSyncUserBalanceInput {
Expand Down
3 changes: 2 additions & 1 deletion modules/user/user.gql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend type Query {
userGetPoolBalances: [GqlUserPoolBalance!]!
userGetPoolBalances(chains: [GqlChain!]!): [GqlUserPoolBalance!]!
userGetStaking: [GqlPoolStaking!]!
userGetPoolJoinExits(first: Int = 10, skip: Int = 0, poolId: String!): [GqlPoolJoinExit!]!
userGetSwaps(first: Int = 10, skip: Int = 0, poolId: String!): [GqlPoolSwap!]!
Expand All @@ -24,4 +24,5 @@ type GqlUserPoolBalance {
totalBalance: AmountHumanReadable!
walletBalance: AmountHumanReadable!
stakedBalance: AmountHumanReadable!
chain: GqlChain!
}
2 changes: 1 addition & 1 deletion modules/user/user.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { tokenService } from '../token/token.service';

const resolvers: Resolvers = {
Query: {
userGetPoolBalances: async (parent, {}, context) => {
userGetPoolBalances: async (parent, { chains }, context) => {
const accountAddress = getRequiredAccountAddress(context);
const tokenPrices = await tokenService.getTokenPrices();
const balances = await userService.getUserPoolBalances(accountAddress);
Expand Down

0 comments on commit 1fb4c7b

Please sign in to comment.