add support for custom page size

This commit is contained in:
strobie
2022-10-13 15:19:10 +02:00
parent 80028439a3
commit d4e72bbaae
8 changed files with 25 additions and 42 deletions

View File

@@ -18,15 +18,14 @@ const getSubgraphUrl = (chain: Chain) => {
if (chain === Chain.ethereum) {
subgraphUrl = "https://api.thegraph.com/subgraphs/name/picodes/borrow";
} else {
subgraphUrl =
"https://api.thegraph.com/subgraphs/name/picodes/" + chain + "-borrow";
subgraphUrl = "https://api.thegraph.com/subgraphs/name/picodes/" + chain + "-borrow";
}
return subgraphUrl;
};
const vaultDataQuery = gql`
query vaultDatas($lastId: ID) {
vaultDatas(first: 1000, where: { isActive: true, id_gt: $lastId }) {
query vaultDatas($lastId: ID, $pageSize: Int) {
vaultDatas(first: $pageSize, where: { isActive: true, id_gt: $lastId }) {
id
collateralAmount
vaultManager {
@@ -52,18 +51,12 @@ type VaultData = {
const getVaultData = async (chain: Chain) => {
const subgraphUrl = getSubgraphUrl(chain);
const vaultData = (await getPagedGql(
subgraphUrl,
vaultDataQuery,
"vaultDatas"
)) as VaultData[];
const vaultData = (await getPagedGql(subgraphUrl, vaultDataQuery, "vaultDatas")) as VaultData[];
return vaultData;
};
const getTokenInfo = async (tokenId: string) => {
const info = (
await axios.get("https://coins.llama.fi/prices/current/" + tokenId)
).data.coins as {
const info = (await axios.get("https://coins.llama.fi/prices/current/" + tokenId)).data.coins as {
[tokenId: string]: {
decimals: number;
price: number;
@@ -87,13 +80,9 @@ const getVaultDebt = async (id: string, chain: Chain) => {
["function getVaultDebt(uint256) view returns (uint256)"],
providers[chain]
);
const vaultDebtRaw = BigNumber(
(await vaultManagerContract.getVaultDebt(vaultId)).toString()
);
const vaultDebtRaw = BigNumber((await vaultManagerContract.getVaultDebt(vaultId)).toString());
// convert vault debt to $
const vaultDebt = vaultDebtRaw.times(
(await getTokenInfo(AGEUR_TOKEN_ID)).price
);
const vaultDebt = vaultDebtRaw.times((await getTokenInfo(AGEUR_TOKEN_ID)).price);
return vaultDebt;
};
@@ -115,13 +104,10 @@ const positions = (chain: Chain) => async () => {
// liquidation price computation
const vaultDebt = await getVaultDebt(vault.id, chain);
const collateralFactor = BigNumber(vault.vaultManager.collateralFactor).div(
10e8
);
const collateralFactor = BigNumber(vault.vaultManager.collateralFactor).div(10e8);
let liqPrice: number;
const collateralDecimals = (await getTokenInfo(chain + ":" + collateral))
.decimals;
const collateralDecimals = (await getTokenInfo(chain + ":" + collateral)).decimals;
if (collateralDecimals != 18) {
// correcting the number of decimals
liqPrice = BigNumber(vaultDebt)
@@ -129,9 +115,7 @@ const positions = (chain: Chain) => async () => {
.times(10 ** (collateralDecimals - 18))
.toNumber();
} else {
liqPrice = BigNumber(vaultDebt)
.div(BigNumber(collateralAmount).times(collateralFactor))
.toNumber();
liqPrice = BigNumber(vaultDebt).div(BigNumber(collateralAmount).times(collateralFactor)).toNumber();
}
positions.push({

View File

@@ -15,8 +15,8 @@ import {
const subgraphUrl = "https://api.thegraph.com/subgraphs/name/yhayun/benqi";
const accountsQuery = gql`
query accounts($lastId: ID) {
accounts(first: 1000, where: { hasBorrowed: true, id_gt: $lastId }) {
query accounts($lastId: ID, $pageSize: Int) {
accounts(first: $pageSize, where: { hasBorrowed: true, id_gt: $lastId }) {
id
tokens {
id

View File

@@ -15,8 +15,8 @@ import {
const subgraphUrl = "https://api.thegraph.com/subgraphs/name/graphprotocol/compound-v2";
const accountsQuery = gql`
query accounts($lastId: ID) {
accounts(first: 1000, where: { hasBorrowed: true, id_gt: $lastId }) {
query accounts($lastId: ID, $pageSize: Int) {
accounts(first: $pageSize, where: { hasBorrowed: true, id_gt: $lastId }) {
id
tokens {
id

View File

@@ -6,9 +6,9 @@ import { Liq } from "../utils/types";
const subgraphUrl = "https://api.thegraph.com/subgraphs/name/euler-xyz/euler-mainnet";
const accountsQuery = gql`
query accounts($lastId: ID) {
query accounts($lastId: ID, $pageSize: Int) {
# subgraph bug - balances_: {amount_not: "0"} filter doesn't work
accounts(first: 1000, where: { id_gt: $lastId }) {
accounts(first: $pageSize, where: { id_gt: $lastId }) {
id
# if account_id != topLevelAccount_id then it's a sub-account, needs to be remapped for "owner" in the end
topLevelAccount {

View File

@@ -31,8 +31,8 @@ type SystemState = {
};
const trovesQuery = gql`
query troves($lastId: String) {
troves(first: 1000, where: { status: open, id_gt: $lastId }) {
query troves($lastId: String, $pageSize: Int) {
troves(first: $pageSize, where: { status: open, id_gt: $lastId }) {
id
collateral
rawCollateral

View File

@@ -6,8 +6,8 @@ import { Liq } from "../utils/types";
const subgraphUrl = "https://api.thegraph.com/subgraphs/name/traderjoe-xyz/lending";
const accountsQuery = gql`
query accounts($lastId: ID) {
accounts(first: 1000, where: { hasBorrowed: true, id_gt: $lastId }) {
query accounts($lastId: ID, $pageSize: Int) {
accounts(first: $pageSize, where: { hasBorrowed: true, id_gt: $lastId }) {
id
health
totalBorrowValueInUSD

View File

@@ -1,6 +1,6 @@
import { request } from "graphql-request";
export async function getPagedGql(url: string, query: string, itemName: string, limit: boolean = false) {
export async function getPagedGql(url: string, query: string, itemName: string, pageSize = 1000) {
let lastId = "";
let all = [] as any[];
let page;
@@ -8,12 +8,11 @@ export async function getPagedGql(url: string, query: string, itemName: string,
page = (
await request(url, query, {
lastId,
pageSize,
})
)[itemName];
all = all.concat(page);
lastId = page[page.length - 1]?.id;
if (limit) break;
} while (page.length === 1e3);
} while (page.length === pageSize);
return all;
}

View File

@@ -18,8 +18,8 @@ const sdk = require("@defillama/sdk");
const subgraphUrl = "https://api.thegraph.com/subgraphs/name/venusprotocol/venus-subgraph";
const accountsQuery = gql`
query accounts($lastId: ID) {
accounts(first: 1000, where: { hasBorrowed: true, id_gt: $lastId }) {
query accounts($lastId: ID, $pageSize: Int) {
accounts(first: $pageSize, where: { hasBorrowed: true, id_gt: $lastId }) {
id
tokens {
id