Skip to content

Commit

Permalink
Merge branch 'main' into feat/layerbank_wip
Browse files Browse the repository at this point in the history
  • Loading branch information
layerbankgoldsky authored Jun 19, 2024
2 parents fd4514c + a386864 commit 1921a71
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 0 deletions.
2 changes: 2 additions & 0 deletions adapters/rhomarkets/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/rhomarkets/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "rhomarkets-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"
}
}
36 changes: 36 additions & 0 deletions adapters/rhomarkets/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import fs from "fs";
import { write } from "fast-csv";
import { getUserTVLByBlock, readBlocksFromCSV } from "./sdk";
import path from "path";

const filePath = path.join(process.cwd(), "hourly_blocks.csv");

readBlocksFromCSV(filePath)
.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) => {
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);
});
61 changes: 61 additions & 0 deletions adapters/rhomarkets/src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from "fs";
import csv from "csv-parser";
import { fetchGraphQLData } from "./request";

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

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

export const getUserTVLByBlock = async (blocks: BlockData) => {
const protocalInfos = await fetchGraphQLData(blocks.blockNumber);

const csvRows: OutputDataSchemaRow[] = protocalInfos.map((item) => ({
protocol: "RhoMarkets",
date: blocks.blockTimestamp,
block_number: blocks.blockNumber,
user_address: item.user_address,
market: item.market,
supply_token: item.supply_token,
borrow_token: item.borrow_token,
}));

return csvRows;
};

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

await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv({ separator: "\t" })) // Specify the separator as '\t' for TSV files
.on("data", (row) => {
const blockNumber = parseInt(row.number, 10);
const blockTimestamp = parseInt(row.block_timestamp, 10);

if (!isNaN(blockNumber) && blockTimestamp) {
blocks.push({ blockNumber: blockNumber, blockTimestamp });
}
})
.on("end", () => {
resolve();
})
.on("error", (err) => {
reject(err);
});
});

return blocks;
};
70 changes: 70 additions & 0 deletions adapters/rhomarkets/src/sdk/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
interface ProtocalData {
timestamp: number;
block_number: number;
user_address: string;
market: string;
supply_token: number;
borrow_token: number;
}

interface GraphQLResponse {
data: {
data: ProtocalData[];
};
errors?: any[];
}

const query = `
query ($limit: Int, $offset: Int, $blockNumber: Int) {
data(first: $limit, skip: $offset, block: { number: $blockNumber }) {
timestamp
block_number
user_address
market
supply_token
borrow_token
}
}
`;

export async function fetchGraphQLData(
blockNumber: number
): Promise<ProtocalData[]> {
const query = `
query RHO_MARKETS {
data(blockNumber: ${blockNumber}) {
timestamp
block_number
user_address
market
supply_token
borrow_token
}
}
`;

try {
const response = await fetch(
"https://drgstmbns1.execute-api.us-east-1.amazonaws.com/default/RhoMarketPoints",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ query }),
}
);

const responseData: GraphQLResponse = await response.json();
if (responseData.errors) {
console.error("GraphQL errors:", responseData.errors);
return [];
}
return responseData.data.data;
} catch (error) {
console.error("Error fetching data:", error);
return [];
}
}
12 changes: 12 additions & 0 deletions adapters/rhomarkets/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "es2022",
"module": "commonjs",
"rootDir": "src/",
"outDir": "dist/",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}

0 comments on commit 1921a71

Please sign in to comment.