File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -2,9 +2,11 @@ import input from "@inquirer/input";
22import {
33 createTestClient ,
44 defineChain ,
5+ fallback ,
56 http ,
67 publicActions ,
78 walletActions ,
9+ webSocket ,
810} from "viem" ;
911import { anvil } from "viem/chains" ;
1012import { 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+
4058export 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 )
You can’t perform that action at this time.
0 commit comments