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

MYX Finance : DEX : tvl by user #135

Merged
merged 4 commits into from
May 20, 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
27 changes: 27 additions & 0 deletions adapters/myx/package.json
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"
}
}
94 changes: 94 additions & 0 deletions adapters/myx/src/index.ts
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);
});
7 changes: 7 additions & 0 deletions adapters/myx/src/sdk/config.ts
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"
}
146 changes: 146 additions & 0 deletions adapters/myx/src/sdk/positionSnapshots.ts
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,
Copy link
Contributor

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

Copy link
Contributor Author

@marvin6en marvin6en May 16, 2024

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

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}%`);
}
Loading
Loading