Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion apps/cli/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import input from "@inquirer/input";
import {
createTestClient,
defineChain,
fallback,
http,
publicActions,
walletActions,
webSocket,
} from "viem";
import { anvil } from "viem/chains";
import { getProjectName } from "./base.js";
Expand Down Expand Up @@ -37,18 +39,35 @@ const getRpcUrl = async (options: {
}
};

const getWsUrl = async (options: { wsUrl?: string; projectName?: string }) => {
if (options.wsUrl) return options.wsUrl;

// otherwise, try to resolve host:port of the docker project
try {
const projectName = getProjectName(options);
const host = await getProjectPort({ projectName });
return `ws://${host}/anvil`;
} catch {
return await input({
message: "WebSocket URL",
default: `ws://127.0.0.1:${PREFERRED_PORT}/anvil`,
});
}
};

export const connect = async (options: {
rpcUrl?: string;
projectName?: string;
}) => {
// resolve rpc url
const rpcUrl = await getRpcUrl(options);
const wsUrl = await getWsUrl(options);

// create test client
const client = createTestClient({
chain: cartesi,
mode: "anvil",
transport: http(rpcUrl),
transport: fallback([webSocket(wsUrl), http(rpcUrl)]),
pollingInterval: 200, // default is 4000ms (12s / 3)
})
.extend(publicActions)
Expand Down