Skip to content

Commit

Permalink
Merge pull request #1908 from morphex-labs/master
Browse files Browse the repository at this point in the history
feat: Update subgraphs for BMX + track Freestyle fees
  • Loading branch information
dtmkeng committed Sep 15, 2024
2 parents cd91e18 + b94a645 commit f72b723
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 54 deletions.
78 changes: 42 additions & 36 deletions dexs/bmx-freestyle/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import BigNumber from "bignumber.js";
import { FetchOptions, FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import {
FetchOptions,
FetchResultVolume,
SimpleAdapter,
} from "../../adapters/types";
import request, { gql } from "graphql-request";
import { CHAIN } from "../../helpers/chains";


const freestyleEndpoints: { [key: string]: string } = {
[CHAIN.BASE]:
"https://api.studio.thegraph.com/query/62454/analytics_base_8_2/version/latest",
"https://api-v2.morphex.trade/subgraph/3KhmYXgsM3CM1bbUCX8ejhcxQCtWwpUGhP7p9aDKZ94Z",
};

interface IGraphResponseFreestyle {
Expand Down Expand Up @@ -66,50 +69,53 @@ const freestyleQuery = gql`
}
`;

const fetchFreestyleVolume = async (timestamp: number, _t: any, options: FetchOptions): Promise<FetchResultVolume> => {
const startTime = options.startOfDay;
const endTime = startTime + ONE_DAY_IN_SECONDS;
const response: IGraphResponseFreestyle = await request(
freestyleEndpoints[options.chain],
freestyleQuery,
{
from: String(startTime),
to: String(endTime),
}
);

let dailyVolume = new BigNumber(0);
let totalVolume = new BigNumber(0);
const fetchFreestyleVolume = async (
timestamp: number,
_t: any,
options: FetchOptions
): Promise<FetchResultVolume> => {
const startTime = options.startOfDay;
const endTime = startTime + ONE_DAY_IN_SECONDS;
const response: IGraphResponseFreestyle = await request(
freestyleEndpoints[options.chain],
freestyleQuery,
{
from: String(startTime),
to: String(endTime),
}
);

response.dailyHistories.forEach((data) => {
dailyVolume = dailyVolume.plus(new BigNumber(data.tradeVolume));
});
response.totalHistories.forEach((data) => {
totalVolume = totalVolume.plus(new BigNumber(data.tradeVolume));
});
let dailyVolume = new BigNumber(0);
let totalVolume = new BigNumber(0);

dailyVolume = dailyVolume.dividedBy(new BigNumber(1e18));
totalVolume = totalVolume.dividedBy(new BigNumber(1e18));
response.dailyHistories.forEach((data) => {
dailyVolume = dailyVolume.plus(new BigNumber(data.tradeVolume));
});
response.totalHistories.forEach((data) => {
totalVolume = totalVolume.plus(new BigNumber(data.tradeVolume));
});

const _dailyVolume = toString(dailyVolume);
const _totalVolume = toString(totalVolume);
dailyVolume = dailyVolume.dividedBy(new BigNumber(1e18));
totalVolume = totalVolume.dividedBy(new BigNumber(1e18));

const _dailyVolume = toString(dailyVolume);
const _totalVolume = toString(totalVolume);

return {
timestamp: timestamp,
dailyVolume: _dailyVolume ?? "0",
totalVolume: _totalVolume ?? "0",
};
return {
timestamp: timestamp,
dailyVolume: _dailyVolume ?? "0",
totalVolume: _totalVolume ?? "0",
};
};

const adapter: SimpleAdapter = {
version: 1,
adapter: {
[CHAIN.BASE]: {
fetch: fetchFreestyleVolume,
start: 0
}
}
}
start: 1714554000,
},
},
};

export default adapter;
17 changes: 4 additions & 13 deletions dexs/bmx/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
import request, { gql } from "graphql-request";
import {
BreakdownAdapter,
Fetch
} from "../../adapters/types";
import { BreakdownAdapter, Fetch } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";
import BigNumber from "bignumber.js";


const startTimestamps: { [chain: string]: number } = {
[CHAIN.BASE]: 1694304000,
[CHAIN.MODE]: 1720627435,
};
const endpoints: { [key: string]: string } = {
[CHAIN.BASE]:
"https://api.studio.thegraph.com/query/71696/bmx-base-stats/version/latest",
"https://api-v2.morphex.trade/subgraph/2vZHkWfx8g27Tri5LkTbhvCExCQcXJ3f28X2BwzFhjf6",
[CHAIN.MODE]:
"https://api.studio.thegraph.com/query/42444/bmx-mode-stats/version/latest",
};
const freestyleEndpoints: { [key: string]: string } = {
[CHAIN.BASE]:
"https://api.studio.thegraph.com/query/62454/analytics_base_8_2/version/latest",
"https://api-v2.morphex.trade/subgraph/8tp7xrDSCuutJ5omjfQKHvkGJpLszqPVWg3pby9XMLEz",
};

