-
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 branch 'delta-hq:main' into add-xfai
- Loading branch information
Showing
136 changed files
with
15,354 additions
and
2,406 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
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 |
---|---|---|
|
@@ -3,4 +3,5 @@ dist/ | |
Ds_Store | ||
.Ds_Store | ||
package-lock.json | ||
bun.lockb | ||
outputData.csv |
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,4 @@ | ||
number,timestamp | ||
4980088,1714773599 | ||
number,timestamp | ||
4980089,1714773599 |
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,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" | ||
} | ||
} |
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,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); | ||
}); |
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,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", | ||
}, | ||
]; |
Oops, something went wrong.