Skip to content

Commit

Permalink
Merge pull request #83 from pimlicolabs/feat/add-rpc-call-log
Browse files Browse the repository at this point in the history
Add custom transport with logs
  • Loading branch information
plusminushalf authored Jan 11, 2024
2 parents b046cd6 + 63e9acb commit 4378ec7
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 18 deletions.
76 changes: 76 additions & 0 deletions packages/cli/src/customTransport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import type { Logger } from "@alto/utils"
import {
type HttpTransport,
type HttpTransportConfig,
RpcRequestError,
UrlRequiredError,
createTransport
} from "viem"
import { type RpcRequest, rpc } from "viem/utils"

export function customTransport(
/** URL of the JSON-RPC API. Defaults to the chain's public RPC URL. */
url_: string,
config: HttpTransportConfig & { logger: Logger }
): HttpTransport {
const {
fetchOptions,
key = "http",
name = "HTTP JSON-RPC",
retryDelay,
logger
} = config

return ({ chain, retryCount: retryCount_, timeout: timeout_ }) => {
const retryCount = config.retryCount ?? retryCount_
const timeout = timeout_ ?? config.timeout ?? 10_000
const url = url_ || chain?.rpcUrls.default.http[0]
if (!url) {
throw new UrlRequiredError()
}

return createTransport(
{
key,
name,
async request({ method, params }) {
const body = { method, params }
const fn = async (body: RpcRequest) => {
logger.info(
{
url: url,
body
},
"Sending request"
)
return [
await rpc.http(url, {
body,
fetchOptions,
timeout
})
]
}

const [{ error, result }] = await fn(body)
if (error) {
throw new RpcRequestError({
body,
error,
url: url
})
}
return result
},
retryCount,
retryDelay,
timeout,
type: "http"
},
{
fetchOptions,
url
}
)
}
}
47 changes: 29 additions & 18 deletions packages/cli/src/handler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BasicExecutor, ExecutorManager, SenderManager } from "@alto/executor"
import {
IReputationManager,
type IReputationManager,
MemoryMempool,
Monitor,
NullRepuationManager,
Expand All @@ -13,25 +13,29 @@ import {
Server,
UnsafeValidator
} from "@alto/rpc"
import { IValidator } from "@alto/types"
import type { IValidator } from "@alto/types"
import {
Logger,
type Logger,
createMetrics,
initDebugLogger,
initProductionLogger
} from "@alto/utils"
import { Registry } from "prom-client"
import {
Chain,
PublicClient,
Transport,
type Chain,
type PublicClient,
type Transport,
createPublicClient,
createWalletClient,
http
createWalletClient
} from "viem"
import * as chains from "viem/chains"
import { fromZodError } from "zod-validation-error"
import { IBundlerArgs, IBundlerArgsInput, bundlerArgsSchema } from "./config"
import {
type IBundlerArgs,
type IBundlerArgsInput,
bundlerArgsSchema
} from "./config"
import { customTransport } from "./customTransport"

const parseArgs = (args: IBundlerArgsInput): IBundlerArgs => {
// validate every arg, make typesafe so if i add a new arg i have to validate it
Expand Down Expand Up @@ -221,17 +225,28 @@ export const bundlerHandler = async (
]
}

let logger: Logger
if (parsedArgs.logEnvironment === "development") {
logger = initDebugLogger(parsedArgs.logLevel)
} else {
logger = initProductionLogger(parsedArgs.logLevel)
}

const getChainId = async () => {
const client = createPublicClient({
transport: http(args.rpcUrl)
transport: customTransport(args.rpcUrl, {
logger: logger.child({ module: "publicCLient" })
})
})
return await client.getChainId()
}
const chainId = await getChainId()

const chain = getChain(chainId)
const client = createPublicClient({
transport: http(args.rpcUrl),
transport: customTransport(args.rpcUrl, {
logger: logger.child({ module: "publicCLient" })
}),
chain
})

Expand All @@ -242,15 +257,11 @@ export const bundlerHandler = async (
await preFlightChecks(client, parsedArgs)

const walletClient = createWalletClient({
transport: http(parsedArgs.executionRpcUrl ?? args.rpcUrl),
transport: customTransport(parsedArgs.executionRpcUrl ?? args.rpcUrl, {
logger: logger.child({ module: "walletClient" })
}),
chain
})
let logger: Logger
if (parsedArgs.logEnvironment === "development") {
logger = initDebugLogger(parsedArgs.logLevel)
} else {
logger = initProductionLogger(parsedArgs.logLevel)
}

const senderManager = new SenderManager(
parsedArgs.signerPrivateKeys,
Expand Down

0 comments on commit 4378ec7

Please sign in to comment.