feat: make polymarket fetcher more efficient
This commit is contained in:
parent
27652407fd
commit
b7409b23f2
|
@ -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,90 +60,76 @@ 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 id = `polymarket-${addressLowerCase.slice(0, 10)}`;
|
let moreMarketAnswer = await fetchIndividualContractData(
|
||||||
let info = infos[addressLowerCase];
|
addressLowerCase
|
||||||
let numforecasts = Number(data.tradesQuantity);
|
);
|
||||||
let tradevolume =
|
if (moreMarketAnswer.length > 0) {
|
||||||
(Number(data.collateralBuyVolume) + Number(data.collateralSellVolume)) /
|
let moreMarketInfo = moreMarketAnswer[0];
|
||||||
units;
|
let id = `polymarket-${addressLowerCase.slice(0, 10)}`;
|
||||||
let liquidity = Number(data.liquidityParameter) / units;
|
console.log(id);
|
||||||
// let isbinary = Number(data.conditions[0].outcomeSlotCount) == 2
|
let numforecasts = Number(moreMarketInfo.tradesQuantity);
|
||||||
// let percentage = Number(data.outcomeTokenPrices[0]) * 100
|
let tradevolume =
|
||||||
// let percentageFormatted = isbinary ? (percentage.toFixed(0) + "%") : "none"
|
(Number(moreMarketInfo.collateralBuyVolume) +
|
||||||
let options = [];
|
Number(moreMarketInfo.collateralSellVolume)) /
|
||||||
for (let outcome in data.outcomeTokenPrices) {
|
units;
|
||||||
options.push({
|
let liquidity = Number(moreMarketInfo.liquidityParameter) / units;
|
||||||
name: info.outcomes[outcome],
|
// let isbinary = Number(moreMarketInfo.conditions[0].outcomeSlotCount) == 2
|
||||||
probability: data.outcomeTokenPrices[outcome],
|
// let percentage = Number(moreMarketInfo.outcomeTokenPrices[0]) * 100
|
||||||
type: "PROBABILITY",
|
// let percentageFormatted = isbinary ? (percentage.toFixed(0) + "%") : "none"
|
||||||
});
|
let options = [];
|
||||||
}
|
for (let outcome in moreMarketInfo.outcomeTokenPrices) {
|
||||||
|
options.push({
|
||||||
|
name: marketInfo.outcomes[outcome],
|
||||||
|
probability: moreMarketInfo.outcomeTokenPrices[outcome],
|
||||||
|
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: {
|
||||||
numforecasts: numforecasts.toFixed(0),
|
numforecasts: numforecasts.toFixed(0),
|
||||||
liquidity: liquidity.toFixed(2),
|
liquidity: liquidity.toFixed(2),
|
||||||
tradevolume: tradevolume.toFixed(2),
|
tradevolume: tradevolume.toFixed(2),
|
||||||
stars: calculateStars("Polymarket", {
|
stars: calculateStars("Polymarket", {
|
||||||
liquidity,
|
liquidity,
|
||||||
option: options[0],
|
option: options[0],
|
||||||
volume: tradevolume,
|
volume: tradevolume,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
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();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user