Skip to content

Commit

Permalink
Merge pull request #876 from MoonKnightDev/master
Browse files Browse the repository at this point in the history
Add based-markets
  • Loading branch information
dtmkeng authored Oct 10, 2023
2 parents 2d27302 + cb0dd07 commit db288c0
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
91 changes: 91 additions & 0 deletions dexs/based-markets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import request, { gql } from "graphql-request";
import { FetchResultVolume, SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import BigNumber from "bignumber.js";
import { getUniqStartOfTodayTimestamp } from "../../helpers/getUniSubgraphVolume";

const ONE_DAY_IN_SECONDS = 60 * 60 * 24

const endpoint = "https://api.thegraph.com/subgraphs/name/symmiograph/base_analytics_8"

const query = gql`
query stats($from: String!, $to: String!) {
dailyHistories(where: {timestamp_gte: $from, timestamp_lte: $to, accountSource: "0x5dE6949717F3AA8E0Fbed5Ce8B611Ebcf1e44AE9"}){
timestamp
platformFee
accountSource
tradeVolume
}
totalHistories(where: {accountSource: "0x5dE6949717F3AA8E0Fbed5Ce8B611Ebcf1e44AE9"}) {
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 (timestamp: number): Promise<FetchResultVolume> => {
const response: IGraphResponse = await request(endpoint, query, {
from: String(timestamp - ONE_DAY_IN_SECONDS),
to: String(timestamp)
})


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

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

dailyVolume = dailyVolume.dividedBy(new BigNumber(1e18))
totalVolume = totalVolume.dividedBy(new BigNumber(1e18))

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

const dayTimestamp = getUniqStartOfTodayTimestamp(new Date((timestamp * 1000)))

return {
timestamp: dayTimestamp,
dailyVolume: _dailyVolume,
totalVolume: _totalVolume,
}

}

const adapter: SimpleAdapter = {
adapter: {
[CHAIN.BASE]: {
fetch: fetchVolume,
start: async () => 1691332847
}
}
}

export default adapter;
113 changes: 113 additions & 0 deletions fees/based-markets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import request, { gql } from "graphql-request";
import { CHAIN } from "../helpers/chains";
import BigNumber from "bignumber.js";
import { getUniqStartOfTodayTimestamp } from "../helpers/getUniSubgraphVolume";
import { FetchResultFees, SimpleAdapter } from "../adapters/types";

const ONE_DAY_IN_SECONDS = 60 * 60 * 24

const endpoint = "https://api.thegraph.com/subgraphs/name/symmiograph/base_analytics_8"

const query = gql`
query stats($from: String!, $to: String!) {
dailyHistories(where: {timestamp_gte: $from, timestamp_lte: $to, accountSource: "0x5dE6949717F3AA8E0Fbed5Ce8B611Ebcf1e44AE9"}){
timestamp
platformFee
accountSource
tradeVolume
}
totalHistories(where: {accountSource: "0x5dE6949717F3AA8E0Fbed5Ce8B611Ebcf1e44AE9"}) {
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 (timestamp: number): Promise<FetchResultFees> => {
const response: IGraphResponse = await request(endpoint, query, {
from: String(timestamp - ONE_DAY_IN_SECONDS),
to: String(timestamp)
})


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';

const dayTimestamp = getUniqStartOfTodayTimestamp(new Date((timestamp * 1000)))

return {
timestamp: dayTimestamp,

dailyFees: _dailyFees,
totalFees: _totalFees,

dailyUserFees,
dailyRevenue,
dailyProtocolRevenue,
dailyHoldersRevenue,
dailySupplySideRevenue,
totalUserFees,
totalRevenue,
totalProtocolRevenue,
totalSupplySideRevenue,
}

}


const adapter: SimpleAdapter = {
adapter: {
[CHAIN.BASE]: {
fetch: fetchVolume,
start: async () => 1691332847
}
}
}
export default adapter;

0 comments on commit db288c0

Please sign in to comment.