Skip to content

Commit

Permalink
Merge branch 'delta-hq:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
sdcrypt0 authored Apr 22, 2024
2 parents 2ec4e4e + 5cac301 commit 3a90ccb
Show file tree
Hide file tree
Showing 75 changed files with 18,649 additions and 47 deletions.
39 changes: 0 additions & 39 deletions .github/workflows/main.yml

This file was deleted.

12 changes: 7 additions & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
name: Print New Folder Name
name: Test getUserTVLByBlock method

on:
pull_request:
types: [assigned, opened, synchronize, reopened]
paths:
- '**/' # Listen for changes in any directory

Expand Down Expand Up @@ -30,9 +29,12 @@ jobs:
uses: actions/[email protected]
with:
ref: ${{ github.event.pull_request.head.sha }}
- name: Run command from t1 folder
- name: Run command from protocol folder
run: |
cd adapters
cd $FOLDER_NAME
# Run your command here
npm run start
npm install
npm run compile
cd ..
npm install
npm run start $FOLDER_NAME
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ The main scripts is generating a output as CSV file.

[Adapter Example](adapters/example/dex/src/index.ts)

## Notes
1. Please don't fork the repo. Just clone it and raise a PR in to the main branch.
2. Please make sure to have a "compile" script in package.json file. So, we are able to compile the typescript files into `dist/index.js` file.

## How to execute this project?

```
Expand Down
14 changes: 14 additions & 0 deletions adapters/celer/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# CELER - TVL by User

In this repo you will find the code responsible to get data from the celer pool the TVL by users.
The main scripts is generating a output as CSV

## How to execute this project?

```
npm install // install all packages
npm run watch //other terminal tab
npm run start // other terminal tab
```

Now you can see the outputData.csv file. That's it.
32 changes: 32 additions & 0 deletions adapters/celer/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "celer",
"version": "1.0.0",
"private": true,
"dependencies": {
"fast-csv": "^5.0.1"
},
"scripts": {
"start": "node dist/index.js",
"dev": "ts-node src/index.ts",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@types/node": "^20.12.7",
"ts-node": "^10.6.0",
"typescript": "^4.6.2"
}
}
67 changes: 67 additions & 0 deletions adapters/celer/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { write } from "fast-csv";
import fs from "fs";

interface BlockData {
blockNumber: number;
blockTimestamp: number;
}
interface entriesData {
user_address: string;
token_address: string;
token_balance: string;
}
interface callbackData {
linea_block_number: string;
linea_block_timestamp: string;
entries: Array<entriesData>;
}

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 (data: BlockData) => {
console.log("downloading...");
const { blockNumber, blockTimestamp } = data;
const csvRows: OutputDataSchemaRow[] = [];
const res = await fetch(
`https://cbridge-prod2.celer.app/v1/getLineaLiquiditySnapshot?linea_block_number=${blockNumber}&linea_block_timestamp=${blockTimestamp}`
);
const resData: callbackData = (await res.json()) as callbackData;
resData.entries?.forEach((item: any) => {
csvRows.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: "0x" + item.user_address,
token_address: item.token_address,
token_balance: item.token_balance,
token_symbol: "",
usd_price: 0,
});
});
return csvRows;
};

const getData = async () => {
// Write the CSV output to a file
const dataList = await getUserTVLByBlock({
blockNumber: 19506984,
blockTimestamp: 1711429021,
});
const ws = fs.createWriteStream("outputData.csv");
write(dataList, { headers: true })
.pipe(ws)
.on("finish", () => {
console.log("CSV file has been written.");
});
};

