Skip to content

Commit

Permalink
Merge pull request #841 from 0xSullivan/master
Browse files Browse the repository at this point in the history
Update thena perp and fees
  • Loading branch information
dtmkeng authored Sep 21, 2023
2 parents 0a38b1e + f4ef540 commit bb376c0
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 46 deletions.
95 changes: 73 additions & 22 deletions dexs/thena-perp/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,91 @@

import request from "graphql-request";
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/navid-fkh/symmetrical_bsc"

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


interface IGraphResponse {
id: string;
closeTradeVolume: string;
platformFee: string;
dailyHistories: Array<{
tiemstamp: string,
platformFee: string,
accountSource: string,
tradeVolume: string
}>
totalHistories: Array<{
tiemstamp: string,
platformFee: string,
accountSource: string,
tradeVolume: BigNumber
}>
}
const url = 'https://api.thegraph.com/subgraphs/name/navid-fkh/symmetrical_bsc';

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

const fetchVolume = async (timestamp: number): Promise<FetchResultVolume> => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp
const query = `
{
dailyHistories(orderBy: timestamp, orderDirection:desc, where: {timestamp_gte: ${fromTimestamp}, timestamp_lte: ${toTimestamp}}) {
id
closeTradeVolume
platformFee
}
}
`;
const response: IGraphResponse[] = (await request(url, query)).dailyHistories as IGraphResponse[]
const value = response.reduce((acc, curr) => acc + Number(curr.closeTradeVolume)/10**18, 0);
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 {
dailyVolume: `${value}`,
timestamp
timestamp: dayTimestamp,
dailyVolume: _dailyVolume,
totalVolume: _totalVolume,
}

}

const adapter: SimpleAdapter = {
adapter: {
[CHAIN.BSC]: {
fetch: fetchVolume,
start: async () => 1687877205
start: async () => 1687880277
}
}
}

export default adapter;
120 changes: 96 additions & 24 deletions fees/thena-perp.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,112 @@

import request from "graphql-request";
import { FetchResultFees, SimpleAdapter } from "../adapters/types";
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/navid-fkh/symmetrical_bsc"

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


interface IGraphResponse {
id: string;
closeTradeVolume: string;
platformFee: string;
dailyHistories: Array<{
tiemstamp: string,
platformFee: string,
accountSource: string,
tradeVolume: string
}>
totalHistories: Array<{
tiemstamp: string,
platformFee: string,
accountSource: string,
tradeVolume: BigNumber
}>
}
const url = 'https://api.thegraph.com/subgraphs/name/navid-fkh/symmetrical_bsc';

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

const fetchVolume = async (timestamp: number): Promise<FetchResultFees> => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp
const query = `
{
dailyHistories(orderBy: timestamp, orderDirection:desc, where: {timestamp_gte: ${fromTimestamp}, timestamp_lte: ${toTimestamp}}) {
id
closeTradeVolume
platformFee
}
}
`;
const response: IGraphResponse[] = (await request(url, query)).dailyHistories as IGraphResponse[]
const value = response.reduce((acc, curr) => acc + Number(curr.platformFee)/10**18, 0);
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 {
dailyFees: `${value}`,
dailyRevenue: `${value}`,
timestamp
timestamp: dayTimestamp,

dailyFees: _dailyFees,
totalFees: _totalFees,

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

}


const adapter: SimpleAdapter = {
adapter: {
[CHAIN.BSC]: {
fetch: fetchVolume,
start: async () => 1687877205
start: async () => 1687880277
}
}
}
Expand Down

0 comments on commit bb376c0

Please sign in to comment.