From e87bd7ad21349520b9544971183d23a6c1ff7881 Mon Sep 17 00:00:00 2001 From: quenta2024 <161811915+quenta2024@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:16:41 +0800 Subject: [PATCH 1/3] add quenta fees --- fees/quenta/index.ts | 121 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 fees/quenta/index.ts diff --git a/fees/quenta/index.ts b/fees/quenta/index.ts new file mode 100644 index 0000000000..8608a21d25 --- /dev/null +++ b/fees/quenta/index.ts @@ -0,0 +1,121 @@ +import * as sdk from "@defillama/sdk"; +import { Adapter } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; +import { gql, GraphQLClient } from "graphql-request"; +import type { ChainEndpoints, FetchOptions } from "../../adapters/types"; +import { Chain } from "@defillama/sdk/build/general"; + + + +const endpoints = { + [CHAIN.IOTEX]: "https://gql.quenta.io/subgraphs/name/iotex/quenta" + }; + +const blockNumberGraph = { + [CHAIN.IOTEX]: "https://graph.mainnet.iotex.io/subgraphs/name/quenta/blocks" +} + +const headers = { 'sex-dev': 'ServerDev'} + +const graphs = (graphUrls: ChainEndpoints) => { + return (chain: Chain) => { + return async ({ toTimestamp }: FetchOptions) => { + + // Get blockNumers + const blockNumerQuery = gql` + { + blocks( + where: {timestamp_lte:${toTimestamp}} + orderBy: timestamp + orderDirection: desc + first: 1 + ) { + id + number + } + } + `; + const last24hBlockNumberQuery = gql` + { + blocks( + where: {timestamp_lte:${toTimestamp - 24 * 60 * 60}} + orderBy: timestamp + orderDirection: desc + first: 1 + ) { + id + number + } + } + `; + + const blockNumberGraphQLClient = new GraphQLClient(blockNumberGraph[chain], { + headers: chain === CHAIN.ZETA ? headers: null, + }); + const graphQLClient = new GraphQLClient(graphUrls[chain], { + headers: chain === CHAIN.ZETA ? headers: null, + }); + + + const blockNumber = ( + await blockNumberGraphQLClient.request(blockNumerQuery) + ).blocks[0].number; + const last24hBlockNumber = ( + await blockNumberGraphQLClient.request(last24hBlockNumberQuery) + ).blocks[0].number; + + + // get total fee + const totalFeeQuery = gql` + { + protocolMetrics(block:{number:${blockNumber}}){ + totalFee + } + } + `; + + // get total fee 24 hours ago + const last24hTotalFeeQuery = gql` + { + protocolMetrics(block:{number:${last24hBlockNumber}}){ + totalFee + } + } + `; + + + let totalFee = ( + await graphQLClient.request(totalFeeQuery) + ).protocolMetrics[0].totalFee + + let last24hTotalFee = ( + await graphQLClient.request(last24hTotalFeeQuery) + ).protocolMetrics[0].totalFee + + totalFee = Number(totalFee) / 10 ** 6 + const dailyFee = Number(totalFee) - (Number(last24hTotalFee) / 10 ** 6) + + return { + dailyFees: dailyFee.toString(), + totalFees: totalFee.toString(), + }; + } + + return { + dailyFees: "0", + totalFees: "0", + }; + }; +}; + +const adapter: Adapter = { + version: 2, + adapter: { + [CHAIN.IOTEX]: { + fetch: graphs(endpoints)(CHAIN.IOTEX), + start: 29643703, + }, + }, +}; + +export default adapter; From c8244a36c2f97f4534c8e5b5e5bee441bfe00a5d Mon Sep 17 00:00:00 2001 From: quenta2024 <161811915+quenta2024@users.noreply.github.com> Date: Fri, 1 Nov 2024 16:19:59 +0800 Subject: [PATCH 2/3] add quenta volume --- dexs/quenta/index.ts | 124 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 dexs/quenta/index.ts diff --git a/dexs/quenta/index.ts b/dexs/quenta/index.ts new file mode 100644 index 0000000000..a1aea0a8d8 --- /dev/null +++ b/dexs/quenta/index.ts @@ -0,0 +1,124 @@ +import * as sdk from "@defillama/sdk"; +import { Adapter } from "../../adapters/types"; +import { CHAIN } from "../../helpers/chains"; +import { request, gql, GraphQLClient } from "graphql-request"; +import type { ChainEndpoints } from "../../adapters/types"; +import { Chain } from "@defillama/sdk/build/general"; + + +const endpoints = { + [CHAIN.IOTEX]: "https://gql.quenta.io/subgraphs/name/iotex/quenta" +}; + +const blockNumberGraph = { + [CHAIN.IOTEX]: "https://graph.mainnet.iotex.io/subgraphs/name/quenta/blocks" +} + +const headers = { 'sex-dev': 'ServerDev'} + +const graphs = (graphUrls: ChainEndpoints) => { + return (chain: Chain) => { + return async (timestamp: number) => { + + // Get blockNumers + const blockNumerQuery = gql` + { + blocks( + where: {timestamp_lte:${timestamp}} + orderBy: timestamp + orderDirection: desc + first: 1 + ) { + id + number + } + } + `; + const last24hBlockNumberQuery = gql` + { + blocks( + where: {timestamp_lte:${timestamp - 24 * 60 * 60}} + orderBy: timestamp + orderDirection: desc + first: 1 + ) { + id + number + } + } + `; + + const blockNumberGraphQLClient = new GraphQLClient(blockNumberGraph[chain], { + headers: chain === CHAIN.ZETA ? headers: null, + }); + const graphQLClient = new GraphQLClient(graphUrls[chain], { + headers: chain === CHAIN.ZETA ? headers: null, + }); + + + const blockNumber = ( + await blockNumberGraphQLClient.request(blockNumerQuery) + ).blocks[0].number; + const last24hBlockNumber = ( + await blockNumberGraphQLClient.request(last24hBlockNumberQuery) + ).blocks[0].number; + + + // get total volume + const tradeVolumeQuery = gql` + { + protocolMetrics(block:{number:${blockNumber}}){ + totalVolume + } + } + `; + + // get total volume 24 hours ago + const lastTradeVolumeQuery = gql` + { + protocolMetrics(block:{number:${last24hBlockNumber}}){ + totalVolume + } + } + `; + + + let tradeVolume = ( + await graphQLClient.request(tradeVolumeQuery) + ).protocolMetrics[0].totalVolume + + let last24hTradeVolume = ( + await graphQLClient.request(lastTradeVolumeQuery) + ).protocolMetrics[0].totalVolume + + const totalVolume = Number(tradeVolume) / 10 ** 6 + const dailyVolume = (Number(tradeVolume) - Number(last24hTradeVolume)) / 10 ** 6 + + return { + timestamp, + totalVolume: totalVolume.toString(), + dailyVolume: dailyVolume.toString(), + }; + } + + + return { + timestamp, + totalVolume: "0", + dailyVolume: "0", + }; + }; + }; + + +const adapter: Adapter = { + adapter: { + + [CHAIN.IOTEX]: { + fetch: graphs(endpoints)(CHAIN.IOTEX), + start: 29643703, + }, + }, +}; + +export default adapter; From fa8e776b89299c26265fd53796de77ba98f04c07 Mon Sep 17 00:00:00 2001 From: g1nt0ki <99907941+g1nt0ki@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:24:20 +0100 Subject: [PATCH 3/3] code refactor --- dexs/quenta/index.ts | 114 +++++++++---------------------------------- fees/quenta/index.ts | 113 ++++++++++-------------------------------- 2 files changed, 49 insertions(+), 178 deletions(-) diff --git a/dexs/quenta/index.ts b/dexs/quenta/index.ts index a1aea0a8d8..67ddb297f5 100644 --- a/dexs/quenta/index.ts +++ b/dexs/quenta/index.ts @@ -1,123 +1,57 @@ -import * as sdk from "@defillama/sdk"; import { Adapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { request, gql, GraphQLClient } from "graphql-request"; -import type { ChainEndpoints } from "../../adapters/types"; -import { Chain } from "@defillama/sdk/build/general"; +import { GraphQLClient } from "graphql-request"; +import type { FetchOptions } from "../../adapters/types"; const endpoints = { [CHAIN.IOTEX]: "https://gql.quenta.io/subgraphs/name/iotex/quenta" }; -const blockNumberGraph = { - [CHAIN.IOTEX]: "https://graph.mainnet.iotex.io/subgraphs/name/quenta/blocks" -} - -const headers = { 'sex-dev': 'ServerDev'} - -const graphs = (graphUrls: ChainEndpoints) => { - return (chain: Chain) => { - return async (timestamp: number) => { - - // Get blockNumers - const blockNumerQuery = gql` - { - blocks( - where: {timestamp_lte:${timestamp}} - orderBy: timestamp - orderDirection: desc - first: 1 - ) { - id - number - } - } - `; - const last24hBlockNumberQuery = gql` - { - blocks( - where: {timestamp_lte:${timestamp - 24 * 60 * 60}} - orderBy: timestamp - orderDirection: desc - first: 1 - ) { - id - number - } - } - `; +async function fetch({ getFromBlock, getToBlock, chain, }: FetchOptions) { + const fromBlock = await getFromBlock() + const toBlock = await getToBlock() - const blockNumberGraphQLClient = new GraphQLClient(blockNumberGraph[chain], { - headers: chain === CHAIN.ZETA ? headers: null, - }); - const graphQLClient = new GraphQLClient(graphUrls[chain], { - headers: chain === CHAIN.ZETA ? headers: null, - }); - - - const blockNumber = ( - await blockNumberGraphQLClient.request(blockNumerQuery) - ).blocks[0].number; - const last24hBlockNumber = ( - await blockNumberGraphQLClient.request(last24hBlockNumberQuery) - ).blocks[0].number; - - - // get total volume - const tradeVolumeQuery = gql` + const graphQLClient = new GraphQLClient(endpoints[chain]); + // get total volume + const tradeVolumeQuery = ` { - protocolMetrics(block:{number:${blockNumber}}){ + protocolMetrics(block:{number:${toBlock}}){ totalVolume } } `; - // get total volume 24 hours ago - const lastTradeVolumeQuery = gql` + // get total volume 24 hours ago + const lastTradeVolumeQuery = ` { - protocolMetrics(block:{number:${last24hBlockNumber}}){ + protocolMetrics(block:{number:${fromBlock}}){ totalVolume } } `; - let tradeVolume = ( - await graphQLClient.request(tradeVolumeQuery) - ).protocolMetrics[0].totalVolume - - let last24hTradeVolume = ( - await graphQLClient.request(lastTradeVolumeQuery) - ).protocolMetrics[0].totalVolume + let { protocolMetrics: [{ totalVolume }] } = await graphQLClient.request(tradeVolumeQuery) + let { protocolMetrics: [{ totalVolume: totalVolumePast }] } = await graphQLClient.request(lastTradeVolumeQuery) - const totalVolume = Number(tradeVolume) / 10 ** 6 - const dailyVolume = (Number(tradeVolume) - Number(last24hTradeVolume)) / 10 ** 6 - - return { - timestamp, - totalVolume: totalVolume.toString(), - dailyVolume: dailyVolume.toString(), - }; - } - - - return { - timestamp, - totalVolume: "0", - dailyVolume: "0", - }; - }; + totalVolume = totalVolume / 1e6 + totalVolumePast = totalVolumePast / 1e6 + console.log(fromBlock, toBlock, totalVolume, totalVolumePast) + return { + totalVolume, + dailyVolume: totalVolume - totalVolumePast, }; +} const adapter: Adapter = { + version: 2, adapter: { - [CHAIN.IOTEX]: { - fetch: graphs(endpoints)(CHAIN.IOTEX), + fetch, start: 29643703, - }, + }, }, }; diff --git a/fees/quenta/index.ts b/fees/quenta/index.ts index 8608a21d25..8fa24b3a7b 100644 --- a/fees/quenta/index.ts +++ b/fees/quenta/index.ts @@ -1,118 +1,55 @@ -import * as sdk from "@defillama/sdk"; import { Adapter } from "../../adapters/types"; import { CHAIN } from "../../helpers/chains"; -import { gql, GraphQLClient } from "graphql-request"; -import type { ChainEndpoints, FetchOptions } from "../../adapters/types"; -import { Chain } from "@defillama/sdk/build/general"; - +import { GraphQLClient } from "graphql-request"; +import type { FetchOptions } from "../../adapters/types"; const endpoints = { - [CHAIN.IOTEX]: "https://gql.quenta.io/subgraphs/name/iotex/quenta" - }; - -const blockNumberGraph = { - [CHAIN.IOTEX]: "https://graph.mainnet.iotex.io/subgraphs/name/quenta/blocks" -} - -const headers = { 'sex-dev': 'ServerDev'} - -const graphs = (graphUrls: ChainEndpoints) => { - return (chain: Chain) => { - return async ({ toTimestamp }: FetchOptions) => { - - // Get blockNumers - const blockNumerQuery = gql` - { - blocks( - where: {timestamp_lte:${toTimestamp}} - orderBy: timestamp - orderDirection: desc - first: 1 - ) { - id - number - } - } - `; - const last24hBlockNumberQuery = gql` - { - blocks( - where: {timestamp_lte:${toTimestamp - 24 * 60 * 60}} - orderBy: timestamp - orderDirection: desc - first: 1 - ) { - id - number - } - } - `; - - const blockNumberGraphQLClient = new GraphQLClient(blockNumberGraph[chain], { - headers: chain === CHAIN.ZETA ? headers: null, - }); - const graphQLClient = new GraphQLClient(graphUrls[chain], { - headers: chain === CHAIN.ZETA ? headers: null, - }); - + [CHAIN.IOTEX]: "https://gql.quenta.io/subgraphs/name/iotex/quenta" +}; - const blockNumber = ( - await blockNumberGraphQLClient.request(blockNumerQuery) - ).blocks[0].number; - const last24hBlockNumber = ( - await blockNumberGraphQLClient.request(last24hBlockNumberQuery) - ).blocks[0].number; +async function fetch({ getFromBlock, getToBlock, chain, }: FetchOptions) { + const fromBlock = await getFromBlock() + const toBlock = await getToBlock() - // get total fee - const totalFeeQuery = gql` + const graphQLClient = new GraphQLClient(endpoints[chain]); + // get total volume + const tradeVolumeQuery = ` { - protocolMetrics(block:{number:${blockNumber}}){ + protocolMetrics(block:{number:${toBlock}}){ totalFee } } `; - // get total fee 24 hours ago - const last24hTotalFeeQuery = gql` + // get total volume 24 hours ago + const lastTradeVolumeQuery = ` { - protocolMetrics(block:{number:${last24hBlockNumber}}){ - totalFee + protocolMetrics(block:{number:${fromBlock}}){ + totalFee } } `; - - - let totalFee = ( - await graphQLClient.request(totalFeeQuery) - ).protocolMetrics[0].totalFee - let last24hTotalFee = ( - await graphQLClient.request(last24hTotalFeeQuery) - ).protocolMetrics[0].totalFee - totalFee = Number(totalFee) / 10 ** 6 - const dailyFee = Number(totalFee) - (Number(last24hTotalFee) / 10 ** 6) + let { protocolMetrics: [{ totalFee: totalVolume }] } = await graphQLClient.request(tradeVolumeQuery) + let { protocolMetrics: [{ totalFee: totalVolumePast }] } = await graphQLClient.request(lastTradeVolumeQuery) - return { - dailyFees: dailyFee.toString(), - totalFees: totalFee.toString(), - }; - } + totalVolume = totalVolume / 1e6 + totalVolumePast = totalVolumePast / 1e6 + return { + totalVolume, + dailyVolume: totalVolume - totalVolumePast, + }; +} - return { - dailyFees: "0", - totalFees: "0", - }; - }; -}; const adapter: Adapter = { version: 2, adapter: { [CHAIN.IOTEX]: { - fetch: graphs(endpoints)(CHAIN.IOTEX), + fetch, start: 29643703, }, },