getData().then(() => {
console.log("Done");
});
20 changes: 20 additions & 0 deletions adapters/celer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"lib": ["es5", "es6"],
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"outDir": "dist",
"resolveJsonModule": true,
"sourceMap": true,
"strict": true,
"target": "es2020"
},
"ts-node": {
"esm": true,
"compiler": "typescript"
},
"include": ["src"]
}
22 changes: 22 additions & 0 deletions adapters/connext/output.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
block_number,timestamp,user_address,token_address,token_balance,token_symbol,usd_price
2954869,1710677449,0x04329901a291484bc461b3b61629389cd0a020df,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,1210808254917845817425,LP,0
2668299,1709531117,0x2ad41b63cf636c794c37293fb8b7613331774e1d,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,55393817407803314274,LP,0
1022900,1701338640,0x2ba4b2effba90779ca446f2e60e7eef4a707d925,0x611c91c807c07b4d358224fb5dcd3999f36167b3,13995840654870840741,LP,0
2531745,1708984899,0x2e47bbd27cf07be8b3c3e46a4f406fc60bb59335,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,492750038786035498752,LP,0
2916886,1710525538,0x4df23313d32b33223d18e1f7437f269d92ed3dd6,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,188,LP,0
1037985,1701459356,0x5d527765252003acee6545416f6a9c8d15ae8402,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,39968772378383533027145,LP,0
2953912,1710673621,0x7231fef69df4648448805c28a79945dd30576372,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,988991644100367474966,LP,0
1713112,1705588962,0x73b6a7a6e39a2e405087164d7f407a0bd2c86dcc,0x611c91c807c07b4d358224fb5dcd3999f36167b3,4993416443158320,LP,0
2288998,1708013774,0x7c8e34d9d5c1e2c40e8860c4511cc132ecb3fa8c,0x611c91c807c07b4d358224fb5dcd3999f36167b3,1795901903915358,LP,0
1952553,1706661478,0x7ce49752ffa7055622f444df3c69598748cb2e5f,0x611c91c807c07b4d358224fb5dcd3999f36167b3,412,LP,0
2776301,1709963126,0x8d6e361347f7dd8f297a3396098dd4343f5bbb45,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,988980051251840569,LP,0
2524689,1708956675,0x998e4a86fc6352037b57f087e60ee816d4d25c16,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,21082536722635166248,LP,0
905571,1700399991,0xade09131c6f43fe22c2cbabb759636c43cfc181e,0x611c91c807c07b4d358224fb5dcd3999f36167b3,40000000000000000,LP,0
964221,1700869352,0xade09131c6f43fe22c2cbabb759636c43cfc181e,0xfb8a9f8b13a6d297a1478af67bde98362be532d6,10217539492437286544516,LP,0
1000729,1701161360,0xade09131c6f43fe22c2cbabb759636c43cfc181e,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,222543182923056325812,LP,0
2890430,1710419642,0xd84076087f8045dcfb78443930423fc366bd79a7,0x611c91c807c07b4d358224fb5dcd3999f36167b3,87,LP,0
2511076,1708902223,0xe0c46719ed5c9d8b2f2f69652a72304aabddd756,0xfb8a9f8b13a6d297a1478af67bde98362be532d6,0,LP,0
2952998,1710669965,0xe5cec7bb5e90ed2548414d49ceb5e5d89f847295,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,718957933202566179473,LP,0
2883051,1710390126,0xe603b396503f312f61be2851f4c1b8783d7c1ea6,0x611c91c807c07b4d358224fb5dcd3999f36167b3,495449646078743738973,LP,0
1157826,1702256219,0xfa53b8cca72c61794ebac3d54b54554aa0f4229d,0xfb8a9f8b13a6d297a1478af67bde98362be532d6,296389691962740119956,LP,0
2873152,1710350530,0xfd9da5905ff0eaf8e7748fd3efab9a2404ad798b,0x66be8926aa5cbdf24f07560d36999bf9b6b2bb87,2957602281459175118717,LP,0
26 changes: 26 additions & 0 deletions adapters/connext/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "connext-calculator",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node dist/index.js",
"dev": "ts-node src/index.ts",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fast-csv": "5.0.1",
"viem": "2.8.16"
},
"devDependencies": {
"@types/node": "20.11.30",
"ts-node": "10.9.2",
"typescript": "5.4.3"
}
}
22 changes: 22 additions & 0 deletions adapters/connext/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { getUserTVLByBlock, writeCsv } from "./utils";

const input = {
blockNumber: 2954869,
blockTimestamp: 1711044141,
}

const fileName = 'output.csv';
console.log('Getting TVL at block:', input.blockNumber);


// returns all user balances at the input block by looking at the latest
// balance for each user and token on the subgraph, capped at given block.
getUserTVLByBlock(input).then((data) => {
if (data.length === 0) {
console.log("no data to write to file");
return;
}
writeCsv(data, fileName).then(() => {
console.log('CSV written to file:', fileName);
})
});
18 changes: 18 additions & 0 deletions adapters/connext/src/utils/csv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { createWriteStream } from 'fs';
import { format } from '@fast-csv/format';

// writes csv from object array with keys as headers
export const writeCsv = async <T = object>(data: T[], filename: string): Promise<void> => {
const stream = format({ headers: true });
const output = createWriteStream(filename);

const completed = new Promise((resolve, reject) => {
stream.pipe(output).on('end', resolve).on('error', reject);
});
// console.log("writing csv to file:", filename);
data.map((row) => stream.write(row));
await completed;
stream.end();
// console.log("csv written to file:", filename);
return;
}
22 changes: 22 additions & 0 deletions adapters/connext/src/utils/getUserTvlByBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { parseUnits } from "viem";
import { getLpAccountBalanceAtBlock } from "./subgraph";
import { BlockData, OutputDataSchemaRow } from "./types";

export const getUserTVLByBlock = async (blocks: BlockData): Promise<OutputDataSchemaRow[]> => {
const { blockNumber } = blocks

const data = await getLpAccountBalanceAtBlock(blockNumber);

return data.map(d => {
return {
block_number: +d.block,
timestamp: +d.modified,
user_address: d.account.id,
token_address: d.token.id,
token_balance: BigInt(parseUnits(d.amount, 18)),
token_symbol: d.token.symbol,
usd_price: 0
}
})
};

3 changes: 3 additions & 0 deletions adapters/connext/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./csv";
export * from "./getUserTvlByBlock";
export * from "./types";
Loading

0 comments on commit 3a90ccb

Please sign in to comment.