|
| 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