-
Notifications
You must be signed in to change notification settings - Fork 49
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
1 parent
b046cd6
commit 63e9acb
Showing
2 changed files
with
105 additions
and
18 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
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 | ||
} | ||
) | ||
} | ||
} |
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