-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: reduce retryprovider env variables (#51)
Co-authored-by: Alexandru Matei <alexandrumatei3693@gmail.com>
- Loading branch information
1 parent
6351fd9
commit 6599fbd
Showing
6 changed files
with
194 additions
and
8 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
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
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
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
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,53 @@ | ||
import { Logger } from "winston"; | ||
import { providers } from "@across-protocol/sdk"; | ||
|
||
import { parseRetryProviderEnvs, parseProvidersUrls } from "../parseEnv"; | ||
import { RedisCache } from "../redis/redisCache"; | ||
import { getChainCacheFollowDistance } from "./constants"; | ||
|
||
export class RetryProvidersFactory { | ||
private retryProviders: Map<number, providers.RetryProvider> = new Map(); | ||
|
||
constructor( | ||
private redisCache: RedisCache, | ||
private logger: Logger, | ||
) {} | ||
|
||
public initializeProviders() { | ||
const providersUrls = parseProvidersUrls(); | ||
|
||
for (const [chainId, providerUrls] of providersUrls.entries()) { | ||
const retryProviderEnvs = parseRetryProviderEnvs(chainId); | ||
if (!providerUrls || providerUrls.length === 0) { | ||
throw new Error(`No provider urls found for chainId: ${chainId}`); | ||
} | ||
const standardTtlBlockDistance = getChainCacheFollowDistance(chainId); | ||
const provider = new providers.RetryProvider( | ||
providerUrls.map((url) => [url, chainId]), | ||
chainId, | ||
retryProviderEnvs.nodeQuorumThreshold, | ||
retryProviderEnvs.retries, | ||
retryProviderEnvs.retryDelay, | ||
retryProviderEnvs.maxConcurrency, | ||
retryProviderEnvs.providerCacheNamespace, | ||
retryProviderEnvs.pctRpcCallsLogged, | ||
this.redisCache, | ||
standardTtlBlockDistance, | ||
retryProviderEnvs.noTtlBlockDistance, | ||
retryProviderEnvs.providerCacheTtl, | ||
this.logger, | ||
); | ||
this.retryProviders.set(chainId, provider); | ||
} | ||
} | ||
|
||
public getProviderForChainId(chainId: number) { | ||
const retryProvider = this.retryProviders.get(chainId); | ||
|
||
if (!retryProvider) { | ||
throw new Error(`No retry provider found for chainId: ${chainId}`); | ||
} | ||
|
||
return retryProvider; | ||
} | ||
} |
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,61 @@ | ||
import { CHAIN_IDs } from "@across-protocol/constants"; | ||
|
||
// This is the block distance at which the bot, by default, stores in redis with no TTL. | ||
// These are all intended to be roughly 2 days of blocks for each chain. | ||
// blocks = 172800 / avg_block_time | ||
export const DEFAULT_NO_TTL_DISTANCE: { [chainId: number]: number } = { | ||
[CHAIN_IDs.ARBITRUM]: 691200, | ||
[CHAIN_IDs.BASE]: 86400, | ||
[CHAIN_IDs.BLAST]: 86400, | ||
[CHAIN_IDs.BOBA]: 86400, | ||
[CHAIN_IDs.LINEA]: 57600, | ||
[CHAIN_IDs.LISK]: 86400, | ||
[CHAIN_IDs.MAINNET]: 14400, | ||
[CHAIN_IDs.MODE]: 86400, | ||
[CHAIN_IDs.OPTIMISM]: 86400, | ||
[CHAIN_IDs.POLYGON]: 86400, | ||
[CHAIN_IDs.REDSTONE]: 86400, | ||
[CHAIN_IDs.SCROLL]: 57600, | ||
[CHAIN_IDs.ZK_SYNC]: 172800, | ||
[CHAIN_IDs.ZORA]: 86400, | ||
}; | ||
|
||
// This is the max anticipated distance on each chain before RPC data is likely to be consistent amongst providers. | ||
// This distance should consider re-orgs, but also the time needed for various RPC providers to agree on chain state. | ||
// Provider caching will not be allowed for queries whose responses depend on blocks closer than this many blocks. | ||
// This is intended to be conservative. | ||
export const CHAIN_CACHE_FOLLOW_DISTANCE: { [chainId: number]: number } = { | ||
[CHAIN_IDs.ARBITRUM]: 32, | ||
[CHAIN_IDs.BASE]: 120, | ||
[CHAIN_IDs.BLAST]: 120, | ||
[CHAIN_IDs.BOBA]: 0, | ||
[CHAIN_IDs.LISK]: 120, | ||
[CHAIN_IDs.LINEA]: 100, // Linea has a soft-finality of 1 block. This value is padded - but at 3s/block the padding is 5 minutes | ||
[CHAIN_IDs.MAINNET]: 128, | ||
[CHAIN_IDs.MODE]: 120, | ||
[CHAIN_IDs.OPTIMISM]: 120, | ||
[CHAIN_IDs.POLYGON]: 256, | ||
[CHAIN_IDs.REDSTONE]: 120, | ||
[CHAIN_IDs.SCROLL]: 100, | ||
[CHAIN_IDs.ZK_SYNC]: 512, | ||
[CHAIN_IDs.ZORA]: 120, | ||
// Testnets: | ||
[CHAIN_IDs.ARBITRUM_SEPOLIA]: 0, | ||
[CHAIN_IDs.BASE_SEPOLIA]: 0, | ||
[CHAIN_IDs.BLAST_SEPOLIA]: 0, | ||
[CHAIN_IDs.LISK_SEPOLIA]: 0, | ||
[CHAIN_IDs.MODE_SEPOLIA]: 0, | ||
[CHAIN_IDs.OPTIMISM_SEPOLIA]: 0, | ||
[CHAIN_IDs.POLYGON_AMOY]: 0, | ||
[CHAIN_IDs.SEPOLIA]: 0, | ||
}; | ||
|
||
export const getChainCacheFollowDistance = (chainId: number) => { | ||
const chainCacheFollowDistance = CHAIN_CACHE_FOLLOW_DISTANCE[chainId]; | ||
|
||
if (!chainCacheFollowDistance) { | ||
throw new Error(`Invalid chain cache distance for chain id ${chainId}`); | ||
} | ||
|
||
return chainCacheFollowDistance; | ||
}; |