Skip to content

Commit

Permalink
Merge pull request #136 from ClipFinance/clip.finance
Browse files Browse the repository at this point in the history
Clip.finance: yield: checklist fixes
  • Loading branch information
nitish-91 committed May 16, 2024
2 parents 56f862f + 99af0ea commit df9ca46
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 60 deletions.
4 changes: 2 additions & 2 deletions adapters/clip-finance/hourly_blocks.csv
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
number,timestamp
4572185,1
number timestamp
4333348 1715043599
79 changes: 34 additions & 45 deletions adapters/clip-finance/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ interface CSVRow {
timestamp: number;
user_address: string;
token_address: string;
token_symbol: string;
token_balance: bigint;
token_symbol: string;
usd_price: number
}

Expand All @@ -27,16 +27,16 @@ export const getUserTVLByBlock = async (blocks: BlockData) => {
for (const block of snapshotBlocks) {
let snapshots = await getUserBalanceSnapshotAtBlock(block, "");

const timestamp = await getTimestampAtBlock(block);
// const timestamp = await getTimestampAtBlock(block);

for (const snapshot of snapshots) {
const csvRow: CSVRow = {
block_number: block,
timestamp: timestamp,
timestamp: blockTimestamp,
user_address: snapshot.id,
token_address: snapshot.token,
token_symbol: snapshot.tokenSymbol,
token_balance: BigInt(snapshot.balance.toString()),
token_symbol: snapshot.tokenSymbol,
usd_price: 0
};
csvRows.push(csvRow);
Expand All @@ -52,60 +52,49 @@ const readBlocksFromCSV = async (filePath: string): Promise<BlockData[]> => {
const blocks: BlockData[] = [];

await new Promise<void>((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv({ separator: ',' })) // Specify the separator as ',' for csv files
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) {

const blockNumber = parseInt(row.number, 10);
const blockTimestamp = parseInt(row.timestamp, 10);
if (!isNaN(blockNumber) && blockTimestamp) {
blocks.push({ blockNumber: blockNumber, blockTimestamp });
}
}
})
.on('end', () => {
resolve();
resolve();
})
.on('error', (err) => {
reject(err);
reject(err);
});
});

return blocks;
};

readBlocksFromCSV('hourly_blocks.csv')
.then(async (blocks) => {
const allCsvRows: any[] = []; // Array to accumulate CSV rows for all blocks
const batchSize = 10; // Size of batch to trigger writing to the file
let i = 0;

for (const block of blocks) {
readBlocksFromCSV('hourly_blocks.csv').then(async (blocks: any[]) => {
console.log(blocks);
const allCsvRows: any[] = [];

for (const block of blocks) {
try {
const result = await getUserTVLByBlock(block);

// Accumulate CSV rows for all blocks
allCsvRows.push(...result);

i++;
console.log(`Processed block ${i}`);

// Write to file when batch size is reached or at the end of loop
if (i % batchSize === 0 || i === blocks.length) {
const ws = fs.createWriteStream(`outputData.csv`, { flags: i === batchSize ? 'w' : 'a' });
write(allCsvRows, { headers: i === batchSize ? true : false })
.pipe(ws)
.on("finish", () => {
console.log(`CSV file has been written.`);
});

// Clear the accumulated CSV rows
allCsvRows.length = 0;
}
const result = await getUserTVLByBlock(block);
allCsvRows.push(...result);
} catch (error) {
console.error(`An error occurred for block ${block}:`, error);
console.error(`An error occurred for block ${block}:`, error);
}

}
})
.catch((err) => {
console.error('Error reading CSV file:', err);
}
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);
});
25 changes: 12 additions & 13 deletions adapters/clip-finance/src/sdk/subgraphDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ export const getUserBalanceSnapshotAtBlock = async (
const strategyRouterSharesMap = new Map<string, UserSharesSnapshot>();
let strategyRouterBalance = new Map<string, UserBalanceSnapshot>();
const addBalance = (balance: UserBalanceSnapshot, share: UserBalanceSnapshot) => {
const contract = "0x".concat(share.id.substring(42));
const user = share.id.substring(0, 42);
const user= share.id.substring(0, 42);
const key = user.concat(balance.token);
if (user == "0xa663f143055254a503467ff8b18aa9e70b9455b6") {
strategyRouterBalance.set(key.concat(balance.token), balance);
Expand Down Expand Up @@ -165,38 +164,38 @@ export const getUserBalanceSnapshotAtBlock = async (
};
if (sharePrice.price0.gt(0)) {
userBalanceSnapshot = {
id: user,
id: user.toLowerCase(),
balance: Big(Math.round(Big(snapshot.shares0).mul(sharePrice.price0).div(1e18).toNumber())),
token : sharePrice.token0,
token : sharePrice.token0.toLowerCase(),
tokenSymbol: sharePrice.token0Symbol,

}
addBalance(userBalanceSnapshot, snapshot);
}
if (sharePrice.price01.gt(0)) {
userBalanceSnapshot = {
id: user,
id: user.toLowerCase(),
balance: Big(Math.round(Big(snapshot.shares0).mul(sharePrice.price01).div(1e18).toNumber())),
token : sharePrice.token1,
token : sharePrice.token1.toLowerCase(),
tokenSymbol: sharePrice.token1Symbol,

}
addBalance(userBalanceSnapshot, snapshot);
}
if (sharePrice.price1.gt(0)) {
userBalanceSnapshot = {
id: user,
id: user.toLowerCase(),
balance: Big(Math.round(Big(snapshot.shares1).mul(sharePrice.price1).div(1e18).toNumber())),
token : sharePrice.token1,
token : sharePrice.token1.toLowerCase(),
tokenSymbol: sharePrice.token1Symbol,
}
addBalance(userBalanceSnapshot, snapshot);
}
if (sharePrice.price10.gt(0)) {
userBalanceSnapshot = {
id: user,
id: user.toLowerCase(),
balance: Big(Math.round(Big(snapshot.shares1).mul(sharePrice.price10).div(1e18).toNumber())),
token : sharePrice.token0,
token : sharePrice.token0.toLowerCase(),
tokenSymbol: sharePrice.token0Symbol,

}
Expand Down Expand Up @@ -240,15 +239,15 @@ export const getUserBalanceSnapshotAtBlock = async (
if (strategyRouterTotalShares.gt(0)) {
let checkBalance = Big(0);
strategyRouterSharesMap.forEach((share: UserSharesSnapshot, id: string)=> {
const user = "0x".concat(share.id.substring(0, 42));
const user = share.id.substring(0, 42);
for (const srbKey of strategyRouterBalance.keys()) {
const balance = strategyRouterBalance.get(srbKey);
if (balance) {
countedTotalShares = countedTotalShares.plus(Big(share.shares0));
const userBalance : UserBalanceSnapshot = {
id: user,
id: user.toLowerCase(),
balance: Big(Math.round(Big(share.shares0).mul(balance.balance).div(strategyRouterTotalShares).toNumber())),
token : balance.token,
token : balance.token.toLowerCase(),
tokenSymbol: balance.tokenSymbol
}

Expand Down

0 comments on commit df9ca46

Please sign in to comment.