Skip to content

Commit 3ce1eef

Browse files
Add SundaeSwap fee adapter (DefiLlama#6001)
* feat: add SundaeSwap fees adapter * refactor --------- Co-authored-by: treeoflife2 <sol.analyzoor@gmail.com>
1 parent 6b74832 commit 3ce1eef

File tree

2 files changed

+119
-21
lines changed

2 files changed

+119
-21
lines changed

dexs/sundaeswap/index.ts

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import fetchURL from "../../utils/fetchURL";
2-
import { FetchOptions, FetchResult, FetchResultV2, FetchResultVolume, SimpleAdapter } from "../../adapters/types";
2+
import { FetchOptions, FetchResult, SimpleAdapter } from "../../adapters/types";
33
import { CHAIN } from "../../helpers/chains";
4-
import { getTimestampAtStartOfDayUTC } from "../../utils/date";
54

65
const historicalVolumeEndpoint = "https://stats.sundaeswap.finance/api/defillama/v0/global-stats/2100"
76

@@ -10,32 +9,22 @@ interface IVolumeall {
109
day: string;
1110
}
1211

13-
const fetch = async (_,_a:any,{ createBalances, startOfDay }: FetchOptions): Promise<FetchResult> => {
14-
const dailyVolume = createBalances()
15-
const dayTimestamp = getTimestampAtStartOfDayUTC(startOfDay);
16-
const dateStr = new Date(dayTimestamp * 1000).toISOString().split('T')[0];
12+
const fetch = async (_a: any, _b: any, options: FetchOptions): Promise<FetchResult> => {
13+
const dailyVolume = options.createBalances()
14+
const dateStr = new Date(options.startOfDay * 1000).toISOString().split('T')[0];
1715
const historicalVolume: IVolumeall[] = (await fetchURL(historicalVolumeEndpoint)).response;
1816
const volume = historicalVolume.find(dayItem => dayItem.day === dateStr)?.volumeLovelace as any
19-
if (!volume) {
20-
return {
21-
timestamp: dayTimestamp,
22-
}
23-
}
17+
if (!volume) return { dailyVolume };
2418
dailyVolume.addGasToken(volume)
25-
return {
26-
timestamp: dayTimestamp,
27-
dailyVolume,
28-
};
19+
20+
return { dailyVolume };
2921
};
3022

3123
const adapter: SimpleAdapter = {
3224
version: 1,
33-
adapter: {
34-
[CHAIN.CARDANO]: {
35-
fetch,
36-
start: '2022-02-01',
37-
},
38-
},
25+
chains: [CHAIN.CARDANO],
26+
fetch,
27+
start: '2022-02-01',
3928
};
4029

4130
export default adapter;

fees/sundaeswap/index.ts

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import { request } from "graphql-request";
2+
import { Adapter, FetchOptions, FetchResult } from "../../adapters/types";
3+
import { CHAIN } from "../../helpers/chains";
4+
import { METRIC } from "../../helpers/metrics";
5+
6+
const ADA_ID = "ada.lovelace";
7+
const endpoint = "https://api.sundae.fi/graphql";
8+
9+
const formatDate = (ts: number) => {
10+
return new Date(ts * 1000).toISOString().replace('T', ' ').substring(0, 19);
11+
};
12+
13+
const formatAsset = (assetId: string) => {
14+
const [policy, name] = assetId.split(".");
15+
return name ? policy + name : assetId;
16+
};
17+
18+
const addFee = (
19+
balanceObj: any,
20+
assetId: string,
21+
quantity: string,
22+
) => {
23+
if (!assetId) return;
24+
25+
if (assetId === ADA_ID) {
26+
const amount = Number(quantity) / 1e6;
27+
balanceObj.addCGToken("cardano", amount);
28+
} else {
29+
balanceObj.addToken(formatAsset(assetId), Number(quantity));
30+
}
31+
};
32+
33+
const fetch = async (options: FetchOptions): Promise<FetchResult> => {
34+
const dailyFees = options.createBalances();
35+
const dailyRevenue = options.createBalances();
36+
const dailySupplySideRevenue = options.createBalances();
37+
38+
const start = formatDate(options.startTimestamp);
39+
const end = formatDate(options.endTimestamp);
40+
41+
const query = `
42+
query fetchPools($start: String!, $end: String!) {
43+
pools {
44+
popular {
45+
ticks(start: $start, end: $end, interval: All) {
46+
rich {
47+
protocolFees { quantity asset { id } }
48+
lpFees(unit: Natural) { quantity asset { id } }
49+
}
50+
}
51+
}
52+
}
53+
}
54+
`;
55+
56+
const data = await request(endpoint, query, { start, end });
57+
const pools = data?.pools?.popular ?? [];
58+
59+
for (const pool of pools) {
60+
const richEntries = pool?.ticks?.rich ?? [];
61+
62+
for (const entry of richEntries) {
63+
const { lpFees, protocolFees } = entry;
64+
65+
if (lpFees?.asset?.id) {
66+
addFee(dailySupplySideRevenue, lpFees.asset.id, lpFees.quantity);
67+
}
68+
69+
if (protocolFees?.asset?.id) {
70+
addFee(dailyRevenue, protocolFees.asset.id, protocolFees.quantity);
71+
}
72+
}
73+
}
74+
75+
dailyFees.addBalances(dailySupplySideRevenue, METRIC.LP_FEES);
76+
dailyFees.addBalances(dailyRevenue, METRIC.PROTOCOL_FEES);
77+
78+
return {
79+
dailyFees,
80+
dailyRevenue,
81+
dailySupplySideRevenue,
82+
dailyProtocolRevenue: dailyRevenue
83+
};
84+
};
85+
86+
const methodology = {
87+
Fees: "The total trading fees paid by users, excluding L1 transaction fees",
88+
Revenue: "A fixed ADA cost per transaction that is collected by the protocol",
89+
SupplySideRevenue: "A percentage cut on all trading volume, paid to Liquidity Providers",
90+
ProtocolRevenue: "A fixed ADA cost per transaction that is collected by the protocol",
91+
};
92+
93+
const breakdownMethodology = {
94+
Fees: {
95+
[METRIC.LP_FEES]: "A percentage cut on all trading volume, paid to Liquidity Providers",
96+
[METRIC.PROTOCOL_FEES]: "A fixed ADA cost per transaction that is collected by the protocol"
97+
}
98+
};
99+
100+
const adapter: Adapter = {
101+
version: 2,
102+
chains: [CHAIN.CARDANO],
103+
fetch,
104+
start: "2022-01-20",
105+
methodology,
106+
breakdownMethodology,
107+
};
108+
109+
export default adapter;

0 commit comments

Comments
 (0)