|
| 1 | +import { FetchOptions, SimpleAdapter, ProtocolType } from "../adapters/types"; |
| 2 | +import { CHAIN } from "../helpers/chains"; |
| 3 | +import { httpGet } from "../utils/fetchURL"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Fetches Algorand blockchain transaction fees using AlgoNode's public API |
| 7 | + * and aggregates them for the given time period |
| 8 | + */ |
| 9 | +const fetch = async (options: FetchOptions) => { |
| 10 | + const dailyFees = options.createBalances(); |
| 11 | + |
| 12 | + // Convert to ISO format for the API |
| 13 | + const startDate = new Date(options.startTimestamp * 1000).toISOString(); |
| 14 | + const endDate = new Date(options.endTimestamp * 1000).toISOString(); |
| 15 | + |
| 16 | + let totalFees = 0; |
| 17 | + let nextToken: string | undefined = undefined; |
| 18 | + let requestCount = 0; |
| 19 | + const MAX_REQUESTS = 100; // Safety limit to prevent infinite loops |
| 20 | + |
| 21 | + // Query transactions from AlgoNode public indexer (more reliable) |
| 22 | + do { |
| 23 | + let url = `https://mainnet-idx.algonode.cloud/v2/transactions?after-time=${startDate}&before-time=${endDate}&limit=1000`; |
| 24 | + if (nextToken) { |
| 25 | + url += `&next=${nextToken}`; |
| 26 | + } |
| 27 | + |
| 28 | + const response = await httpGet(url); |
| 29 | + const txns = response.transactions || []; |
| 30 | + |
| 31 | + // Sum all transaction fees (fees are in microAlgos) |
| 32 | + for (const txn of txns) { |
| 33 | + if (txn.fee && typeof txn.fee === 'number') { |
| 34 | + totalFees += txn.fee; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + nextToken = response['next-token']; |
| 39 | + requestCount++; |
| 40 | + |
| 41 | + // Safety checks |
| 42 | + if (!nextToken || txns.length === 0 || requestCount >= MAX_REQUESTS) { |
| 43 | + break; |
| 44 | + } |
| 45 | + } while (nextToken); |
| 46 | + |
| 47 | + // Convert from microAlgos to ALGO (1 ALGO = 1,000,000 microAlgos) |
| 48 | + dailyFees.addCGToken('algorand', totalFees / 1e6); |
| 49 | + |
| 50 | + return { |
| 51 | + dailyFees, |
| 52 | + dailyRevenue: dailyFees, |
| 53 | + dailyHoldersRevenue: dailyFees, // All transaction fees on Algorand are burned |
| 54 | + }; |
| 55 | +}; |
| 56 | + |
| 57 | +const methodology = { |
| 58 | + Fees: "All transaction fees paid by users on the Algorand blockchain", |
| 59 | + Revenue: "All transaction fees on Algorand are burned, effectively benefiting all ALGO holders through reduced supply", |
| 60 | + HoldersRevenue: "All transaction fees on Algorand are burned, effectively benefiting all ALGO holders through reduced supply" |
| 61 | +}; |
| 62 | + |
| 63 | +const adapter: SimpleAdapter = { |
| 64 | + version: 2, |
| 65 | + adapter: { |
| 66 | + [CHAIN.ALGORAND]: { |
| 67 | + fetch, |
| 68 | + start: '2019-06-11', // Algorand mainnet launch date |
| 69 | + }, |
| 70 | + }, |
| 71 | + protocolType: ProtocolType.CHAIN, |
| 72 | + methodology, |
| 73 | +}; |
| 74 | + |
| 75 | +export default adapter; |
0 commit comments