Skip to content

Commit

Permalink
join-exit queries
Browse files Browse the repository at this point in the history
  • Loading branch information
gmbronco committed Mar 4, 2024
1 parent 237ecc8 commit ead05b8
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 19 deletions.
1 change: 1 addition & 0 deletions modules/actions/pool/sync-join-exits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const syncJoinExits = async (vaultSubgraphClient: V3VaultSubgraphClient,
userAddress: event.user.id,
blockNumber: Number(event.blockNumber),
blockTimestamp: Number(event.blockTimestamp),
logPosition: Number(event.id.substring(66)),
amountUsd: usd.reduce((acc, token) => acc + Number(token.amountUsd), 0),
payload: {
tokens: usd,
Expand Down
72 changes: 72 additions & 0 deletions modules/controllers/queries-controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Responsible for handling all the queries – can be split based on models
*/
import { GqlPoolJoinExit, QueryPoolGetJoinExitsArgs } from '../../schema';
import { prisma } from '../../prisma/prisma-client';
import { Chain } from '@prisma/client';

export function QueriesController(tracer?: any) {
return {
// TODO: I'd like to merge it with the swaps and return all as pool events
getJoinExits: async ({ first, skip, where }: QueryPoolGetJoinExitsArgs): Promise<GqlPoolJoinExit[]> => {
// Setting default values
first = first ?? 5;
skip = skip ?? 0;
where = where ?? {};
let { chainIn, poolIdIn } = where;
chainIn = chainIn ?? []; // 🤔 when no chain, shouldn't we be returning all the chains?
poolIdIn = poolIdIn ?? [];

const conditions: { poolId?: { in: string[] }; chain?: { in: Chain[] } } = {};
if (chainIn.length) {
conditions.chain = {
in: chainIn,
};
}
if (poolIdIn.length) {
conditions.poolId = {
in: poolIdIn,
};
}

const dbJoinExists = await prisma.poolEvent.findMany({
where: conditions,
take: first,
skip,
orderBy: [
{
blockNumber: 'desc',
},
{
logPosition: 'desc',
},
],
});

const results: GqlPoolJoinExit[] = dbJoinExists.map((joinExit) => {
const payload = joinExit.payload as {
tokens: { address: string; amount: string; amountUsd: string }[];
};

return {
__typename: 'GqlPoolJoinExit',
amounts: payload.tokens.map((token: any) => ({
address: token.address,
amount: token.amount,
valueUsd: token.amountUsd,
})),
chain: joinExit.chain,
id: joinExit.id,
poolId: joinExit.poolId,
sender: joinExit.userAddress,
timestamp: joinExit.blockTimestamp,
tx: joinExit.tx,
type: joinExit.type === 'JOIN' ? 'Join' : 'Exit',
valueUSD: String(joinExit.amountUsd),
};
});

return results;
},
};
}
8 changes: 8 additions & 0 deletions modules/pool/pool.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,14 @@ model PoolEvent {
userAddress String
blockNumber Int
blockTimestamp Int
logPosition Int
amountUsd Float
payload Json
@@index([type])
@@index([chain])
@@index([chain, poolId])
@@index([userAddress])
@@index([blockNumber])
@@index([logPosition])
}
5 changes: 4 additions & 1 deletion modules/pool/pool.resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { isAdminRoute } from '../auth/auth-context';
import { prisma } from '../../prisma/prisma-client';
import { networkContext } from '../network/network-context.service';
import { headerChain } from '../context/header-chain';
import { QueriesController } from '../controllers/queries-controller';

const balancerResolvers: Resolvers = {
Query: {
Expand Down Expand Up @@ -47,7 +48,9 @@ const balancerResolvers: Resolvers = {
} else if (!args.where?.chainIn) {
throw new Error('poolGetJoinExits error: Provide "where.chainIn" param');
}
return poolService.getPoolJoinExits(args);
const v2events = await poolService.getPoolJoinExits(args);
const v3events = await QueriesController().getJoinExits(args);
return [...v2events, ...v3events];
},
poolGetFeaturedPoolGroups: async (parent, { chains }, context) => {
const currentChain = headerChain();
Expand Down
18 changes: 18 additions & 0 deletions modules/sources/subgraphs/universal-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { getVaultSubgraphClient } from './balancer-v3-vault';
import { getPoolsSubgraphClient } from './balancer-v3-pools';
import { BalancerSubgraphService } from '../../subgraphs/balancer-subgraph/balancer-subgraph.service';

export const getBalancerUniversalSubgraphClient = async (
vaultSubgraphUrl: string,
poolsSubgraphUrl: string,
v2SubgraphUrl: string,
chainId: number,
) => {
const vaultSubgraphClient = getVaultSubgraphClient(vaultSubgraphUrl);
const poolsSubgraphClient = getPoolsSubgraphClient(poolsSubgraphUrl);
const v2SubgraphClient = new BalancerSubgraphService(v2SubgraphUrl, chainId);

return {
getEvents: async () => {},
};
};
18 changes: 0 additions & 18 deletions prisma/migrations/20240229152312_add_pool_events/migration.sql

This file was deleted.

37 changes: 37 additions & 0 deletions prisma/migrations/20240304114442_add_pool_events/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- CreateEnum
CREATE TYPE "PoolEventType" AS ENUM ('JOIN', 'EXIT', 'SWAP');

-- CreateTable
CREATE TABLE "PoolEvent" (
"id" TEXT NOT NULL,
"tx" TEXT NOT NULL,
"type" "PoolEventType" NOT NULL,
"chain" "Chain" NOT NULL,
"poolId" TEXT NOT NULL,
"userAddress" TEXT NOT NULL,
"blockNumber" INTEGER NOT NULL,
"blockTimestamp" INTEGER NOT NULL,
"logPosition" INTEGER NOT NULL,
"amountUsd" DOUBLE PRECISION NOT NULL,
"payload" JSONB NOT NULL,

CONSTRAINT "PoolEvent_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE INDEX "PoolEvent_type_idx" ON "PoolEvent"("type");

-- CreateIndex
CREATE INDEX "PoolEvent_chain_idx" ON "PoolEvent"("chain");

-- CreateIndex
CREATE INDEX "PoolEvent_chain_poolId_idx" ON "PoolEvent"("chain", "poolId");

-- CreateIndex
CREATE INDEX "PoolEvent_userAddress_idx" ON "PoolEvent"("userAddress");

-- CreateIndex
CREATE INDEX "PoolEvent_blockNumber_idx" ON "PoolEvent"("blockNumber");

-- CreateIndex
CREATE INDEX "PoolEvent_logPosition_idx" ON "PoolEvent"("logPosition");
8 changes: 8 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,16 @@ model PoolEvent {
userAddress String
blockNumber Int
blockTimestamp Int
logPosition Int
amountUsd Float
payload Json
@@index([type])
@@index([chain])
@@index([chain, poolId])
@@index([userAddress])
@@index([blockNumber])
@@index([logPosition])
}


Expand Down

0 comments on commit ead05b8

Please sign in to comment.