Skip to content

Commit

Permalink
Merge pull request #291 from zerolend/main
Browse files Browse the repository at this point in the history
Added foxy markets for LXP rewards
  • Loading branch information
0xroll authored Aug 22, 2024
2 parents 15bef9b + b59e4cf commit a098b5f
Show file tree
Hide file tree
Showing 7 changed files with 193,199 additions and 57 deletions.
51 changes: 28 additions & 23 deletions adapters/zerolend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import { write } from "fast-csv";
import fs from "fs";
import csv from "csv-parser";
import { BlockData } from "./sdk/types";
import { getUserTVLByBlock } from "./sdk/tvl";
import { BlockData, OutputDataSchemaRow } from "./sdk/types";
import { getUserTVLLegacyByBlock } from "./sdk/tvl";
import { getUserStakeByBlock } from "./sdk/stake";
import { getUserLPByBlock } from "./sdk/lp";

module.exports = {
getUserTVLByBlock,
};
import { getUserTVLFoxyByBlock } from "./sdk/foxy";

const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
const blocks: BlockData[] = [];
Expand Down Expand Up @@ -37,26 +34,12 @@ const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
readBlocksFromCSV("hourly_blocks.csv")
.then(async (blocks: BlockData[]) => {
console.log(blocks);
const allCsvRows: any[] = []; // Array to accumulate CSV rows for all blocks
const batchSize = 1000; // Size of batch to trigger writing to the file
let i = 0;
let allCsvRows: OutputDataSchemaRow[] = []; // Array to accumulate CSV rows for all blocks

for (const block of blocks) {
try {
const resultTvl = await getUserTVLByBlock(block);
for (let i = 0; i < resultTvl.length; i++) {
allCsvRows.push(resultTvl[i]);
}

const resultStake = await getUserStakeByBlock(block);
for (let i = 0; i < resultStake.length; i++) {
allCsvRows.push(resultStake[i]);
}

const resultLp = await getUserLPByBlock(block);
for (let i = 0; i < resultLp.length; i++) {
allCsvRows.push(resultLp[i]);
}
const data = await getUserTVLByBlock(block);
allCsvRows = allCsvRows.concat(data);
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
}
Expand All @@ -74,3 +57,25 @@ readBlocksFromCSV("hourly_blocks.csv")
.catch((err) => {
console.error("Error reading CSV file:", err);
});

const getUserTVLByBlock = async (block: BlockData): Promise<any> => {
let allCsvRows: OutputDataSchemaRow[] = []; // Array to accumulate CSV rows for all blocks

const resultTvlFoxy = await getUserTVLFoxyByBlock(block);
allCsvRows = allCsvRows.concat(resultTvlFoxy);

const resultStake = await getUserStakeByBlock(block);
allCsvRows = allCsvRows.concat(resultStake);

const resultLp = await getUserLPByBlock(block);
allCsvRows = allCsvRows.concat(resultLp);

const resultTvlLegacy = await getUserTVLLegacyByBlock(block);
allCsvRows = allCsvRows.concat(resultTvlLegacy);

return allCsvRows;
};

module.exports = {
getUserTVLByBlock,
};
79 changes: 79 additions & 0 deletions adapters/zerolend/src/sdk/foxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {
BlockData,
IUserReserve,
ILPResponse,
OutputDataSchemaRow,
} from "./types";

const queryURL =
"https://api.goldsky.com/api/public/project_clsk1wzatdsls01wchl2e4n0y/subgraphs/zerolend-linea-foxy/1.0.0/gn";

export const getUserTVLFoxyByBlock = async (
blocks: BlockData
): Promise<OutputDataSchemaRow[]> => {
const timestamp = blocks.blockTimestamp;
const first = 1000;
const rows: OutputDataSchemaRow[] = [];

let lastAddress = "0x0000000000000000000000000000000000000000";

const remapFoxy = (addr: string) =>
addr == "0x5fbdf89403270a1846f5ae7d113a989f850d1566"
? "0x000000000000000000000000000000000000foxy"
: addr;

console.log("working on foxy data");
do {
const query = `{
userReserves(
first: ${first}
where: {user_gt: "${lastAddress}"}
) {
user {
id
}
currentTotalDebt
currentATokenBalance
reserve {
underlyingAsset
symbol
name
}
liquidityRate
}
}`;

const response = await fetch(queryURL, {
method: "POST",
body: JSON.stringify({ query }),
headers: { "Content-Type": "application/json" },
});
const batch: ILPResponse = await response.json();

if (!batch.data || batch.data.userReserves.length == 0) break;

batch.data.userReserves.forEach((data: IUserReserve) => {
const balance =
BigInt(data.currentATokenBalance) - BigInt(data.currentTotalDebt);

if (balance !== 0n)
rows.push({
block_number: blocks.blockNumber,
timestamp,
user_address: data.user.id,
token_address: remapFoxy(data.reserve.underlyingAsset),
token_balance: BigInt(balance),
token_symbol: data.reserve.symbol,
usd_price: 0,
});

lastAddress = data.user.id;
});

console.log(
`Processed ${rows.length} rows. Last address is ${lastAddress}`
);
} while (true);

return rows;
};
23 changes: 12 additions & 11 deletions adapters/zerolend/src/sdk/lp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ export const getUserLPByBlock = async (

let lastAddress = "0x0000000000000000000000000000000000000000";

console.log("working on LP stakers data");
do {
const query = `{
tokenBalances(
where: {id_gt: "${lastAddress}", balance_omni_lp_gt: "0"}
first: ${first}
) {
id
balance_omni_lp
}
}`;
tokenBalances(
where: {id_gt: "${lastAddress}", balance_omni_lp_gt: "0"}
first: ${first}
) {
id
balance_omni_lp
}
}`;

const response = await fetch(queryURL, {
method: "POST",
Expand All @@ -46,7 +47,7 @@ export const getUserLPByBlock = async (
timestamp,
user_address: data.id,
token_address: tokenAddress,
token_balance: Number(data.balance_omni_lp),
token_balance: BigInt(data.balance_omni_lp),
token_symbol: symbol,
usd_price: 0,
});
Expand All @@ -55,9 +56,9 @@ export const getUserLPByBlock = async (
});

console.log(
`Processed ${rows.length} rows. Last address is ${lastAddress}`
`Processed ${rows.length} rows for DLP stakers. Last address is ${lastAddress}`
);
} while (true);

return rows;
return rows.filter((r) => r.token_balance > 1);
};
28 changes: 15 additions & 13 deletions adapters/zerolend/src/sdk/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
BlockData,
IOmniStakingData,
IOmniStakingResponse,
ITVLResponse,
OutputDataSchemaRow,
} from "./types";

Expand All @@ -20,17 +19,20 @@ export const getUserStakeByBlock = async (
const rows: OutputDataSchemaRow[] = [];

let lastAddress = "0x0000000000000000000000000000000000000000";

console.log("working on ZERO stakers data");
do {
const query = `{
tokenBalances(
where: {id_gt: "${lastAddress}", balance_omni_gt: "0"}
first: ${first}
) {
id
balance_omni
}
}`;
tokenBalances(
where: {
id_gt: "${lastAddress}",
balance_omni_gt: "0"
}
first: ${first}
) {
id
balance_omni
}
}`;

const response = await fetch(queryURL, {
method: "POST",
Expand All @@ -47,7 +49,7 @@ export const getUserStakeByBlock = async (
timestamp,
user_address: data.id,
token_address: tokenAddress,
token_balance: Number(data.balance_omni),
token_balance: BigInt(data.balance_omni),
token_symbol: symbol,
usd_price: 0,
});
Expand All @@ -56,9 +58,9 @@ export const getUserStakeByBlock = async (
});

console.log(
`Processed ${rows.length} rows. Last address is ${lastAddress}`
`Processed ${rows.length} rows for single stakers. Last address is ${lastAddress}`
);
} while (true);

return rows;
return rows.filter((r) => r.token_balance > 1);
};
13 changes: 7 additions & 6 deletions adapters/zerolend/src/sdk/tvl.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
BlockData,
ITVLData,
ITVLResponse,
IUserReserve,
ILPResponse,
OutputDataSchemaRow,
} from "./types";

const queryURL =
"https://api.goldsky.com/api/public/project_clsk1wzatdsls01wchl2e4n0y/subgraphs/zerolend-linea/1.0.0/gn";

export const getUserTVLByBlock = async (
export const getUserTVLLegacyByBlock = async (
blocks: BlockData
): Promise<OutputDataSchemaRow[]> => {
const timestamp = blocks.blockTimestamp;
Expand All @@ -17,6 +17,7 @@ export const getUserTVLByBlock = async (

let lastAddress = "0x0000000000000000000000000000000000000000";

console.log("working on legacy lending pool data");
do {
const query = `{
userReserves(
Expand All @@ -43,11 +44,11 @@ export const getUserTVLByBlock = async (
body: JSON.stringify({ query }),
headers: { "Content-Type": "application/json" },
});
const batch: ITVLResponse = await response.json();
const batch: ILPResponse = await response.json();

if (!batch.data || batch.data.userReserves.length == 0) break;

batch.data.userReserves.forEach((data: ITVLData) => {
batch.data.userReserves.forEach((data: IUserReserve) => {
const balance =
BigInt(data.currentATokenBalance) - BigInt(data.currentTotalDebt);

Expand All @@ -57,7 +58,7 @@ export const getUserTVLByBlock = async (
timestamp,
user_address: data.user.id,
token_address: data.reserve.underlyingAsset,
token_balance: Number(balance),
token_balance: BigInt(balance),
token_symbol: data.reserve.symbol,
usd_price: 0,
});
Expand Down
8 changes: 4 additions & 4 deletions adapters/zerolend/src/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type OutputDataSchemaRow = {
timestamp: number;
user_address: string;
token_address: string;
token_balance: number;
token_balance: bigint;
token_symbol: string;
usd_price: number;
};
Expand All @@ -25,13 +25,13 @@ export interface BlockData {
blockTimestamp: number;
}

export interface ITVLResponse {
export interface ILPResponse {
data: {
userReserves: ITVLData[];
userReserves: IUserReserve[];
};
}

export interface ITVLData {
export interface IUserReserve {
user: {
id: string;
};
Expand Down
Loading

0 comments on commit a098b5f

Please sign in to comment.