Skip to content

Commit

Permalink
made some structural changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nitish-91 committed Mar 21, 2024
1 parent fb9ac94 commit 3242056
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 108 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ Please complete the following:
1. With path being `/<your_protocol_handle>`
4. Submit your contract addresses through this [Form](https://forms.gle/DJ2975hZwhz32t5r6)

### Code Changes Expected

1. Create a function like below:

export const getUserTVLByBlock = async (blocks: BlockData) => {
const { blockNumber, blockTimestamp } = blocks
// Retrieve data using block number and timestamp
// YOUR LOGIC HERE
return csvRows

};

2. Interface for input Block Data is, in below blockTimestamp is in epoch format.

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

3. Output "csvRow" is a list.
const csvRows: OutputDataSchemaRow[] = [];

type OutputDataSchemaRow = {
block_number: number;
timestamp: number;
user_address: string;
token_address: string;
token_balance: bigint;
token_symbol: string; //token symbol should be empty string if it is not available
};

4. You can check the index.ts in Gravita project to refer this in use.

### Data Requirement
Goal: **Hourly snapshot of TVL by User by Asset**

Expand Down
4 changes: 2 additions & 2 deletions adapters/example/dex/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"start": "dist/example/dex/src/index.js",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist"
Expand All @@ -28,4 +28,4 @@
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
}
201 changes: 144 additions & 57 deletions adapters/example/dex/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { getLPValueByUserAndPoolFromPositions, getPositionAtBlock, getPositionDe
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};
// @ts-ignore
import { main, getUserTVLByBlock } from "/Users/nitishsharma/Desktop/git-obl/l2-lxp-liquidity-reward/adapters/gravita/dist/index.js";

import { promisify } from 'util';
import stream from 'stream';
import csv from 'csv-parser';
import fs from 'fs';
import { format } from 'fast-csv';
import { write } from 'fast-csv';
import { pipeline as streamPipeline } from 'stream';
import { captureRejectionSymbol } from "events";


//Uncomment the following lines to test the getPositionAtBlock function

Expand Down Expand Up @@ -57,70 +62,152 @@ const pipeline = promisify(stream.pipeline);
// Assuming you have the following functions and constants already defined
// getPositionsForAddressByPoolAtBlock, CHAINS, PROTOCOLS, AMM_TYPES, getPositionDetailsFromPosition, getLPValueByUserAndPoolFromPositions, BigNumber

