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: add nile adapter #54

Merged
merged 1 commit into from
Apr 17, 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
33 changes: 33 additions & 0 deletions adapters/nile/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "nile-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",
"prettier": "^3.2.5",
"typescript": "^5.3.3"
}
}
173 changes: 173 additions & 0 deletions adapters/nile/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import fs from "fs";
import { write } from "fast-csv";
import { BlockData, OutputSchemaRow, UserVote } from "./sdk/types";
import {
getV2UserPositionsAtBlock,
getV3UserPositionsAtBlock,
} from "./sdk/positions";
import { getTimestampAtBlock } from "./sdk/common";
import { VE_NILE_ADDRESS, fetchUserVotes } from "./sdk/lensDetails";
import BigNumber from "bignumber.js";

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

const csvRows: OutputSchemaRow[] = [];

for (let block of snapshotBlocks) {
const timestamp = await getTimestampAtBlock(block);
csvRows.push(
...(await getUserTVLByBlock({
blockNumber: block,
blockTimestamp: timestamp,
})),
);
}

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

export const getUserTVLByBlock = async ({
blockNumber,
blockTimestamp,
}: BlockData): Promise<OutputSchemaRow[]> => {
const result: OutputSchemaRow[] = [];

const combinedPositions = await getUserLiquidityTVLByBlock({
blockNumber,
blockTimestamp,
});

const balances: Record<string, Record<string, bigint>> = {};
for (const position of combinedPositions) {
balances[position.user_address] = balances[position.user_address] || {};

if (position.token_balance > 0n)
balances[position.user_address][position.token_address] =
(balances?.[position.user_address]?.[position.token_address] ?? 0n) +
position.token_balance;
}

for (const [user, tokenBalances] of Object.entries(balances)) {
for (const [token, balance] of Object.entries(tokenBalances)) {
result.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: user,
token_address: token,
token_balance: balance,
});
}
}

return result;
};

export const getUserLiquidityTVLByBlock = async ({
blockNumber,
blockTimestamp,
}: BlockData): Promise<OutputSchemaRow[]> => {
const result: OutputSchemaRow[] = [];

const [v2Positions, v3Positions] = await Promise.all([
getV2UserPositionsAtBlock(blockNumber),
getV3UserPositionsAtBlock(blockNumber),
]);

const userAddresses = [...v2Positions, ...v3Positions]
.map((pos) => pos.user)
.reduce(
(prev, curr) => (prev.includes(curr) ? prev : [...prev, curr]),
[] as string[],
);

const userVotes = await getUserVotesTVLByBlock(blockNumber, userAddresses);

// combine v2 & v3
const combinedPositions = [...v2Positions, ...v3Positions];
const balances: Record<string, Record<string, bigint>> = {};
for (const position of combinedPositions) {
balances[position.user] = balances[position.user] || {};

if (position.token0.balance > 0n)
balances[position.user][position.token0.address] =
(balances?.[position.user]?.[position.token0.address] ?? 0n) +
position.token0.balance;

if (position.token1.balance > 0n)
balances[position.user][position.token1.address] =
(balances?.[position.user]?.[position.token1.address] ?? 0n) +
position.token1.balance;
}

for (const [user, tokenBalances] of Object.entries(balances)) {
for (const [token, balance] of Object.entries(tokenBalances)) {
result.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: user,
token_address: token,
token_balance: balance,
});
}
}

for (const userVote of userVotes) {
result.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: userVote.user,
token_address: VE_NILE_ADDRESS,
token_balance: userVote.balance,
});
}

return result;
};

export const getUserVotesTVLByBlock = async (
blockNumber: number,
userAddresses: string[],
): Promise<UserVote[]> => {
const result: UserVote[] = [];
const tokenBalanceMap = {} as {
[userAddress: string]: BigNumber;
};

const userVotesFetch = [];

for (const user of userAddresses) {
userVotesFetch.push(fetchUserVotes(BigInt(blockNumber), user));
}

const userVotesResult = await Promise.all(userVotesFetch);

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

Object.entries(tokenBalanceMap).forEach(([userAddress, balance]) => {
if (balance.dp(0).lte(0)) {
return;
}
result.push({
user: userAddress,
balance: BigInt(balance.dp(0).toNumber()),
});
});
return result;
};

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