@@ -90,13 +90,13 @@ template depositContractAddress(m: Eth1Monitor): Eth1Address =
9090template web3Url (m: Eth1Monitor ): string =
9191 m.dataProvider.url
9292
93- proc fixupInfuraUrls * (web3Url: var string ) =
93+ proc fixupWeb3Urls * (web3Url: var string ) =
9494 # # Converts HTTP and HTTPS Infura URLs to their WebSocket equivalents
9595 # # because we are missing a functional HTTPS client.
9696 let normalizedUrl = toLowerAscii (web3Url)
9797 var pos = 0
9898
99- template skip (x: string ): bool =
99+ template skip (x: string ): bool {. dirty .} =
100100 if normalizedUrl.len - pos >= x.len and
101101 normalizedUrl.toOpenArray (pos, pos + x.len - 1 ) == x:
102102 pos += x.len
@@ -105,22 +105,28 @@ proc fixupInfuraUrls*(web3Url: var string) =
105105 false
106106
107107 if not (skip (" https://" ) or skip (" http://" )):
108+ if not (skip (" ws://" ) or skip (" wss://" )):
109+ web3Url = " ws://" & web3Url
110+ warn " The Web3 URL does not specify a protocol. Assuming a WebSocket server" , web3Url
108111 return
109112
110- let
111- isMainnet = skip (" mainnet" )
112- isGoerli = skip (" goerli" )
113+ block infuraRewrite:
114+ var pos = pos
115+ let network = if skip (" mainnet" ): mainnet
116+ elif skip (" goerli" ): goerli
117+ else : break
113118
114- if not (isMainnet or isGoerli ):
115- return
119+ if not skip ( " .infura.io/v3/ " ):
120+ break
116121
117- if not skip (" .infura.io/v3/" ):
118- return
122+ template infuraKey : string = normalizedUrl.substr (pos)
119123
120- template infuraKey : string = normalizedUrl.substr (pos)
124+ web3Url = " wss://" & $ network & " .infura.io/ws/v3/" & infuraKey
125+ return
121126
122- web3Url = " wss://" & (if isMainnet: " mainnet" else : " goerli" ) &
123- " .infura.io/ws/v3/" & infuraKey
127+ block gethRewrite:
128+ web3Url = " ws://" & normalizedUrl.substr (pos)
129+ warn " Only WebSocket web3 providers are supported. Rewriting URL" , web3Url
124130
125131# TODO : Add preset validation
126132# MIN_GENESIS_ACTIVE_VALIDATOR_COUNT should be larger than SLOTS_PER_EPOCH
@@ -392,7 +398,7 @@ proc init*(T: type Eth1Monitor,
392398 depositContractDeployedAt: string ,
393399 eth1Network: Option [Eth1Network ]): Future [Result [T, string ]] {.async .} =
394400 var web3Url = web3Url
395- fixupInfuraUrls web3Url
401+ fixupWeb3Urls web3Url
396402
397403 let web3 = try : await newWeb3 (web3Url)
398404 except CatchableError as err:
0 commit comments