From a59a096d5cee01b33c4ba598319f66b7b1ab8f85 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Wed, 24 May 2023 15:27:19 +0000 Subject: [PATCH 1/2] init alchemix --- fees/alchemix.ts | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 fees/alchemix.ts diff --git a/fees/alchemix.ts b/fees/alchemix.ts new file mode 100644 index 0000000000..272b4f094d --- /dev/null +++ b/fees/alchemix.ts @@ -0,0 +1,92 @@ +import { Adapter, FetchResultFees } from "../adapters/types"; +import { CHAIN} from "../helpers/chains"; +import { request, gql } from "graphql-request"; +import type { ChainEndpoints } from "../adapters/types" +import { Chain } from '@defillama/sdk/build/general'; +import { getPrices } from "../utils/prices"; + + +interface IData { + yieldToken: string; + totalHarvested: string; + minimumAmountOut: string; +} + +const endpoints = { + [CHAIN.ETHEREUM]: "https://api.thegraph.com/subgraphs/name/alchemix-finance/alchemix_v2", + [CHAIN.FANTOM]: "https://api.thegraph.com/subgraphs/name/alchemix-finance/alchemix_v2_ftm", + [CHAIN.OPTIMISM]: "https://api.thegraph.com/subgraphs/name/alchemix-finance/alchemix_v2_optimisim" +}; + +const graph = (graphUrls: ChainEndpoints) => { + const graphQuery = gql`query fees($timestampFrom: Int!, $timestampTo: Int!) + { + alchemistHarvestEvents( + first: 1000 + orderBy: timestamp + orderDirection: desc + where: { + timestamp_gte: $timestampFrom + timestamp_lte: $timestampTo + } + ){ + timestamp, + yieldToken, + totalHarvested, + minimumAmountOut, + contract { + id + } + } + }`; + + return (chain: Chain) => { + return async (timestamp: number): Promise => { + + const fromTimestamp = timestamp - 60 * 60 * 24 + const toTimestamp = timestamp + + const graphRes: IData[] = (await request(graphUrls[chain], graphQuery, { + timestampFrom: fromTimestamp, + timestampTo: toTimestamp + })).alchemistHarvestEvents; + + const coins = [...new Set(graphRes.map((a: IData) => `${chain}:${a.yieldToken.toLowerCase()}`))] + const prices = await getPrices(coins, timestamp); + const feesAmount = graphRes.map((a: IData) => { + const price = prices[`${chain}:${a.yieldToken.toLowerCase()}`].price; + const decimals = prices[`${chain}:${a.yieldToken.toLowerCase()}`].decimals; + const amount = ((Number(a.totalHarvested)) / 10 ** decimals) * price; + return amount; + }).reduce((a: number, b: number) => a + b, 0); + const dailyFee = feesAmount; + const dailyRevenue = dailyFee; + + return { + dailyFees: `${dailyFee}`, + dailyRevenue: `${dailyRevenue}`, + timestamp + } + } + } +}; + + +const adapter: Adapter = { + adapter: { + [CHAIN.ETHEREUM]: { + fetch: graph(endpoints)(CHAIN.ETHEREUM), + start: async () => 1669852800 + }, + [CHAIN.FANTOM]: { + fetch: graph(endpoints)(CHAIN.FANTOM), + start: async () => 1669852800 + }, + [CHAIN.OPTIMISM]: { + fetch: graph(endpoints)(CHAIN.OPTIMISM), + start: async () => 1669852800 + } + } +} + +export default adapter; From e86b59586077b68772c8c6c9cb9f2106f348fb00 Mon Sep 17 00:00:00 2001 From: 0xgnek <0xgnek@gmail.com> Date: Fri, 23 Jun 2023 06:16:28 +0000 Subject: [PATCH 2/2] ignore fantom --- fees/alchemix.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fees/alchemix.ts b/fees/alchemix.ts index 272b4f094d..27f4acb220 100644 --- a/fees/alchemix.ts +++ b/fees/alchemix.ts @@ -78,10 +78,10 @@ const adapter: Adapter = { fetch: graph(endpoints)(CHAIN.ETHEREUM), start: async () => 1669852800 }, - [CHAIN.FANTOM]: { - fetch: graph(endpoints)(CHAIN.FANTOM), - start: async () => 1669852800 - }, + // [CHAIN.FANTOM]: { + // fetch: graph(endpoints)(CHAIN.FANTOM), + // start: async () => 1669852800 + // }, [CHAIN.OPTIMISM]: { fetch: graph(endpoints)(CHAIN.OPTIMISM), start: async () => 1669852800