Skip to content

Commit

Permalink
Merge pull request #674 from DefiLlama/rollup-finace
Browse files Browse the repository at this point in the history
add Rollup.Finace
  • Loading branch information
dtmkeng authored Aug 2, 2023
2 parents 3abcf62 + facea24 commit 40e0443
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 0 deletions.
97 changes: 97 additions & 0 deletions dexs/rollup-finace/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import request, { gql } from "graphql-request";
import { BreakdownAdapter, Fetch, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";

const endpoints: { [key: string]: string } = {
[CHAIN.ERA]: "https://subgraph.rollup.finance/subgraphs/name/rollUp/stats",
}

const historicalDataSwap = gql`
query get_volume($period: String!, $id: String!) {
volumeStats(where: {period: $period, id: $id}) {
swap
}
}
`

const historicalDataDerivatives = gql`
query get_volume($period: String!, $id: String!) {
volumeStats(where: {period: $period, id: $id}) {
liquidation
margin
}
}
`

interface IGraphResponse {
volumeStats: Array<{
burn: string,
liquidation: string,
margin: string,
mint: string,
swap: string,
}>
}

const getFetch = (query: string)=> (chain: string): Fetch => async (timestamp: number) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date((timestamp * 1000)))
const fromTimestamp = dayTimestamp - 60 * 60 * 24
const dailyData: IGraphResponse = await request(endpoints[chain], query, {
id: String(dayTimestamp),
period: 'daily',
})
const yesterDay: IGraphResponse = await request(endpoints[chain], query, {
id: String(fromTimestamp),
period: 'daily',
})
const totalData: IGraphResponse = await request(endpoints[chain], query, {
id: 'total',
period: 'total',
})

const todayVolume = Number(Object.values(dailyData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30
const yesterdayVolume = Number(Object.values(yesterDay.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30
const dailyVolume = (todayVolume - yesterdayVolume);
return {
timestamp: dayTimestamp,
dailyVolume: `${dailyVolume}`,
totalVolume:
totalData.volumeStats.length == 1
? String(Number(Object.values(totalData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30)
: undefined,

}
}

const getStartTimestamp = async (chain: string) => {
const startTimestamps: { [chain: string]: number } = {
[CHAIN.ERA]: 1688256000,
}
return startTimestamps[chain]
}

const adapter: BreakdownAdapter = {
breakdown: {
// "swap": Object.keys(endpoints).reduce((acc, chain) => {
// return {
// ...acc,
// [chain]: {
// fetch: getFetch(historicalDataSwap)(chain),
// start: async () => getStartTimestamp(chain)
// }
// }
// }, {}),
"derivatives": Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: getFetch(historicalDataDerivatives)(chain),
start: async () => getStartTimestamp(chain)
}
}
}, {})
}
}

export default adapter;
74 changes: 74 additions & 0 deletions fees/rollup-finace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { Adapter } from "../adapters/types";
import { ARBITRUM, AVAX, CHAIN } from "../helpers/chains";
import { request, gql } from "graphql-request";
import type { ChainEndpoints } from "../adapters/types"
import { Chain } from '@defillama/sdk/build/general';
import { getTimestampAtStartOfDayUTC } from "../utils/date";

const endpoints = {
[CHAIN.ERA]: "https://subgraph.rollup.finance/subgraphs/name/rollUp/stats",
}


const graphs = (graphUrls: ChainEndpoints) => {
return (chain: Chain) => {
return async (timestamp: number) => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp)
const fromTimestamp = todaysTimestamp - 60 * 60 * 24
const searchTimestamp = todaysTimestamp;

const graphQuery = gql
`{
feeStat(id: "${searchTimestamp}") {
mint
burn
marginAndLiquidation
}
}`;

const graphQueryYesterday = gql
`{
feeStat(id: "${fromTimestamp}") {
mint
burn
marginAndLiquidation
}
}`;


const graphRes = await request(graphUrls[chain], graphQuery);
const graphResYesterday = await request(graphUrls[chain], graphQueryYesterday);


const dailyFee = parseInt(graphRes.feeStat.mint) + parseInt(graphRes.feeStat.burn) + parseInt(graphRes.feeStat.marginAndLiquidation);
const finalDailyFee = (dailyFee / 1e30);
const userFee = parseInt(graphRes.feeStat.marginAndLiquidation)
const finalUserFee = (userFee / 1e30);

const dailyFeePrev = parseInt(graphResYesterday.feeStat.mint) + parseInt(graphResYesterday.feeStat.burn) + parseInt(graphResYesterday.feeStat.marginAndLiquidation);
const finalDailyFeePrev = (dailyFeePrev / 1e30);
const userFeePrev = parseInt(graphResYesterday.feeStat.marginAndLiquidation)
const finalUserFeePrev = (userFeePrev / 1e30);
const dailyFees = finalDailyFee - finalDailyFeePrev;
const dailyUserFees = finalUserFee - finalUserFeePrev;

return {
timestamp,
dailyFees: dailyFees ? dailyFees.toString() : undefined,
dailyUserFees: dailyUserFees ? dailyUserFees.toString() : undefined,
};
};
};
};


const adapter: Adapter = {
adapter: {
[CHAIN.ERA]: {
fetch: graphs(endpoints)(CHAIN.ERA),
start: async () => 1688256000,
},
}
}

export default adapter;

0 comments on commit 40e0443

Please sign in to comment.