Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions scripts/build-products.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { CoverProducts, Cover, addresses } = require('@nexusmutual/deployments');
const ethers = require('ethers');
const fetch = require('node-fetch');

const { parseProductCoverAssets, parseFilePath, getCoverAssetsSymbols } = require('./utils');
const { parseProductCoverAssets, parseFilePath, getCoverAssetsSymbols, fetchEventsInBatches } = require('./utils');
const { allPrivateProductsIds } = require(path.join(__dirname, '../src/constants/privateProducts.js'));
const productMetadata = require('../data/legacy-product-metadata.json');

Expand Down Expand Up @@ -60,9 +60,12 @@ const createLogoDict = async logosDir => {
return map;
};

const EVENTS_START_BLOCK = 7700000;

const fetchProducts = async (coverContract, coverProducts, provider) => {
const eventFilter = coverProducts.filters.ProductSet();
const events = await coverProducts.queryFilter(eventFilter);

const events = await fetchEventsInBatches(coverProducts, eventFilter, EVENTS_START_BLOCK, provider);

const logos = await createLogoDict(path.join(__dirname, '../src/logos'));

Expand Down
30 changes: 30 additions & 0 deletions scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,39 @@ const parseFilePath = filePath => {
return { id, filename, extension };
};

/**
* Fetches events in batches to avoid RPC limitations.
* All batches are fetched in parallel for better performance.
*
* @param {ethers.Contract} contract - The contract to query events from.
* @param {ethers.EventFilter} eventFilter - The event filter to apply.
* @param {number} startBlock - The starting block number.
* @param {ethers.providers.Provider} provider - The Ethereum provider to interact with the blockchain.
* @param {number} [batchSize=10000] - The number of blocks to query per batch.
* @returns {Promise<ethers.Event[]>} A promise that resolves to an array of events.
*/
const fetchEventsInBatches = async (contract, eventFilter, startBlock, provider, batchSize = 10_000) => {
const endBlock = await provider.getBlockNumber();
const batches = [];

for (let fromBlock = startBlock; fromBlock <= endBlock; fromBlock += batchSize) {
const toBlock = Math.min(fromBlock + batchSize - 1, endBlock);
batches.push({ fromBlock, toBlock });
}

const batchResults = await Promise.all(
batches.map(({ fromBlock, toBlock }) => {
return contract.queryFilter(eventFilter, fromBlock, toBlock);
}),
);

return batchResults.flat();
};

module.exports = {
parseProductCoverAssets,
parseCoverAssetToProductAsset,
parseFilePath,
getCoverAssetsSymbols,
fetchEventsInBatches,
};
2 changes: 2 additions & 0 deletions src/constants/privateProducts.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ const allPrivateProductsIds = [
400, // OpenCover Steakhouse H Yield I USDC
401, // PT-stcUSD-23JUL2026 (Morpho)
402, // PT-cUSD-23JUL2026 (Morpho)
403, // TermMax
405, // OpenCover YoUSD
];

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions src/constants/products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,5 @@ export const productCategoryMap: { [productId: number]: ProductCategoryEnum } =
387: ProductCategoryEnum.Depeg, // Neutrl USD Depeg
388: ProductCategoryEnum.Dex, // Hybra Finance
391: ProductCategoryEnum.YieldOptimizer, // IPOR
404: ProductCategoryEnum.Custody, // BingX Custody
};
26 changes: 26 additions & 0 deletions src/logos/403-termmax.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/logos/404-bingx.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions src/logos/405-open-cover.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.