Skip to content

Commit

Permalink
chain filter defaults to the header (#490)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco authored Oct 19, 2023
1 parent d634405 commit 0e67b9e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
29 changes: 29 additions & 0 deletions modules/context/header-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getRequestScopeContextValue } from '../context/request-scoped-context';
import { Chain } from '@prisma/client';

const chainIdToChain: { [id: string]: Chain } = {
'1': Chain.MAINNET,
'10': Chain.OPTIMISM,
'100': Chain.GNOSIS,
'137': Chain.POLYGON,
'250': Chain.FANTOM,
'1101': Chain.ZKEVM,
'8453': Chain.BASE,
'42161': Chain.ARBITRUM,
'43114': Chain.AVALANCHE,
}

/**
* Setup to transition out from the old header-based chainIDs to the new required chain query filters.
*
* @returns The chain of the current request, if any.
*/
export const headerChain = (): Chain | undefined => {
const chainId = getRequestScopeContextValue<string>('chainId');

if (chainId) {
return chainIdToChain[chainId];
}

return undefined;
}
15 changes: 13 additions & 2 deletions modules/token/token.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,26 @@ import _ from 'lodash';
import { isAdminRoute } from '../auth/auth-context';
import { tokenService } from './token.service';
import { networkContext } from '../network/network-context.service';
import { headerChain } from '../context/header-chain';

const resolvers: Resolvers = {
Query: {
tokenGetTokens: async (parent, { chains }, context) => {
chains = chains && chains.length > 0 ? chains : [networkContext.chain];
const currentChain = headerChain()
if (!chains && currentChain) {
chains = [currentChain];
} else if (!chains) {
chains = [];
}
return tokenService.getTokenDefinitions(chains);
},
tokenGetCurrentPrices: async (parent, { chains }, context) => {
chains = chains && chains.length > 0 ? chains : [networkContext.chain];
const currentChain = headerChain()
if (!chains && currentChain) {
chains = [currentChain];
} else if (!chains) {
chains = [];
}
const prices = await tokenService.getWhiteListedTokenPrices(chains);

return prices.map((price) => ({
Expand Down
9 changes: 7 additions & 2 deletions modules/user/user.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { Resolvers } from '../../schema';
import { userService } from './user.service';
import { getRequiredAccountAddress, isAdminRoute } from '../auth/auth-context';
import { tokenService } from '../token/token.service';
import { networkContext } from '../network/network-context.service';
import { headerChain } from '../context/header-chain';

const resolvers: Resolvers = {
Query: {
userGetPoolBalances: async (parent, { chains, address }, context) => {
chains = chains && chains.length > 0 ? chains : [networkContext.chain];
const currentChain = headerChain()
if (!chains && currentChain) {
chains = [currentChain];
} else if (!chains) {
chains = [];
}
const accountAddress = address || getRequiredAccountAddress(context);
const tokenPrices = await tokenService.getTokenPricesForChains(chains);
const balances = await userService.getUserPoolBalances(accountAddress, chains);
Expand Down

0 comments on commit 0e67b9e

Please sign in to comment.