Skip to content

Commit

Permalink
custombackfill
Browse files Browse the repository at this point in the history
  • Loading branch information
waynebruce0x committed Mar 5, 2024
1 parent 70a7a68 commit f35b6b1
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 75 deletions.
2 changes: 1 addition & 1 deletion adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export type BaseAdapter = {
start: IStartTimestamp | number
fetch: Fetch|FetchV2;
runAtCurrTime?: boolean;
customBackfill?: Fetch;
customBackfill?: Fetch|FetchV2;
meta?: {
methodology?: string | IJSON<string>
hallmarks?: [number, string][]
Expand Down
1 change: 1 addition & 0 deletions dexs/baseswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const startTimeV3 = {
};

const adapter: BreakdownAdapter = {
version: 2,
breakdown: {
v2: Object.keys(v2Endpoints).reduce((acc, chain) => {
return {
Expand Down
1 change: 1 addition & 0 deletions dexs/beamswap-v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const methodologyv3 = {
};

const adapter: BreakdownAdapter = {
version: 2,
breakdown: {
v3: {
[CHAIN.MOONBEAN]: {
Expand Down
1 change: 1 addition & 0 deletions dexs/beamswap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const methodologyStable = {
};

const adapter: BreakdownAdapter = {
version: 2,
breakdown: {
classic: {
[CHAIN.MOONBEAN]: {
Expand Down
1 change: 1 addition & 0 deletions dexs/dackieswap-v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const methodology = {
};

const adapter: SimpleAdapter = {
version: 2,
adapter: Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
Expand Down
184 changes: 119 additions & 65 deletions dexs/swapbased/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import customBackfill from "../../helpers/customBackfill";
import { DEFAULT_TOTAL_VOLUME_FACTORY, DEFAULT_TOTAL_VOLUME_FIELD, DEFAULT_DAILY_VOLUME_FACTORY, DEFAULT_DAILY_VOLUME_FIELD, getChainVolume } from "../../helpers/getUniSubgraphVolume";
import {
DEFAULT_TOTAL_VOLUME_FACTORY,
DEFAULT_TOTAL_VOLUME_FIELD,
DEFAULT_DAILY_VOLUME_FACTORY,
DEFAULT_DAILY_VOLUME_FIELD,
getChainVolume,
} from "../../helpers/getUniSubgraphVolume";
import { CHAIN } from "../../helpers/chains";
import type { Fetch, ChainEndpoints, BreakdownAdapter } from "../../adapters/types";
import type {
Fetch,
ChainEndpoints,
BreakdownAdapter,
} from "../../adapters/types";
import { getGraphDimensions } from "../../helpers/getUniSubgraph";
import request, { gql } from "graphql-request";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
Expand All @@ -24,16 +34,17 @@ const graphs = getGraphDimensions({
},
feesPercent: {
type: "volume",
UserFees: 0.30,
UserFees: 0.3,
SupplySideRevenue: 0.25,
ProtocolRevenue: 0.05,
Revenue: 0.25,
Fees: 0.30,
}
Fees: 0.3,
},
});

const endpointsV3 = {
[CHAIN.BASE]: "https://api.thegraph.com/subgraphs/name/chimpydev/swapbased-algebra-core",
[CHAIN.BASE]:
"https://api.thegraph.com/subgraphs/name/chimpydev/swapbased-algebra-core",
};
const graphsV3 = getChainVolume({
graphUrls: endpointsV3,
Expand All @@ -44,7 +55,7 @@ const graphsV3 = getChainVolume({
dailyVolume: {
factory: "algebraDayData",
field: "volumeUSD",
dateField: "date"
dateField: "date",
},
});

Expand All @@ -59,106 +70,149 @@ const methodology = {
/* PERPS */

const endpointsPerps: { [key: string]: string } = {
[CHAIN.BASE]: "https://api.thegraph.com/subgraphs/name/chimpydev/swapbased-perps-core",
}
[CHAIN.BASE]:
"https://api.thegraph.com/subgraphs/name/chimpydev/swapbased-perps-core",
};

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

const historicalOI = gql`
query get_trade_stats($period: String!, $id: String!) {
tradingStats(where: {period: $period, id: $id}) {
tradingStats(where: { period: $period, id: $id }) {
id
longOpenInterest
shortOpenInterest
}
}
`

`;

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

interface IGraphResponseOI {
tradingStats: Array<{
id: string,
longOpenInterest: string,
shortOpenInterest: string,
}>
id: string;
longOpenInterest: string;
shortOpenInterest: string;
}>;
}

const getFetch = (query: string)=> (chain: string): any => async (timestamp: number) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(new Date((timestamp * 1000)))
const dailyData: IGraphResponse = await request(endpointsPerps[chain], query, {
id: String(dayTimestamp) + ':daily',
period: 'daily',
})
const totalData: IGraphResponse = await request(endpointsPerps[chain], query, {
id: 'total',
period: 'total',
})

const tradingStats: IGraphResponseOI = await request(endpointsPerps[chain], historicalOI, {
id: String(dayTimestamp) + ':daily',
period: 'daily',
})

const dailyOpenInterest = Number(tradingStats.tradingStats[0]?.longOpenInterest || 0) + Number(tradingStats.tradingStats[0]?.shortOpenInterest || 0);
const dailyLongOpenInterest = Number(tradingStats.tradingStats[0]?.longOpenInterest || 0);
const dailyShortOpenInterest = Number(tradingStats.tradingStats[0]?.shortOpenInterest || 0);

return {
timestamp: dayTimestamp,
dailyLongOpenInterest: dailyLongOpenInterest ? String(dailyLongOpenInterest * 10 ** -30) : undefined,
dailyShortOpenInterest: dailyShortOpenInterest ? String(dailyShortOpenInterest * 10 ** -30) : undefined,
dailyOpenInterest: dailyOpenInterest ? String(dailyOpenInterest * 10 ** -30) : undefined,
dailyVolume:
dailyData.volumeStats.length == 1
? String(Number(Object.values(dailyData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30)
const getFetch =
(query: string) =>
(chain: string): any =>
async (timestamp: number) => {
const dayTimestamp = getUniqStartOfTodayTimestamp(
new Date(timestamp * 1000),
);
const dailyData: IGraphResponse = await request(
endpointsPerps[chain],
query,
{
id: String(dayTimestamp) + ":daily",
period: "daily",
},
);
const totalData: IGraphResponse = await request(
endpointsPerps[chain],
query,
{
id: "total",
period: "total",
},
);

const tradingStats: IGraphResponseOI = await request(
endpointsPerps[chain],
historicalOI,
{
id: String(dayTimestamp) + ":daily",
period: "daily",
},
);

const dailyOpenInterest =
Number(tradingStats.tradingStats[0]?.longOpenInterest || 0) +
Number(tradingStats.tradingStats[0]?.shortOpenInterest || 0);
const dailyLongOpenInterest = Number(
tradingStats.tradingStats[0]?.longOpenInterest || 0,
);
const dailyShortOpenInterest = Number(
tradingStats.tradingStats[0]?.shortOpenInterest || 0,
);

return {
timestamp: dayTimestamp,
dailyLongOpenInterest: dailyLongOpenInterest
? String(dailyLongOpenInterest * 10 ** -30)
: undefined,
totalVolume:
totalData.volumeStats.length == 1
? String(Number(Object.values(totalData.volumeStats[0]).reduce((sum, element) => String(Number(sum) + Number(element)))) * 10 ** -30)
dailyShortOpenInterest: dailyShortOpenInterest
? String(dailyShortOpenInterest * 10 ** -30)
: undefined,

}
}
dailyOpenInterest: dailyOpenInterest
? String(dailyOpenInterest * 10 ** -30)
: undefined,
dailyVolume:
dailyData.volumeStats.length == 1
? String(
Number(
Object.values(dailyData.volumeStats[0]).reduce((sum, element) =>
String(Number(sum) + Number(element)),
),
) *
10 ** -30,
)
: undefined,
totalVolume:
totalData.volumeStats.length == 1
? String(
Number(
Object.values(totalData.volumeStats[0]).reduce((sum, element) =>
String(Number(sum) + Number(element)),
),
) *
10 ** -30,
)
: undefined,
};
};

const adapter: BreakdownAdapter = {
version: 2,
breakdown: {
v2: {
[CHAIN.BASE]: {
fetch: graphs(CHAIN.BASE),
start: 1690495200,
customBackfill: customBackfill(CHAIN.BASE, graphs),
meta: { methodology },
}
},
},
v3: {
[CHAIN.BASE]: {
fetch: graphsV3(CHAIN.BASE),
start: 1690443269,
}
},
},
perps: {
[CHAIN.BASE]: {
fetch: getFetch(historicalDataSwap)(CHAIN.BASE),
start: 1688913853,
}
}
},
},
},
};

Expand Down
25 changes: 16 additions & 9 deletions helpers/customBackfill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChainBlocks, Fetch, FetchResultGeneric } from "../adapters/types"
import { FetchOptions, FetchResultGeneric, Fetch, FetchV2, ChainBlocks } from "../adapters/types"
import { getBlock } from "./getBlock"
import { util } from '@defillama/sdk';
import { Chain } from "@defillama/sdk/build/general";
Expand All @@ -7,16 +7,23 @@ import BigNumber from "bignumber.js";
const { blocks: { chainsForBlocks } } = util
const ONE_DAY_IN_SECONDS = 60 * 60 * 24

export type IGraphs = (chain: Chain) => (timestamp: number, chainBlocks: ChainBlocks) => Promise<FetchResultGeneric>
export type IGraphs = (chain: Chain) => (options: FetchOptions|number, chainBlocks: ChainBlocks) => Promise<FetchResultGeneric>

export default (chain: Chain, graphs: IGraphs): Fetch => async (timestamp: number, chainBlocks: ChainBlocks): Promise<FetchResultGeneric> => {
export default (chain: Chain, graphs: any): Fetch|FetchV2 => async (options: FetchOptions|number, chainBlocks: ChainBlocks): Promise<FetchResultGeneric> => {
const fetchGetVolume = graphs(chain)
const resultDayN = await fetchGetVolume(timestamp, chainBlocks)
const timestampPreviousDay = timestamp - ONE_DAY_IN_SECONDS
let chainBlocksPreviousDay = {}
if (chainsForBlocks.includes(chain) || chain === "ethereum")
chainBlocksPreviousDay = { [chain]: await getBlock(timestampPreviousDay, chain, {}).catch(() => { }) }
const resultPreviousDayN = await fetchGetVolume(timestampPreviousDay, chainBlocksPreviousDay)
let resultPreviousDayN
let resultDayN
if (typeof options == 'number') {
resultDayN = await fetchGetVolume(options, chainBlocks)
const timestampPreviousDay = options - ONE_DAY_IN_SECONDS
let chainBlocksPreviousDay = {}
if (chainsForBlocks.includes(chain) || chain === "ethereum")
chainBlocksPreviousDay = { [chain]: await getBlock(timestampPreviousDay, chain, {}).catch(() => { }) }
resultPreviousDayN = await fetchGetVolume(timestampPreviousDay, chainBlocksPreviousDay)
} else {
resultDayN = await fetchGetVolume(options)
resultPreviousDayN = await fetchGetVolume(options)
}
const response: FetchResultGeneric = resultDayN
Object.keys(resultPreviousDayN).filter((key) => key.includes('total')).forEach(key => {
const dimension = `daily${key.slice(5)}`
Expand Down

0 comments on commit f35b6b1

Please sign in to comment.