Skip to content

Commit

Permalink
fix: add check for sendAsync method inside middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
vanya2h committed Jan 24, 2022
1 parent 244b981 commit 82bd5cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 8 additions & 4 deletions packages/estimate-middleware/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { createFetchMiddleware, providerAsMiddleware, providerFromEngine } from
import { JsonRpcEngine } from "json-rpc-engine"
import type { SafeEventEmitterProvider } from "eth-json-rpc-middleware/dist/utils/cache"
import { createEstimateGasMiddleware } from "./middleware"
import { isSafeEventEmitterProvider } from "./utils"

export function estimate(provider: any, estimate?: JsonRpcEngine | string, force?: boolean): SafeEventEmitterProvider {
const engine = new JsonRpcEngine()
engine.push(createEstimateGasMiddleware(getEstimateEngine(provider, estimate), force))
engine.push(providerAsMiddleware(provider))
return providerFromEngine(engine)
if (isSafeEventEmitterProvider(provider)) {
const engine = new JsonRpcEngine()
engine.push(createEstimateGasMiddleware(getEstimateEngine(provider, estimate), force))
engine.push(providerAsMiddleware(provider))
return providerFromEngine(engine)
}
return provider
}

function getEstimateEngine(provider: any, estimate: JsonRpcEngine | string | undefined): JsonRpcEngine {
Expand Down
5 changes: 5 additions & 0 deletions packages/estimate-middleware/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { SafeEventEmitterProvider } from "eth-json-rpc-middleware/dist/utils/cache"

export function isSafeEventEmitterProvider(x: unknown): x is SafeEventEmitterProvider {
return typeof x === "object" && x !== null && "send" in x && "sendAsync" in x
}

0 comments on commit 82bd5cb

Please sign in to comment.