-
Notifications
You must be signed in to change notification settings - Fork 85
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
MYX Finance : DEX : tvl by user #135
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,27 @@ | ||
{ | ||
"name": "myx", | ||
"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": { | ||
"csv-parser": "^3.0.0", | ||
"fast-csv": "^5.0.1", | ||
"tiny-invariant": "^1.3.1", | ||
"toformat": "^2.0.0", | ||
"decimal.js": "^10.4.3" | ||
}, | ||
"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,94 @@ | ||
import { | ||
getPositionsForAddressByPoolAtBlock as getSyncSwapPositionsForAddressByPoolAtBlock | ||
} from "./sdk/positionSnapshots" | ||
|
||
import {promisify} from 'util'; | ||
import stream from 'stream'; | ||
import csv from 'csv-parser'; | ||
import fs from 'fs'; | ||
import {write} from 'fast-csv'; | ||
|
||
|
||
export interface OutputDataSchemaRow { | ||
block_number: number; //block_number which was given as input | ||
timestamp: number; // block timestamp which was given an input, epoch format | ||
user_address: string; // wallet address, all lowercase | ||
token_address: string; // token address all lowercase | ||
token_balance: bigint; // token balance, raw amount. Please dont divide by decimals | ||
token_symbol: string; //token symbol should be empty string if it is not available | ||
usd_price: number; //assign 0 if not available | ||
} | ||
|
||
interface BlockData { | ||
blockNumber: number; | ||
blockTimestamp: number; | ||
} | ||
|
||
|
||
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<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; | ||
}; | ||
|
||
export const getUserTVLByBlock = async (blocks: BlockData) => { | ||
const {blockNumber, blockTimestamp} = blocks | ||
// Retrieve data using block number and timestamp | ||
|
||
const csvRows = await getSyncSwapPositionsForAddressByPoolAtBlock(blockNumber); | ||
// console.log(csvRows); | ||
return csvRows; | ||
}; | ||
|
||
readBlocksFromCSV('hourly_blocks.csv').then(async (blocks: any[]) => { | ||
console.log(blocks); | ||
const allCsvRows: any[] = []; | ||
|
||
// test | ||
// const result = await getUserTVLByBlock({blockNumber:4605383, blockTimestamp: 1715864188}); | ||
// allCsvRows.push(...result); | ||
|
||
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; | ||
}); | ||
}); | ||
|
||
}).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,7 @@ | ||
export const enum CHAINS{ | ||
LINEA = 59144, | ||
} | ||
|
||
export const SUBGRAPH_URLS = { | ||
[CHAINS.LINEA]: "https://subgraph-linea.myx.finance/subgraphs/name/myx-subgraph" | ||
} |
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,146 @@ | ||
import {CHAINS, SUBGRAPH_URLS} from "./config"; | ||
import Decimal from "decimal.js"; | ||
import {OutputDataSchemaRow} from "../index"; | ||
|
||
|
||
interface LiquidityPositionSnapshot { | ||
recipient: string | ||
lPToken: { | ||
id: string | ||
address: string | ||
symbol: string | ||
} | ||
token0: { | ||
id: string | ||
address: string | ||
symbol: string | ||
} | ||
token1: { | ||
id: string | ||
address: string | ||
symbol: string | ||
} | ||
pairIndex: number | ||
lpAmount: bigint | ||
token0Amount: string | ||
token1Amount: string | ||
block: number | ||
timestamp: number | ||
} | ||
|
||
interface SubgraphResponse { | ||
data: { | ||
userLPStats: LiquidityPositionSnapshot[] | ||
} | ||
} | ||
|
||
export const getPositionsForAddressByPoolAtBlock = async ( | ||
snapshotBlockNumber: number | ||
): Promise<OutputDataSchemaRow[]> => { | ||
const userPositionSnapshotsAtBlockData: OutputDataSchemaRow[] = [] | ||
let snapshotsArrays: LiquidityPositionSnapshot[] = [] | ||
const snapshotsMap = new Map<string, Map<string, LiquidityPositionSnapshot>>() // user => pool => snapshot | ||
let skip = 0 | ||
const b_end = snapshotBlockNumber | ||
let b_start = 0 | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
let query = ` | ||
query filterSnapshots { | ||
userLPStats ( | ||
skip: ${skip}, | ||
first: 1000, | ||
orderBy: block, | ||
orderDirection: asc, | ||
where: { | ||
block_gt: ${b_start}, | ||
block_lte: ${b_end}, | ||
} | ||
) { | ||
lPToken { | ||
address | ||
symbol | ||
} | ||
token0 { | ||
address | ||
symbol | ||
} | ||
token1 { | ||
address | ||
symbol | ||
} | ||
pairIndex | ||
recipient | ||
lpAmount | ||
token0Amount | ||
token1Amount | ||
timestamp | ||
} | ||
} | ||
` | ||
const res = await fetch(SUBGRAPH_URLS[CHAINS.LINEA], { | ||
method: "POST", | ||
body: JSON.stringify({query}), | ||
headers: {"Content-Type": "application/json"}, | ||
}).then(res => res.json()) as SubgraphResponse | ||
snapshotsArrays = snapshotsArrays.concat(res.data.userLPStats) | ||
if (res.data.userLPStats.length !== 1000) { | ||
break | ||
} | ||
skip += 1000 | ||
if (skip > 5000) { | ||
skip = 0 | ||
b_start = snapshotsArrays[snapshotsArrays.length - 1].block + 1 | ||
} | ||
writeProgress(b_end, b_start, b_end) | ||
} | ||
for (const snapshot of snapshotsArrays) { | ||
let userPositionSnapshotMap = snapshotsMap.get(snapshot.recipient) | ||
if (!userPositionSnapshotMap) { | ||
userPositionSnapshotMap = new Map<string, LiquidityPositionSnapshot>() | ||
} | ||
userPositionSnapshotMap.set(snapshot.lPToken.address, snapshot) | ||
snapshotsMap.set(snapshot.recipient, userPositionSnapshotMap) | ||
} | ||
snapshotsMap.forEach((userPositionSnapshotMap => { | ||
userPositionSnapshotMap.forEach((positionSnapshot) => { | ||
userPositionSnapshotsAtBlockData.push({ | ||
user_address: positionSnapshot.recipient, | ||
timestamp: Number(new Date(positionSnapshot.timestamp * 1000).toISOString()), | ||
token_address: positionSnapshot.token0.address, | ||
block_number: snapshotBlockNumber, | ||
token_symbol: positionSnapshot.token0.symbol, | ||
token_balance: BigInt(new Decimal(positionSnapshot.token0Amount).toFixed(0)), | ||
usd_price: 0 | ||
}) | ||
|
||
const exists = userPositionSnapshotsAtBlockData.find((value) => { | ||
return value.user_address == positionSnapshot.recipient && value.token_address == positionSnapshot.token1.address; | ||
}) | ||
if (exists) { | ||
exists.token_balance = BigInt(new Decimal(positionSnapshot.token1Amount).add(exists.token_balance.toString()).toFixed(0)); | ||
} else { | ||
userPositionSnapshotsAtBlockData.push({ | ||
user_address: positionSnapshot.recipient, | ||
timestamp: Number(new Date(positionSnapshot.timestamp * 1000).toISOString()), | ||
token_address: positionSnapshot.token1.address, | ||
block_number: snapshotBlockNumber, | ||
token_symbol: positionSnapshot.token1.symbol, | ||
token_balance: BigInt(new Decimal(positionSnapshot.token1Amount).toFixed(0)), | ||
usd_price: 0 | ||
}) | ||
} | ||
}) | ||
})) | ||
return userPositionSnapshotsAtBlockData | ||
} | ||
|
||
|
||
function writeProgress(endBlock: number, numCompleted: number, total: number): void { | ||
const percentage_progress = (numCompleted / total * 100).toFixed(2); | ||
const filled_bar = Math.floor(parseFloat(percentage_progress) / 10); | ||
const empty_bar = 10 - filled_bar; | ||
process.stdout.clearLine(0); | ||
process.stdout.cursorTo(0); | ||
process.stdout.write(`Block ${endBlock} - Progress:[${'#'.repeat(filled_bar)}${'-'.repeat(empty_bar)}] ${percentage_progress}%`); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this return multiple block numbers for 1 block input?
Same question for timestamp as well
@marvin6en
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
at line 55-58,Already filtered based on blocknumber