Skip to content

Commit d14ad04

Browse files
committed
refactor
1 parent d493113 commit d14ad04

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

web3.nim

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,43 +102,42 @@ proc onSubscription(w: Web3, subscription: string, params: JsonString) =
102102
func newWeb3*(provider: RpcClient): Web3 =
103103
result = Web3(provider: provider)
104104

105-
if provider of RpcConnection:
106-
let provider = RpcConnection(provider)
107-
108-
let w3 = result
109-
if provider.router == nil:
110-
provider.router = RpcRouter.new()
111-
112-
provider.router[].rpc("eth_subscription") do(
113-
subscription: string, params: JsonString
114-
) -> void:
115-
w3.onSubscription(subscription, params)
116-
117105
proc newWeb3*(
118106
uri: string,
119107
getHeaders: GetJsonRpcRequestHeaders = nil,
120-
httpFlags: HttpClientFlags = {}):
121-
Future[Web3] {.async.} =
108+
httpFlags: HttpClientFlags = {},
109+
): Future[Web3] {.async.} =
122110
let u = parseUri(uri)
123111
var provider: RpcClient
124112
case u.scheme
125113
of "http", "https":
126-
let p = newRpcHttpClient(getHeaders = getHeaders,
127-
flags = httpFlags)
114+
let p = newRpcHttpClient(getHeaders = getHeaders, flags = httpFlags)
128115
await p.connect(uri)
129-
provider = p
116+
117+
let w3 = newWeb3(p)
118+
p.onDisconnect = proc() =
119+
if not w3.onDisconnect.isNil:
120+
w3.onDisconnect()
121+
w3
130122
of "ws", "wss":
131-
let p = newRpcWebSocketClient(getHeaders = getHeaders)
123+
let router = RpcRouter.new()
124+
125+
let p = newRpcWebSocketClient(getHeaders = getHeaders, router = router)
132126
await p.connect(uri)
133-
provider = p
127+
128+
let w3 = newWeb3(p)
129+
router[].rpc("eth_subscription") do(
130+
subscription: string, params: JsonString
131+
) -> void:
132+
w3.onSubscription(subscription, params)
133+
134+
p.onDisconnect = proc() =
135+
w3.subscriptions.clear()
136+
if not w3.onDisconnect.isNil:
137+
w3.onDisconnect()
138+
w3
134139
else:
135140
raise newException(CatchableError, "Unknown web3 url scheme")
136-
result = newWeb3(provider)
137-
let r = result
138-
provider.onDisconnect = proc() =
139-
r.subscriptions.clear()
140-
if not r.onDisconnect.isNil:
141-
r.onDisconnect()
142141

143142
proc close*(web3: Web3): Future[void] = web3.provider.close()
144143

0 commit comments

Comments
 (0)