Skip to content

Commit

Permalink
Merge branch 'delta-hq:main' into add-xfai
Browse files Browse the repository at this point in the history
  • Loading branch information
notdian authored May 31, 2024
2 parents fd22e3a + 353c985 commit 4b60193
Show file tree
Hide file tree
Showing 136 changed files with 15,354 additions and 2,406 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
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
4 changes: 4 additions & 0 deletions adapters/3ADAO/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
number,timestamp
4980088,1714773599
number,timestamp
4980089,1714773599
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);
});
65 changes: 65 additions & 0 deletions adapters/3ADAO/src/sdk/ABIs/surgeHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export const surgeHelperABI = [
{
inputs: [],
name: "CONVERSION_PRICE_FEED",
outputs: [{ internalType: "address", name: "", type: "address" }],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "DECIMAL_PRECISION",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
inputs: [],
name: "VERSION",
outputs: [{ internalType: "string", name: "", type: "string" }],
stateMutability: "view",
type: "function",
},
{
inputs: [{ internalType: "uint256", name: "euroAmount", type: "uint256" }],
name: "convertEuroToDollar",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "_vaultFactory", type: "address" },
],
name: "getAllVaults",
outputs: [{ internalType: "address[]", name: "", type: "address[]" }],
stateMutability: "view",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "vaultAddress", type: "address" },
{ internalType: "address", name: "collateralAddress", type: "address" },
],
name: "getVaultTVLAndCollateralAmount",
outputs: [
{ internalType: "uint256", name: "", type: "uint256" },
{ internalType: "uint256", name: "", type: "uint256" },
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{ internalType: "address", name: "vaultAddress", type: "address" },
{ internalType: "address", name: "receiptTokenAddress", type: "address" },
],
name: "getVaultTVLAndUnderlyingAmount",
outputs: [
{ internalType: "uint256", name: "", type: "uint256" },
{ internalType: "uint256", name: "", type: "uint256" },
],
stateMutability: "view",
type: "function",
},
];
Loading

0 comments on commit 4b60193

Please sign in to comment.