Skip to content

Commit

Permalink
bit of a cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Mar 5, 2024
1 parent 655584b commit 0fc8cf0
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 158 deletions.
122 changes: 0 additions & 122 deletions modules/pool/lib/pool-swap.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,128 +32,6 @@ export class PoolSwapService {
return networkContext.chain;
}

// TODO: remove this
public async getJoinExits(args: QueryPoolGetJoinExitsArgs): Promise<GqlPoolJoinExit[]> {
const first = !args.first || args.first > 100 ? 10 : args.first;

const allChainsJoinExits: GqlPoolJoinExit[] = [];

if (args.where?.chainIn) {
for (const chain of args.where.chainIn) {
const balancerSubgraphService = AllNetworkConfigsKeyedOnChain[chain].services.balancerSubgraphService;

const { joinExits } = await balancerSubgraphService.getPoolJoinExits({
where: { pool_in: args.where?.poolIdIn },
first,
skip: args.skip,
orderBy: JoinExit_OrderBy.Timestamp,
orderDirection: OrderDirection.Desc,
});

const mappedJoinExits: GqlPoolJoinExit[] = joinExits.map((joinExit) => ({
...joinExit,
__typename: 'GqlPoolJoinExit',
chain: chain,
poolId: joinExit.pool.id,
amounts: joinExit.amounts.map((amount, index) => ({
address: joinExit.pool.tokensList[index],
amount,
})),
}));

allChainsJoinExits.push(...mappedJoinExits);
}
}

return allChainsJoinExits;
}

// TODO: remove this
public async getUserJoinExitsForPool(
userAddress: string,
poolId: string,
chain: Chain,
first = 10,
skip = 0,
): Promise<GqlPoolJoinExit[]> {
const balancerSubgraphService = AllNetworkConfigsKeyedOnChain[chain].services.balancerSubgraphService;

const { joinExits } = await balancerSubgraphService.getPoolJoinExits({
where: { pool: poolId, user: userAddress },
first,
skip: skip,
orderBy: JoinExit_OrderBy.Timestamp,
orderDirection: OrderDirection.Desc,
});

return joinExits.map((joinExit) => ({
...joinExit,
__typename: 'GqlPoolJoinExit',
poolId: joinExit.pool.id,
chain: chain,
amounts: joinExit.amounts.map((amount, index) => ({ address: joinExit.pool.tokensList[index], amount })),
}));
}

public async getSwaps(args: QueryPoolGetSwapsArgs): Promise<PrismaPoolSwap[]> {
const take = !args.first || args.first > 100 ? 10 : args.first;

return prisma.prismaPoolSwap.findMany({
take,
skip: args.skip || undefined,
where: {
poolId: {
in: args.where?.poolIdIn || undefined,
},
tokenIn: {
in: args.where?.tokenInIn || undefined,
},
tokenOut: {
in: args.where?.tokenOutIn || undefined,
},
chain: {
in: args.where?.chainIn || undefined,
},
},
orderBy: { timestamp: 'desc' },
});
}

public async getUserSwapsForPool(
userAddress: string,
poolId: string,
chain: Chain,
first = 10,
skip = 0,
): Promise<GqlPoolSwap[]> {
const balancerSubgraphService = AllNetworkConfigsKeyedOnChain[chain].services.balancerSubgraphService;

const result = await balancerSubgraphService.getSwaps({
first,
skip,
where: {
poolId,
userAddress,
},
orderBy: Swap_OrderBy.Timestamp,
orderDirection: OrderDirection.Desc,
});

return result.swaps.map((swap) => ({
id: swap.id,
chain: chain,
userAddress,
poolId: swap.poolId.id,
tokenIn: swap.tokenIn,
tokenAmountIn: swap.tokenAmountIn,
tokenOut: swap.tokenOut,
tokenAmountOut: swap.tokenAmountOut,
valueUSD: parseFloat(swap.valueUSD),
timestamp: swap.timestamp,
tx: swap.tx,
}));
}

