Skip to content

Commit

Permalink
Merge pull request #653 from joehquak/add/sobal
Browse files Browse the repository at this point in the history
Add: Sobal DEX Volume & Fees
  • Loading branch information
dtmkeng authored Jul 30, 2023
2 parents a87c6b9 + 65223d6 commit e0ced26
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 3 deletions.
49 changes: 49 additions & 0 deletions dexs/sobal/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { ChainEndpoints, BreakdownAdapter, BaseAdapter } from "../../adapters/types";
import { getChainVolume } from "../../helpers/getUniSubgraphVolume";
import customBackfill from "../../helpers/customBackfill";
import { CHAIN } from "../../helpers/chains";
import { Chain } from "@defillama/sdk/build/general";
import { getStartTimestamp } from "../../helpers/getStartTimestamp";

const endpoints: ChainEndpoints = {
[CHAIN.NEON]:
"https://neon-subgraph.sobal.fi/sobal-pools",
};

const graphParams = {
totalVolume: {
factory: "balancers",
field: "totalSwapVolume",
},
hasDailyVolume: false,
}

const graphs = getChainVolume({
graphUrls: endpoints,
...graphParams
});

const adapter: BreakdownAdapter = {
breakdown: {
v2: Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
[chain]: {
fetch: graphs(chain as Chain),
customBackfill: customBackfill(chain as Chain, graphs),
start: getStartTimestamp({
endpoints,
chain: chain,
dailyDataField: `balancerSnapshots`,
dateField: 'timestamp',
volumeField: 'totalSwapVolume'
}),
}
}
}, {} as BaseAdapter)
}
}

export default adapter;

// TODO custom backfill have to get specific block at start of each day
81 changes: 81 additions & 0 deletions fees/sobal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Adapter } 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 BigNumber from "bignumber.js";
import { getTimestampAtStartOfDayUTC } from "../utils/date";

const v2Endpoints = {
[CHAIN.NEON]:
"https://neon-subgraph.sobal.fi/sobal-pools",
};

const v2Graphs = (graphUrls: ChainEndpoints) => {
return (chain: Chain) => {
return async (timestamp: number) => {
const startTimestamp = getTimestampAtStartOfDayUTC(timestamp)
const dayId = Math.floor(startTimestamp / 86400)

const graphQuery = gql
`query fees($dayId: String!, $yesterdayId: String!) {
today: balancerSnapshot(id: $dayId) {
totalSwapFee
}
yesterday: balancerSnapshot(id: $yesterdayId) {
totalSwapFee
}
}`;

const graphRes = await request(graphUrls[chain], graphQuery, {
dayId: `2-${dayId}`,
yesterdayId: `2-${dayId - 1}`
});
const currentTotalSwapFees = new BigNumber(graphRes["today"]["totalSwapFee"])

const dailyFee = currentTotalSwapFees.minus(new BigNumber(graphRes["yesterday"]["totalSwapFee"]))

// Currently 50% of Fees
const dailyRevenue = dailyFee.multipliedBy(0.5);
const totalRevenue = currentTotalSwapFees.multipliedBy(0.5);

return {
timestamp,
totalUserFees: graphRes["today"]["totalSwapFee"],
dailyUserFees: dailyFee.toString(),
totalFees: graphRes["today"]["totalSwapFee"],
dailyFees: dailyFee.toString(),
totalRevenue: totalRevenue.toString(),
dailyRevenue: dailyRevenue.toString(),
totalProtocolRevenue: totalRevenue.toString(),
dailyProtocolRevenue: dailyRevenue.toString(),
totalSupplySideRevenue: new BigNumber(graphRes["today"]["totalSwapFee"]).minus(totalRevenue.toString()).toString(),
dailySupplySideRevenue: new BigNumber(dailyFee.toString()).minus(dailyRevenue.toString()).toString(),
};
};
};
};

const methodology = {
UserFees: "Trading fees paid by users, ranging from 0.0001% to 10%",
Fees: "All trading fees collected (doesn't include withdrawal and flash loan fees)",
Revenue: "Protocol revenue from all fees collected",
ProtocolRevenue: "Currently no protocol swap fee in place",
SupplySideRevenue: "A small percentage of the trade paid by traders to pool LPs, set by the pool creator or managed by protocol.",
}

const adapter: Adapter = {
breakdown: {
v2: {
[CHAIN.NEON]: {
fetch: v2Graphs(v2Endpoints)(CHAIN.NEON),
start: async () => 1689613200, // 17TH JULY 5PM GMT
meta: {
methodology
}
}
}
}
}

export default adapter;
7 changes: 4 additions & 3 deletions helpers/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ export enum CHAIN {
MANTLE = "mantle",
ICP="icp",
LINEA = "linea",
BASE = "base"
BASE = "base",
NEON = "neon_evm",
}

// Don´t use
Expand All @@ -132,7 +133,7 @@ const POLYGON = "polygon";
const RONIN = "ronin";
const XDAI = "xdai";
const AURORA = "aurora";
const MOONRIVER = "moonriver"
const MOONRIVER = "moonriver";
const BITCOIN = "bitcoin";
const LITECOIN = "litecoin";
const DOGE = "doge";
Expand All @@ -158,5 +159,5 @@ export {
MOONRIVER,
BITCOIN,
LITECOIN,
DOGE
DOGE,
};

0 comments on commit e0ced26

Please sign in to comment.