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
153 additions
and
32 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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { custom, type CustomTransport, type Transport } from "viem"; | ||
|
||
export interface SplitTransportParams { | ||
overrides: { | ||
methods: string[]; | ||
transport: Transport; | ||
}[]; | ||
fallback: Transport; | ||
} | ||
|
||
/** | ||
* The Split Transport allows you to split RPC traffic for specific methods across | ||
* different RPC providers. This is done by specifying the methods you want handled | ||
* specially as overrides and providing a fallback transport for all other methods. | ||
* | ||
* @example | ||
* ```ts | ||
* import { createPublicClient, http } from "viem"; | ||
* import { split } from "@alchemy/aa-core"; | ||
* | ||
* const bundlerMethods = [ | ||
* "eth_sendUserOperation", | ||
* "eth_estimateUserOperationGas", | ||
* "eth_getUserOperationReceipt", | ||
* "eth_getUserOperationByHash", | ||
* "eth_supportedEntryPoints" | ||
* ]; | ||
* | ||
* const clientWithSplit = createPublicClient({ | ||
* transport: split({ | ||
* overrides: [{ | ||
* methods: bundlerMethods, | ||
* transport: http(BUNDLER_RPC_URL) | ||
* }] | ||
* fallback: http(OTHER_RPC_URL) | ||
* }), | ||
* }); | ||
* ``` | ||
* | ||
* @param params {@link SplitTransportParams} split transport configuration containing the methods overrides and fallback transport | ||
* @returns a {@link CustomTransport} that splits traffic | ||
*/ | ||
export const split = (params: SplitTransportParams): CustomTransport => { | ||
const overrideMap = params.overrides.reduce((accum, curr) => { | ||
curr.methods.forEach((method) => { | ||
if (accum.has(method) && accum.get(method) !== curr.transport) { | ||
throw new Error( | ||
"A method cannot be handled by more than one transport" | ||
); | ||
} | ||
|
||
accum.set(method, curr.transport); | ||
}); | ||
|
||
return accum; | ||
}, new Map<string, Transport>()); | ||
|
||
return (opts) => | ||
custom({ | ||
request: async (args) => { | ||
const transportOverride = overrideMap.get(args.method); | ||
if (transportOverride != null) { | ||
return transportOverride(opts).request(args); | ||
} | ||
|
||
return params.fallback(opts).request(args); | ||
}, | ||
})(opts); | ||
}; |
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,55 @@ | ||
--- | ||
outline: deep | ||
head: | ||
- - meta | ||
- property: og:title | ||
content: Split Transport | ||
- - meta | ||
- name: description | ||
content: Learn how to use a different RPC provider for your bundler traffic and node traffic | ||
- - meta | ||
- property: og:description | ||
content: Learn how to use a different RPC provider for your bundler traffic and node traffic | ||
next: | ||
text: SmartAccountClient | ||
--- | ||
|
||
# split | ||
|
||
The split transport allows you to provide overrides for specific RPC methods. This is useful, for example, if you want to send bundler traffic to one provider and node traffic to another. | ||
|
||
## Import | ||
|
||
```ts | ||
import { split } from "@alchemy/aa-core"; | ||
``` | ||
|
||
## Usage | ||
|
||
<<< @/snippets/aa-core/splitTransport.ts | ||
|
||
## Parameters | ||
|
||
```ts | ||
import { type SplitTransportProps } from "@alchemy/aa-core"; | ||
``` | ||
|
||
### overrides | ||
|
||
`{transport: Transport, methods: string[]}[]` | ||
|
||
The overrides param is an array of objects containing a `transport` param of type `Transport` from viem (eg. `http`) and an array of `methods` that this transport should handle | ||
|
||
### fallback | ||
|
||
`Transport` | ||
|
||
This is the `Transport` to use for all other methods | ||
|
||
## Return Type | ||
|
||
```ts | ||
import { type CustomTransport } from "viem"; | ||
``` | ||
|
||
Returns a `CustomTransport` that can be passed to any compatible `viem` Client. This includes any `SmartAccountClient` instance. |
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,25 @@ | ||
import { createSmartAccountClient, sepolia, split } from "@alchemy/aa-core"; | ||
import { http } from "viem"; | ||
|
||
const bundlerMethods = [ | ||
"eth_sendUserOperation", | ||
"eth_estimateUserOperationGas", | ||
"eth_getUserOperationReceipt", | ||
"eth_getUserOperationByHash", | ||
"eth_supportedEntryPoints", | ||
]; | ||
|
||
const splitTransport = split({ | ||
overrides: [ | ||
{ | ||
methods: bundlerMethods, | ||
transport: http("BUNDLER_RPC_URL"), | ||
}, | ||
], | ||
fallback: http("OTHER_RPC_URL"), | ||
}); | ||
|
||
export const client = createSmartAccountClient({ | ||
chain: sepolia, | ||
transport: splitTransport, | ||
}); |
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