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