Skip to content

Commit

Permalink
Merge pull request #323 from delta-hq/zerolend-recreate
Browse files Browse the repository at this point in the history
zerolend with staker details only
  • Loading branch information
0xroll authored Nov 8, 2024
2 parents fc41aab + efd3df4 commit a365d0b
Show file tree
Hide file tree
Showing 9 changed files with 193,620 additions and 0 deletions.
2 changes: 2 additions & 0 deletions adapters/zerolend_zero/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
number,timestamp
4243360,1714773599
27 changes: 27 additions & 0 deletions adapters/zerolend_zero/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "zerolend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"start": "node dist/index.js",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist",
"test": "node "
},
"keywords": [],
"author": "",
"license": "UNLICENSED",
"dependencies": {
"axios": "^1.7.7",
"axios-rate-limit": "^1.4.0",
"csv-parser": "^3.0.0",
"fast-csv": "^5.0.1"
},
"devDependencies": {
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
73 changes: 73 additions & 0 deletions adapters/zerolend_zero/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { write } from "fast-csv";
import fs from "fs";
import csv from "csv-parser";
import { BlockData, OutputDataSchemaRow } from "./sdk/types";
import { getUserStakeByBlock } from "./sdk/stake";
import { getUserLPByBlock } from "./sdk/lp";

const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
const blocks: BlockData[] = [];

await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv()) // Specify the separator as '\t' for TSV files
.on("data", (row) => {
const blockNumber = parseInt(row.number, 10);
const blockTimestamp = parseInt(row.timestamp, 10);
if (!isNaN(blockNumber) && blockTimestamp) {
blocks.push({ blockNumber: blockNumber, blockTimestamp });
}
})
.on("end", () => {
resolve();
})
.on("error", (err) => {
reject(err);
});
});

return blocks;
};

readBlocksFromCSV("hourly_blocks.csv")
.then(async (blocks: BlockData[]) => {
console.log(blocks);
let allCsvRows: OutputDataSchemaRow[] = []; // Array to accumulate CSV rows for all blocks

for (const block of blocks) {
try {
const data = await getUserTVLByBlock(block);
allCsvRows = allCsvRows.concat(data);
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
}
}
await new Promise((resolve, reject) => {
const ws = fs.createWriteStream(`outputData.csv`, { flags: "w" });
write(allCsvRows, { headers: true })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
resolve;
});
});
})
.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 resultStake = await getUserStakeByBlock(block);
allCsvRows = allCsvRows.concat(resultStake);

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

return allCsvRows;
};

module.exports = {
getUserTVLByBlock,
};
74 changes: 74 additions & 0 deletions adapters/zerolend_zero/src/sdk/lp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import axios from "axios";
import rateLimit from "axios-rate-limit";
import {
BlockData,
IOmniStakingData,
IOmniStakingResponse,
OutputDataSchemaRow,
} from "./types";

const axiosInstance = rateLimit(axios.create(), {
maxRequests: 5,
perMilliseconds: 1000,
});

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

const tokenAddress = "0x78354f8dccb269a615a7e0a24f9b0718fdc3c7a7"; //do we need to convert the case
const symbol = "ZERO";

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

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
}
}`;

const response = await axiosInstance.post(
queryURL,
{ query },
{
headers: { "Content-Type": "application/json" },
}
);

const batch: IOmniStakingResponse = await response.data;

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

batch.data.tokenBalances.forEach((data: IOmniStakingData) => {
rows.push({
block_number: blocks.blockNumber,
timestamp,
user_address: data.id,
token_address: tokenAddress,
token_balance: BigInt(data.balance_omni_lp),
token_symbol: symbol,
usd_price: 0,
});

lastAddress = data.id;
});

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

return rows.filter((r) => r.token_balance > 1);
};
76 changes: 76 additions & 0 deletions adapters/zerolend_zero/src/sdk/stake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import axios from "axios";
import rateLimit from "axios-rate-limit";
import {
BlockData,
IOmniStakingData,
IOmniStakingResponse,
OutputDataSchemaRow,
} from "./types";

const axiosInstance = rateLimit(axios.create(), {
maxRequests: 5,
perMilliseconds: 1000,
});

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

const tokenAddress = "0x78354f8dccb269a615a7e0a24f9b0718fdc3c7a7"; //do we need to convert the case
const symbol = "ZERO";

export const getUserStakeByBlock = async (
blocks: BlockData
): Promise<OutputDataSchemaRow[]> => {
const timestamp = blocks.blockTimestamp;
const first = 1000;
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
}
}`;

const response = await axiosInstance.post(
queryURL,
{ query },
{
headers: { "Content-Type": "application/json" },
}
);

const batch: IOmniStakingResponse = await response.data;

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

batch.data.tokenBalances.forEach((data: IOmniStakingData) => {
rows.push({
block_number: blocks.blockNumber,
timestamp,
user_address: data.id,
token_address: tokenAddress,
token_balance: BigInt(data.balance_omni),
token_symbol: symbol,
usd_price: 0,
});

lastAddress = data.id;
});

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

return rows.filter((r) => r.token_balance > 1);
};
46 changes: 46 additions & 0 deletions adapters/zerolend_zero/src/sdk/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export interface IOmniStakingResponse {
data: {
tokenBalances: IOmniStakingData[];
};
}

export interface IOmniStakingData {
id: string;
balance_omni: string;
balance_omni_lp: string;
}

export type OutputDataSchemaRow = {
block_number: number;
timestamp: number;
user_address: string;
token_address: string;
token_balance: bigint;
token_symbol: string;
usd_price: number;
};

export interface BlockData {
blockNumber: number;
blockTimestamp: number;
}

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

export interface IUserReserve {
user: {
id: string;
};
currentTotalDebt: string;
currentATokenBalance: string;
reserve: {
underlyingAsset: string;
symbol: string;
name: string;
};
liquidityRate: "0";
}
Loading

0 comments on commit a365d0b

Please sign in to comment.