Skip to content

Commit af20937

Browse files
committed
feat(cli): connecting to anvil using WebSocket
1 parent be9d016 commit af20937

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

apps/cli/src/wallet.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import input from "@inquirer/input";
22
import {
33
createTestClient,
44
defineChain,
5+
fallback,
56
http,
67
publicActions,
78
walletActions,
9+
webSocket,
810
} from "viem";
911
import { anvil } from "viem/chains";
1012
import { getProjectName } from "./base.js";
@@ -37,18 +39,35 @@ const getRpcUrl = async (options: {
3739
}
3840
};
3941

42+
const getWsUrl = async (options: { wsUrl?: string; projectName?: string }) => {
43+
if (options.wsUrl) return options.wsUrl;
44+
45+
// otherwise, try to resolve host:port of the docker project
46+
try {
47+
const projectName = getProjectName(options);
48+
const host = await getProjectPort({ projectName });
49+
return `ws://${host}/anvil`;
50+
} catch {
51+
return await input({
52+
message: "WebSocket URL",
53+
default: `ws://127.0.0.1:${PREFERRED_PORT}/anvil`,
54+
});
55+
}
56+
};
57+
4058
export const connect = async (options: {
4159
rpcUrl?: string;
4260
projectName?: string;
4361
}) => {
4462
// resolve rpc url
4563
const rpcUrl = await getRpcUrl(options);
64+
const wsUrl = await getWsUrl(options);
4665

4766
// create test client
4867
const client = createTestClient({
4968
chain: cartesi,
5069
mode: "anvil",
51-
transport: http(rpcUrl),
70+
transport: fallback([webSocket(wsUrl), http(rpcUrl)]),
5271
pollingInterval: 200, // default is 4000ms (12s / 3)
5372
})
5473
.extend(publicActions)

0 commit comments

Comments
 (0)