2021-08-31 19:14:55 +00:00
|
|
|
/* Imports */
|
2022-02-11 14:21:36 +00:00
|
|
|
import axios from "axios";
|
2022-02-11 19:15:09 +00:00
|
|
|
import { databaseUpsert } from "../database/database-wrapper.js";
|
2022-03-23 22:48:36 +00:00
|
|
|
import { calculateStars } from "../utils/stars.js";
|
2021-08-31 19:14:55 +00:00
|
|
|
|
|
|
|
/* Definitions */
|
2022-02-11 14:21:36 +00:00
|
|
|
let jsonEndpoint = "https://trading-api.kalshi.com/v1/cached/markets/"; //"https://subgraph-matic.poly.market/subgraphs/name/TokenUnion/polymarket"//"https://subgraph-backup.poly.market/subgraphs/name/TokenUnion/polymarket"//'https://subgraph-matic.poly.market/subgraphs/name/TokenUnion/polymarket3'
|
2021-08-31 19:14:55 +00:00
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
async function fetchAllMarkets() {
|
|
|
|
// for info which the polymarket graphql API
|
|
|
|
let response = await axios
|
|
|
|
.get(jsonEndpoint)
|
|
|
|
.then((response) => response.data.markets);
|
2021-08-31 19:14:55 +00:00
|
|
|
// console.log(response)
|
2022-02-11 14:21:36 +00:00
|
|
|
return response;
|
2021-08-31 19:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function processMarkets(markets) {
|
2022-02-11 14:21:36 +00:00
|
|
|
let dateNow = new Date().toISOString();
|
2021-08-31 19:14:55 +00:00
|
|
|
// console.log(markets)
|
2022-02-11 14:21:36 +00:00
|
|
|
markets = markets.filter((market) => market.close_date > dateNow);
|
|
|
|
let results = await markets.map((market) => {
|
|
|
|
let probability = market.last_price / 100;
|
2021-08-31 19:14:55 +00:00
|
|
|
let options = [
|
|
|
|
{
|
2022-02-11 14:21:36 +00:00
|
|
|
name: "Yes",
|
|
|
|
probability: probability,
|
|
|
|
type: "PROBABILITY",
|
2021-08-31 19:14:55 +00:00
|
|
|
},
|
|
|
|
{
|
2022-02-11 14:21:36 +00:00
|
|
|
name: "No",
|
|
|
|
probability: 1 - probability,
|
|
|
|
type: "PROBABILITY",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
let id = `kalshi-${market.id}`;
|
|
|
|
let result = {
|
|
|
|
id: id,
|
|
|
|
title: market.title.replaceAll("*", ""),
|
|
|
|
url: `https://kalshi.com/markets/${market.ticker_name}`,
|
|
|
|
platform: "Kalshi",
|
|
|
|
description: `${market.settle_details}. The resolution source is: ${market.ranged_group_name} (${market.settle_source_url})`,
|
|
|
|
options: options,
|
|
|
|
timestamp: new Date().toISOString(),
|
|
|
|
qualityindicators: {
|
|
|
|
stars: calculateStars("Kalshi", {
|
|
|
|
shares_volume: market.volume,
|
|
|
|
interest: market.open_interest,
|
|
|
|
}),
|
|
|
|
yes_bid: market.yes_bid,
|
|
|
|
yes_ask: market.yes_ask,
|
|
|
|
spread: Math.abs(market.yes_bid - market.yes_ask),
|
|
|
|
shares_volume: market.volume, // Assuming that half of all buys are for yes and half for no, which is a big if.
|
|
|
|
// "open_interest": market.open_interest, also in shares
|
|
|
|
},
|
|
|
|
};
|
|
|
|
return result;
|
|
|
|
});
|
2021-08-31 19:14:55 +00:00
|
|
|
//console.log(results.length)
|
|
|
|
// console.log(results.map(result => result.title))
|
|
|
|
// console.log(results.map(result => result.title).length)
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log([...new Set(results.map((result) => result.title))]);
|
|
|
|
console.log(
|
|
|
|
"Number of unique questions: ",
|
|
|
|
[...new Set(results.map((result) => result.title))].length
|
|
|
|
);
|
2021-08-31 19:14:55 +00:00
|
|
|
// console.log([...new Set(results.map(result => result.title))].length)
|
2022-02-11 14:21:36 +00:00
|
|
|
return results; //resultsProcessed
|
2021-08-31 19:14:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Body */
|
|
|
|
export async function kalshi() {
|
2022-02-11 14:21:36 +00:00
|
|
|
let markets = await fetchAllMarkets();
|
|
|
|
let results = await processMarkets(markets); // somehow needed
|
2022-02-12 06:27:56 +00:00
|
|
|
await databaseUpsert({ contents: results, group: "kalshi" });
|
|
|
|
|
2022-02-11 14:21:36 +00:00
|
|
|
console.log("Done");
|
2021-08-31 19:14:55 +00:00
|
|
|
}
|
|
|
|
// kalshi()
|