-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from mendi-finance/main
feat: add mendi adapter
- Loading branch information
Showing
9 changed files
with
2,548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.