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
miler012 authored May 28, 2024
2 parents ce9cdfa + ae9767e commit 06eb534
Show file tree
Hide file tree
Showing 130 changed files with 15,336 additions and 2,365 deletions.
5 changes: 4 additions & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@
}).catch((err) => {
console.error('Error reading CSV file:', err);
});
```
```
- [ ] **Your code is handling Pagination to make sure all data for a given block is returned**

2. **Output data**
- [ ] Created a folder test on the same level as src and put sample outputData.csv with 15-20 records generated by your code
- [ ] Data is returned for underlying tokens only. Not for special tokens (lp/veTokens etc)
- [ ] Follows the exact sequence mentioned in OutputDataSchemaRow . This is needed as we want same column ordering in output csv
- Value of each field is :
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/deploy-prod.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Trigger deployment to production

on:
push:
branches:
- main
paths:
- adapters/**

jobs:
deploy-docker-image:
runs-on: ubuntu-latest
steps:
- name: Trigger workflow
uses: peter-evans/repository-dispatch@v2
with:
token: ${{ secrets.GH_PAT }}
repository: delta-hq/openblocklabs-airflow-mwaa
event-type: trigger-workflow
client-payload: '{"ref": "main", "ENV": "prod"}'
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ dist/
Ds_Store
.Ds_Store
package-lock.json
bun.lockb
outputData.csv
30 changes: 30 additions & 0 deletions adapters/3ADAO/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "3a-dao",
"version": "1.0.0",
"description": "3A DAO Adapter",
"main": "index.js",
"type": "commonjs",
"scripts": {
"start": "node dist/index.js",
"compile": "tsc",
"watch": "tsc -w",
"clear": "rm -rf dist",
"test": "node "
},
"keywords": [],
"author": "0xCR6",
"license": "ISC",
"dependencies": {
"axios": "^1.6.8",
"csv-parser": "^3.0.0",
"ethers": "^6.11.1",
"fast-csv": "^5.0.1",
"ts-node": "^10.9.2",
"write": "^2.0.0"
},
"devDependencies": {
"@types/node": "^20.11.17",
"@types/write": "^2.0.4",
"typescript": "^5.3.3"
}
}
120 changes: 120 additions & 0 deletions adapters/3ADAO/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import * as fs from "fs";
import { write } from "fast-csv";
import csv from "csv-parser";
import { OutputDataSchemaRow } from "./sdk/types";
import { getTvlByVaultAtBlock } from "./sdk/helpers";
import { euro3Price } from "./sdk/euro3Price";
import { BlockData } from "./sdk/interfaces";

//* Goal: Hourly snapshot of TVL by User in his Vault.
//* Note: The calculation is made via RPC calls, as there is no way to calculate the TVL via events in our protocol at a block.

export const getTVLByVault = async (blocks: BlockData, logOutput?: boolean) => {
const { blockNumber, blockTimestamp } = blocks;
const csvRowsTvl: OutputDataSchemaRow[] = [];

const euro3Prices = await euro3Price();
const { vaultsTvl, owners, collateralsByVaults, balancesByVault } =
await getTvlByVaultAtBlock(blockNumber);

for (let i = 0; i < owners.length; i++) {
for (let j = 0; j < collateralsByVaults[i].length; j++) {
csvRowsTvl.push({
block_number: blockNumber,
timestamp: blockTimestamp,
user_address: owners[i].toLocaleLowerCase(),
token_address: collateralsByVaults[i][j].toLocaleLowerCase(),
token_balance: balancesByVault[i][j],
token_symbol: "",
usd_price: Number((vaultsTvl[i][j] * euro3Prices.USD).toFixed(2)),
});
}
}

if (logOutput) console.log(csvRowsTvl);
return csvRowsTvl;
};

export const getUserTVLByBlock = async (
blocks: BlockData[],
logOutput?: boolean
) => {
const allCsvRows: OutputDataSchemaRow[] = []; // Array to accumulate CSV rows for all blocks

for (let i = 0; i < blocks.length; i++) {
const { blockNumber, blockTimestamp } = blocks[i];
try {
// Retrieve data using block number and timestamp
const csvRowsTvl = await getTVLByVault(
{
blockNumber,
blockTimestamp,
},
logOutput
);

allCsvRows.push(...csvRowsTvl);
i++;
// console.log(`Processed block ${i}`);
} catch (error) {
console.error(`An error occurred for block ${blockNumber}:`, error);
}
}
return allCsvRows;
};

// * Test
// const when = { blockNumber: 3394331, blockTimestamp: 0 };
// getUserTVLByBlock ([when], true);

const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
const blocks: BlockData[] = [];

await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv()) // Specify the separator as '\t' for TSV files
.on("data", (row) => {
const blockNumber = parseInt(row.number, 10);
const blockTimestamp = parseInt(row.timestamp, 10);
if (!isNaN(blockNumber) && blockTimestamp) {
blocks.push({ blockNumber, blockTimestamp });
}
})
.on("end", () => {
resolve();
})
.on("error", (err) => {
reject(err);
});
});

return blocks;
};

readBlocksFromCSV("hourly_blocks.csv")
.then(async (blocks: any[]) => {
const allCsvRows: any[] = [];
for (const block of blocks) {
try {
console.log({ block });
const result = await getUserTVLByBlock([block]);
allCsvRows.push(...result);
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
}
}
await new Promise((resolve, reject) => {
const ws = fs.createWriteStream(`outputData.csv`, {
flags: "w",
});
write(allCsvRows, { headers: true })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
resolve;
});
});
})
.catch((err) => {
console.error("Error reading CSV file:", err);
});
13 changes: 13 additions & 0 deletions adapters/3ADAO/src/sdk/ABIs/balanceGetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const balanceGetterABI = [
{ inputs: [], stateMutability: "payable", type: "constructor" },
{
inputs: [
{ internalType: "address", name: "owner", type: "address" },
{ internalType: "address[]", name: "tokens", type: "address[]" },
],
name: "getBalances",
outputs: [{ internalType: "uint256[]", name: "", type: "uint256[]" }],
stateMutability: "view",
type: "function",
},
];
Loading

0 comments on commit 06eb534

Please sign in to comment.