feat: Add insight markets

So far restricted to:
- Binary markets
- Denominated in USD
This commit is contained in:
NunoSempere 2022-10-28 10:28:34 +01:00
parent d7843b52c3
commit f5bf50456a

View File

@ -2,6 +2,7 @@
import axios from "axios"; import axios from "axios";
import {FetchedQuestion, Platform} from "."; import {FetchedQuestion, Platform} from ".";
import toMarkdown from "../utils/toMarkdown";
/* Definitions */ /* Definitions */
const platformName = "insight"; const platformName = "insight";
@ -10,9 +11,43 @@ const getMarketEndpoint = (id : number) => `https://insightprediction.com/api/ma
/* Support functions */ /* Support functions */
// 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
async function fetchPage(bearer: string, pageNum: number) { async function fetchPage(bearer: string, pageNum: number) {
let pageUrl = `${marketsEnpoint}&page=${pageNum}` let pageUrl = `${marketsEnpoint}&page=${pageNum}`
console.log(`Fetching page #${pageNum}: ${pageUrl}`) console.log(`Fetching page #${pageNum}`) // : ${pageUrl}
const response = await axios({ const response = await axios({
url: pageUrl, // &orderBy=is_resolved&sortedBy=desc`, url: pageUrl, // &orderBy=is_resolved&sortedBy=desc`,
method: "GET", method: "GET",
@ -41,41 +76,17 @@ async function fetchMarket(bearer: string, marketId: number) {
} }
const excludeMarketFromTitle = (title : any) => {
if (!!title) {
return title.includes(" vs ") || title.includes(" Over: ")
} else {
return true
}
}
const isObject = (x : any) => {
return typeof x === 'object' && !Array.isArray(x) && x !== null
}
const isObjectNotEmpty = (x : any) => isObject(x) && Object.keys(x).length != 0
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 isObjectNotEmpty(yes) && isObjectNotEmpty(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 processMarket = (market : any) => { const processMarket = (market : any) => {
let hasData = !!market && !!market.answer && !!market.answer.data let hasData = !!market && !!market.answer && !!market.answer.data
if (hasData) { if (hasData) {
let data = market.answer.data let data = market.answer.data
// console.log("has data")
if (isBinaryQuestion(data)) { if (isBinaryQuestion(data)) {
let orderbook = data[0].orderbook let orderbook = data[0].orderbook
if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) { // console.log("has orderbook")
// console.log(JSON.stringify(orderbook, null, 2))
if (!! orderbook && hasActiveYesNoOrderBook(orderbook)) { // console.log("has active orderbook")
let yes_min_cents = orderbook.yes.buy[0].price let yes_min_cents = orderbook.yes.buy[0].price
let yes_max_cents = orderbook.yes.sell[0].price let yes_max_cents = orderbook.yes.sell[0].price
let yes_min = Number(yes_min_cents.slice(0, -1)) let yes_min = Number(yes_min_cents.slice(0, -1))
@ -84,7 +95,7 @@ const processMarket = (market : any) => {
let latest_yes_price = data[0].latest_yes_price 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 let yes_probability = latest_yes_price ? geomMean(latest_yes_price, yes_price_orderbook) : yes_price_orderbook
const id = `${platformName}-${ const id = `${platformName}-${
data.id market.id
}`; }`;
const probability = yes_probability / 100; const probability = yes_probability / 100;
const options: FetchedQuestion["options"] = [ const options: FetchedQuestion["options"] = [
@ -99,14 +110,14 @@ const processMarket = (market : any) => {
}, },
]; ];
const result: FetchedQuestion = { const result: FetchedQuestion = {
id, id: id,
title: data.title, title: market.title,
url: data.urls, url: market.url,
description: data.rules || "", description: processDescriptionText(market.rules),
options, options,
qualityindicators: { qualityindicators: market.coin_id == "USD" ? (
trade_volume: data.volume {volume: market.volume}
} ) : ({})
}; };
return result; return result;
@ -122,7 +133,7 @@ const processMarket = (market : any) => {
} }
async function fetchAllMarkets(bearer: string) { async function fetchAllMarkets(bearer: string) {
let pageNum = 122 let pageNum = 1
let markets = [] let markets = []
let categories = [] let categories = []
let isEnd = false let isEnd = false
@ -142,12 +153,17 @@ async function fetchAllMarkets(bearer: string) {
let fullMarketData = fullMarketDataResponse.data let fullMarketData = fullMarketDataResponse.data
let processedMarketData = processMarket(fullMarketData) let processedMarketData = processMarket(fullMarketData)
let title = fullMarketData.title console.log(`- Adding: ${
console.log(`Adding: ${title}`) fullMarketData.title
}`)
console.group() console.group()
console.log(fullMarketData)
console.log(JSON.stringify(processedMarketData, null, 2)) console.log(JSON.stringify(processedMarketData, null, 2))
console.groupEnd() console.groupEnd()
if (processedMarketData != null) {
markets.push(processedMarketData) markets.push(processedMarketData)
}
let category = fullMarketData.category let category = fullMarketData.category
categories.push(category) categories.push(category)
@ -159,6 +175,7 @@ async function fetchAllMarkets(bearer: string) {
} }
console.log(markets) console.log(markets)
console.log(categories) console.log(categories)
return markets
} }
/* /*
async function fetchQuestionStats(bearer : string, marketId : number) { async function fetchQuestionStats(bearer : string, marketId : number) {
@ -260,12 +277,12 @@ export const insight: Platform = {
let bearer = process.env.INSIGHT_BEARER; let bearer = process.env.INSIGHT_BEARER;
if (!! bearer) { if (!! bearer) {
let data = await fetchAllMarkets(bearer); let data = await fetchAllMarkets(bearer);
console.log(data); return data
} else { } else {
throw Error("No INSIGHT_BEARER available in environment") throw Error("No INSIGHT_BEARER available in environment")
} }
let results: FetchedQuestion[] = []; // await processPredictions(data); // somehow needed // let results: FetchedQuestion[] = []; // await processPredictions(data); // somehow needed
return results; // return results;
}, },
calculateStars(data) { calculateStars(data) {
return 2; return 2;