const readBlocksFromCSV = async (filePath: string): Promise<number[]> => {
const blocks: number[] = [];
await pipeline(
fs.createReadStream(filePath),
csv(),
async function* (source) {
for await (const chunk of source) {
// Assuming each row in the CSV has a column 'block' with the block number
if (chunk.block) blocks.push(parseInt(chunk.block, 10));
}
}
);
// const readBlocksFromCSV = async (filePath: string): Promise<number[]> => {
// const blocks: number[] = [];
// await pipeline(
// fs.createReadStream(filePath),
// csv(),
// async function* (source) {
// for await (const chunk of source) {
// // Assuming each row in the CSV has a column 'block' with the block number
// if (chunk.block) blocks.push(parseInt(chunk.block, 10));
// }
// }
// );
// return blocks;
// };


// const getData = async () => {
// const snapshotBlocks = [
// 3116208, 3159408, 3202608, 3245808, 3289008, 3332208,
// 3375408, 3418608, 3461808, 3505008, 3548208, 3591408,
// 3634608, 3677808, 3721008, 3764208, 3807408, 3850608,
// 3893808, 3937008, 3980208, 3983003,
// ]; //await readBlocksFromCSV('src/sdk/L2_CHAIN_ID_chain_daily_blocks.csv');

// const csvRows: CSVRow[] = [];

// for (let block of snapshotBlocks) {
// const positions = await getPositionsForAddressByPoolAtBlock(
// block, "", "", CHAINS.L2_CHAIN_ID, PROTOCOLS.PROTOCOL_NAME, AMM_TYPES.UNISWAPV3
// );

// console.log(`Block: ${block}`);
// console.log("Positions: ", positions.length);

// // Assuming this part of the logic remains the same
// let positionsWithUSDValue = positions.map(getPositionDetailsFromPosition);
// let lpValueByUsers = getLPValueByUserAndPoolFromPositions(positionsWithUSDValue);

// lpValueByUsers.forEach((value, key) => {
// let positionIndex = 0; // Define how you track position index
// value.forEach((lpValue, poolKey) => {
// const lpValueStr = lpValue.toString();
// // Accumulate CSV row data
// csvRows.push({
// user: key,
// pool: poolKey,
// block,
// position: positions.length, // Adjust if you have a specific way to identify positions
// lpvalue: lpValueStr,
// });
// });
// });
// }

// // Write the CSV output to a file
// const ws = fs.createWriteStream('outputData.csv');
// write(csvRows, { headers: true }).pipe(ws).on('finish', () => {
// console.log("CSV file has been written.");
// });
// };




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

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

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

const getData = async () => {
const snapshotBlocks = [
3116208, 3159408, 3202608, 3245808, 3289008, 3332208,
3375408, 3418608, 3461808, 3505008, 3548208, 3591408,
3634608, 3677808, 3721008, 3764208, 3807408, 3850608,
3893808, 3937008, 3980208, 3983003,
]; //await readBlocksFromCSV('src/sdk/L2_CHAIN_ID_chain_daily_blocks.csv');

const csvRows: CSVRow[] = [];

for (let block of snapshotBlocks) {
const positions = await getPositionsForAddressByPoolAtBlock(
block, "", "", CHAINS.L2_CHAIN_ID, PROTOCOLS.PROTOCOL_NAME, AMM_TYPES.UNISWAPV3
);

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

// Assuming this part of the logic remains the same
let positionsWithUSDValue = positions.map(getPositionDetailsFromPosition);
let lpValueByUsers = getLPValueByUserAndPoolFromPositions(positionsWithUSDValue);

lpValueByUsers.forEach((value, key) => {
let positionIndex = 0; // Define how you track position index
value.forEach((lpValue, poolKey) => {
const lpValueStr = lpValue.toString();
// Accumulate CSV row data
csvRows.push({
user: key,
pool: poolKey,
block,
position: positions.length, // Adjust if you have a specific way to identify positions
lpvalue: lpValueStr,
});
});
});
}
readBlocksFromCSV('/Users/nitishsharma/Downloads/block_numbers.tsv')
.then(async (blocks) => {
console.log(blocks);
const allCsvRows: any[] = []; // Array to accumulate CSV rows for all blocks
const batchSize = 10; // Size of batch to trigger writing to the file
let i = 0;

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);

// Accumulate CSV rows for all blocks
allCsvRows.push(...result);

i++;
console.log(`Processed block ${i}`);

// Write to file when batch size is reached or at the end of loop
if (i % batchSize === 0 || i === blocks.length) {
const ws = fs.createWriteStream(`outputData.csv`, { flags: i === batchSize ? 'w' : 'a' });
write(allCsvRows, { headers: i === batchSize ? true : false })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
});

// Clear the accumulated CSV rows
allCsvRows.length = 0;
}
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
}

// Write the CSV output to a file
const ws = fs.createWriteStream('outputData.csv');
write(csvRows, { headers: true }).pipe(ws).on('finish', () => {
console.log("CSV file has been written.");
}
})
.catch((err) => {
console.error('Error reading CSV file:', err);
});
};

getData().then(() => {
console.log("Done");
});


// main().then(() => {
// console.log("Done");
// });
// getPrice(new BigNumber('1579427897588720602142863095414958'), 6, 18); //Uniswap
// getPrice(new BigNumber('3968729022398277600000000'), 18, 6); //PROTOCOL_NAME

26 changes: 9 additions & 17 deletions adapters/example/dex/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig to read more about this file */

/* Projects */
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */

/* Language and Environment */
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
// "jsx": "preserve", /* Specify what JSX code is generated. */
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
Expand All @@ -23,10 +21,9 @@
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "src/", /* Specify the root folder within your source files. */
"module": "commonjs", /* Specify what module code is generated. */
"rootDir": "/Users/nitishsharma/Desktop/git-obl/l2-lxp-liquidity-reward/adapters/", /* Specify the root folder within your source files. */
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
Expand All @@ -42,20 +39,18 @@
// "resolveJsonModule": true, /* Enable importing .json files. */
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */

/* JavaScript Support */
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */

/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
"outDir": "dist/", /* Specify an output folder for all emitted files. */
"outDir": "dist/", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
// "noEmit": true, /* Disable emitting files from a compilation. */
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
Expand All @@ -72,17 +67,15 @@
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */

/* Interop Constraints */
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */

"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true, /* Enable all strict type-checking options. */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
Expand All @@ -101,9 +94,8 @@
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */

/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
}
3 changes: 2 additions & 1 deletion adapters/gravita/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "commonjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
Expand All @@ -20,4 +21,4 @@
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
}
Loading

0 comments on commit 3242056

Please sign in to comment.