feat: make polymarket fetcher more efficient

This commit is contained in:
NunoSempere 2022-03-28 22:24:44 -04:00
parent 27652407fd
commit b7409b23f2

View File

@ -1,4 +1,3 @@
/* Imports */
import axios from "axios";
import { databaseUpsert } from "../database/database-wrapper";
import { calculateStars } from "../utils/stars";
@ -12,14 +11,15 @@ async function fetchAllContractInfo() {
// for info which the polymarket graphql API
let response = await axios
.get(
"https://strapi-matic.poly.market/markets?active=true&_sort=volume:desc&_limit=-1"
"https://strapi-matic.poly.market/markets?active=true&_sort=volume:desc&closed=false&_limit=-1"
// "https://strapi-matic.poly.market/markets?active=true&_sort=volume:desc&_limit=-1" to get all markets, including closed ones
)
.then((query) => query.data);
response = response.filter((res) => res.closed != true);
return response;
}
async function fetchAllContractData() {
async function fetchIndividualContractData(marketMakerAddress) {
let daysSinceEra = Math.round(Date.now() / (1000 * 24 * 60 * 60)) - 7; // last week
let response = await axios({
url: graphQLendpoint,
@ -30,6 +30,7 @@ async function fetchAllContractData() {
{
fixedProductMarketMakers(first: 1000
where: {
id: "${marketMakerAddress}"
lastActiveDay_gt: ${daysSinceEra}
}){
id
@ -59,58 +60,44 @@ async function fetchAllContractData() {
return response;
}
async function fetch_all() {
let allData = await fetchAllContractData();
let allInfo = await fetchAllContractInfo();
let infos = {};
for (let info of allInfo) {
let address = info.marketMakerAddress;
let addressLowerCase = address.toLowerCase();
//delete info.history
if (info.outcomes[0] != "Long" || info.outcomes[1] != "Long")
infos[addressLowerCase] = {
title: info.question,
url: "https://polymarket.com/market/" + info.slug,
address: address,
description: info.description,
outcomes: info.outcomes,
options: [],
category: info.category,
};
}
async function fetchAll() {
let results = [];
for (let data of allData) {
let addressLowerCase = data.id;
// console.log(data)
if (infos[addressLowerCase] != undefined) {
// console.log(addressLowerCase)
let webpageEndpointData = await fetchAllContractInfo();
for (let marketInfo of webpageEndpointData) {
let address = marketInfo.marketMakerAddress;
let addressLowerCase = address.toLowerCase();
if (marketInfo.outcomes[0] != "Long" || marketInfo.outcomes[1] != "Long") {
let moreMarketAnswer = await fetchIndividualContractData(
addressLowerCase
);
if (moreMarketAnswer.length > 0) {
let moreMarketInfo = moreMarketAnswer[0];
let id = `polymarket-${addressLowerCase.slice(0, 10)}`;
let info = infos[addressLowerCase];
let numforecasts = Number(data.tradesQuantity);
console.log(id);
let numforecasts = Number(moreMarketInfo.tradesQuantity);
let tradevolume =
(Number(data.collateralBuyVolume) + Number(data.collateralSellVolume)) /
(Number(moreMarketInfo.collateralBuyVolume) +
Number(moreMarketInfo.collateralSellVolume)) /
units;
let liquidity = Number(data.liquidityParameter) / units;
// let isbinary = Number(data.conditions[0].outcomeSlotCount) == 2
// let percentage = Number(data.outcomeTokenPrices[0]) * 100
let liquidity = Number(moreMarketInfo.liquidityParameter) / units;
// let isbinary = Number(moreMarketInfo.conditions[0].outcomeSlotCount) == 2
// let percentage = Number(moreMarketInfo.outcomeTokenPrices[0]) * 100
// let percentageFormatted = isbinary ? (percentage.toFixed(0) + "%") : "none"
let options = [];
for (let outcome in data.outcomeTokenPrices) {
for (let outcome in moreMarketInfo.outcomeTokenPrices) {
options.push({
name: info.outcomes[outcome],
probability: data.outcomeTokenPrices[outcome],
name: marketInfo.outcomes[outcome],
probability: moreMarketInfo.outcomeTokenPrices[outcome],
type: "PROBABILITY",
});
}
let result = {
id: id,
title: info.title,
url: info.url,
title: marketInfo.title,
url: marketInfo.url,
platform: "PolyMarket",
description: info.description,
description: marketInfo.description,
options: options,
timestamp: new Date().toISOString(),
qualityindicators: {
@ -124,25 +111,25 @@ async function fetch_all() {
}),
},
extra: {
address: info.address,
address: marketInfo.address,
},
/*
*/
};
if (info.category != "Sports") {
if (marketInfo.category != "Sports") {
// console.log(result)
results.push(result);
}
}
}
return results; //resultsProcessed
}
return results;
}
/* Body */
export async function polymarket() {
let results = await fetch_all();
let results = await fetchAll();
await databaseUpsert({ contents: results, group: "polymarket" });
console.log("Done");
}
// polymarket()
// polymarket();