Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layerbank : Lending : tvl by user #1

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions adapters/layerbank/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
number,timestamp
4243360,1714773599
35 changes: 35 additions & 0 deletions adapters/layerbank/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "layerbank-tvl",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/big.js": "^6.2.2",
"big.js": "^6.2.1",
"bignumber.js": "^9.1.2",
"csv-parser": "^3.0.0",
"decimal.js-light": "^2.5.1",
"fast-csv": "^5.0.1",
"jsbi": "^4.3.0",
"tiny-invariant": "^1.3.1",
"toformat": "^2.0.0",
"viem": "^2.13.1"
},
"engines": {
"node": ">=18.0.0"
},
"devDependencies": {
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
128 changes: 128 additions & 0 deletions adapters/layerbank/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { CHAINS, PROTOCOLS } from "./sdk/config";
import {
getAccountStatesForAddressByPoolAtBlock,
getTimestampAtBlock,
} from "./sdk/subgraphDetails";

(BigInt.prototype as any).toJSON = function () {
return this.toString();
};

import fs from "fs";
import csv from "csv-parser";
import { write } from "fast-csv";
import { getMarketInfos, updateBorrowBalances } from "./sdk/marketDetails";
import { bigMath } from "./sdk/abi/helpers";
import { exit } from "process";

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

type OutputDataSchemaRow = {
protocol: string;
date: number;
block_number: number;
user_address: string;
market: string;
supply_token: bigint;
borrow_token: bigint;
};

export const getUserTVLByBlock = async (blocks: BlockData) => {
const marketInfos = await getMarketInfos(
"0xEC53c830f4444a8A56455c6836b5D2aA794289Aa"
);

const csvRows: OutputDataSchemaRow[] = [];
const block = blocks.blockNumber;

let states = await getAccountStatesForAddressByPoolAtBlock(
block,
"",
"",
CHAINS.SCROLL,
PROTOCOLS.LAYERBANK
);
states = states.filter(
(s) => marketInfos.findIndex((lu) => lu.address == s.account) == -1
);

console.log(`Block: ${block}`);
console.log("States: ", states.length);

await updateBorrowBalances(states);

states.forEach((state) => {
const marketInfo = marketInfos.find(
(mi) => mi.underlyingAddress == state.token.toLowerCase()
);

// Accumulate CSV row data
csvRows.push({
protocol: "Layerbank",
date: blocks.blockTimestamp,
block_number: blocks.blockNumber,
user_address: state.account,
market: state.token,
supply_token: state.lentAmount,
borrow_token: state.borrowAmount,
});
});

return csvRows;
};

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: any[]) => {
console.log(blocks);
const allCsvRows: any[] = []; // Array to accumulate CSV rows for all blocks

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);
for (let i = 0; i < result.length; i++) {
allCsvRows.push(result[i]);
}
} 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);
});
Loading