-
Notifications
You must be signed in to change notification settings - Fork 221
Expand file tree
/
Copy pathgetSessionClient.ts
More file actions
62 lines (57 loc) · 1.91 KB
/
Copy pathgetSessionClient.ts
File metadata and controls
62 lines (57 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { Account, Address, Chain, Client, LocalAccount, RpcSchema, Transport } from "viem";
import { smartAccountActions } from "permissionless";
import { callFrom, sendUserOperationFrom } from "@latticexyz/world/internal";
import { createBundlerClient } from "./createBundlerClient";
import { SessionClient } from "./common";
import { SmartAccount } from "viem/account-abstraction";
import { getBundlerTransport } from "./getBundlerTransport";
export async function getSessionClient({
userAddress,
sessionAccount,
sessionSigner,
worldAddress,
}: {
userAddress: Address;
sessionAccount: SmartAccount;
sessionSigner: LocalAccount;
worldAddress: Address;
}): Promise<SessionClient> {
const client = sessionAccount.client;
if (!clientHasChain(client)) {
throw new Error("Session account client had no associated chain.");
}
const bundlerClient = createBundlerClient({
transport: getBundlerTransport(client.chain),
client,
account: sessionAccount,
});
const sessionClient = bundlerClient
.extend(smartAccountActions)
.extend(
callFrom({
worldAddress,
delegatorAddress: userAddress,
publicClient: client,
}),
)
.extend(
sendUserOperationFrom({
worldAddress,
delegatorAddress: userAddress,
publicClient: client,
}),
)
// TODO: add observer once we conditionally fetch receipts while bridge is open
.extend(() => ({ userAddress, worldAddress, internal_signer: sessionSigner }));
return sessionClient;
}
function clientHasChain<
transport extends Transport = Transport,
chain extends Chain | undefined = Chain | undefined,
account extends Account | undefined = Account | undefined,
rpcSchema extends RpcSchema | undefined = undefined,
>(
client: Client<transport, chain, account, rpcSchema>,
): client is Client<transport, Exclude<chain, undefined>, account, rpcSchema> {
return client.chain != null;
}