public async getBatchSwaps(args: QueryPoolGetBatchSwapsArgs): Promise<PrismaPoolBatchSwapWithSwaps[]> {
const take = !args.first || args.first > 100 ? 10 : args.first;

Expand Down
10 changes: 6 additions & 4 deletions modules/pool/pool.resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { poolService } from './pool.service';
import { Resolvers } from '../../schema';
import { GqlPoolSwap, Resolvers } from '../../schema';
import { isAdminRoute } from '../auth/auth-context';
import { prisma } from '../../prisma/prisma-client';
import { networkContext } from '../network/network-context.service';
Expand All @@ -23,14 +23,16 @@ const balancerResolvers: Resolvers = {
poolGetPoolsCount: async (parent, args, context) => {
return poolService.getPoolsCount(args);
},
// TODO: Deprecate in favor of poolGetEvents
poolGetSwaps: async (parent, args, context) => {
const currentChain = headerChain();
if (!args.where?.chainIn && currentChain) {
args.where = { ...args.where, chainIn: [currentChain] };
} else if (!args.where?.chainIn) {
throw new Error('poolGetSwaps error: Provide "where.chainIn" param');
}
return poolService.getPoolSwaps(args);
const swaps = await QueriesController().getEvents({ ...args, where: { ...args.where, typeIn: ['SWAP'] } });
return swaps as GqlPoolSwap[];
},
poolGetBatchSwaps: async (parent, args, context) => {
const currentChain = headerChain();
Expand All @@ -41,15 +43,15 @@ const balancerResolvers: Resolvers = {
}
return poolService.getPoolBatchSwaps(args);
},
poolGetJoinExits: async (parent, args, context) => {
poolGetEvents: async (parent, args, context) => {
// TODO: is default header safe to remove?
// const currentChain = headerChain();
// if (!args.where?.chainIn && currentChain) {
// args.where = { ...args.where, chainIn: [currentChain] };
// } else if (!args.where?.chainIn) {
// throw new Error('poolGetJoinExits error: Provide "where.chainIn" param');
// }
const events = await QueriesController().getJoinExits(args);
const events = await QueriesController().getEvents(args);
return events;
},
poolGetFeaturedPoolGroups: async (parent, { chains }, context) => {
Expand Down
9 changes: 0 additions & 9 deletions modules/pool/pool.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ export class PoolService {
return prisma.prismaPoolFilter.findMany({ where: { chain: this.chain } });
}

public async getPoolSwaps(args: QueryPoolGetSwapsArgs): Promise<PrismaPoolSwap[]> {
return this.poolSwapService.getSwaps(args);
}

public async getPoolBatchSwaps(args: QueryPoolGetBatchSwapsArgs): Promise<GqlPoolBatchSwap[]> {
const batchSwaps = await this.poolSwapService.getBatchSwaps(args);

Expand All @@ -117,11 +113,6 @@ export class PoolService {
}));
}

// TODO: This can be removed in favour of fetching the data directly from the database in the QueriesController
public async getPoolJoinExits(args: QueryPoolGetJoinExitsArgs): Promise<GqlPoolJoinExit[]> {
return this.poolSwapService.getJoinExits(args);
}

public async getFeaturedPoolGroups(chains: Chain[]): Promise<GqlPoolFeaturedPoolGroup[]> {
return this.poolGqlLoaderService.getFeaturedPoolGroups(chains);
}
Expand Down
17 changes: 14 additions & 3 deletions modules/user/user.resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Resolvers } from '../../schema';
import { GqlPoolJoinExit, GqlPoolSwap, Resolvers } from '../../schema';
import { userService } from './user.service';
import { getRequiredAccountAddress, isAdminRoute } from '../auth/auth-context';
import { tokenService } from '../token/token.service';
import { headerChain } from '../context/header-chain';
import { QueriesController } from '../controllers/queries-controller';

const resolvers: Resolvers = {
Query: {
Expand Down Expand Up @@ -31,7 +32,12 @@ const resolvers: Resolvers = {
}
const accountAddress = address || getRequiredAccountAddress(context);

return userService.getUserPoolInvestments(accountAddress, poolId, chain, first, skip);
const swaps = await QueriesController().getEvents({
first,
skip,
where: { typeIn: ['JOIN', 'EXIT'], poolIdIn: [poolId], chainIn: [chain], userAddress: accountAddress },
});
return swaps as GqlPoolJoinExit[];
},
userGetSwaps: async (parent, { first, skip, poolId, chain, address }, context) => {
const currentChain = headerChain();
Expand All @@ -41,7 +47,12 @@ const resolvers: Resolvers = {
throw new Error('userGetSwaps error: Provide "chain" param');
}
const accountAddress = address || getRequiredAccountAddress(context);
return userService.getUserSwaps(accountAddress, poolId, chain, first, skip);
const swaps = await QueriesController().getEvents({
first,
skip,
where: { typeIn: ['SWAP'], poolIdIn: [poolId], chainIn: [chain], userAddress: accountAddress },
});
return swaps as GqlPoolSwap[];
},
userGetStaking: async (parent, { chains, address }, context) => {
const currentChain = headerChain();
Expand Down
20 changes: 0 additions & 20 deletions modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,6 @@ export class UserService {
return this.userBalanceService.getUserPoolBalances(address, chains);
}

public async getUserPoolInvestments(
address: string,
poolId: string,
chain: Chain,
first?: number,
skip?: number,
): Promise<GqlPoolJoinExit[]> {
return this.poolSwapService.getUserJoinExitsForPool(address, poolId, chain, first, skip);
}

public async getUserSwaps(
address: string,
poolId: string,
chain: Chain,
first?: number,
skip?: number,
): Promise<GqlPoolSwap[]> {
return this.poolSwapService.getUserSwapsForPool(address, poolId, chain, first, skip);
}

public async getUserFbeetsBalance(address: string): Promise<Omit<UserPoolBalance, 'poolId'>> {
return this.userBalanceService.getUserFbeetsBalance(address);
}
Expand Down

0 comments on commit 0fc8cf0

Please sign in to comment.