From 376db7adf11133173c50067f66f734bbeb53b347 Mon Sep 17 00:00:00 2001 From: Tim Robinson Date: Tue, 15 Aug 2023 18:22:21 +1000 Subject: [PATCH] Got basic pools pulling --- src/lib/utils/api.ts | 2 + .../api/graphql/generated/api-types.ts | 18 ++++- src/services/api/graphql/pool.graphql | 8 ++- .../balancer/api/entities/pools/index.ts | 68 ++++++++++++++++--- 4 files changed, 83 insertions(+), 13 deletions(-) diff --git a/src/lib/utils/api.ts b/src/lib/utils/api.ts index 22f1e87ce6..de2b63dc21 100644 --- a/src/lib/utils/api.ts +++ b/src/lib/utils/api.ts @@ -13,6 +13,8 @@ export function mapApiChain( return Network.ARBITRUM; case GqlChain.Avalanche: return Network.AVALANCHE; + case GqlChain.Base: + return Network.BASE; case GqlChain.Gnosis: return Network.GNOSIS; case GqlChain.Fantom: diff --git a/src/services/api/graphql/generated/api-types.ts b/src/services/api/graphql/generated/api-types.ts index 331b5b4be1..db6e023588 100644 --- a/src/services/api/graphql/generated/api-types.ts +++ b/src/services/api/graphql/generated/api-types.ts @@ -1356,7 +1356,11 @@ export type QueryUserGetSwapsArgs = { skip?: InputMaybe; }; -export type GetPoolsQueryVariables = Exact<{ [key: string]: never }>; +export type GetPoolsQueryVariables = Exact<{ + first?: InputMaybe; + orderBy?: InputMaybe; + orderDirection?: InputMaybe; +}>; export type GetPoolsQuery = { __typename?: 'Query'; @@ -1489,8 +1493,16 @@ export type VeBalGetVotingListQuery = { }; export const GetPoolsDocument = gql` - query GetPools { - pools: poolGetPools { + query GetPools( + $first: Int + $orderBy: GqlPoolOrderBy + $orderDirection: GqlPoolOrderDirection + ) { + pools: poolGetPools( + first: $first + orderBy: $orderBy + orderDirection: $orderDirection + ) { id chain address diff --git a/src/services/api/graphql/pool.graphql b/src/services/api/graphql/pool.graphql index d02fc47ed5..290e9c9c8a 100644 --- a/src/services/api/graphql/pool.graphql +++ b/src/services/api/graphql/pool.graphql @@ -1,5 +1,9 @@ -query GetPools { - pools: poolGetPools { +query GetPools( + $first: Int + $orderBy: GqlPoolOrderBy + $orderDirection: GqlPoolOrderDirection +) { + pools: poolGetPools (first: $first, orderBy: $orderBy, orderDirection: $orderDirection) { id chain address diff --git a/src/services/balancer/api/entities/pools/index.ts b/src/services/balancer/api/entities/pools/index.ts index 3bba348a7d..b96e029e7b 100644 --- a/src/services/balancer/api/entities/pools/index.ts +++ b/src/services/balancer/api/entities/pools/index.ts @@ -1,16 +1,21 @@ import { configService } from '@/services/config/config.service'; import { getApi } from '@/dependencies/balancer-api'; import { Pool } from '@/services/pool/types'; -import { GetPoolsQuery } from '@/services/api/graphql/generated/api-types'; +import { + GetPoolsQuery, + GqlPoolOrderBy, + GqlPoolOrderDirection, +} from '@/services/api/graphql/generated/api-types'; import { PoolsQueryBuilder } from '@/types/subgraph'; import { + AprBreakdown, GraphQLArgs, GraphQLQuery, PoolToken, PoolType, PoolsBalancerAPIRepository, } from '@balancer-labs/sdk'; -import _ from 'lodash'; +import { Network } from '@/lib/config'; import Service from '../../balancer-api.service'; import queryBuilder from './query'; @@ -33,14 +38,29 @@ export default class Pools { this.queryBuilder = _queryBuilder; } - public async get(args: GraphQLArgs = {}, attrs: any = {}): Promise { + public async get(args: GraphQLArgs = {}): Promise { const api = getApi(); - const response = await api.GetPools(); + console.log('Getting pools from API'); + const response = await api.GetPools({ + first: args.first || 10, + orderBy: GqlPoolOrderBy.TotalLiquidity, + orderDirection: GqlPoolOrderDirection.Desc, + }); const pools: ApiPools = response.pools; + console.log('Got pools from API', pools); - const convertedPools: Pool[] = pools.map((pool: ApiPool) => { + // Temp until I get pools by network ID from the beets API + const filteredPools: ApiPools = pools.filter((pool: ApiPool) => { + const poolChain: Network = mapApiChain(pool.chain); + return poolChain === configService.network.chainId; + }); + + console.log('Converting pools'); + const convertedPools: Pool[] = filteredPools.map((pool: ApiPool) => { + console.log('Converting pool ', pool.id); return this.mapPool(pool); }); + console.log('Done converting, pools is: ', convertedPools); return convertedPools; @@ -81,7 +101,14 @@ export default class Pools { // Converts a pool from the API subgraph to frontend pool type private mapPool(apiPool: ApiPool): Pool { - return { + console.log('Mapping pool: ', apiPool.id); + if ( + apiPool.id === + '0xcde67b70e8144d7d2772de59845b3a67266c2ca7000200000000000000000009' + ) { + console.log('Full pool: ', apiPool); + } + const converted: Pool = { id: apiPool.id, name: apiPool.name || '', address: apiPool.address, @@ -103,6 +130,7 @@ export default class Pools { totalShares: apiPool.dynamicData.totalShares, totalSwapFee: apiPool.dynamicData.lifetimeSwapFees, totalSwapVolume: apiPool.dynamicData.lifetimeVolume, + apr: this.mapApr(apiPool.dynamicData.apr), // priceRateProviders: apiPool.priceRateProviders ?? undefined, // onchain: subgraphPool.onchain, createTime: apiPool.createTime, @@ -112,8 +140,8 @@ export default class Pools { // wrappedTokens: subgraphPool.wrappedTokens, // unwrappedTokens: subgraphPool.unwrappedTokens, // isNew: subgraphPool.isNew, - // volumeSnapshot: subgraphPool.volumeSnapshot, - // feesSnapshot: subgraphPool.???, // Approximated last 24h fees + volumeSnapshot: apiPool.dynamicData.volume24h, + feesSnapshot: apiPool.dynamicData.fees24h, // boost: subgraphPool.boost, totalWeight: '1', lowerTarget: '0', @@ -121,6 +149,8 @@ export default class Pools { // isInRecoveryMode: apiPool.isInRecoveryMode ?? false, // isPaused: apiPool.isPaused ?? false, }; + console.log('Done on pool conversion'); + return converted; } private mapToken(apiToken: ApiPool['allTokens'][number]): PoolToken { @@ -130,6 +160,28 @@ export default class Pools { }; } + private mapApr(apiApr: ApiPool['dynamicData']['apr']): AprBreakdown { + console.log('Converting apr: ', apiApr); + return { + swapFees: Number(apiApr.swapApr) * 100, + tokenAprs: { + total: 0, + breakdown: {}, + }, + stakingApr: { + min: 0, + max: 0, + }, + rewardAprs: { + total: 0, + breakdown: {}, + }, + protocolApr: 0, + min: Number(apiApr.apr) * 100, + max: Number(apiApr.apr) * 100, + }; + } + get skip(): number { return this.repository ? this.repository.skip : 0; }