-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
80 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from "./rateLimitedProvider"; | ||
export * from "./cachedProvider"; | ||
export * from "./retryProvider"; | ||
export * from "./speedProvider"; | ||
export * from "./constants"; | ||
export * from "./types"; | ||
export * from "./utils"; |
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,63 @@ | ||
import { ethers } from "ethers"; | ||
import { CachingMechanismInterface } from "../interfaces"; | ||
import { CacheProvider } from "./cachedProvider"; | ||
import { formatProviderError } from "./utils"; | ||
import { PROVIDER_CACHE_TTL } from "./constants"; | ||
import { Logger } from "winston"; | ||
|
||
/** | ||
* RPC provider that sends requests to multiple providers in parallel and returns the fastest response. | ||
*/ | ||
export class SpeedProvider extends ethers.providers.StaticJsonRpcProvider { | ||
readonly providers: ethers.providers.StaticJsonRpcProvider[]; | ||
|
||
constructor( | ||
params: ConstructorParameters<typeof ethers.providers.StaticJsonRpcProvider>[], | ||
chainId: number, | ||
readonly maxConcurrencySpeed: number, | ||
readonly maxConcurrencyRateLimit: number, | ||
providerCacheNamespace: string, | ||
pctRpcCallsLogged: number, | ||
redisClient?: CachingMechanismInterface, | ||
standardTtlBlockDistance?: number, | ||
noTtlBlockDistance?: number, | ||
providerCacheTtl = PROVIDER_CACHE_TTL, | ||
logger?: Logger | ||
) { | ||
// Initialize the super just with the chainId, which stops it from trying to immediately send out a .send before | ||
// this derived class is initialized. | ||
super(undefined, chainId); | ||
this.providers = params.map( | ||
(inputs) => | ||
new CacheProvider( | ||
providerCacheNamespace, | ||
redisClient, | ||
standardTtlBlockDistance, | ||
noTtlBlockDistance, | ||
providerCacheTtl, | ||
maxConcurrencyRateLimit, | ||
pctRpcCallsLogged, | ||
logger, | ||
...inputs | ||
) | ||
); | ||
} | ||
|
||
override async send(method: string, params: Array<unknown>): Promise<unknown> { | ||
try { | ||
const providersToUse = this.providers.slice(0, this.maxConcurrencySpeed); | ||
const result = await Promise.any(providersToUse.map((provider) => provider.send(method, params))); | ||
return result; | ||
} catch (error) { | ||
// Only thrown if all providers failed to respond | ||
if (error instanceof AggregateError) { | ||
const errors = error.errors.map((error, index) => { | ||
const provider = this.providers[index]; | ||
return formatProviderError(provider, error.message); | ||
}); | ||
throw new Error("All providers errored:\n" + errors.join("\n")); | ||
} | ||
throw error; | ||
} | ||
} | ||
} |
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