Skip to content

Commit

Permalink
lynex adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Apegurus committed Mar 23, 2024
1 parent bac95d8 commit 3fea99f
Show file tree
Hide file tree
Showing 9 changed files with 2,769 additions and 0 deletions.
32 changes: 32 additions & 0 deletions adapters/lynex/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "lynex-adapter",
"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.8.13"
},
"devDependencies": {
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
110 changes: 110 additions & 0 deletions adapters/lynex/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import fs from "fs";
import { write } from "fast-csv";
import { getTimestampAtBlock, getUserAddresses } from "./sdk/subgraphDetails";
import {
VE_LYNX_ADDRESS,
fetchUserPools,
fetchUserVotes,
} from "./sdk/lensDetails";
import BigNumber from "bignumber.js";

interface CSVRow {
block_number: string;
timestamp: string;
user_address: string;
token_address: string;
token_balance: string;
}

const getData = async () => {
const snapshotBlocks = [2999728];

const csvRows: CSVRow[] = [];

for (let block of snapshotBlocks) {
const [userAddresses] = await Promise.all([getUserAddresses(block)]);
console.log(`Block: ${block}`);
console.log("UserAddresses: ", userAddresses.length);

const tokenBalanceMap = {} as {
[userAddress: string]: { [tokenAddress: string]: BigNumber };
};

const userPoolFetch = [];
const userVotesFetch = [];

for (const user of userAddresses) {
userPoolFetch.push(fetchUserPools(BigInt(block), user.id, user.pools));
userVotesFetch.push(fetchUserVotes(BigInt(block), user.id));
}

const userFetchResult = await Promise.all(userPoolFetch);
const userVotesResult = await Promise.all(userVotesFetch);
const block_number = block.toString();
const timestamp = new Date(await getTimestampAtBlock(block)).toISOString();

for (const userFetchedPools of userFetchResult) {
for (const userPool of userFetchedPools) {
const user_address = userPool.result.userAddress.toLowerCase();
const totalLPBalance =
userPool.result.account_lp_balance +
userPool.result.account_gauge_balance;
const total0 =
(totalLPBalance * userPool.result.reserve0) /
userPool.result.total_supply;
const total1 =
(totalLPBalance * userPool.result.reserve1) /
userPool.result.total_supply;
const token0Address = userPool.result.token0.toLowerCase();
const token1Address = userPool.result.token1.toLowerCase();

// Aggregate tokens
tokenBalanceMap[user_address] = tokenBalanceMap[user_address] ?? {};
tokenBalanceMap[user_address][token0Address] = BigNumber(
tokenBalanceMap[user_address][token0Address] ?? 0
).plus(total0.toString());
tokenBalanceMap[user_address] = tokenBalanceMap[user_address] ?? {};
tokenBalanceMap[user_address][token1Address] = BigNumber(
tokenBalanceMap[user_address][token1Address] ?? 0
).plus(total1.toString());
}
}

for (const userFecthedVotes of userVotesResult) {
for (const userVote of userFecthedVotes) {
const user_address = userVote.result.userAddress.toLowerCase();
const token0Address = VE_LYNX_ADDRESS.toLowerCase();
tokenBalanceMap[user_address] = tokenBalanceMap[user_address] ?? {};
tokenBalanceMap[user_address][token0Address] = BigNumber(
tokenBalanceMap[user_address][token0Address] ?? 0
).plus(userVote.result.amount.toString());
}
}

Object.entries(tokenBalanceMap).forEach(([user_address, balances]) => {
Object.entries(balances).forEach(([token_address, token_balance]) => {
if (token_balance.dp(0).lte(0)) {
return;
}
csvRows.push({
block_number,
timestamp,
user_address,
token_address,
token_balance: token_balance.dp(0).toString(),
});
});
});

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

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

0 comments on commit 3fea99f

Please sign in to comment.