Skip to content

Commit

Permalink
and opti
Browse files Browse the repository at this point in the history
  • Loading branch information
dtmkeng committed Nov 6, 2023
1 parent fea923a commit d83964b
Showing 1 changed file with 82 additions and 67 deletions.
149 changes: 82 additions & 67 deletions dexs/contango/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@ import { SimpleAdapter } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getBlock } from "../../helpers/getBlock";
import { getPrices } from "../../utils/prices";
import { Chain } from "@defillama/sdk/build/general";

type IEndpoint = {
[chain: string]: string;
}

const endpoint: IEndpoint = {
[CHAIN.ARBITRUM]: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-arbitrum",
[CHAIN.OPTIMISM]: "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-optimism",
}

const url = "https://api.thegraph.com/subgraphs/name/contango-xyz/v2-arbitrum";
interface IAssetTotals {
id: string;
symbol: string;
Expand All @@ -22,79 +31,85 @@ interface IAsset {
openInterest: string;
fees: string;
}
const fetchVolume = async (timestamp: number) => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp
const toBlock = (await getBlock(toTimestamp, CHAIN.ARBITRUM, {}));
const fromBlock = (await getBlock(fromTimestamp, CHAIN.ARBITRUM, {}));
const query = `
{
today:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${toBlock}}) {
id
symbol
totalVolume
openInterest
totalFees
},
yesterday:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${fromBlock}}) {
id
symbol
totalVolume
openInterest
totalFees
const fetchVolume = (chain: Chain) => {
return async (timestamp: number) => {
const fromTimestamp = timestamp - 60 * 60 * 24
const toTimestamp = timestamp
const toBlock = (await getBlock(toTimestamp, chain, {}));
const fromBlock = (await getBlock(fromTimestamp, chain, {}));
const query = `
{
today:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${toBlock}}) {
id
symbol
totalVolume
openInterest
totalFees
},
yesterday:assetTotals(where: {totalVolume_not: "0"}, block: {number: ${fromBlock}}) {
id
symbol
totalVolume
openInterest
totalFees
}
}
}
`;
const response: IResponse = (await request(url, query));
const data: IAsset[] = response.today.map((asset) => {
const yesterday = response.yesterday.find((e: IAssetTotals) => e.id === asset.id);
const totalVolume = Number(asset.totalVolume) - Number(yesterday?.totalVolume || 0);
const totalFees = Number(asset.totalFees) - Number(yesterday?.totalFees || 0);
const openInterest = Math.abs(Number(asset.openInterest));
return {
id: asset.id,
openInterest: openInterest ? `${openInterest}` : 0,
fees: totalFees ? `${totalFees}` : 0,
volume: totalVolume ? `${totalVolume}` : 0,
} as IAsset
})
const coins = data.map((e: IAsset) => `${CHAIN.ARBITRUM}:${e.id}`);
const prices = await getPrices(coins, timestamp);
const dailyVolume = data.reduce((acc, { volume, id }) => {
const price = prices[`${CHAIN.ARBITRUM}:${id}`]?.price || 0;
return acc + Number(volume) * price;
}, 0);
const dailyFees = data.reduce((acc, { fees, id }) => {
const price = prices[`${CHAIN.ARBITRUM}:${id}`]?.price || 0;
return acc + Number(fees) * price;
},0);
const dailyOpenInterest = data.reduce((acc, { openInterest, id }) => {
const price = prices[`${CHAIN.ARBITRUM}:${id}`]?.price || 0;
return acc + Number(openInterest) * price;
},0);
const totalFees = response.today.reduce((acc , { totalFees, id }) => {
const price = prices[`${CHAIN.ARBITRUM}:${id}`]?.price || 0;
return acc + Number(totalFees) * price;
}, 0);
const totalVolume = response.today.reduce((acc , { totalVolume, id }) => {
const price = prices[`${CHAIN.ARBITRUM}:${id}`]?.price || 0;
return acc + Number(totalVolume) * price;
}, 0);
`;
const response: IResponse = (await request(endpoint[chain], query));
const data: IAsset[] = response.today.map((asset) => {
const yesterday = response.yesterday.find((e: IAssetTotals) => e.id === asset.id);
const totalVolume = Number(asset.totalVolume) - Number(yesterday?.totalVolume || 0);
const totalFees = Number(asset.totalFees) - Number(yesterday?.totalFees || 0);
const openInterest = Math.abs(Number(asset.openInterest));
return {
id: asset.id,
openInterest: openInterest ? `${openInterest}` : 0,
fees: totalFees ? `${totalFees}` : 0,
volume: totalVolume ? `${totalVolume}` : 0,
} as IAsset
})
const coins = data.map((e: IAsset) => `${chain}:${e.id}`);
const prices = await getPrices(coins, timestamp);
const dailyVolume = data.reduce((acc, { volume, id }) => {
const price = prices[`${chain}:${id}`]?.price || 0;
return acc + Number(volume) * price;
}, 0);
const dailyFees = data.reduce((acc, { fees, id }) => {
const price = prices[`${chain}:${id}`]?.price || 0;
return acc + Number(fees) * price;
},0);
const dailyOpenInterest = data.reduce((acc, { openInterest, id }) => {
const price = prices[`${chain}:${id}`]?.price || 0;
return acc + Number(openInterest) * price;
},0);
const totalFees = response.today.reduce((acc , { totalFees, id }) => {
const price = prices[`${chain}:${id}`]?.price || 0;
return acc + Number(totalFees) * price;
}, 0);
const totalVolume = response.today.reduce((acc , { totalVolume, id }) => {
const price = prices[`${chain}:${id}`]?.price || 0;
return acc + Number(totalVolume) * price;
}, 0);

return {
dailyOpenInterest: dailyOpenInterest ? `${dailyOpenInterest}` : undefined,
dailyFees: dailyFees ? `${dailyFees}` : undefined,
dailyVolume: dailyVolume ? `${dailyVolume}` : undefined,
totalFees: totalFees ? `${totalFees}` : undefined,
totalVolume: totalVolume ? `${totalVolume}` : undefined,
timestamp
};
return {
dailyOpenInterest: dailyOpenInterest ? `${dailyOpenInterest}` : undefined,
dailyFees: `${dailyFees}`,
dailyVolume: `${dailyVolume}`,
totalFees: totalFees ? `${totalFees}` : undefined,
totalVolume: totalVolume ? `${totalVolume}` : undefined,
timestamp
};
}
}

const adapter: SimpleAdapter = {
adapter: {
[CHAIN.ARBITRUM]: {
fetch: fetchVolume,
fetch: fetchVolume(CHAIN.ARBITRUM),
start: async () => 1696291200,
},
[CHAIN.OPTIMISM]: {
fetch: fetchVolume(CHAIN.OPTIMISM),
start: async () => 1696291200,
},
}
Expand Down

0 comments on commit d83964b

Please sign in to comment.