Skip to content

Commit

Permalink
fix: added pagination to gauntlet
Browse files Browse the repository at this point in the history
  • Loading branch information
frazarshad committed Dec 16, 2024
1 parent c8b90e5 commit b2cb4bf
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 19 deletions.
36 changes: 17 additions & 19 deletions workers/apis/src/gauntlet.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import { graphqlQuery, LIQUIDATION_ORACLE_PRICES_DAILIES_QUERY } from './queries.js';
import { SUBQUERY_URL } from './constants.js';
import { getDateKey } from './utils.js';
import { graphqlQuery, LIQUIDATION_ORACLE_PRICES_DAILIES_QUERY, PAGINATINATED_DATA_QUERIES } from './queries.js';
import { fetchSubquery, getDateKey, parseFromPaginatedData } from './utils.js';

async function getOraclPricesDailiesForLiquidatedVaults(vaultLiquidations) {
const oraclePricesDatekeyMap = vaultLiquidations?.nodes.reduce((agg, vault) => {
const { denom } = vault;
const prevDenomKeys = agg[denom] || [];
return { ...agg, [denom]: [...prevDenomKeys, getDateKey(new Date(vault.blockTime)).key] };
}, {});
const oraclePricesResponse = await fetch(SUBQUERY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(LIQUIDATION_ORACLE_PRICES_DAILIES_QUERY(oraclePricesDatekeyMap)),
});
const oraclePricesResponse = await fetchSubquery(LIQUIDATION_ORACLE_PRICES_DAILIES_QUERY(oraclePricesDatekeyMap));
const { data } = await oraclePricesResponse.json();

const oraclePricesDailies =
Expand All @@ -37,20 +29,26 @@ async function getOraclPricesDailiesForLiquidatedVaults(vaultLiquidations) {

export async function handleGauntletRequest(env) {
try {
const response = await fetch(SUBQUERY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(graphqlQuery),
});
const response = await fetchSubquery(graphqlQuery);

const { data } = await response.json();
console.log('Data Fetched Successfully');

const { oraclePrices, oraclePriceDailies, vaultManagerMetrics, vaultManagerGovernances, vaults, vaultLiquidations, _metadata } = data;

const paginatedDataResponse = await fetchSubquery(
PAGINATINATED_DATA_QUERIES(
Math.floor(vaults.totalCount / 100),
Math.floor(vaultLiquidations.totalCount / 100),
Math.floor(oraclePriceDailies.totalCount / 100)
)
);
const { data: paginatedData } = await paginatedDataResponse.json();

vaults.nodes.push(...parseFromPaginatedData('vaults', paginatedData));
vaultLiquidations.nodes.push(...parseFromPaginatedData('vaultLiquidations', paginatedData));
oraclePriceDailies.nodes.push(...parseFromPaginatedData('oraclePriceDailies', paginatedData));

const liquidationOraclePricesDailies = await getOraclPricesDailiesForLiquidatedVaults(vaultLiquidations);

const oraclePricesData = transformOraclePrices(oraclePriceDailies);
Expand Down
72 changes: 72 additions & 0 deletions workers/apis/src/queries.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { range } from './utils';

export const graphqlQuery = {
query: `
{
Expand All @@ -24,6 +26,7 @@ export const graphqlQuery = {
blockTimeLast
blockHeightLast
}
totalCount
}
vaultManagerMetrics {
nodes {
Expand Down Expand Up @@ -61,6 +64,7 @@ export const graphqlQuery = {
lockedValue
coin
}
totalCount
}
vaultLiquidations (filter: {state: {equalTo: "liquidated"}}) {
nodes {
Expand Down Expand Up @@ -88,10 +92,78 @@ export const graphqlQuery = {
blockTime
}
}
totalCount
}
}`,
};

export const PAGINATINATED_DATA_QUERIES = (vaultsPagesCount, vaultLiquidationsPagesCount, oraclePriceDailiesPagesCount) => ({
query: `{
${range(vaultsPagesCount).map(
(index) => `vaults_${index}:vaults (filter: {state: {equalTo: "active"}}, first: 100, offset:${(index + 1) * 100}) {
nodes {
id
blockTime
blockHeight
denom
balance
state
debt
lockedValue
coin
}
}`
)}
${range(vaultLiquidationsPagesCount).map(
(index) => `vaultLiquidations_${index}:vaultLiquidations (filter: {state: {equalTo: "liquidated"}}, first: 100, offset:${
(index + 1) * 100
}) {
nodes {
id
blockTime
blockHeight
denom
debt
state
balance
currentState {
id
denom
debt
state
balance
blockTime
}
liquidatingState {
id
denom
debt
state
balance
blockTime
}
}
}`
)}
${range(oraclePriceDailiesPagesCount).map(
(
index
) => `oraclePriceDailies_${index}:oraclePriceDailies (filter: {dateKey: { greaterThan: 20240501 } }, orderBy: DATE_KEY_DESC, first: 100, offset:${
(index + 1) * 100
}) {
nodes {
typeInName
typeOutName
typeInAmountLast
typeOutAmountLast
blockTimeLast
blockHeightLast
}
}`
)}
}`,
});

export const LIQUIDATION_ORACLE_PRICES_DAILIES_QUERY = (tokens) => ({
query: `
{
Expand Down
20 changes: 20 additions & 0 deletions workers/apis/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
import { SUBQUERY_URL } from './constants.js';

export const getDateKey = (date, daysToSubtract = 0) => {
const dateObject = new Date(date);
dateObject.setDate(dateObject.getDate() - daysToSubtract);
const startDateFormatDate = dateObject.toISOString().slice(0, 10);
const startDateKey = Number(startDateFormatDate.replaceAll('-', ''));
return { key: startDateKey, formattedDate: startDateFormatDate };
};

export const range = (stop) => [...Object(Array(stop)).keys()];

export const fetchSubquery = async (query) =>
await fetch(SUBQUERY_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
},
body: JSON.stringify(query),
});

export const parseFromPaginatedData = (key, paginatedData) =>
Object.entries(paginatedData)
.filter(([k, _]) => k.split('_')['0'] === key)
.map(([_, v]) => v.nodes)
.flat();

0 comments on commit b2cb4bf

Please sign in to comment.