diff --git a/src/endpoints/mex/graphql/farms.query.ts b/src/endpoints/mex/graphql/farms.query.ts new file mode 100644 index 000000000..95b3f3c99 --- /dev/null +++ b/src/endpoints/mex/graphql/farms.query.ts @@ -0,0 +1,98 @@ +import { gql } from "graphql-request"; + +export const farmsQuery = gql` + query { + farms { + ... on FarmModelV1_2 { + version + address + farmToken { + collection + name + ticker + __typename + } + farmingToken { + name + identifier + decimals + __typename + } + farmedToken { + name + identifier + decimals + __typename + } + farmTokenPriceUSD + farmingTokenPriceUSD + farmedTokenPriceUSD + } + ... on FarmModelV1_3 { + version + address + farmToken { + collection + name + ticker + __typename + } + farmingToken { + name + identifier + decimals + __typename + } + farmedToken { + name + identifier + decimals + __typename + } + farmTokenPriceUSD + farmingTokenPriceUSD + farmedTokenPriceUSD + } + ... on FarmModelV2 { + version + address + farmToken { + collection + name + ticker + __typename + } + farmingToken { + name + identifier + decimals + __typename + } + farmedToken { + name + identifier + decimals + __typename + } + farmTokenPriceUSD + farmingTokenPriceUSD + farmedTokenPriceUSD + } + } + stakingFarms { + address + farmingToken { + name + identifier + decimals + __typename + } + farmToken { + name + collection + decimals + __typename + } + } + } + `; diff --git a/src/endpoints/mex/graphql/filtered.pairs.query.ts b/src/endpoints/mex/graphql/filtered.pairs.query.ts new file mode 100644 index 000000000..189688771 --- /dev/null +++ b/src/endpoints/mex/graphql/filtered.pairs.query.ts @@ -0,0 +1,51 @@ +import { gql } from "graphql-request"; + +export const filteredPairsQuery = (includeFarms: boolean = false) => { + const farmFields = includeFarms ? ` + hasFarms + hasDualFarms` : ''; + + return gql` + query filteredPairs($pagination: ConnectionArgs!, $filters: PairsFilter!) { + filteredPairs(pagination: $pagination, filters: $filters) { + edges { + cursor + node { + address + liquidityPoolToken { + identifier + name + __typename + } + liquidityPoolTokenPriceUSD + firstToken { + name + identifier + previous24hPrice + __typename + } + secondToken { + name + identifier + previous24hPrice + __typename + } + firstTokenPriceUSD + secondTokenPriceUSD + state + type + lockedValueUSD + volumeUSD24h + tradesCount + tradesCount24h + deployedAt + ${farmFields} + } + } + pageInfo { + hasNextPage + } + } + } + `; +}; diff --git a/src/endpoints/mex/graphql/pairs.count.query.ts b/src/endpoints/mex/graphql/pairs.count.query.ts new file mode 100644 index 000000000..50eb5efdd --- /dev/null +++ b/src/endpoints/mex/graphql/pairs.count.query.ts @@ -0,0 +1,8 @@ +import { gql } from "graphql-request"; + +export const pairCountQuery = gql` +query PairCount { + factory { + pairCount + } + }`; diff --git a/src/endpoints/mex/graphql/settings.query.ts b/src/endpoints/mex/graphql/settings.query.ts new file mode 100644 index 000000000..66401c089 --- /dev/null +++ b/src/endpoints/mex/graphql/settings.query.ts @@ -0,0 +1,60 @@ +import { gql } from "graphql-request"; + +export const settingsQuery = (pairLimitCount: number) => gql` +query { + filteredPairs(pagination: {first: ${pairLimitCount}}, filters: {state: ["Active"]}) { + edges { + node { + address + } + } + } + proxy { + address + lockedAssetTokens { + collection + } + } + farms { + ... on FarmModelV1_2 { + state + address + } + ... on FarmModelV1_3 { + state + address + } + ... on FarmModelV2 { + state + address + } + } + wrappingInfo { + address + wrappedToken { + identifier + } + } + distribution { + address + } + lockedAssetFactory { + address + } + stakingFarms { + state + address + } + stakingProxies { + address + } + factory { + address + } + simpleLockEnergy { + baseAssetToken { + identifier + } + } +} +`; diff --git a/src/endpoints/mex/graphql/staking.proxy.query.ts b/src/endpoints/mex/graphql/staking.proxy.query.ts new file mode 100644 index 000000000..d1afc2111 --- /dev/null +++ b/src/endpoints/mex/graphql/staking.proxy.query.ts @@ -0,0 +1,12 @@ +import { gql } from "graphql-request"; + +export const stakingProxyQuery = gql` +query StakingProxy { + stakingProxies { + address + dualYieldToken { + name + collection + } + } +}`; diff --git a/src/endpoints/mex/graphql/token.prices.hour.resolution.query.ts b/src/endpoints/mex/graphql/token.prices.hour.resolution.query.ts new file mode 100644 index 000000000..cf87057b3 --- /dev/null +++ b/src/endpoints/mex/graphql/token.prices.hour.resolution.query.ts @@ -0,0 +1,13 @@ +import { gql } from "graphql-request"; + +export const tokenPricesHourResolutionQuery = (tokenIdentifier: string) => gql` + query tokenPricesHourResolution { + values24h( + series: "${tokenIdentifier}", + metric: "priceUSD" + ) { + timestamp + value + } + } + `; diff --git a/src/endpoints/mex/graphql/tokens.query.ts b/src/endpoints/mex/graphql/tokens.query.ts new file mode 100644 index 000000000..4d69a9420 --- /dev/null +++ b/src/endpoints/mex/graphql/tokens.query.ts @@ -0,0 +1,9 @@ +import { gql } from "graphql-request"; + +export const tokensQuery = gql` + query tokens { + tokens { + identifier + type + } + }`; diff --git a/src/endpoints/mex/mex.farm.service.ts b/src/endpoints/mex/mex.farm.service.ts index 0bce98f5e..4cebd7b1f 100644 --- a/src/endpoints/mex/mex.farm.service.ts +++ b/src/endpoints/mex/mex.farm.service.ts @@ -1,7 +1,6 @@ import { Constants } from "@multiversx/sdk-nestjs-common"; import { CacheService } from "@multiversx/sdk-nestjs-cache"; import { forwardRef, Inject, Injectable } from "@nestjs/common"; -import { gql } from "graphql-request"; import { QueryPagination } from "src/common/entities/query.pagination"; import { CacheInfo } from "src/utils/cache.info"; import { GraphQlService } from "src/common/graphql/graphql.service"; @@ -9,6 +8,8 @@ import { MexFarm } from "./entities/mex.farm"; import { MexTokenService } from "./mex.token.service"; import { MexStakingProxy } from "./entities/mex.staking.proxy"; import { ApiConfigService } from "src/common/api-config/api.config.service"; +import { farmsQuery } from "./graphql/farms.query"; +import { stakingProxyQuery } from "./graphql/staking.proxy.query"; @Injectable() export class MexFarmService { @@ -53,104 +54,7 @@ export class MexFarmService { } private async getAllMexFarmsRaw(): Promise { - const query = gql` - query { - farms { - ... on FarmModelV1_2 { - version - address - farmToken { - collection - name - ticker - __typename - } - farmingToken { - name - identifier - decimals - __typename - } - farmedToken { - name - identifier - decimals - __typename - } - farmTokenPriceUSD - farmingTokenPriceUSD - farmedTokenPriceUSD - } - ... on FarmModelV1_3 { - version - address - farmToken { - collection - name - ticker - __typename - } - farmingToken { - name - identifier - decimals - __typename - } - farmedToken { - name - identifier - decimals - __typename - } - farmTokenPriceUSD - farmingTokenPriceUSD - farmedTokenPriceUSD - } - ... on FarmModelV2 { - version - address - farmToken { - collection - name - ticker - __typename - } - farmingToken { - name - identifier - decimals - __typename - } - farmedToken { - name - identifier - decimals - __typename - } - farmTokenPriceUSD - farmingTokenPriceUSD - farmedTokenPriceUSD - } - } - stakingFarms { - address - farmingToken { - name - identifier - decimals - __typename - } - farmToken { - name - collection - decimals - __typename - } - } - } - `; - - const response: any = await this.graphQlService.getExchangeServiceData(query, {}); + const response: any = await this.graphQlService.getExchangeServiceData(farmsQuery, {}); if (!response) { return []; } @@ -178,18 +82,7 @@ export class MexFarmService { } private async getAllStakingProxiesRaw(): Promise { - const query = gql` - query StakingProxy { - stakingProxies { - address - dualYieldToken { - name - collection - } - } - }`; - - const response: any = await this.graphQlService.getExchangeServiceData(query, {}); + const response: any = await this.graphQlService.getExchangeServiceData(stakingProxyQuery, {}); if (!response) { return []; } diff --git a/src/endpoints/mex/mex.pair.service.ts b/src/endpoints/mex/mex.pair.service.ts index c318538f1..a3ad3824c 100644 --- a/src/endpoints/mex/mex.pair.service.ts +++ b/src/endpoints/mex/mex.pair.service.ts @@ -1,7 +1,6 @@ import { Constants } from '@multiversx/sdk-nestjs-common'; import { CacheService } from '@multiversx/sdk-nestjs-cache'; import { BadRequestException, Injectable } from '@nestjs/common'; -import { gql } from 'graphql-request'; import { CacheInfo } from 'src/utils/cache.info'; import { GraphQlService } from 'src/common/graphql/graphql.service'; import { MexPair } from './entities/mex.pair'; @@ -13,6 +12,7 @@ import { ApiConfigService } from 'src/common/api-config/api.config.service'; import { MexPairExchange } from './entities/mex.pair.exchange'; import { MexPairsFilter } from './entities/mex.pairs..filter'; import { MexPairStatus } from './entities/mex.pair.status'; +import { filteredPairsQuery } from './graphql/filtered.pairs.query'; @Injectable() export class MexPairService { @@ -77,61 +77,15 @@ export class MexPairService { let cursor: string | null = null; let hasNextPage = true; - const farmFields = includeFarms ? ` - hasFarms - hasDualFarms` : ''; - - const query = gql` - query filteredPairs($pagination: ConnectionArgs!, $filters: PairsFilter!) { - filteredPairs(pagination: $pagination, filters: $filters) { - edges { - cursor - node { - address - liquidityPoolToken { - identifier - name - __typename - } - liquidityPoolTokenPriceUSD - firstToken { - name - identifier - previous24hPrice - __typename - } - secondToken { - name - identifier - previous24hPrice - __typename - } - firstTokenPriceUSD - secondTokenPriceUSD - state - type - lockedValueUSD - volumeUSD24h - tradesCount - tradesCount24h - deployedAt - ${farmFields} - } - } - pageInfo { - hasNextPage - } - } - } - `; - while (hasNextPage) { const variables = { pagination: { first: 25, after: cursor }, filters: { state: [MexPairStatus.active] }, }; + const query = filteredPairsQuery(includeFarms); const result: any = await this.graphQlService.getExchangeServiceData(query, variables); + if (!result) { break; } diff --git a/src/endpoints/mex/mex.settings.service.ts b/src/endpoints/mex/mex.settings.service.ts index 64b49e17c..243c8a6f9 100644 --- a/src/endpoints/mex/mex.settings.service.ts +++ b/src/endpoints/mex/mex.settings.service.ts @@ -1,13 +1,14 @@ import { Constants } from "@multiversx/sdk-nestjs-common"; import { CacheService } from "@multiversx/sdk-nestjs-cache"; import { Injectable } from "@nestjs/common"; -import { gql } from "graphql-request"; import { CacheInfo } from "src/utils/cache.info"; import { GraphQlService } from "src/common/graphql/graphql.service"; import { TransactionMetadata } from "../transactions/transaction-action/entities/transaction.metadata"; import { TransactionMetadataTransfer } from "../transactions/transaction-action/entities/transaction.metadata.transfer"; import { MexSettings } from "./entities/mex.settings"; import { ApiConfigService } from "src/common/api-config/api.config.service"; +import { settingsQuery } from "./graphql/settings.query"; +import { pairCountQuery } from "./graphql/pairs.count.query"; @Injectable() export class MexSettingsService { @@ -88,67 +89,7 @@ export class MexSettingsService { public async getSettingsRaw(): Promise { const pairLimitCount = await this.getPairLimitCount(); - - const query = gql` - query { - filteredPairs(pagination: {first: ${pairLimitCount}}, filters: {state: ["Active"]}) { - edges { - node { - address - } - } - } - proxy { - address - lockedAssetTokens { - collection - } - } - farms { - ... on FarmModelV1_2 { - state - address - } - ... on FarmModelV1_3 { - state - address - } - ... on FarmModelV2 { - state - address - } - } - wrappingInfo { - address - wrappedToken { - identifier - } - } - distribution { - address - } - lockedAssetFactory { - address - } - stakingFarms { - state - address - } - stakingProxies { - address - } - factory { - address - } - simpleLockEnergy { - baseAssetToken { - identifier - } - } - } - `; - - const response = await this.graphQlService.getExchangeServiceData(query); + const response = await this.graphQlService.getExchangeServiceData(settingsQuery(pairLimitCount)); if (!response) { return null; } @@ -169,14 +110,7 @@ export class MexSettingsService { } private async getPairLimitCount(): Promise { - const pairsLimit = gql` - query PairCount { - factory { - pairCount - } - }`; - - const response = await this.graphQlService.getExchangeServiceData(pairsLimit); + const response = await this.graphQlService.getExchangeServiceData(pairCountQuery); if (!response) { return 500; } diff --git a/src/endpoints/mex/mex.token.charts.service.ts b/src/endpoints/mex/mex.token.charts.service.ts index 8d2e62b94..0b3dde374 100644 --- a/src/endpoints/mex/mex.token.charts.service.ts +++ b/src/endpoints/mex/mex.token.charts.service.ts @@ -6,6 +6,7 @@ import { MexTokenChart } from "./entities/mex.token.chart"; import { MexTokenService } from "./mex.token.service"; import { CacheService } from "@multiversx/sdk-nestjs-cache"; import { CacheInfo } from "src/utils/cache.info"; +import { tokenPricesHourResolutionQuery } from "./graphql/token.prices.hour.resolution.query"; @Injectable() export class MexTokenChartsService { @@ -31,19 +32,8 @@ export class MexTokenChartsService { return undefined; } - const query = gql` - query tokenPricesHourResolution { - values24h( - series: "${tokenIdentifier}", - metric: "priceUSD" - ) { - timestamp - value - } - } - `; - try { + const query = tokenPricesHourResolutionQuery(tokenIdentifier); const data = await this.graphQlService.getExchangeServiceData(query); return this.convertToMexTokenChart(data?.values24h) || []; } catch (error) { diff --git a/src/endpoints/mex/mex.token.service.ts b/src/endpoints/mex/mex.token.service.ts index b9851301b..7907bf258 100644 --- a/src/endpoints/mex/mex.token.service.ts +++ b/src/endpoints/mex/mex.token.service.ts @@ -12,7 +12,7 @@ import { OriginLogger } from "@multiversx/sdk-nestjs-common"; import { QueryPagination } from "src/common/entities/query.pagination"; import { MexTokenType } from "./entities/mex.token.type"; import { GraphQlService } from "src/common/graphql/graphql.service"; -import { gql } from "graphql-request"; +import { tokensQuery } from "./graphql/tokens.query"; @Injectable() export class MexTokenService { @@ -259,16 +259,7 @@ export class MexTokenService { throw new BadRequestException('Could not fetch MEX tokens'); } - const query = gql` - query tokens { - tokens { - identifier - type - } - } - `; - - const result: any = await this.graphQlService.getExchangeServiceData(query); + const result: any = await this.graphQlService.getExchangeServiceData(tokensQuery); if (!result || !result.tokens) { return []; }