Skip to content

Commit

Permalink
Merge pull request #738 from DefiLlama/vertex-protocol-fees
Browse files Browse the repository at this point in the history
add vertex-protocol
  • Loading branch information
dtmkeng authored Aug 13, 2023
2 parents 7984263 + b4594f9 commit a5abd85
Showing 1 changed file with 114 additions and 0 deletions.
114 changes: 114 additions & 0 deletions fees/vertex-protocol.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import axios from 'axios';
import { CHAIN } from '../helpers/chains';
import { Adapter, FetchResultFees } from '../adapters/types';

interface MarketSnapshots {
interval: {
count: number;
granularity: number;
max_time: number;
};
}

interface QueryBody {
market_snapshots: MarketSnapshots;
}
interface IData {
[s: string]: string;
}

interface Snapshot {
[s: string]: IData;
}

interface Response {
snapshots: Snapshot[];
}

const query = async (max_time: number): Promise<Response> => {
const body: QueryBody = {
market_snapshots: {
interval: {
count: 2,
granularity: 86400,
max_time: max_time,
},
},
};

const url = 'https://prod.vertexprotocol-backend.com';
const response = await axios.post(url + '/indexer', body);
return response.data;
};

const sumAllProductStats = (stat_map: IData): number => {
let stat_sum = 0;
for (const v of Object.values(stat_map)) {
stat_sum += parseInt(v);
}
return stat_sum / 1e18;
};

const get24hrStat = async (field: string, max_time: number): Promise<number> => {
const response = await query(max_time);
const cur_res: Snapshot = response.snapshots[0];
const past_res: Snapshot = response.snapshots[1];
return sumAllProductStats(cur_res[field]) - sumAllProductStats(past_res[field]);
};

const getCumulativeStat = async (field: string, max_time: number): Promise<number> => {
const response = await query(max_time);
const cur_res = response.snapshots[0];
return sumAllProductStats(cur_res[field]);
};

const getCumulativeFees = async (max_time: number): Promise<number> => {
const fees = await getCumulativeStat('cumulative_taker_fees', max_time);
const sequencer_fees = await getCumulativeStat('cumulative_sequencer_fees', max_time);
return fees - sequencer_fees;
};

const getCumulativeRevenue = async (max_time: number): Promise<number> => {
const fees = await getCumulativeFees(max_time);
const rebates = await getCumulativeStat('cumulative_maker_fees', max_time);
return fees + rebates;
};

const get24hrFees = async (max_time: number): Promise<number> => {
const fees = await get24hrStat('cumulative_taker_fees', max_time);
const sequencer_fees = await get24hrStat('cumulative_sequencer_fees', max_time);
return fees - sequencer_fees;
};

const get24hrRevenue = async (max_time: number): Promise<number> => {
const fees = await get24hrFees(max_time);
const rebates = await get24hrStat('cumulative_maker_fees', max_time);
return fees + rebates;
};


const fetch = async (timestamp: number): Promise<FetchResultFees> => {
const dailyFees = await get24hrFees(timestamp);
const dailyRevenue = await get24hrRevenue(timestamp);
const totalFees = await getCumulativeFees(timestamp);
const totalRev = await getCumulativeRevenue(timestamp);
return {
dailyFees: `${dailyFees}`,
dailyRevenue: `${dailyRevenue}`,
totalRevenue: `${totalRev}`,
totalFees: `${totalFees}`,
timestamp
}
}

const adapter: Adapter = {
adapter: {
[CHAIN.ARBITRUM]: {
fetch: fetch,
runAtCurrTime: true,
start: async () => 1682514000,
},
}
}

export default adapter;

0 comments on commit a5abd85

Please sign in to comment.