Skip to content

Commit

Permalink
Merge pull request #38 from mendi-finance/main
Browse files Browse the repository at this point in the history
feat: add mendi adapter
  • Loading branch information
RamonReis authored Apr 22, 2024
2 parents 5cac301 + c6c3b74 commit 7899953
Show file tree
Hide file tree
Showing 9 changed files with 2,548 additions and 0 deletions.
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

0 comments on commit 7899953

Please sign in to comment.