Skip to content

Commit

Permalink
fix: set block range to 1k max for event fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinGbz committed Jul 16, 2024
1 parent 8f8e132 commit c3e8516
Showing 1 changed file with 68 additions and 28 deletions.
96 changes: 68 additions & 28 deletions src/services/UnderlyingYieldService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { AaveV2Ethereum, MiscEthereum } from '@bgd-labs/aave-address-book';
import { Provider } from '@ethersproject/providers';
import { Contract } from 'ethers';
import { Contract, EventFilter } from 'ethers';
import { formatUnits } from 'ethers/lib/utils';
import { FORK_ENABLED } from 'src/utils/marketsAndNetworksConfig';

import {
cbEthOracle,
Expand Down Expand Up @@ -125,11 +126,14 @@ export class UnderlyingYieldService {

const blocksInDay = DAY_IN_SECONDS / 12;

const events = await connectedContract.queryFilter(
connectedContract.filters.TokenRebased(),
currentBlockNumber - blocksInDay * EVENTS_PERIOD_DAYS,
currentBlockNumber
);
const events = await this.fetchEventsInBatches({
connectedContract,
eventFilter: connectedContract.filters.TokenRebased(),
fromBlock: currentBlockNumber - blocksInDay * EVENTS_PERIOD_DAYS,
toBlock: currentBlockNumber,
});

console.log('events', events);

const latestEvent = events.length === 0 ? null : events[events.length - 1];

Expand Down Expand Up @@ -198,11 +202,12 @@ export class UnderlyingYieldService {

const contract = new Contract(rocketNetworkBalances, abi);
const connectedContract = contract.connect(provider);
const events = await connectedContract.queryFilter(
connectedContract.filters.BalancesUpdated(),
currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
currentBlockNumber
);
const events = await this.fetchEventsInBatches({
connectedContract,
eventFilter: connectedContract.filters.BalancesUpdated(),
fromBlock: currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
toBlock: currentBlockNumber,
});

const rates = events
.map((event) => {
Expand Down Expand Up @@ -246,12 +251,12 @@ export class UnderlyingYieldService {

const contract = new Contract(staderLabsOracle, abi); // Stader Labs Oracle
const connectedContract = contract.connect(provider);

const events = await connectedContract.queryFilter(
connectedContract.filters.ExchangeRateUpdated(),
currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
currentBlockNumber
);
const events = await this.fetchEventsInBatches({
connectedContract,
eventFilter: connectedContract.filters.ExchangeRateUpdated(),
fromBlock: currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
toBlock: currentBlockNumber,
});

const rates = events
.map((event) => {
Expand Down Expand Up @@ -285,12 +290,12 @@ export class UnderlyingYieldService {

const contract = new Contract(cbEthOracle, abi); // cbETH Oracle
const connectedContract = contract.connect(provider);

const events = await connectedContract.queryFilter(
connectedContract.filters.ExchangeRateUpdated(),
currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
currentBlockNumber
);
const events = await this.fetchEventsInBatches({
connectedContract,
eventFilter: connectedContract.filters.ExchangeRateUpdated(),
fromBlock: currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
toBlock: currentBlockNumber,
});

if (events && events.length > 2) {
const lastestEventArgs = events[events.length - 1].args;
Expand Down Expand Up @@ -346,11 +351,12 @@ export class UnderlyingYieldService {
];
const contract = new Contract(etherfiLiquidityPool, abi); // Etherfi LiquidityPool
const connectedContract = contract.connect(provider);
const events = await connectedContract.queryFilter(
connectedContract.filters.Rebase(),
currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
currentBlockNumber
);
const events = await this.fetchEventsInBatches({
connectedContract,
eventFilter: connectedContract.filters.Rebase(),
fromBlock: currentBlockNumber - BLOCKS_A_DAY * EVENTS_PERIOD_DAYS,
toBlock: currentBlockNumber,
});

if (events && events.length > 2) {
const lastestEventArgs = events[events.length - 1].args;
Expand Down Expand Up @@ -381,4 +387,38 @@ export class UnderlyingYieldService {
return await getApyFromApi();
}
};

fetchEventsInBatches = async (parameters: {
connectedContract: Contract;
eventFilter: EventFilter;
fromBlock: number;
toBlock: number;
batchSize?: number;
}) => {
const { connectedContract, eventFilter, fromBlock, toBlock } = parameters;

let batchSize;

if (parameters.batchSize) {
batchSize = parameters.batchSize;
} else if (FORK_ENABLED) {
batchSize = 1000;
} else {
batchSize = 100000;
}

let startBlock = fromBlock;

const allEvents = [];

while (startBlock <= toBlock) {
const nextBlock = startBlock + batchSize - 1;
const endBlock = nextBlock < toBlock ? nextBlock : toBlock;
const events = await connectedContract.queryFilter(eventFilter, startBlock, endBlock);
allEvents.push(...events);
startBlock = endBlock + 1;
}

return allEvents;
};
}

0 comments on commit c3e8516

Please sign in to comment.