Skip to content

Commit

Permalink
Merge pull request DefiLlama#1370 from muteio/master
Browse files Browse the repository at this point in the history
Create koi-finance Fees
  • Loading branch information
dtmkeng authored Apr 3, 2024
2 parents d0cf802 + 85309bd commit 83e0f6d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dexs/mute.io/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 83e0f6d

Please sign in to comment.