2022-07-09 02:25:11 +00:00
|
|
|
/* Imports */
|
|
|
|
import axios from "axios";
|
|
|
|
|
2022-07-28 17:33:40 +00:00
|
|
|
import {FetchedQuestion, Platform} from ".";
|
2022-10-28 09:28:34 +00:00
|
|
|
import toMarkdown from "../utils/toMarkdown";
|
2022-07-09 02:25:11 +00:00
|
|
|
|
|
|
|
/* Definitions */
|
|
|
|
const platformName = "insight";
|
2022-10-27 22:35:44 +00:00
|
|
|
const marketsEnpoint = "https://insightprediction.com/api/markets?orderBy=is_resolved&sortedBy=asc";
|
2022-07-28 17:33:40 +00:00
|
|
|
const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/markets/${id}`;
|
2022-07-09 02:25:11 +00:00
|
|
|
|
|
|
|
/* Support functions */
|
|
|
|
|
2022-10-28 09:28:34 +00:00
|
|
|
// Stubs
|
|
|
|
const excludeMarketFromTitle = (title : any) => {
|
|
|
|
if (!!title) {
|
|
|
|
return title.includes(" vs ") || title.includes(" Over: ")
|
|
|
|
} else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const hasActiveYesNoOrderBook = (orderbook : any) => {
|
|
|
|
if (!!orderbook) {
|
|
|
|
let yes = !!orderbook.yes && !!orderbook.yes.buy && Array.isArray(orderbook.yes.buy) && orderbook.yes.buy.length != 0 && !!orderbook.yes.buy[0].price && !!orderbook.yes.sell && Array.isArray(orderbook.yes.sell) && orderbook.yes.sell.length != 0 && !!orderbook.yes.sell[0].price
|
|
|
|
let no = !!orderbook.no && !!orderbook.no.buy && Array.isArray(orderbook.no.buy) && orderbook.no.buy.length != 0 && !!orderbook.no.buy[0].price && !!orderbook.no.sell && Array.isArray(orderbook.no.sell) && orderbook.no.sell.length != 0 && !!orderbook.no.sell[0].price
|
|
|
|
return yes && no
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isBinaryQuestion = (data : any) => Array.isArray(data) && data.length == 1
|
|
|
|
|
|
|
|
const geomMean = (a : number, b : number) => Math.sqrt(a * b)
|
|
|
|
|
|
|
|
const processRelativeUrls = (a : string) => a.replaceAll("] (/", "](http://insightprediction.com/").replaceAll("](/", "](http://insightprediction.com/")
|
|
|
|
|
|
|
|
const processDescriptionText = (text : any) => {
|
|
|
|
if (typeof text === 'string') {
|
|
|
|
return processRelativeUrls(toMarkdown(text))
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetching
|
2022-10-27 22:35:44 +00:00
|
|
|
async function fetchPage(bearer: string, pageNum: number) {
|
|
|
|
let pageUrl = `${marketsEnpoint}&page=${pageNum}`
|
2022-10-28 09:28:34 +00:00
|
|
|
console.log(`Fetching page #${pageNum}`) // : ${pageUrl}
|
2022-10-27 22:35:44 +00:00
|
|
|
const response = await axios({
|
|
|
|
url: pageUrl, // &orderBy=is_resolved&sortedBy=desc`,
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "application/json",
|
|
|
|
Authorization: `Bearer ${bearer}`
|
|
|
|
}
|
|
|
|
}).then((res) => res.data);
|
|
|
|
// console.log(response);
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchMarket(bearer: string, marketId: number) {
|
2022-10-09 11:52:01 +00:00
|
|
|
const response = await axios({
|
|
|
|
url: getMarketEndpoint(marketId),
|
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "application/json",
|
|
|
|
Authorization: `Bearer ${bearer}`
|
|
|
|
}
|
|
|
|
}).then((res) => res.data);
|
|
|
|
// console.log(response)
|
|
|
|
return response;
|
2022-10-27 22:35:44 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const processMarket = (market : any) => {
|
|
|
|
let hasData = !!market && !!market.answer && !!market.answer.data
|
|
|
|
if (hasData) {
|
|
|
|
let data = market.answer.data
|
2022-10-28 09:28:34 +00:00
|
|
|
// console.log("has data")
|
2022-10-27 22:35:44 +00:00
|
|
|
if (isBinaryQuestion(data)) {
|
|
|
|
let orderbook = data[0].orderbook
|
2022-10-28 09:28:34 +00:00
|
|
|
// console.log("has orderbook")
|
|
|
|
// console.log(JSON.stringify(orderbook, null, 2))
|
|
|
|
if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) { // console.log("has active orderbook")
|
2022-10-27 22:35:44 +00:00
|
|
|
let yes_min_cents = orderbook.yes.buy[0].price
|
|
|
|
let yes_max_cents = orderbook.yes.sell[0].price
|
|
|
|
let yes_min = Number(yes_min_cents.slice(0, -1))
|
|
|
|
let yes_max = Number(yes_max_cents.slice(0, -1))
|
|
|
|
let yes_price_orderbook = geomMean(yes_min, yes_max)
|
|
|
|
let latest_yes_price = data[0].latest_yes_price
|
|
|
|
let yes_probability = latest_yes_price ? geomMean(latest_yes_price, yes_price_orderbook) : yes_price_orderbook
|
|
|
|
const id = `${platformName}-${
|
2022-10-28 09:28:34 +00:00
|
|
|
market.id
|
2022-10-27 22:35:44 +00:00
|
|
|
}`;
|
|
|
|
const probability = yes_probability / 100;
|
|
|
|
const options: FetchedQuestion["options"] = [
|
|
|
|
{
|
|
|
|
name: "Yes",
|
|
|
|
probability: probability,
|
|
|
|
type: "PROBABILITY"
|
|
|
|
}, {
|
|
|
|
name: "No",
|
|
|
|
probability: 1 - probability,
|
|
|
|
type: "PROBABILITY"
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const result: FetchedQuestion = {
|
2022-10-28 09:28:34 +00:00
|
|
|
id: id,
|
|
|
|
title: market.title,
|
|
|
|
url: market.url,
|
|
|
|
description: processDescriptionText(market.rules),
|
2022-10-27 22:35:44 +00:00
|
|
|
options,
|
2022-10-28 09:28:34 +00:00
|
|
|
qualityindicators: market.coin_id == "USD" ? (
|
|
|
|
{volume: market.volume}
|
|
|
|
) : ({})
|
2022-10-27 22:35:44 +00:00
|
|
|
};
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
} else { // non binary question
|
|
|
|
return null // for now
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function fetchAllMarkets(bearer: string) {
|
2022-10-28 09:28:34 +00:00
|
|
|
let pageNum = 1
|
2022-10-27 22:35:44 +00:00
|
|
|
let markets = []
|
|
|
|
let categories = []
|
|
|
|
let isEnd = false
|
|
|
|
while (! isEnd) {
|
|
|
|
let page = await fetchPage(bearer, pageNum)
|
|
|
|
// console.log(JSON.stringify(page, null, 2))
|
|
|
|
let data = page.data
|
|
|
|
if (!! data && Array.isArray(data) && data.length > 0) {
|
|
|
|
let lastMarket = data[data.length - 1]
|
|
|
|
let isLastMarketResolved = lastMarket.is_resolved
|
|
|
|
if (isLastMarketResolved == true) {
|
|
|
|
isEnd = true
|
|
|
|
}
|
|
|
|
let newMarkets = data.filter(market => !market.is_resolved && !market.is_expired && ! excludeMarketFromTitle(market.title))
|
|
|
|
for (let initMarketData of newMarkets) {
|
|
|
|
let fullMarketDataResponse = await fetchMarket(bearer, initMarketData.id)
|
|
|
|
let fullMarketData = fullMarketDataResponse.data
|
|
|
|
let processedMarketData = processMarket(fullMarketData)
|
2022-10-28 09:28:34 +00:00
|
|
|
|
|
|
|
console.log(`- Adding: ${
|
|
|
|
fullMarketData.title
|
|
|
|
}`)
|
2022-10-27 22:35:44 +00:00
|
|
|
console.group()
|
2022-10-28 09:28:34 +00:00
|
|
|
console.log(fullMarketData)
|
2022-10-27 22:35:44 +00:00
|
|
|
console.log(JSON.stringify(processedMarketData, null, 2))
|
|
|
|
console.groupEnd()
|
|
|
|
|
2022-10-28 09:28:34 +00:00
|
|
|
if (processedMarketData != null) {
|
|
|
|
markets.push(processedMarketData)
|
|
|
|
}
|
|
|
|
|
2022-10-27 22:35:44 +00:00
|
|
|
let category = fullMarketData.category
|
|
|
|
categories.push(category)
|
2022-10-28 09:28:34 +00:00
|
|
|
|
2022-10-27 22:35:44 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isEnd = true
|
|
|
|
} pageNum = pageNum + 1
|
|
|
|
}
|
|
|
|
console.log(markets)
|
|
|
|
console.log(categories)
|
2022-10-28 09:28:34 +00:00
|
|
|
return markets
|
2022-10-27 22:35:44 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
async function fetchQuestionStats(bearer : string, marketId : number) {
|
2022-10-09 11:52:01 +00:00
|
|
|
const response = await axios({
|
2022-10-27 22:35:44 +00:00
|
|
|
url: getMarketEndpoint(marketId),
|
2022-10-09 11:52:01 +00:00
|
|
|
method: "GET",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Accept: "application/json",
|
|
|
|
Authorization: `Bearer ${bearer}`
|
|
|
|
}
|
|
|
|
}).then((res) => res.data);
|
2022-10-27 22:35:44 +00:00
|
|
|
// console.log(response)
|
2022-10-09 11:52:01 +00:00
|
|
|
return response;
|
2022-07-09 02:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 22:35:44 +00:00
|
|
|
|
|
|
|
|
2022-10-09 11:52:01 +00:00
|
|
|
async function fetchData(bearer : string) {
|
|
|
|
let pageNum = 1;
|
|
|
|
let reachedEnd = false;
|
|
|
|
let results = [];
|
|
|
|
while (! reachedEnd) {
|
|
|
|
let newPage = await fetchPage(bearer, pageNum);
|
|
|
|
let newPageData = newPage.data;
|
|
|
|
let marketsFromPage = []
|
|
|
|
for (let market of newPageData) {
|
|
|
|
let response = await fetchQuestionStats(bearer, market.id);
|
|
|
|
let marketData = response.data
|
|
|
|
let marketAnswer = marketData.answer.data
|
|
|
|
delete marketData.answer
|
|
|
|
// These are the options and their prices.
|
|
|
|
let marketOptions = marketAnswer.map(answer => {
|
|
|
|
return({name: answer.title, probability: answer.latest_yes_price, type: "PROBABILITY"})
|
|
|
|
})
|
|
|
|
marketsFromPage.push({
|
|
|
|
... marketData,
|
|
|
|
options: marketOptions
|
|
|
|
});
|
|
|
|
}
|
2022-07-28 17:33:40 +00:00
|
|
|
|
2022-10-09 11:52:01 +00:00
|
|
|
let finalObject = marketsFromPage
|
2022-07-09 02:25:11 +00:00
|
|
|
|
2022-10-09 11:52:01 +00:00
|
|
|
console.log(`Page = #${pageNum}`);
|
|
|
|
// console.log(newPageData)
|
|
|
|
console.dir(finalObject, {depth: null});
|
|
|
|
results.push(... finalObject);
|
2022-07-09 02:25:11 +00:00
|
|
|
|
2022-10-09 11:52:01 +00:00
|
|
|
let newPagination = newPage.meta.pagination;
|
|
|
|
if (newPagination.total_pages == pageNum) {
|
|
|
|
reachedEnd = true;
|
|
|
|
} else {
|
|
|
|
pageNum = pageNum + 1;
|
2022-07-09 02:25:11 +00:00
|
|
|
}
|
2022-10-09 11:52:01 +00:00
|
|
|
}
|
|
|
|
return results
|
2022-07-09 02:25:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 11:52:01 +00:00
|
|
|
async function processPredictions(predictions : any[]) {
|
|
|
|
let results = await predictions.map((prediction) => {
|
|
|
|
const id = `${platformName}-${
|
|
|
|
prediction.id
|
|
|
|
}`;
|
|
|
|
const probability = prediction.probability;
|
|
|
|
const options: FetchedQuestion["options"] = [
|
|
|
|
{
|
|
|
|
name: "Yes",
|
|
|
|
probability: probability,
|
|
|
|
type: "PROBABILITY"
|
|
|
|
}, {
|
|
|
|
name: "No",
|
|
|
|
probability: 1 - probability,
|
|
|
|
type: "PROBABILITY"
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const result: FetchedQuestion = {
|
|
|
|
id,
|
|
|
|
title: prediction.title,
|
|
|
|
url: "https://example.com",
|
|
|
|
description: prediction.description,
|
|
|
|
options,
|
|
|
|
qualityindicators: {
|
|
|
|
// other: prediction.otherx,
|
|
|
|
// indicators: prediction.indicatorx,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
});
|
|
|
|
return results; // resultsProcessed
|
2022-07-09 02:25:11 +00:00
|
|
|
}
|
2022-10-09 11:52:01 +00:00
|
|
|
*/
|
2022-07-09 02:25:11 +00:00
|
|
|
/* Body */
|
|
|
|
export const insight: Platform = {
|
2022-10-09 11:52:01 +00:00
|
|
|
name: platformName,
|
|
|
|
label: "Insight Prediction",
|
|
|
|
color: "#ff0000",
|
|
|
|
version: "v1",
|
|
|
|
async fetcher() {
|
|
|
|
let bearer = process.env.INSIGHT_BEARER;
|
2022-10-27 22:35:44 +00:00
|
|
|
if (!! bearer) {
|
|
|
|
let data = await fetchAllMarkets(bearer);
|
2022-10-28 09:28:34 +00:00
|
|
|
return data
|
2022-10-27 22:35:44 +00:00
|
|
|
} else {
|
|
|
|
throw Error("No INSIGHT_BEARER available in environment")
|
|
|
|
}
|
2022-10-28 09:28:34 +00:00
|
|
|
// let results: FetchedQuestion[] = []; // await processPredictions(data); // somehow needed
|
|
|
|
// return results;
|
2022-10-09 11:52:01 +00:00
|
|
|
},
|
|
|
|
calculateStars(data) {
|
|
|
|
return 2;
|
|
|
|
}
|
2022-07-09 02:25:11 +00:00
|
|
|
};
|