Skip to content

Commit

Permalink
number fixes plus track mux op and ftm volume
Browse files Browse the repository at this point in the history
  • Loading branch information
supakawaiidesu committed May 1, 2024
1 parent 6e0095b commit 3133438
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 59 deletions.
71 changes: 28 additions & 43 deletions dexs/unidex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ import { CHAIN } from "../../helpers/chains";
import { getTimestampAtStartOfDayUTC } from "../../utils/date";
import { Chain } from "@defillama/sdk/build/general";
import request, { gql } from "graphql-request";
import { adapteraggderivative } from './unidex-agg-perp/index'
import { adapter_dexs_agg } from './unidex-dexs-agg/index'

type TChainIDs = {
[key in Chain]?: number;
};
import { adapteraggderivative } from './unidex-agg-perp/index';
import { adapter_dexs_agg } from './unidex-dexs-agg/index';

type TChainIDs = { [key in Chain]?: number; };
const chainIDs: TChainIDs = {
[CHAIN.FANTOM]: 250,
[CHAIN.ARBITRUM]: 42161,
Expand All @@ -22,36 +19,38 @@ const chainIDs: TChainIDs = {

interface IDayProduct {
cumulativeVolumeUsd: number;
_id: string;
chainId: number;
}

const fetch = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);

const graphQuery = gql`
query MyQuery {
DayProducts(limit: 0, filter: {date: ${todaysTimestamp}}) {
DayProducts(limit: 0, filter: { date: ${todaysTimestamp} }) {
cumulativeVolumeUsd
_id
chainId
}
}
`;

const endpoint = "https://arkiverbackup.moltennetwork.com/graphql";
const endpoint = "https://arkiver.moltennetwork.com/graphql";
const response = await request(endpoint, graphQuery);
const dayProducts: IDayProduct[] = response.DayProducts;

const chainID = chainIDs[chain];
let dailyVolumeUSD = 0;

const volumeByChain: { [chainId: number]: number } = {};
dayProducts.forEach((product) => {
const productChainID = parseInt(product._id.split(":")[2]);
if (productChainID === chainID) {
dailyVolumeUSD += product.cumulativeVolumeUsd;
const chainId = product.chainId;
if (chainId === 360) {
// Combine volume for chainID 360 with chainID 42161
volumeByChain[42161] = (volumeByChain[42161] || 0) + product.cumulativeVolumeUsd;
} else {
volumeByChain[chainId] = (volumeByChain[chainId] || 0) + product.cumulativeVolumeUsd;
}
});

const chainID = chainIDs[chain];
const dailyVolumeUSD = chainID !== undefined ? volumeByChain[chainID] || 0 : 0;

return {
dailyVolume: dailyVolumeUSD.toString(),
timestamp: todaysTimestamp,
Expand All @@ -60,60 +59,45 @@ const fetch = (chain: Chain) => {
};

const methodology = {
dailyVolume:
"Sum of cumulativeVolumeUsd for all products on the specified chain for the given day",
dailyVolume: "Sum of cumulativeVolumeUsd for all products on the specified chain for the given day",
};

const adapter: any = {
adapter: {
[CHAIN.OPTIMISM]: {
fetch: fetch(CHAIN.OPTIMISM),
start: 1687422746,
meta: {
methodology,
},
meta: { methodology },
},
[CHAIN.ERA]: {
fetch: fetch(CHAIN.ERA),
start: 1687422746,
meta: {
methodology,
},
meta: { methodology },
},
[CHAIN.ARBITRUM]: {
fetch: fetch(CHAIN.ARBITRUM),
start: 1687422746,
meta: {
methodology,
},
meta: { methodology },
},
[CHAIN.BASE]: {
fetch: fetch(CHAIN.BASE),
start: 1687422746,
meta: {
methodology,
},
meta: { methodology },
},
[CHAIN.FANTOM]: {
fetch: fetch(CHAIN.FANTOM),
start: 1687422746,
meta: {
methodology,
},
meta: { methodology },
},
[CHAIN.METIS]: {
fetch: fetch(CHAIN.METIS),
start: 1687898060,
meta: {
methodology,
},
meta: { methodology },
},
[CHAIN.EVMOS]: {
fetch: fetch(CHAIN.EVMOS),
start: 1700104066,
meta: {
methodology,
},
meta: { methodology },
},
},
};
Expand All @@ -124,5 +108,6 @@ const adapterbreakdown: BreakdownAdapter = {
"unidex-agg-derivative": adapteraggderivative["adapter"],
"unidex-dexs-agg": adapter_dexs_agg["adapter"],
}
}
export default adapterbreakdown;
};

export default adapterbreakdown;
48 changes: 32 additions & 16 deletions dexs/unidex/unidex-agg-perp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const chainIDs: TChainIDs = {

interface IDayProduct {
cumulativeVolumeUsd: number;
_id: string;
chainId: number;
}

const fetchReferralVolume = async (timestamp: number): Promise<number> => {
Expand Down Expand Up @@ -53,7 +53,7 @@ const fetchReferralVolume = async (timestamp: number): Promise<number> => {
};


const fetchMuxReferralVolume = async (timestamp: number): Promise<number> => {
const fetchMuxReferralVolume = async (chain: Chain, timestamp: number): Promise<number> => {
const startOfDayTimestamp = getTimestampAtStartOfDayUTC(timestamp);
const endOfDayTimestamp = startOfDayTimestamp + 86400; // Add one day's worth of seconds for the end of the day

Expand All @@ -77,7 +77,22 @@ const fetchMuxReferralVolume = async (timestamp: number): Promise<number> => {
timestamp_lte: endOfDayTimestamp.toString()
};

const referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-arb';
let referralEndpoint = '';

switch (chain) {
case CHAIN.ARBITRUM:
referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-arb';
break;
case CHAIN.OPTIMISM:
referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-op';
break;
case CHAIN.FANTOM:
referralEndpoint = 'https://api.thegraph.com/subgraphs/name/mux-world/mux-referral-ftm';
break;
default:
return 0; // Return 0 for unsupported chains
}

const referralRes = await request(referralEndpoint, referralQuery, variables);

// Sum up the volumes
Expand All @@ -98,9 +113,6 @@ const fetchMuxReferralVolume = async (timestamp: number): Promise<number> => {






const fetch = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);
Expand All @@ -109,7 +121,7 @@ const fetch = (chain: Chain) => {
query MyQuery {
DayProducts(filter: {date: ${todaysTimestamp}}) {
cumulativeVolumeUsd
_id
chainId
}
}
`;
Expand All @@ -118,19 +130,23 @@ const fetch = (chain: Chain) => {
const response = await request(endpoint, graphQuery);
const dayProducts: IDayProduct[] = response.DayProducts;

const chainID = chainIDs[chain];
let dailyVolumeUSD = 0;

const volumeByChain: { [chainId: number]: number } = {};
dayProducts.forEach((product) => {
const productChainID = parseInt(product._id.split(':')[2]);
if (productChainID === chainID) {
dailyVolumeUSD += product.cumulativeVolumeUsd;
const chainId = product.chainId;
if (chainId === 360) {
// Combine volume for chainID 360 with chainID 42161
volumeByChain[42161] = (volumeByChain[42161] || 0) + product.cumulativeVolumeUsd;
} else {
volumeByChain[chainId] = (volumeByChain[chainId] || 0) + product.cumulativeVolumeUsd;
}
});

if (chain === CHAIN.ARBITRUM) {
const chainID = chainIDs[chain];
let dailyVolumeUSD = chainID !== undefined ? volumeByChain[chainID] || 0 : 0;

if (chain === CHAIN.ARBITRUM || chain === CHAIN.OPTIMISM || chain === CHAIN.FANTOM) {
const referralVolumeUSD = await fetchReferralVolume(timestamp);
const muxReferralVolumeUSD = await fetchMuxReferralVolume(timestamp); // errror
const muxReferralVolumeUSD = await fetchMuxReferralVolume(chain, timestamp);
dailyVolumeUSD += referralVolumeUSD + muxReferralVolumeUSD;
}

Expand Down Expand Up @@ -203,4 +219,4 @@ const adapteraggderivative: any = {

export {
adapteraggderivative
}
}

0 comments on commit 3133438

Please sign in to comment.