const historicalDataSwap = gql`
Expand Down Expand Up @@ -154,7 +146,6 @@ const getFetch =
};
};


const adapter: BreakdownAdapter = {
breakdown: {
swap: Object.keys(endpoints).reduce((acc, chain) => {
Expand All @@ -166,7 +157,7 @@ const adapter: BreakdownAdapter = {
},
};
}, {}),
"derivatives": Object.keys(endpoints).reduce((acc, chain) => {
derivatives: Object.keys(endpoints).reduce((acc, chain) => {
return {
...acc,
[chain]: {
Expand Down
113 changes: 113 additions & 0 deletions fees/bmx-freestyle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import BigNumber from "bignumber.js";
import request, { gql } from "graphql-request";
import { FetchOptions, SimpleAdapter } from "../adapters/types";
import { CHAIN } from "../helpers/chains";

const endpoint =
"https://api-v2.morphex.trade/subgraph/3KhmYXgsM3CM1bbUCX8ejhcxQCtWwpUGhP7p9aDKZ94Z";

const query = gql`
query stats($from: String!, $to: String!) {
dailyHistories(
where: {
timestamp_gte: $from
timestamp_lte: $to
accountSource: "0x6D63921D8203044f6AbaD8F346d3AEa9A2719dDD"
}
) {
timestamp
platformFee
accountSource
tradeVolume
}
totalHistories(
where: { accountSource: "0x6D63921D8203044f6AbaD8F346d3AEa9A2719dDD" }
) {
timestamp
platformFee
accountSource
tradeVolume
}
}
`;

interface IGraphResponse {
dailyHistories: Array<{
tiemstamp: string;
platformFee: string;
accountSource: string;
tradeVolume: string;
}>;
totalHistories: Array<{
tiemstamp: string;
platformFee: string;
accountSource: string;
tradeVolume: BigNumber;
}>;
}

const toString = (x: BigNumber) => {
if (x.isEqualTo(0)) return undefined;
return x.toString();
};

const fetchVolume = async ({ endTimestamp, startTimestamp }: FetchOptions) => {
const response: IGraphResponse = await request(endpoint, query, {
from: String(startTimestamp),
to: String(endTimestamp),
});

// Merging both responses
let dailyFees = new BigNumber(0);
response.dailyHistories.forEach((data) => {
dailyFees = dailyFees.plus(new BigNumber(data.platformFee));
});

let totalFees = new BigNumber(0);
response.totalHistories.forEach((data) => {
totalFees = totalFees.plus(new BigNumber(data.platformFee));
});

dailyFees = dailyFees.dividedBy(new BigNumber(1e18));
totalFees = totalFees.dividedBy(new BigNumber(1e18));

const _dailyFees = toString(dailyFees);
const _totalFees = toString(totalFees);

const dailyUserFees = _dailyFees;
const dailyRevenue = _dailyFees;
const dailyProtocolRevenue = "0";
const dailyHoldersRevenue = _dailyFees;
const dailySupplySideRevenue = "0";

const totalUserFees = _totalFees;
const totalRevenue = _totalFees;
const totalProtocolRevenue = "0";
const totalSupplySideRevenue = "0";

return {
dailyFees: _dailyFees ?? "0",
totalFees: _totalFees ?? "0",

dailyUserFees: dailyUserFees ?? "0",
dailyRevenue: dailyRevenue ?? "0",
dailyProtocolRevenue: dailyProtocolRevenue ?? "0",
dailyHoldersRevenue: dailyHoldersRevenue ?? "0",
dailySupplySideRevenue: dailySupplySideRevenue ?? "0",
totalUserFees: totalUserFees ?? "0",
totalRevenue: totalRevenue ?? "0",
totalProtocolRevenue: totalProtocolRevenue ?? "0",
totalSupplySideRevenue: totalSupplySideRevenue ?? "0",
};
};

const adapter: SimpleAdapter = {
version: 2,
adapter: {
[CHAIN.BASE]: {
fetch: fetchVolume,
start: 1714554000,
},
},
};
export default adapter;
9 changes: 4 additions & 5 deletions fees/bmx.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Adapter } from "../adapters/types";
import { CHAIN } from "../helpers/chains";
import { request, gql } from "graphql-request";
import type { FetchV2 } from "../adapters/types"
import type { FetchV2 } from "../adapters/types";
import { getTimestampAtStartOfDayUTC } from "../utils/date";

const endpoints: { [key: string]: string } = {
[CHAIN.BASE]:
"https://api.studio.thegraph.com/query/71696/bmx-base-stats/version/latest",
"https://api-v2.morphex.trade/subgraph/2vZHkWfx8g27Tri5LkTbhvCExCQcXJ3f28X2BwzFhjf6",
[CHAIN.MODE]:
"https://api.studio.thegraph.com/query/42444/bmx-mode-stats/version/latest",
"https://api-v2.morphex.trade/subgraph/8tp7xrDSCuutJ5omjfQKHvkGJpLszqPVWg3pby9XMLEz",
};

const methodology = {
Expand Down Expand Up @@ -48,7 +48,6 @@ const graphs: FetchV2 = async ({ chain, endTimestamp }) => {
const finalUserFee = userFee / 1e30;

return {
endTimestamp,
dailyFees: finalDailyFee.toString(),
dailyUserFees: finalUserFee.toString(),
dailyRevenue: (finalDailyFee * 0.4).toString(),
Expand All @@ -73,7 +72,7 @@ const adapter: Adapter = {
meta: {
methodology,
},
}
},
},
};

Expand Down

0 comments on commit f72b723

Please sign in to comment.