-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
2,769 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{ | ||
"name": "lynex-adapter", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node dist/index.js", | ||
"compile": "tsc", | ||
"watch": "tsc -w", | ||
"clear": "rm -rf dist" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@types/big.js": "^6.2.2", | ||
"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", | ||
"tiny-invariant": "^1.3.1", | ||
"toformat": "^2.0.0", | ||
"viem": "^2.8.13" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.11.17", | ||
"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,110 @@ | ||
import fs from "fs"; | ||
import { write } from "fast-csv"; | ||
import { getTimestampAtBlock, getUserAddresses } from "./sdk/subgraphDetails"; | ||
import { | ||
VE_LYNX_ADDRESS, | ||
fetchUserPools, | ||
fetchUserVotes, | ||
} from "./sdk/lensDetails"; | ||
import BigNumber from "bignumber.js"; | ||
|
||
interface CSVRow { | ||
block_number: string; | ||
timestamp: string; | ||
user_address: string; | ||
token_address: string; | ||
token_balance: string; | ||
} | ||
|
||
const getData = async () => { | ||
const snapshotBlocks = [2999728]; | ||
|
||
const csvRows: CSVRow[] = []; | ||
|
||
for (let block of snapshotBlocks) { | ||
const [userAddresses] = await Promise.all([getUserAddresses(block)]); | ||
console.log(`Block: ${block}`); | ||
console.log("UserAddresses: ", userAddresses.length); | ||
|
||
const tokenBalanceMap = {} as { | ||
[userAddress: string]: { [tokenAddress: string]: BigNumber }; | ||
}; | ||
|
||
const userPoolFetch = []; | ||
const userVotesFetch = []; | ||
|
||
for (const user of userAddresses) { | ||
userPoolFetch.push(fetchUserPools(BigInt(block), user.id, user.pools)); | ||
userVotesFetch.push(fetchUserVotes(BigInt(block), user.id)); | ||
} | ||
|
||
const userFetchResult = await Promise.all(userPoolFetch); | ||
const userVotesResult = await Promise.all(userVotesFetch); | ||
const block_number = block.toString(); | ||
const timestamp = new Date(await getTimestampAtBlock(block)).toISOString(); | ||
|
||
for (const userFetchedPools of userFetchResult) { | ||
for (const userPool of userFetchedPools) { | ||
const user_address = userPool.result.userAddress.toLowerCase(); | ||
const totalLPBalance = | ||
userPool.result.account_lp_balance + | ||
userPool.result.account_gauge_balance; | ||
const total0 = | ||
(totalLPBalance * userPool.result.reserve0) / | ||
userPool.result.total_supply; | ||
const total1 = | ||
(totalLPBalance * userPool.result.reserve1) / | ||
userPool.result.total_supply; | ||
const token0Address = userPool.result.token0.toLowerCase(); | ||
const token1Address = userPool.result.token1.toLowerCase(); | ||
|
||
// Aggregate tokens | ||
tokenBalanceMap[user_address] = tokenBalanceMap[user_address] ?? {}; | ||
tokenBalanceMap[user_address][token0Address] = BigNumber( | ||
tokenBalanceMap[user_address][token0Address] ?? 0 | ||
).plus(total0.toString()); | ||
tokenBalanceMap[user_address] = tokenBalanceMap[user_address] ?? {}; | ||
tokenBalanceMap[user_address][token1Address] = BigNumber( | ||
tokenBalanceMap[user_address][token1Address] ?? 0 | ||
).plus(total1.toString()); | ||
} | ||
} | ||
|
||
for (const userFecthedVotes of userVotesResult) { | ||
for (const userVote of userFecthedVotes) { | ||
const user_address = userVote.result.userAddress.toLowerCase(); | ||
const token0Address = VE_LYNX_ADDRESS.toLowerCase(); | ||
tokenBalanceMap[user_address] = tokenBalanceMap[user_address] ?? {}; | ||
tokenBalanceMap[user_address][token0Address] = BigNumber( | ||
tokenBalanceMap[user_address][token0Address] ?? 0 | ||
).plus(userVote.result.amount.toString()); | ||
} | ||
} | ||
|
||
Object.entries(tokenBalanceMap).forEach(([user_address, balances]) => { | ||
Object.entries(balances).forEach(([token_address, token_balance]) => { | ||
if (token_balance.dp(0).lte(0)) { | ||
return; | ||
} | ||
csvRows.push({ | ||
block_number, | ||
timestamp, | ||
user_address, | ||
token_address, | ||
token_balance: token_balance.dp(0).toString(), | ||
}); | ||
}); | ||
}); | ||
|
||
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"); | ||
}); |
Oops, something went wrong.