Skip to content

Commit

Permalink
adding query params (#519)
Browse files Browse the repository at this point in the history
* adding query params

* changeset
  • Loading branch information
franzns authored Jul 1, 2024
1 parent d5cbaf3 commit 798c947
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/modern-pandas-jump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'backend': patch
---

adding chain and user address as query params to vebal queries
2 changes: 1 addition & 1 deletion modules/vebal/vebal-balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('vebal debugging', () => {
initRequestScopedContext();
setRequestScopedContextValue('chainId', '1');
await veBalService.syncVeBalBalances();
const holder = await veBalService.getVeBalUserData('0x4ec8459bb6bab83d8987373f6ae47b9a60bd5a6a');
const holder = await veBalService.getVeBalUserData('MAINNET', '0x4ec8459bb6bab83d8987373f6ae47b9a60bd5a6a');
expect(holder.balance).not.toBe('0.0');
expect(holder.locked).not.toBe('0.0');
expect(holder.lockedUsd).not.toBe('0.00');
Expand Down
6 changes: 3 additions & 3 deletions modules/vebal/vebal.gql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
extend type Query {
veBalGetUserBalance: AmountHumanReadable!
veBalGetTotalSupply: AmountHumanReadable!
veBalGetUserBalance(chain: GqlChain, address: String): AmountHumanReadable!
veBalGetTotalSupply(chain: GqlChain): AmountHumanReadable!
veBalGetVotingList: [GqlVotingPool!]!
veBalGetUser: GqlVeBalUserData!
veBalGetUser(chain: GqlChain, address: String!): GqlVeBalUserData!
}

extend type Mutation {
Expand Down
37 changes: 29 additions & 8 deletions modules/vebal/vebal.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@ import { Resolvers } from '../../schema';
import { getRequiredAccountAddress, isAdminRoute } from '../auth/auth-context';
import { veBalService } from './vebal.service';
import { veBalVotingListService } from './vebal-voting-list.service';
import { headerChain } from '../context/header-chain';

const resolvers: Resolvers = {
Query: {
veBalGetUserBalance: async (parent, {}, context) => {
const accountAddress = getRequiredAccountAddress(context);
return veBalService.getVeBalUserBalance(accountAddress);
veBalGetUserBalance: async (parent, { chain, address }, context) => {
const currentChain = headerChain();
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('veBalGetUserBalance error: Provide "chains" param');
}

const accountAddress = address || getRequiredAccountAddress(context);
return veBalService.getVeBalUserBalance(chain, accountAddress);
},
veBalGetUser: async (parent, {}, context) => {
const accountAddress = getRequiredAccountAddress(context);
return veBalService.getVeBalUserData(accountAddress);
veBalGetUser: async (parent, { chain, address }, context) => {
const currentChain = headerChain();
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('veBalGetUser error: Provide "chains" param');
}

const accountAddress = address || getRequiredAccountAddress(context);
return veBalService.getVeBalUserData(chain, accountAddress);
},
veBalGetTotalSupply: async (parent, {}, context) => {
return veBalService.getVeBalTotalSupply();
veBalGetTotalSupply: async (parent, { chain }, context) => {
const currentChain = headerChain();
if (!chain && currentChain) {
chain = currentChain;
} else if (!chain) {
throw new Error('veBalGetTotalSupply error: Provide "chains" param');
}
return veBalService.getVeBalTotalSupply(chain);
},

/*
Expand Down
15 changes: 8 additions & 7 deletions modules/vebal/vebal.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import { AmountHumanReadable } from '../common/global-types';
import { GqlVeBalUserData } from '../../schema';
import { AllNetworkConfigs } from '../network/network-config';
import VeBalABI from './abi/vebal.json';
import { Chain } from '@prisma/client';

export class VeBalService {
public async getVeBalUserBalance(userAddress: string): Promise<AmountHumanReadable> {
public async getVeBalUserBalance(chain: Chain, userAddress: string): Promise<AmountHumanReadable> {
if (networkContext.data.veBal) {
const veBalUser = await prisma.prismaVeBalUserBalance.findFirst({
where: { chain: networkContext.chain, userAddress: userAddress.toLowerCase() },
where: { chain: chain, userAddress: userAddress.toLowerCase() },
});
if (veBalUser?.balance) {
return veBalUser.balance;
Expand All @@ -26,13 +27,13 @@ export class VeBalService {
return '0.0';
}

public async getVeBalUserData(userAddress: string): Promise<GqlVeBalUserData> {
public async getVeBalUserData(chain: Chain, userAddress: string): Promise<GqlVeBalUserData> {
let rank = 1;
let balance = '0.0';
let locked = '0.0';
if (networkContext.data.veBal) {
const veBalUsers = await prisma.prismaVeBalUserBalance.findMany({
where: { chain: networkContext.chain },
where: { chain: chain },
});

const veBalUsersNum = veBalUsers.map((user) => ({
Expand All @@ -57,7 +58,7 @@ export class VeBalService {

if (locked !== '0.0') {
veBalPrice = await prisma.prismaTokenCurrentPrice.findFirstOrThrow({
where: { chain: networkContext.chain, tokenAddress: AllNetworkConfigs['1'].data.veBal!.bptAddress },
where: { chain: chain, tokenAddress: AllNetworkConfigs['1'].data.veBal!.bptAddress },
});
}

Expand All @@ -69,10 +70,10 @@ export class VeBalService {
};
}

public async getVeBalTotalSupply(): Promise<AmountHumanReadable> {
public async getVeBalTotalSupply(chain: Chain): Promise<AmountHumanReadable> {
if (networkContext.data.veBal) {
const veBal = await prisma.prismaVeBalTotalSupply.findFirst({
where: { chain: networkContext.chain },
where: { chain: chain },
});
if (veBal?.totalSupply) {
return veBal.totalSupply;
Expand Down

0 comments on commit 798c947

Please sign in to comment.