-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #122 from zerodevapp/feat/fallback-provider
feat: add fallback kernel account client
- Loading branch information
Showing
7 changed files
with
1,241 additions
and
1 deletion.
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,62 @@ | ||
import type { EntryPoint } from "permissionless/types" | ||
import type { Chain, Transport } from "viem" | ||
import type { KernelSmartAccount } from "../accounts/index.js" | ||
import type { KernelAccountClient } from "./kernelAccountClient.js" | ||
|
||
export const createFallbackKernelAccountClient = < | ||
TEntryPoint extends EntryPoint, | ||
TTransport extends Transport, | ||
TChain extends Chain | undefined, | ||
TSmartAccount extends KernelSmartAccount<TEntryPoint> | undefined | ||
>( | ||
clients: Array< | ||
KernelAccountClient<TEntryPoint, TTransport, TChain, TSmartAccount> | ||
> | ||
): KernelAccountClient<TEntryPoint, TTransport, TChain, TSmartAccount> => { | ||
const proxyClient = new Proxy(clients[0], { | ||
get(_target, prop, receiver) { | ||
for (const client of clients) { | ||
const value = Reflect.get(client, prop, receiver) | ||
if (value !== undefined) { | ||
// If the property is a function, wrap it to add fallback logic | ||
if (typeof value === "function") { | ||
// biome-ignore lint/suspicious/noExplicitAny: <explanation> | ||
return async (...args: any[]) => { | ||
for (let i = 0; i < clients.length; i++) { | ||
try { | ||
const method = Reflect.get( | ||
clients[i], | ||
prop, | ||
receiver | ||
) | ||
if (typeof method === "function") { | ||
// Attempt to call the function on the current client | ||
return await method(...args) | ||
} | ||
} catch (error) { | ||
console.error( | ||
`Action ${String( | ||
prop | ||
)} failed with client ${ | ||
client.transport.url | ||
}, trying next if available.`, | ||
error | ||
) | ||
if (i === clients.length - 1) { | ||
throw error | ||
} | ||
} | ||
} | ||
} | ||
} | ||
// For non-function properties, return the first defined value found | ||
return value | ||
} | ||
} | ||
// If no clients have a defined value for the property, return undefined | ||
return undefined | ||
} | ||
}) | ||
|
||
return proxyClient | ||
} |
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
Oops, something went wrong.