Skip to content

Commit

Permalink
Merge pull request #252 from DODOEX/main
Browse files Browse the repository at this point in the history
dodo: dex : tvl by user
  • Loading branch information
0xroll authored Jul 22, 2024
2 parents b98b2d4 + 8c08e84 commit 5a43b9e
Show file tree
Hide file tree
Showing 7 changed files with 1,035 additions and 0 deletions.
33 changes: 33 additions & 0 deletions adapters/dodo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "dodo-calculator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm run compile && node dist/index.js",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@types/big.js": "^6.2.2",
"axios": "^1.6.8",
"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",
"moment": "^2.30.1",
"tiny-invariant": "^1.3.1",
"toformat": "^2.0.0"
},
"devDependencies": {
"@types/node": "^20.11.17",
"typescript": "^5.3.3"
}
}
183 changes: 183 additions & 0 deletions adapters/dodo/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import { AMM_TYPES, CHAINS, PROTOCOLS } from "./sdk/config";
import {
getLPValueByUserAndPoolFromPositions,
getPositionDetailsFromPosition,
getPositionsForAddressByPoolAtBlock,
getTokenPriceFromPositions,
} from "./sdk/subgraphDetails";
(BigInt.prototype as any).toJSON = function () {
return this.toString();
};

import stream from "stream";
import { promisify } from "util";

//Uncomment the following lines to test the getPositionAtBlock function

// const position = getPositionAtBlock(
// 0, // block number 0 for latest block
// 2, // position id
// CHAINS.MODE, // chain id
// PROTOCOLS.SUPSWAP, // protocol
// AMM_TYPES.UNISWAPV3 // amm type
// );
// position.then((position) => {
// // print response
// const result = getPositionDetailsFromPosition(position);
// console.log(`${JSON.stringify(result,null, 4)}
// `)
// });

interface LPValueDetails {
pool: string;
lpValue: string;
}

interface UserLPData {
totalLP: string;
pools: LPValueDetails[];
}

// Define an object type that can be indexed with string keys, where each key points to a UserLPData object
interface OutputData {
[key: string]: UserLPData;
}

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

interface CSVRow {
block_number: number;
timestamp: number;
user_address: string;
token_address: string;
token_balance: bigint;
token_symbol: string;
usd_price: number;
}

const pipeline = promisify(stream.pipeline);

// Assuming you have the following functions and constants already defined
// getPositionsForAddressByPoolAtBlock, CHAINS, PROTOCOLS, AMM_TYPES, getPositionDetailsFromPosition, getLPValueByUserAndPoolFromPositions, BigNumber

// const readBlocksFromCSV = async (filePath: string): Promise<number[]> => {
// const blocks: number[] = [];
// await pipeline(
// fs.createReadStream(filePath),
// csv(),
// async function* (source) {
// for await (const chunk of source) {
// // Assuming each row in the CSV has a column 'block' with the block number
// if (chunk.block) blocks.push(parseInt(chunk.block, 10));
// }
// }
// );
// return blocks;
// };

// const getData = async () => {
// const snapshotBlocks = [2862898, 2892898]; //await readBlocksFromCSV('src/sdk/mode_chain_daily_blocks.csv');

// const csvRows: CSVRow[] = [];

// for (let block of snapshotBlocks) {
// const positions = await getPositionsForAddressByPoolAtBlock(
// block,
// "",
// "",
// CHAINS.LINEA,
// PROTOCOLS.DODOEX,
// AMM_TYPES.DODO
// );

// console.log(`Block: ${block}`);
// console.log("Positions: ", positions.length);

// // Assuming this part of the logic remains the same
// let positionsWithUSDValue = [];
// // Add price to token
// await getTokenPriceFromPositions(positions, "linea");
// for (let position of positions) {
// const res = await getPositionDetailsFromPosition(position);
// positionsWithUSDValue.push(res);
// }
// let lpValueByUsers = await getLPValueByUserAndPoolFromPositions(
// positionsWithUSDValue
// );

// lpValueByUsers.forEach((value, key) => {
// let positionIndex = 0; // Define how you track position index
// value.forEach((lpValue, poolKey) => {
// const lpValueStr = lpValue.toString();
// // Accumulate CSV row data
// csvRows.push({
// block_number: block,
// user_address: key,
// pool: poolKey,
// position: positions.length, // Adjust if you have a specific way to identify positions
// lpvalue: lpValueStr,
// });
// });
// });
// }

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

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


export const getUserTVLByBlock = async (blocks: BlockData) => {
const { blockNumber, blockTimestamp } = blocks
const positions = await getPositionsForAddressByPoolAtBlock(
blockNumber,
"",
"",
CHAINS.LINEA,
PROTOCOLS.DODOEX,
AMM_TYPES.DODO
);

console.log(`Block: ${blockNumber}`);
console.log("Positions: ", positions.length);

// Assuming this part of the logic remains the same
let positionsWithUSDValue = [];
// Add price to token
await getTokenPriceFromPositions(positions, "linea");
for (let position of positions) {
const res = await getPositionDetailsFromPosition(position);
positionsWithUSDValue.push(res);
}
let lpValueByUsers = await getLPValueByUserAndPoolFromPositions(
positionsWithUSDValue
);

const csvRows: CSVRow[] = [];
lpValueByUsers.forEach((value, owner) => {
value.forEach((tokenBalance, tokenAddress) => {
csvRows.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: owner,
token_address: tokenAddress,
token_symbol: tokenBalance.tokenSymbol,
token_balance: tokenBalance.tokenBalance,
usd_price: tokenBalance.usdPrice,
});
});
});

return csvRows
}
32 changes: 32 additions & 0 deletions adapters/dodo/src/sdk/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export const enum CHAINS {
MODE = 34443,
LINEA = 59144,
}
export const enum PROTOCOLS {
SUPSWAP = 0,
DODOEX = 1,
}

export const enum AMM_TYPES {
UNISWAPV3 = 0,
DODO = 1,
}

export const SUBGRAPH_URLS = {
[CHAINS.MODE]: {
[PROTOCOLS.SUPSWAP]: {
[AMM_TYPES.UNISWAPV3]:
"https://api.goldsky.com/api/public/project_clrhmyxsvvuao01tu4aqj653e/subgraphs/supswap-exchange-v3/1.0.0/gn",
},
},
[CHAINS.LINEA]: {
[PROTOCOLS.DODOEX]: {
[AMM_TYPES.DODO]:
"https://api.dodoex.io/graphql?chainId=59144&schemaName=dodoex&apikey=graphqldefiLlamadodoYzj5giof",
},
},
};
export const RPC_URLS = {
[CHAINS.MODE]: "https://rpc.goldsky.com",
[CHAINS.LINEA]: "https://rpc.linea.build",
};
Loading

0 comments on commit 5a43b9e

Please sign in to comment.