From 05c28828c871a00acc34a228c39c00c34bf45a44 Mon Sep 17 00:00:00 2001 From: Mohamed Hassen Mami Date: Wed, 7 Aug 2024 16:58:17 +0200 Subject: [PATCH] use archive nodes only for timeseries --- .../staking/2.0/BPTStateTracker.ts | 12 ++--- .../2.0/ClaimableSePSP1StateTracker.ts | 2 +- .../staking/2.0/ERC20StateTracker.ts | 4 +- src/lib/constants.ts | 18 +++++--- src/lib/provider.ts | 44 ++++++++++++++++++- 5 files changed, 65 insertions(+), 15 deletions(-) diff --git a/scripts/gas-refund-program/staking/2.0/BPTStateTracker.ts b/scripts/gas-refund-program/staking/2.0/BPTStateTracker.ts index 73c83036..873924fa 100644 --- a/scripts/gas-refund-program/staking/2.0/BPTStateTracker.ts +++ b/scripts/gas-refund-program/staking/2.0/BPTStateTracker.ts @@ -110,7 +110,7 @@ export default class BPTStateTracker extends AbstractStateTracker { this.bVaultContract = new Contract( BalancerVaultAddress, // same address for all chains BVaultABI, - Provider.getJsonRpcProvider(this.chainId), + Provider.getArchiveJsonRpcProvider(this.chainId), ) as BVaultContract; const poolId = Balancer_80PSP_20WETH_address[this.chainId]; @@ -118,7 +118,7 @@ export default class BPTStateTracker extends AbstractStateTracker { this.bptAsERC20 = new Contract( poolId, ERC20ABI, - Provider.getJsonRpcProvider(this.chainId), + Provider.getArchiveJsonRpcProvider(this.chainId), ) as MinERC20; } @@ -161,7 +161,7 @@ export default class BPTStateTracker extends AbstractStateTracker { ), this.startBlock, this.endBlock, - { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId]} + { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId] }, )) as PoolBalanceChanged[]; const blockNumToTimestamp = await fetchBlockTimestampForEvents( @@ -214,7 +214,7 @@ export default class BPTStateTracker extends AbstractStateTracker { ), this.startBlock, this.endBlock, - { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId]} + { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId] }, )) as Swap[]; const blockNumToTimestamp = await fetchBlockTimestampForEvents( @@ -268,14 +268,14 @@ export default class BPTStateTracker extends AbstractStateTracker { this.bptAsERC20.filters.Transfer(NULL_ADDRESS), this.startBlock, this.endBlock, - { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId]} + { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId] }, ), queryFilterBatched( this.bptAsERC20, this.bptAsERC20.filters.Transfer(null, NULL_ADDRESS), this.startBlock, this.endBlock, - { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId]} + { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId] }, ), ]) ).flat() as Transfer[]; diff --git a/scripts/gas-refund-program/staking/2.0/ClaimableSePSP1StateTracker.ts b/scripts/gas-refund-program/staking/2.0/ClaimableSePSP1StateTracker.ts index a78cc346..5c6377f2 100644 --- a/scripts/gas-refund-program/staking/2.0/ClaimableSePSP1StateTracker.ts +++ b/scripts/gas-refund-program/staking/2.0/ClaimableSePSP1StateTracker.ts @@ -72,7 +72,7 @@ export class ClaimableSePSP1StateTracker extends AbstractStateTracker { this.contract = new Contract( MerkleRedeemAddressSePSP1[this.network], MerkleRedeemAbi, - Provider.getJsonRpcProvider(this.network), + Provider.getArchiveJsonRpcProvider(this.network), ); } diff --git a/scripts/gas-refund-program/staking/2.0/ERC20StateTracker.ts b/scripts/gas-refund-program/staking/2.0/ERC20StateTracker.ts index 3c652428..bba6551f 100644 --- a/scripts/gas-refund-program/staking/2.0/ERC20StateTracker.ts +++ b/scripts/gas-refund-program/staking/2.0/ERC20StateTracker.ts @@ -69,7 +69,7 @@ export default class ERC20StateTracker extends AbstractStateTracker { this.contract = new Contract( contractAddress, ERC20ABI, - Provider.getJsonRpcProvider(chainId), + Provider.getArchiveJsonRpcProvider(chainId), ); } @@ -107,7 +107,7 @@ export default class ERC20StateTracker extends AbstractStateTracker { this.contract.filters.Transfer(), this.startBlock, this.endBlock, - { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId]} + { batchSize: QUERY_EVENT_BATCH_SIZE_BY_CHAIN[this.chainId] }, )) as Transfer[]; logger.info( diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 8ffd5aa7..a6ee614d 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -39,6 +39,11 @@ export const Web3Provider: { [network: number]: string } = { [CHAIN_ID_AVALANCHE]: process.env.HTTP_PROVIDER_43114 || '', }; +export const Web3ProviderArchive: { [network: number]: string } = { + [CHAIN_ID_MAINNET]: process.env.HTTP_ARCHIVE_PROVIDER || '', + [CHAIN_ID_OPTIMISM]: process.env.HTTP_ARCHIVE_PROVIDER_10 || '', +}; + // TODO: in future we can fetch it from the api directly export const AugustusV5Address: { [network: number]: string } = { [CHAIN_ID_MAINNET]: '0xdef171fe48cf0115b1d80b88dc8eab59176fee57', @@ -95,15 +100,18 @@ export const SAFETY_MODULE_ADDRESS = //All these must be lower-cased! export const AUGUSTUS_V5_ADDRESS = '0xdef171fe48cf0115b1d80b88dc8eab59176fee57'; -export const AUGUSTUS_V6_0_ADDRESS = '0x00000000fdac7708d0d360bddc1bc7d097f47439'; -export const AUGUSTUS_V6_1_ADDRESS = '0x000db803a70511e09da650d4c0506d0000100000'; -export const AUGUSTUS_V6_2_ADDRESS = '0x6a000f20005980200259b80c5102003040001068'; +export const AUGUSTUS_V6_0_ADDRESS = + '0x00000000fdac7708d0d360bddc1bc7d097f47439'; +export const AUGUSTUS_V6_1_ADDRESS = + '0x000db803a70511e09da650d4c0506d0000100000'; +export const AUGUSTUS_V6_2_ADDRESS = + '0x6a000f20005980200259b80c5102003040001068'; export const AUGUSTUS_SWAPPERS_V6_OMNICHAIN = new Set([ AUGUSTUS_V6_0_ADDRESS, AUGUSTUS_V6_1_ADDRESS, - AUGUSTUS_V6_2_ADDRESS -]) + AUGUSTUS_V6_2_ADDRESS, +]); export const BalancerVaultAddress = '0xba12222222228d8ba445958a75a0704d566bf2c8'; diff --git a/src/lib/provider.ts b/src/lib/provider.ts index 7da444fe..0694d48a 100644 --- a/src/lib/provider.ts +++ b/src/lib/provider.ts @@ -1,5 +1,5 @@ import { JsonRpcProvider } from '@ethersproject/providers'; -import { Web3Provider } from './constants'; +import { Web3Provider, Web3ProviderArchive } from './constants'; import { retryDecorator } from 'ts-retry-promise'; // these params worked best during indexing @@ -9,6 +9,7 @@ const RETRY_ATTEMPTS = 5; const logger = global.LOGGER('provider'); export class Provider { static jsonRpcProviders: { [network: number]: JsonRpcProvider } = {}; + static archiveJsonRpcProviders: { [network: number]: JsonRpcProvider } = {}; static getJsonRpcProvider(network: number): JsonRpcProvider { if (!this.jsonRpcProviders[network]) { if (!Web3Provider[network]) @@ -48,4 +49,45 @@ export class Provider { return this.jsonRpcProviders[network]; } + + // COPY PASTING FROM UP + static getArchiveJsonRpcProvider(network: number): JsonRpcProvider { + if (!this.archiveJsonRpcProviders[network]) { + if (!Web3Provider[network]) + throw new Error(`Provider not defined for network ${network}`); + this.archiveJsonRpcProviders[network] = new JsonRpcProvider({ + url: Web3ProviderArchive[network], + timeout: TIMEOUT_MS, + }); + this.archiveJsonRpcProviders[network] = new Proxy( + this.archiveJsonRpcProviders[network], + { + get: (target, prop) => { + if (prop === 'send') { + const fn = Reflect.get(target, prop).bind(target); + return async function send( + ...args: Parameters + ) { + return retryDecorator(fn, { + retries: RETRY_ATTEMPTS, + delay: DELAY_MS, + timeout: TIMEOUT_MS, + logger: msg => { + logger.warn(msg.substring(0, 200), { network }); + }, + })(...args); + }; + } + return Reflect.get(target, prop); + }, + }, + ); + + // this.jsonRpcProviders[network].on('debug', (info: any) => { + // // logger.debug(info); + // }); + } + + return this.archiveJsonRpcProviders[network]; + } }