Close provider by unsubscribing and closing client
This commit is contained in:
parent
f8cac08cde
commit
a27c2de41c
|
@ -90,6 +90,12 @@ await writableToken.transfer(accounts[7], 42.u256)
|
|||
|
||||
Which transfers 42 tokens from account 3 to account 7
|
||||
|
||||
And lastly, don't forget to close the provider when you're done:
|
||||
|
||||
```
|
||||
await provider.close()
|
||||
```
|
||||
|
||||
Events
|
||||
------
|
||||
|
||||
|
|
|
@ -208,3 +208,6 @@ proc confirm*(tx: Future[?TransactionResponse],
|
|||
)
|
||||
|
||||
return await txResp.confirm(wantedConfirms, timeoutInBlocks)
|
||||
|
||||
method close*(provider: Provider) {.async, base.} =
|
||||
discard
|
||||
|
|
|
@ -177,6 +177,13 @@ method subscribe*(provider: JsonRpcProvider,
|
|||
let subscriptions = await provider.subscriptions
|
||||
return await subscriptions.subscribeBlocks(onBlock)
|
||||
|
||||
method close*(provider: JsonRpcProvider) {.async.} =
|
||||
convertError:
|
||||
let client = await provider.client
|
||||
let subscriptions = await provider.subscriptions
|
||||
await subscriptions.close()
|
||||
await client.close()
|
||||
|
||||
# Signer
|
||||
|
||||
method provider*(signer: JsonRpcSigner): Provider =
|
||||
|
|
|
@ -35,6 +35,11 @@ method unsubscribe(subscriptions: JsonRpcSubscriptions,
|
|||
{.async, base.} =
|
||||
raiseAssert "not implemented"
|
||||
|
||||
method close*(subscriptions: JsonRpcSubscriptions) {.async.} =
|
||||
let ids = toSeq subscriptions.callbacks.keys
|
||||
for id in ids:
|
||||
await subscriptions.unsubscribe(id)
|
||||
|
||||
method unsubscribe(subscription: JsonRpcSubscription) {.async.} =
|
||||
let subscriptions = subscription.subscriptions
|
||||
let id = subscription.id
|
||||
|
|
|
@ -16,6 +16,10 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
|
|||
setup:
|
||||
provider = JsonRpcProvider.new(url, pollingInterval = 100.millis)
|
||||
|
||||
|
||||
teardown:
|
||||
await provider.close()
|
||||
|
||||
test "can be instantiated with a default URL":
|
||||
discard JsonRpcProvider.new()
|
||||
|
||||
|
|
|
@ -12,6 +12,9 @@ suite "JsonRpcSigner":
|
|||
provider = JsonRpcProvider.new()
|
||||
accounts = await provider.listAccounts()
|
||||
|
||||
teardown:
|
||||
await provider.close()
|
||||
|
||||
test "is connected to the first account of the provider by default":
|
||||
let signer = provider.getSigner()
|
||||
check (await signer.getAddress()) == accounts[0]
|
||||
|
|
|
@ -37,10 +37,23 @@ template subscriptionTests(subscriptions, client) =
|
|||
discard await client.call("evm_mine", newJArray())
|
||||
check eventually count > 0
|
||||
await subscription.unsubscribe()
|
||||
let endcount = count
|
||||
count = 0
|
||||
discard await client.call("evm_mine", newJArray())
|
||||
await sleepAsync(100.millis)
|
||||
check count == endcount
|
||||
check count == 0
|
||||
|
||||
test "stops listening to new blocks when provider is closed":
|
||||
var count = 0
|
||||
proc callback(blck: Block) {.async.} =
|
||||
inc count
|
||||
let subscription = await subscriptions.subscribeBlocks(callback)
|
||||
discard await client.call("evm_mine", newJArray())
|
||||
check eventually count > 0
|
||||
await subscriptions.close()
|
||||
count = 0
|
||||
discard await client.call("evm_mine", newJArray())
|
||||
await sleepAsync(100.millis)
|
||||
check count == 0
|
||||
|
||||
suite "Web socket subscriptions":
|
||||
|
||||
|
@ -52,6 +65,10 @@ suite "Web socket subscriptions":
|
|||
await client.connect("ws://localhost:8545")
|
||||
subscriptions = JsonRpcSubscriptions.new(client)
|
||||
|
||||
teardown:
|
||||
await subscriptions.close()
|
||||
await client.close()
|
||||
|
||||
subscriptionTests(subscriptions, client)
|
||||
|
||||
suite "HTTP polling subscriptions":
|
||||
|
@ -65,4 +82,8 @@ suite "HTTP polling subscriptions":
|
|||
subscriptions = JsonRpcSubscriptions.new(client,
|
||||
pollingInterval = 100.millis)
|
||||
|
||||
teardown:
|
||||
await subscriptions.close()
|
||||
await client.close()
|
||||
|
||||
subscriptionTests(subscriptions, client)
|
||||
|
|
|
@ -33,6 +33,7 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
|
|||
|
||||
teardown:
|
||||
discard await provider.send("evm_revert", @[snapshot])
|
||||
await provider.close()
|
||||
|
||||
test "can call constant functions":
|
||||
check (await token.name()) == "TestToken"
|
||||
|
|
|
@ -22,6 +22,7 @@ suite "Contract enum parameters and return values":
|
|||
|
||||
teardown:
|
||||
discard await provider.send("evm_revert", @[snapshot])
|
||||
await provider.close()
|
||||
|
||||
test "handles enum parameter and return value":
|
||||
proc returnValue(contract: TestEnums,
|
||||
|
|
|
@ -33,6 +33,7 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]:
|
|||
|
||||
teardown:
|
||||
discard await provider.send("evm_revert", @[snapshot])
|
||||
await provider.close()
|
||||
|
||||
test "retrieves basic information":
|
||||
check (await token.name()) == "TestToken"
|
||||
|
|
|
@ -21,6 +21,7 @@ suite "Contract return values":
|
|||
|
||||
teardown:
|
||||
discard await provider.send("evm_revert", @[snapshot])
|
||||
await provider.close()
|
||||
|
||||
test "handles static size structs":
|
||||
proc getStatic(contract: TestReturns): Static {.contract, pure.}
|
||||
|
|
|
@ -88,6 +88,7 @@ suite "Testing helpers - provider":
|
|||
|
||||
teardown:
|
||||
discard await provider.send("evm_revert", @[snapshot])
|
||||
await provider.close()
|
||||
|
||||
test "revert works with provider":
|
||||
check await helpersContract.revertsWith(revertReason).reverts(revertReason)
|
||||
|
|
|
@ -20,9 +20,10 @@ suite "Wallet":
|
|||
setup:
|
||||
provider = JsonRpcProvider.new()
|
||||
snapshot = await provider.send("evm_snapshot")
|
||||
|
||||
|
||||
teardown:
|
||||
discard await provider.send("evm_revert", @[snapshot])
|
||||
await provider.close()
|
||||
|
||||
test "Can create Wallet with private key":
|
||||
discard Wallet.new(pk1)
|
||||
|
@ -114,7 +115,7 @@ suite "Wallet":
|
|||
gasLimit: some 22_000.u256)
|
||||
let testToken = Erc20.new(wallet.address, wallet)
|
||||
await testToken.transfer(wallet.address, 24.u256, overrides)
|
||||
|
||||
|
||||
test "Can call state-changing function automatically EIP1559":
|
||||
#TODO add actual token contract, not random address. Should work regardless
|
||||
let wallet = Wallet.new(pk_with_funds, provider)
|
||||
|
|
Loading…
Reference in New Issue