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 mendi adapter #38

Merged
merged 2 commits into from
Apr 22, 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
32 changes: 32 additions & 0 deletions adapters/mendi/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "mendi-tvl",
"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"
}
}
87 changes: 87 additions & 0 deletions adapters/mendi/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { CHAINS, PROTOCOLS } from "./sdk/config";
import {
getLPValueByUserAndPoolFromActivities,
getActivitiesForAddressByPoolAtBlock,
getTimestampAtBlock,
} from "./sdk/subgraphDetails";

(BigInt.prototype as any).toJSON = function () {
return this.toString();
};

import fs from "fs";
import { write } from "fast-csv";
import { getMarketInfos } from "./sdk/marketDetails";
import { bigMath } from "./sdk/abi/helpers";

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

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
usd_price: number; //assign 0 if not available
};

export const getUserTVLByBlock = async (blocks: BlockData) => {
const marketInfos = await getMarketInfos(
"0x1b4d3b0421ddc1eb216d230bc01527422fb93103"
);

const csvRows: OutputDataSchemaRow[] = [];
const block = blocks.blockNumber;

const { tokens, accountBorrows } = await getActivitiesForAddressByPoolAtBlock(
block,
"",
"",
CHAINS.LINEA,
PROTOCOLS.MENDI
);

console.log(`Block: ${block}`);
console.log("Tokens: ", tokens.length);
console.log("Account Borrows: ", accountBorrows.length);

let lpValueByUsers = getLPValueByUserAndPoolFromActivities(
tokens,
accountBorrows
);

const timestamp = await getTimestampAtBlock(block);

lpValueByUsers.forEach((value, owner) => {
value.forEach((amount, market) => {
if (bigMath.abs(amount) < 1) return;

const marketInfo = marketInfos.get(market.toLowerCase());

// Accumulate CSV row data
csvRows.push({
block_number: block,
timestamp: timestamp,
user_address: owner,
token_address: marketInfo?.underlyingAddress ?? "",
token_balance: amount / BigInt(1e18),
token_symbol: marketInfo?.underlyingSymbol ?? "",
usd_price: 0,
});
});
});

// 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.");
});

return csvRows;
};
Loading