Skip to content

Commit

Permalink
Merge branch 'DefiLlama:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonpac authored Sep 2, 2024
2 parents 11f34bc + b192db9 commit 2e8cf36
Show file tree
Hide file tree
Showing 146 changed files with 4,797 additions and 782 deletions.
13 changes: 11 additions & 2 deletions adapters/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ export type IStartTimestamp = () => Promise<number>
export type BaseAdapter = {
[chain: string]: {
start: IStartTimestamp | number
fetch: Fetch|FetchV2;
fetch: Fetch | FetchV2;
runAtCurrTime?: boolean;
customBackfill?: Fetch|FetchV2;
customBackfill?: Fetch | FetchV2;
meta?: {
methodology?: string | IJSON<string>
hallmarks?: [number, string][]
Expand Down Expand Up @@ -177,6 +177,15 @@ export enum AdapterType {

export type FetchResult = FetchResultVolume & FetchResultFees & FetchResultAggregators & FetchResultOptions & FetchResultIncentives

export const whitelistedDimensionKeys = new Set([
'startTimestamp', 'chain', 'timestamp','block',

'dailyVolume', 'totalVolume', 'dailyShortOpenInterest', 'dailyLongOpenInterest', 'dailyOpenInterest', 'dailyBridgeVolume', 'totalBridgeVolume',
'totalFees', 'dailyFees', 'dailyUserFees', 'totalRevenue', 'dailyRevenue', 'dailyProtocolRevenue', 'dailyHoldersRevenue', 'dailySupplySideRevenue', 'totalProtocolRevenue', 'totalSupplySideRevenue', 'totalUserFees', 'dailyBribesRevenue', 'dailyTokenTaxes', 'totalHoldersRevenue',
'tokenIncentives',
'totalPremiumVolume', 'totalNotionalVolume', 'dailyPremiumVolume', 'dailyNotionalVolume',
])

// End of specific adaptors type

export interface IJSON<T> {
Expand Down
9 changes: 7 additions & 2 deletions adapters/utils/runAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function getUnixTimeNow() {
}

export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurrentDayTimestamp: number, chainBlocks: ChainBlocks, id?: string, version?: string, {
adapterVersion = 1
adapterVersion = 1,
isTest = false,
}: any = {}) {

const closeToCurrentTime = Math.trunc(Date.now() / 1000) - cleanCurrentDayTimestamp < 24 * 60 * 60 // 12 hours
Expand All @@ -23,7 +24,11 @@ export default async function runAdapter(volumeAdapter: BaseAdapter, cleanCurren
}
await Promise.all(chains.map(setChainValidStart))

const response = await Promise.all(chains.filter(chain => validStart[chain]?.canRun).map(getChainResult))
const response = await Promise.all(chains.filter(chain => {
const res = validStart[chain]?.canRun
if (isTest && !res) console.log(`Skipping ${chain} because the configured start time is ${new Date(validStart[chain]?.startTimestamp * 1e3).toUTCString()} \n\n`)
return validStart[chain]?.canRun
}).map(getChainResult))
return response

async function getChainResult(chain: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import * as sdk from "@defillama/sdk";
import { SimpleAdapter, FetchResultVolume } from "../../../adapters/types";
import { CHAIN } from "../../../helpers/chains";
import { getTimestampAtStartOfDayUTC } from "../../../utils/date";
import { FetchResultVolume } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";
import { getTimestampAtStartOfDayUTC } from "../../utils/date";
import { Chain } from "@defillama/sdk/build/general";
import request, { gql } from "graphql-request";
import customBackfill from "../../helpers/customBackfill";

interface IReferralRecord {
volume: string; // Assuming volume is a string that represents a number
Expand Down Expand Up @@ -34,86 +35,25 @@ const fetchReferralVolume = async (timestamp: number): Promise<number> => {

const referralQuery = gql`
{
referrerStats(
where: {referrer: "0x8c128f336b479b142429a5f351af225457a987fa", timestamp_gt: "${todaysTimestamp}"}
affiliateStats(
where: {affiliate: "0x8c128f336b479b142429a5f351af225457a987fa", timestamp_gt: "${todaysTimestamp}"}
) {
volume
}
}
`;

const referralEndpoint = sdk.graph.modifyEndpoint('Hww5kAfumpAbMm5iGWqEe83vJJCEE98kccmBBcMRy5fU');
const referralEndpoint = "https://subgraph.satsuma-prod.com/3b2ced13c8d9/gmx/gmx-arbitrum-referrals/api";
const referralRes = await request(referralEndpoint, referralQuery);

// If there's no volume data, return 0
if (!referralRes.referrerStats || referralRes.referrerStats.length === 0) {
if (!referralRes.affiliateStats || referralRes.affiliateStats.length === 0) {
return 0;
}

return Number(referralRes.referrerStats[0].volume) / 10 ** 30;
return Number(referralRes.affiliateStats[0].volume) / 10 ** 30;
};


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

const referralQuery = gql`
query MyQuery($timestamp_gte: BigInt = "", $timestamp_lte: BigInt = "") {
referralRecords(
where: {
referralCode: "0x556e694465780000000000000000000000000000000000000000000000000000",
timestamp_gte: $timestamp_gte,
timestamp_lte: $timestamp_lte
}
) {
volume
timestamp
}
}
`;

const variables = {
timestamp_gte: startOfDayTimestamp.toString(),
timestamp_lte: endOfDayTimestamp.toString()
};

let referralEndpoint = '';

switch (chain) {
case CHAIN.ARBITRUM:
referralEndpoint = sdk.graph.modifyEndpoint('GbsdbMy5X2xHoj8qrRKKTs3LhMgma3CzZ8nZCqo9T97v');
break;
case CHAIN.OPTIMISM:
referralEndpoint = sdk.graph.modifyEndpoint('7CmYmJd9mghA17EP8NXqrLZPqT3vjw4B8PLAbA1K4PdJ');
break;
case CHAIN.FANTOM:
referralEndpoint = sdk.graph.modifyEndpoint('2KNaZgvAu9zjn1oAomgoMgiafQHNBbsS3Eu4UwucPUC6');
break;
default:
return 0; // Return 0 for unsupported chains
}

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

// Sum up the volumes
let totalVolume = 0;

if (referralRes.referralRecords && Array.isArray(referralRes.referralRecords)) {
referralRes.referralRecords.forEach((record: IReferralRecord) => {
const volume = parseFloat(record.volume);
if (!isNaN(volume)) {
totalVolume += volume / 10 ** 18; // Adjust the unit conversion as needed
}
});
}

return totalVolume;
};




const fetch = (chain: Chain) => {
return async (timestamp: number): Promise<FetchResultVolume> => {
const todaysTimestamp = getTimestampAtStartOfDayUTC(timestamp);
Expand Down Expand Up @@ -145,10 +85,9 @@ const fetch = (chain: Chain) => {
const chainID = chainIDs[chain];
let dailyVolumeUSD = chainID !== undefined ? volumeByChain[chainID] || 0 : 0;

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

return {
Expand Down Expand Up @@ -183,6 +122,7 @@ const adapteraggderivative: any = {
[CHAIN.ARBITRUM]: {
fetch: fetch(CHAIN.ARBITRUM),
start: 1687422746,
customBackfill: customBackfill(CHAIN.ARBITRUM, fetch),
meta: {
methodology,
},
Expand Down Expand Up @@ -218,6 +158,4 @@ const adapteraggderivative: any = {
}
};

export {
adapteraggderivative
}
export default adapteraggderivative;
91 changes: 73 additions & 18 deletions aggregator-derivatives/vooi/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,88 @@
import fetchURL from "../../utils/fetchURL";
import { FetchResult } from "../../adapters/types";
import { FetchResult, SimpleAdapter, FetchOptions } from "../../adapters/types";
import { CHAIN } from "../../helpers/chains";

const URL = "https://defilama-stats.vooi.workers.dev/";
const endpoint = "";
const startTimestamp = 1714608000; // 02.05.2024
const URL = "https://vooi-rebates.fly.dev/";
const endpoint = "defillama/volumes";
const startTimestampArbitrum = 1714608000; // 02.05.2024
const startTimestampBlast = 1719792000; // 01.07.2024
const startTimestampOpBNB = 1717200000; // 01.06.2024
const startTimestampBase = 1722470400; // 01.08.2024


interface IAPIResponse {
dailyVolume: string;
totalVolume: string;
}
const fetch = async (timestamp: number): Promise<FetchResult> => {
const { dailyVolume, totalVolume }: IAPIResponse = (
(await fetchURL(`${URL}${endpoint}?ts=${timestamp}`)).data
);
const fetchArbitrum = async (options: FetchOptions): Promise<FetchResult> => {
const timestamp = options.toTimestamp
const fetchData = await fetchURL(`${URL}${endpoint}?ts=${timestamp}`) // returns data for the day before
let orderlyItem = fetchData.find(((item) => item.protocol == "orderly"))
if (!orderlyItem) {
orderlyItem = {dailyVolume: 0, totalVolume: 0}
}
let synfuturesItem = fetchData.filter(((item) => item.protocol == "synfutures"))
if (!synfuturesItem) {
synfuturesItem = {dailyVolume: 0, totalVolume: 0}
}
let kiloexItem = fetchData.find(((item) => item.protocol == "kiloex"))
if (!kiloexItem) {
kiloexItem = {dailyVolume: 0, totalVolume: 0}
}
let dailyVolume = Number(orderlyItem.dailyVolume) + Number(kiloexItem.dailyVolume)
let totalVolume = Number(orderlyItem.totalVolume) + Number(kiloexItem.totalVolume)
for (let i in synfuturesItem){
dailyVolume = Number(dailyVolume) + Number(synfuturesItem[i].dailyVolume)
totalVolume = Number(totalVolume) + Number(synfuturesItem[i].totalVolume)
}
return {
timestamp,
dailyVolume,
totalVolume,
timestamp
};
};


const fetchOpBNB = async (options: any): Promise<FetchResult> => {
const timestamp = options.toTimestamp
return {
dailyVolume: 0,
totalVolume: 0,
timestamp
};
};

const fetchBlast = async (options: any): Promise<FetchResult> => {
const timestamp = options.toTimestamp
return {
dailyVolume: 0,
totalVolume: 0,
timestamp
};
};

export default {
const fetchBase = async (options: any): Promise<FetchResult> => {
const timestamp = options.toTimestamp
return {
dailyVolume: 0,
totalVolume: 0,
timestamp
};
};

const adapter: SimpleAdapter = {
adapter: {
[CHAIN.ARBITRUM]: {
fetch: fetch,
start: startTimestamp
fetch: fetchArbitrum,
start: startTimestampArbitrum
},
[CHAIN.OP_BNB]: {
fetch: fetchOpBNB,
start: startTimestampOpBNB
},
[CHAIN.BLAST]: {
fetch: fetchBlast,
start: startTimestampBlast
},
[CHAIN.BASE]: {
fetch: fetchBase,
start: startTimestampBase
},
},
// version: 2 // data accepts only one input to timestamp
}
export default adapter
3 changes: 2 additions & 1 deletion aggregators/akka/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ const fetch = async (timestamp: number): Promise<FetchResult> => {
}

const adapter: SimpleAdapter = {
version: 2,
version: 1,
adapter: {
[CHAIN.CORE]: {
fetch,
runAtCurrTime: true,
start: startTimestamp,
},
},
Expand Down
23 changes: 23 additions & 0 deletions aggregators/cetus-aggregator/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CHAIN } from "../../helpers/chains";
import { httpGet } from "../../utils/fetchURL";
import { FetchOptions } from "../../adapters/types";

const fetchVolume = async (options: FetchOptions) => {
const url = `https://api-sui.cetus.zone/v2/sui/aggregator_vol?startTimestamp=${options.startOfDay}&endTimestamp=${options.startOfDay}`;
const res = await httpGet(url);
return {
dailyVolume: res.data.list[0].totalUSD,
}
};

const adapter_agge: any = {
version: 2,
adapter: {
[CHAIN.SUI]: {
fetch: fetchVolume,
start: 1721260800,
},
},
};

export default adapter_agge;
Loading

0 comments on commit 2e8cf36

Please sign in to comment.