-
Notifications
You must be signed in to change notification settings - Fork 6
/
manifold.ts
140 lines (131 loc) · 3.88 KB
/
manifold.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Imports */
import axios from "axios";
import { average } from "../../utils";
import { FetchedQuestion, Platform } from "./";
/* Definitions */
const platformName = "manifold";
const ENDPOINT = "https://api.manifold.markets/v0/markets";
// See https://manifoldmarkets.notion.site/Manifold-Markets-API-5e7d0aef4dcf452bb04b319e178fabc5
/* Support functions */
async function fetchPage(endpoint: string) {
let response = await axios({
url: endpoint,
method: "GET",
headers: {
"Content-Type": "text/json",
},
}).then((response) => response.data);
// console.log(response)
return response;
}
async function fetchAllData() {
let endpoint = ENDPOINT;
let end = false;
let allData = [];
let counter = 1;
while (!end) {
console.log(`Query #${counter}: ${endpoint}`);
let newData = await fetchPage(endpoint);
// console.log(newData)
if (Array.isArray(newData)) {
allData.push(...newData);
let hasReachedEnd =
newData.length == 0 ||
newData[newData.length - 1] == undefined ||
newData[newData.length - 1].id == undefined;
if (!hasReachedEnd) {
let lastId = newData[newData.length - 1].id;
endpoint = `${ENDPOINT}?before=${lastId}`;
} else {
end = true;
}
} else {
end = true;
}
counter = counter + 1;
}
return allData;
}
function showStatistics(results: FetchedQuestion[]) {
console.log(`Num unresolved markets: ${results.length}`);
let sum = (arr: number[]) => arr.reduce((tally, a) => tally + a, 0);
let num2StarsOrMore = results.filter(
(result) => manifold.calculateStars(result) >= 2
);
console.log(
`Manifold has ${num2StarsOrMore.length} markets with 2 stars or more`
);
console.log(
`Mean volume: ${
sum(results.map((result) => result.qualityindicators.volume7Days || 0)) /
results.length
}; mean pool: ${
sum(results.map((result) => result.qualityindicators.pool)) /
results.length
}`
);
}
function processPredictions(predictions: any[]): FetchedQuestion[] {
let results: FetchedQuestion[] = predictions.map((prediction) => {
let id = `${platformName}-${prediction.id}`; // oops, doesn't match platform name
let probability = prediction.probability;
let options: FetchedQuestion["options"] = [
{
name: "Yes",
probability: probability,
type: "PROBABILITY",
},
{
name: "No",
probability: 1 - probability,
type: "PROBABILITY",
},
];
const result: FetchedQuestion = {
id: id,
title: prediction.question,
url: prediction.url,
description: prediction.description || "",
options,
qualityindicators: {
createdTime: prediction.createdTime,
// volume7Days: prediction.volume7Days, // deprecated.
volume24Hours: prediction.volume24Hours,
pool: prediction.pool, // normally liquidity, but I don't actually want to show it.
},
extra: {
isResolved: prediction.isResolved,
},
};
return result;
});
const unresolvedResults = results.filter(
(result) => !(result.extra as any).isResolved
);
return unresolvedResults;
}
export const manifold: Platform = {
name: platformName,
label: "Manifold Markets",
color: "#793466",
version: "v1",
async fetcher() {
let data = await fetchAllData();
let results = processPredictions(data); // somehow needed
showStatistics(results);
return results;
},
calculateStars(data) {
let nuno = () =>
(data.qualityindicators.volume24Hours || 0) > 100 ||
((data.qualityindicators.pool || 0) > 500 &&
(data.qualityindicators.volume24Hours || 0) > 50)
? 2
: 1;
let eli = () => null;
let misha = () => null;
let starsDecimal = average([nuno()]); //, eli(data), misha(data)])
let starsInteger = Math.round(starsDecimal);
return starsInteger;
},
};