Skip to content

Commit 1fdf896

Browse files
authored
velora holder revenue and prefetch to optimize api call (DefiLlama#3427)
1 parent 4faf679 commit 1fdf896

File tree

1 file changed

+65
-69
lines changed

1 file changed

+65
-69
lines changed

aggregators/paraswap/paraswapApi.ts

Lines changed: 65 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ import { CHAIN } from "../../helpers/chains";
33
import { getTimestampAtStartOfDayUTC } from "../../utils/date";
44
import { Chain } from "../../adapters/types";
55
import { httpGet } from "../../utils/fetchURL";
6+
import { FetchOptions } from "../../adapters/types";
67

8+
interface IResponse {
9+
daily: any[];
10+
allTime: any;
11+
}
712

813
const feesMMURL = "https://api.paraswap.io/stk/volume-stats/breakdown-by-chain";
9-
type TChainId = {
10-
[l: string | Chain]: string;
11-
}
12-
const mapChainId: TChainId = {
14+
15+
const mapChainId: Record<string, string> = {
1316
[CHAIN.ETHEREUM]: '1',
1417
[CHAIN.OPTIMISM]: '10',
1518
[CHAIN.BSC]: '56',
@@ -21,88 +24,81 @@ const mapChainId: TChainId = {
2124
[CHAIN.AVAX]: '43114',
2225
}
2326

24-
type IRequest = {
25-
[key: string]: Promise<any>;
27+
const prefetch = async (_: any) => {
28+
const headers: any = {
29+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
30+
"accept-language": "en-US,en;q=0.9",
31+
"cache-control": "max-age=0",
32+
"priority": "u=0, i",
33+
"upgrade-insecure-requests": "1",
34+
"referrerPolicy": "strict-origin-when-cross-origin",
35+
};
36+
return await httpGet(feesMMURL, {headers});
2637
}
27-
const requests: IRequest = {}
2838

29-
const fetchCacheURL = (url: string) => {
30-
const key = url;
31-
if (!requests[key]) {
32-
const headers: any = {
33-
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
34-
"accept-language": "en-US,en;q=0.9",
35-
"cache-control": "max-age=0",
36-
"priority": "u=0, i",
37-
"upgrade-insecure-requests": "1",
38-
"referrerPolicy": "strict-origin-when-cross-origin",
39-
};
40-
requests[key] = httpGet(url, {headers});
41-
}
42-
return requests[key];
43-
}
39+
const fetchFees = (chain: Chain) => {
40+
return async (timestamp: number, _: any, options: FetchOptions): Promise<FetchResultFees> => {
41+
if (chain == CHAIN.FANTOM && timestamp > 1744416000) return {} as FetchResultFees; // fantom delisted at 2025-04-12
42+
const timestampToday = getTimestampAtStartOfDayUTC(timestamp)
43+
const response: IResponse = options.preFetchedResults || [];
44+
const dailyResultFees: any[] = response.daily;
4445

45-
interface IResponse {
46-
daily: any[];
47-
allTime: any;
46+
const [dailyVolume, partnerRev, protocolRev]: number[] = dailyResultFees.filter(([time]: any) => time === timestampToday)
47+
.map(([_, data]: any) => data[mapChainId[chain]]).flat()
48+
49+
const dailyFees = partnerRev || 0 + protocolRev || 0;
50+
const dailyRevenue = (protocolRev || 0);
51+
const holdersRevenue = dailyRevenue * 0.8; // 80% staking rewards
52+
const protocolRevenue = dailyRevenue * 0.2; // 20% protocol revenue
53+
54+
return {
55+
dailyFees : dailyFees || 0,
56+
dailyUserFees: dailyFees || 0,
57+
dailyRevenue : dailyRevenue || 0,
58+
dailyProtocolRevenue : protocolRevenue || 0,
59+
dailyHoldersRevenue: holdersRevenue || 0,
60+
}
61+
}
4862
}
4963

50-
export function getParaswapAdapter(type:"fees"|"volume"){
51-
const fetch = (chain: Chain) => {
52-
return async (timestamp: number): Promise<FetchResultFees|FetchResultVolume> => {
53-
if (chain == CHAIN.FANTOM && timestamp > 1744416000) return {} as FetchResultFees; // fantom delisted at 2025-04-12
64+
const fetchVolume = (chain: Chain) => {
65+
return async (timestamp: number, _: any, options: FetchOptions): Promise<FetchResultVolume> => {
66+
if (chain == CHAIN.FANTOM && timestamp > 1744416000) return {} as FetchResultVolume; // fantom delisted at 2025-04-12
5467
const timestampToday = getTimestampAtStartOfDayUTC(timestamp)
55-
const response: IResponse = (await fetchCacheURL(feesMMURL));
68+
const response: IResponse = options.preFetchedResults || [];
5669
const dailyResultFees: any[] = response.daily;
57-
const [totalVolume,totalPartnerRevenue, totalProtocolRevenue]: number[] = response.allTime[mapChainId[chain]];
58-
const [dailyVolume, partnerRevenue, protocolRevenue]: number[] = dailyResultFees.filter(([time]: any) => time === timestampToday)
70+
71+
const [dailyVolume, partnerRev, protocolRev]: number[] = dailyResultFees.filter(([time]: any) => time === timestampToday)
5972
.map(([_, data]: any) => data[mapChainId[chain]]).flat()
60-
const otherFees = partnerRevenue || 0 + protocolRevenue || 0;
61-
const otherProtocolReveune = protocolRevenue || 0;
6273

63-
const dailyFees = otherFees;
64-
if (dailyFees > 1_000_000) {
65-
return {} as FetchResultFees;
66-
}
67-
const dailyRevenue = otherProtocolReveune;
68-
const totalFees = totalPartnerRevenue + totalProtocolRevenue;
69-
const totalRevenue = totalProtocolRevenue;
70-
if(type === "fees"){
71-
return {
72-
dailyFees : dailyFees || 0,
73-
dailyRevenue : dailyRevenue || 0,
74-
dailyProtocolRevenue : dailyRevenue || 0,
75-
totalRevenue: totalRevenue,
76-
totalFees: totalFees,
77-
timestamp
78-
}
79-
} else {
80-
return {
81-
dailyVolume: dailyVolume || 0,
82-
totalVolume: totalVolume,
83-
timestamp
84-
}
74+
return {
75+
dailyVolume: dailyVolume || 0
8576
}
8677
}
8778
}
8879

89-
const adapter: Adapter = {
90-
version : 1,
91-
adapter: Object.keys(mapChainId).reduce((all, chain)=>({
92-
...all,
93-
[chain]:{
94-
fetch: fetch(chain),
80+
const createAdapter = (fetchFunction: (chain: Chain) => any): Adapter => ({
81+
version: 1,
82+
adapter: Object.fromEntries(
83+
Object.keys(mapChainId).map(chain => [
84+
chain,
85+
{
86+
fetch: fetchFunction(chain),
9587
start: '2022-03-22',
9688
meta: {
9789
methodology: {
98-
Fees: "All trading fees paid by users.",
99-
Revenue: "Trading fees are collected by Velora protocol.",
100-
ProtocolRevenue: "Trading fees are collected by Velora protocol.",
90+
Fees: "All trading fees paid by users",
91+
Revenue: "Trading fees collected by Velora protocol",
92+
ProtocolRevenue: "20% of revenue to Velora protocol",
93+
HoldersRevenue: "80% of revenue to stakers as part of PSP 2.0 staking rewards",
10194
}
10295
}
103-
}
104-
}), {} as any)
105-
}
96+
}
97+
])
98+
),
99+
prefetch,
100+
})
106101

107-
return adapter
102+
export function getParaswapAdapter(type: "fees" | "volume"): Adapter {
103+
return createAdapter(type === "fees" ? fetchFees : fetchVolume);
108104
}

0 commit comments

Comments
 (0)