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

feat(mitosis): support miezETH #297

Merged
merged 3 commits into from
Aug 27, 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/mitosis/hourly_blocks.csv
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ number,timestamp
4881275,1716263772
4891275,1716233771
6653493,1720710200
8635572,1724682125
8635660,1724682301
101 changes: 55 additions & 46 deletions adapters/mitosis/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import fs from 'fs';
import fs from "fs";
import { write } from "fast-csv";
import csv from "csv-parser";

const assetWhitelist = [
'miweETH',
'miuniETH',
];
const assetWhitelist = ["miezETH", "miweETH", "miuniETH"];

const GRAPHQL_ENDPOINT = "https://api.goldsky.com/api/public/project_clxioqhjdzy1901wmgqmp2ygj/subgraphs/mitosis-linea-lxp/1.1.1/gn";
const GRAPHQL_ENDPOINT =
"https://api.goldsky.com/api/public/project_clxioqhjdzy1901wmgqmp2ygj/subgraphs/mitosis-linea-lxp/1.1.3/gn";

const makeBalancesQuery = (blockNumber: number, next = "") => `query {
tokenBalances(
Expand Down Expand Up @@ -69,25 +67,35 @@ async function post<T = any>(url: string, query: any): Promise<{ data: T }> {
});

return response.json();
};
}

const toOutput = ({ blockNumber, blockTimestamp }: BlockData, { tokenBalances }: TokenBalancesResponse): OutputDataSchemaRow[] =>
tokenBalances.map((v) => ({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: v.account.id,
token_address: v.token.id,
token_balance: BigInt(v.value),
token_symbol: v.token.symbol,
usd_price: 0
})).filter((v) => assetWhitelist.includes(v.token_symbol));

export const getUserTVLByBlock = async (blocks: BlockData): Promise<OutputDataSchemaRow[]> => {
const toOutput = (
{ blockNumber, blockTimestamp }: BlockData,
{ tokenBalances }: TokenBalancesResponse
): OutputDataSchemaRow[] =>
tokenBalances
.map((v) => ({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: v.account.id,
token_address: v.token.id,
token_balance: BigInt(v.value),
token_symbol: v.token.symbol,
usd_price: 0,
}))
.filter((v) => assetWhitelist.includes(v.token_symbol));

export const getUserTVLByBlock = async (
blocks: BlockData
): Promise<OutputDataSchemaRow[]> => {
let next = "";
let output: OutputDataSchemaRow[] = [];

while (true) {
const { data: resp } = await post<TokenBalancesResponse>(GRAPHQL_ENDPOINT, makeBalancesQuery(blocks.blockNumber, next));
const { data: resp } = await post<TokenBalancesResponse>(
GRAPHQL_ENDPOINT,
makeBalancesQuery(blocks.blockNumber, next)
);
if (resp.tokenBalances.length === 0) break;

output = output.concat(toOutput(blocks, resp));
Expand All @@ -103,46 +111,47 @@ const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv()) // Specify the separator as '\t' for TSV files
.on('data', (row) => {
.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', () => {
.on("end", () => {
resolve();
})
.on('error', (err) => {
.on("error", (err) => {
reject(err);
});
});

return blocks;
};

readBlocksFromCSV('hourly_blocks.csv').then(async (blocks: any[]) => {
console.log(blocks);
const allCsvRows: any[] = [];

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);
allCsvRows.push(...result);
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
readBlocksFromCSV("hourly_blocks.csv")
.then(async (blocks: any[]) => {
console.log(blocks);
const allCsvRows: any[] = [];

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);
allCsvRows.push(...result);
} 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;
});
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);
});

}).catch((err) => {
console.error('Error reading CSV file:', err);
});
14 changes: 14 additions & 0 deletions adapters/mitosis/test/sample_output_data.csv
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,17 @@ block_number,timestamp,user_address,token_address,token_balance,token_symbol,usd
6653493,1720710200,0xfffff9b1c2c387b1e3d19af292d91d913374f42b,0x3478de5e82431676c87113001bbeeb359cb5eaa5,9164866447070119,miweETH,0
6653493,1720710200,0x1206608fdc30925492b9afcd32aea0a9e579a03a,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,3733150882302883,miuniETH,0
6653493,1720710200,0x13a4b4f78ab4b93ec1cecf963e19f26f4214578e,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,33581033672474670,miuniETH,0
8635572,1724682125,0xefad0d64546c5bf9019b5815f68535488d025c61,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,263245513807494718,miuniETH,0
8635572,1724682125,0xf6ccf8751fd02de2e14cdf0ff0aa36d6e7a1ab28,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,27232824043906343,miuniETH,0
8635572,1724682125,0xf6ceecf804eb2e0add76f1f8e00ca903fa30ffd5,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,38992690497099636,miuniETH,0
8635572,1724682125,0xb517d68a0a0ea0619423a03fc043537185431b83,0x96d6ce4e83db947ff6bd1ab0b377f23cd5d9ec2d,1000000000000000,miezETH,0
8635660,1724682301,0x00000001b01122945d67cf7c972f1a2063ca4008,0x3478de5e82431676c87113001bbeeb359cb5eaa5,221020382526712301,miweETH,0
8635660,1724682301,0x00000002d88f9b3f4eb303564817fff4adcde46f,0x3478de5e82431676c87113001bbeeb359cb5eaa5,515016007998652343,miweETH,0
8635660,1724682301,0x0000000452bbfc5541279949e5e9400821a0c92d,0x3478de5e82431676c87113001bbeeb359cb5eaa5,164229002009667173,miweETH,0
8635660,1724682301,0xfffc36c3134daaa997d499b0ee918dfff1d705c3,0x3478de5e82431676c87113001bbeeb359cb5eaa5,129504578905856451,miweETH,0
8635660,1724682301,0xfffcbafd962feb657b5fd1f54bc2f58c18977752,0x3478de5e82431676c87113001bbeeb359cb5eaa5,0,miweETH,0
8635660,1724682301,0xfffff9b1c2c387b1e3d19af292d91d913374f42b,0x3478de5e82431676c87113001bbeeb359cb5eaa5,9164866447070119,miweETH,0
8635660,1724682301,0x1206608fdc30925492b9afcd32aea0a9e579a03a,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,3733150882302883,miuniETH,0
8635660,1724682301,0x13a4b4f78ab4b93ec1cecf963e19f26f4214578e,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,33581033672474670,miuniETH,0
8635660,1724682301,0x1429ae34844b7dc55e33817f8b42085b49afe453,0x56ced49205e5d9b4d8d9b29f4abfbe7bb8b08768,108294776457294118,miuniETH,0
8635660,1724682301,0xb517d68a0a0ea0619423a03fc043537185431b83,0x96d6ce4e83db947ff6bd1ab0b377f23cd5d9ec2d,0,miezETH,0
Loading