Skip to content

Commit

Permalink
add koi-finance fees
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt21 committed Apr 2, 2024
1 parent 70a1d2d commit ef11777
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions dexs/mute.io/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { univ2Adapter } from "../../helpers/getUniSubgraphVolume";
import { CHAIN } from "../../helpers/chains";

const endpoints = {
[CHAIN.ERA]: "https://api.studio.thegraph.com/query/12332/muteswitch---zksync-era/v0.0.5",
[CHAIN.ERA]: "https://api.goldsky.com/api/public/project_clmtie4nnezuh2nw6hhjg6mo7/subgraphs/mute_switch/v0.0.7/gn",
};

const adapter = univ2Adapter(endpoints, {
Expand All @@ -15,4 +15,4 @@ const adapter = univ2Adapter(endpoints, {

adapter.adapter.era.start = 1679529600

export default adapter
export default adapter
27 changes: 27 additions & 0 deletions fees/koi-finance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Adapter, FetchResultFees } from '../../adapters/types';
import { ZKSYNC } from '../../helpers/chains';
import { fetchV1 } from './koi-finance';


const getFees = async (timestamp: number): Promise<FetchResultFees> => {
const [feeV1] = await Promise.all([fetchV1()(timestamp)]);
const dailyFees = Number(feeV1.dailyFees);
const dailyRevenue = Number(feeV1.dailyRevenue);

return {
dailyFees: `${dailyFees}`,
dailyRevenue: `${dailyRevenue}`,
timestamp
}
}

const adapter: Adapter = {
adapter: {
[ZKSYNC]: {
fetch: getFees,
start: 1677110400, // TODO: Add accurate timestamp
},
},
};

export default adapter;
62 changes: 62 additions & 0 deletions fees/koi-finance/koi-finance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import request, { gql } from "graphql-request";
import { getBlock } from "../../helpers/getBlock";
import {
getTimestampAtStartOfDayUTC,
getTimestampAtStartOfPreviousDayUTC
} from "../../utils/date";
import BigNumber from "bignumber.js";

const endpoint = "https://api.goldsky.com/api/public/project_clmtie4nnezuh2nw6hhjg6mo7/subgraphs/mute_switch/v0.0.7/gn"

export const fetchV1 = () => {
return async (timestamp: number) => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);
const yesterdaysTimestamp = getTimestampAtStartOfPreviousDayUTC(timestamp);
const todaysBlock = await getBlock(
todaysTimestamp,
"era",
{}
);

const yesterdaysBlock = await getBlock(yesterdaysTimestamp, "era", {});

const query = gql`
query fees {
yesterday: pairs(block: {number: ${yesterdaysBlock}}, where: {volumeUSD_gt: "0"}, first: 1000, orderBy:volumeUSD, orderDirection:desc) {
id
stable
pairFee
volumeUSD
}
today: pairs(block: {number: ${todaysBlock}}, where: {volumeUSD_gt: "0"}, first: 1000, orderBy:volumeUSD, orderDirection:desc) {
id
stable
pairFee
volumeUSD
}
}
`;
const todayVolume: { [id: string]: BigNumber } = {};
const graphRes = await request(endpoint, query);
let dailyFee = new BigNumber(0);
for (const pool of graphRes["today"]) {
todayVolume[pool.id] = new BigNumber(pool.volumeUSD);
}

for (const pool of graphRes["yesterday"]) {
if (!todayVolume[pool.id]) continue;

const dailyVolume = BigNumber(todayVolume[pool.id]).minus(
pool.volumeUSD
);

dailyFee = dailyFee.plus(dailyVolume.times(pool.pairFee).div(10000))
}

return {
timestamp,
dailyFees: dailyFee.toString(),
dailyRevenue: dailyFee.times(0.2).toString(),
};
};
};

0 comments on commit ef11777

Please sign in to comment.