/
index.ts
79 lines (69 loc) · 2.44 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { FetchOptions, SimpleAdapter } from "../../adapters/types";
import fetchURL from "../../utils/fetchURL";
import { CHAIN } from "../../helpers/chains";
import { getTimestampAtStartOfNextDayUTC } from "../../utils/date";
const methodology = {
Volume: "Scan the blockchain for AnyHedge input pattern, add up all such inputs BCH value. The daily volume is the volume of all settled contracts for the day. Indexer: https://gitlab.com/0353F40E/anyhedge-stats",
Fees: "N/A",
Revenue: "N/A",
}
interface IAnyhedgeVolumeResponse {
daily_volume: number;
total_volume: number;
}
// day formatted as 2011-12-13
export const anyhedgeVolumeEndpoint = (day: string) => {
// Data & calculation method is fully reproducible, see:
// https://gitlab.com/0353F40E/anyhedge-stats/-/blob/master/readme.md
return "https://gitlab.com/0353F40E/anyhedge-stats/-/raw/master/stats_daily/" + day + ".csv";
}
const fetchAnyhedgeVolumeData: Fetch = async (timestamp: number, _: ChainBlocks, options: FetchOptions) => {
const dayString = new Date(timestamp * 1000).toISOString().slice(0,10);
const anyhedgeVolumeData = await getAnyhedgeVolumeData(anyhedgeVolumeEndpoint(dayString));
const dailyVolume = options.createBalances();
const totalVolume = options.createBalances();
dailyVolume.addCGToken('bitcoin-cash', Number(anyhedgeVolumeData?.daily_volume));
totalVolume.addCGToken('bitcoin-cash', Number(anyhedgeVolumeData?.total_volume));
return {
timestamp,
dailyVolume,
totalVolume,
};
}
async function getAnyhedgeVolumeData(endpoint: string): Promise<IAnyhedgeVolumeResponse | null> {
try {
let data = await fetchURL(endpoint);
data = parseCSV(data);
const retval: IAnyhedgeVolumeResponse = {} as IAnyhedgeVolumeResponse;
retval.daily_volume = data[0].volume_closed;
retval.total_volume = data[0].volume_closed_cumulative;
return retval;
} catch {
return null;
}
}
function parseCSV(csvData) {
csvData = csvData.replaceAll('\r', '').split('\n').map(i => i.split(','))
const headers = csvData.shift()
const retval = csvData.map(row => toObject(headers, row));
return retval;
}
function toObject(keys, values) {
const res = {}
keys.forEach((key, i) => {
res[key] = values[i]
})
return res
}
const adapter: SimpleAdapter = {
adapter: {
[CHAIN.BITCOIN_CASH]: {
fetch: fetchAnyhedgeVolumeData,
start: 1654787405,
meta: {
methodology
}
},
},
};
export default adapter;