-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from CarlWiles/feat/rho-markets
feat: support rho markets block info query
- Loading branch information
Showing
6 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
number block_timestamp | ||
4243360 1714773599 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), "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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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[]> { | ||
// limit: number, | ||
// offset: number, | ||
// data(block: { number: ${blockNumber} }) { | ||
const query = ` | ||
query RHO_MARKETS { | ||
data { | ||
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 []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |