2023-06-22 12:47:19 +02:00
|
|
|
import std/tables
|
2023-06-27 14:10:12 +02:00
|
|
|
import std/sequtils
|
2024-11-28 14:48:10 +01:00
|
|
|
import std/strutils
|
2023-06-22 12:47:19 +02:00
|
|
|
import pkg/chronos
|
2024-11-28 14:48:10 +01:00
|
|
|
import pkg/questionable
|
2023-06-22 12:47:19 +02:00
|
|
|
import pkg/json_rpc/rpcclient
|
2024-10-28 14:06:20 +11:00
|
|
|
import pkg/serde
|
2023-06-22 12:47:19 +02:00
|
|
|
import ../../basics
|
2024-11-28 14:48:10 +01:00
|
|
|
import ../../errors
|
2023-06-22 12:47:19 +02:00
|
|
|
import ../../provider
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
include ../../nimshims/hashes
|
2023-06-22 12:47:19 +02:00
|
|
|
import ./rpccalls
|
|
|
|
|
import ./conversions
|
|
|
|
|
|
2024-10-28 14:06:20 +11:00
|
|
|
export serde
|
|
|
|
|
|
2023-06-22 12:47:19 +02:00
|
|
|
type
|
|
|
|
|
JsonRpcSubscriptions* = ref object of RootObj
|
|
|
|
|
client: RpcClient
|
|
|
|
|
callbacks: Table[JsonNode, SubscriptionCallback]
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
methodHandlers: Table[string, MethodHandler]
|
2025-03-26 14:55:42 +01:00
|
|
|
# Used by both PollingSubscriptions and WebsocketSubscriptions to store
|
|
|
|
|
# subscription filters so the subscriptions can be recreated. With
|
|
|
|
|
# PollingSubscriptions, the RPC node might prune/forget about them, and with
|
|
|
|
|
# WebsocketSubscriptions, when using hardhat, subscriptions are dropped after 5
|
|
|
|
|
# minutes.
|
2025-03-19 13:40:37 +01:00
|
|
|
logFilters: Table[JsonNode, EventFilter]
|
2025-03-20 16:16:52 +01:00
|
|
|
when defined(ws_resubscribe):
|
2025-03-20 15:45:18 +01:00
|
|
|
resubscribeFut: Future[void]
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
MethodHandler* = proc (j: JsonNode) {.gcsafe, raises: [].}
|
2024-11-28 14:48:10 +01:00
|
|
|
SubscriptionCallback = proc(id: JsonNode, arguments: ?!JsonNode) {.gcsafe, raises:[].}
|
2023-06-22 12:47:19 +02:00
|
|
|
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
{.push raises:[].}
|
|
|
|
|
|
2025-03-20 16:16:52 +01:00
|
|
|
when defined(ws_resubscribe):
|
2025-03-20 15:45:18 +01:00
|
|
|
# This is a workaround to manage the 5 minutes limit due to hardhat.
|
|
|
|
|
# See https://github.com/NomicFoundation/hardhat/issues/2053#issuecomment-1061374064
|
|
|
|
|
proc resubscribeWebsocketEventsOnTimeout*(subscriptions: JsonRpcSubscriptions) {.async.} =
|
|
|
|
|
while true:
|
|
|
|
|
await sleepAsync(4.int64.minutes)
|
|
|
|
|
for id, callback in subscriptions.callbacks:
|
|
|
|
|
var newId: JsonNode
|
|
|
|
|
if id in subscriptions.logFilters:
|
|
|
|
|
let filter = subscriptions.logFilters[id]
|
|
|
|
|
newId = await subscriptions.client.eth_subscribe("logs", filter)
|
|
|
|
|
subscriptions.logFilters[newId] = filter
|
|
|
|
|
subscriptions.logFilters.del(id)
|
|
|
|
|
else:
|
|
|
|
|
newId = await subscriptions.client.eth_subscribe("newHeads")
|
|
|
|
|
|
|
|
|
|
subscriptions.callbacks[newId] = callback
|
|
|
|
|
subscriptions.callbacks.del(id)
|
|
|
|
|
discard await subscriptions.client.eth_unsubscribe(id)
|
|
|
|
|
|
2024-12-09 16:22:25 +01:00
|
|
|
template convertErrorsToSubscriptionError(body) =
|
|
|
|
|
try:
|
|
|
|
|
body
|
|
|
|
|
except CancelledError as error:
|
|
|
|
|
raise error
|
|
|
|
|
except CatchableError as error:
|
|
|
|
|
raise error.toErr(SubscriptionError)
|
|
|
|
|
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
template `or`(a: JsonNode, b: typed): JsonNode =
|
|
|
|
|
if a.isNil: b else: a
|
|
|
|
|
|
|
|
|
|
func start*(subscriptions: JsonRpcSubscriptions) =
|
|
|
|
|
subscriptions.client.onProcessMessage =
|
|
|
|
|
proc(client: RpcClient,
|
|
|
|
|
line: string): Result[bool, string] {.gcsafe, raises: [].} =
|
|
|
|
|
if json =? JsonNode.fromJson(line):
|
|
|
|
|
if "method" in json:
|
|
|
|
|
let methodName = json{"method"}.getStr()
|
|
|
|
|
if methodName in subscriptions.methodHandlers:
|
|
|
|
|
let handler = subscriptions.methodHandlers.getOrDefault(methodName)
|
|
|
|
|
if not handler.isNil:
|
|
|
|
|
handler(json{"params"} or newJArray())
|
|
|
|
|
# false = do not continue processing message using json_rpc's
|
|
|
|
|
# default processing handler
|
|
|
|
|
return ok false
|
|
|
|
|
|
|
|
|
|
# true = continue processing message using json_rpc's default message handler
|
|
|
|
|
return ok true
|
|
|
|
|
|
|
|
|
|
proc setMethodHandler(
|
|
|
|
|
subscriptions: JsonRpcSubscriptions,
|
|
|
|
|
`method`: string,
|
|
|
|
|
handler: MethodHandler
|
|
|
|
|
) =
|
|
|
|
|
subscriptions.methodHandlers[`method`] = handler
|
|
|
|
|
|
2023-06-22 12:47:19 +02:00
|
|
|
method subscribeBlocks*(subscriptions: JsonRpcSubscriptions,
|
|
|
|
|
onBlock: BlockHandler):
|
2023-06-28 11:02:21 +02:00
|
|
|
Future[JsonNode]
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [SubscriptionError, CancelledError]), base,.} =
|
2023-06-22 12:47:19 +02:00
|
|
|
raiseAssert "not implemented"
|
|
|
|
|
|
|
|
|
|
method subscribeLogs*(subscriptions: JsonRpcSubscriptions,
|
2023-07-20 15:51:28 +10:00
|
|
|
filter: EventFilter,
|
2023-06-22 12:47:19 +02:00
|
|
|
onLog: LogHandler):
|
2023-06-28 11:02:21 +02:00
|
|
|
Future[JsonNode]
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [SubscriptionError, CancelledError]), base.} =
|
2023-06-22 12:47:19 +02:00
|
|
|
raiseAssert "not implemented"
|
|
|
|
|
|
2023-06-28 11:02:21 +02:00
|
|
|
method unsubscribe*(subscriptions: JsonRpcSubscriptions,
|
|
|
|
|
id: JsonNode)
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [CancelledError]), base.} =
|
|
|
|
|
raiseAssert "not implemented "
|
2023-06-22 12:47:19 +02:00
|
|
|
|
2024-12-09 16:22:25 +01:00
|
|
|
method close*(subscriptions: JsonRpcSubscriptions) {.async: (raises: [SubscriptionError, CancelledError]), base.} =
|
2023-06-27 16:40:29 +02:00
|
|
|
let ids = toSeq subscriptions.callbacks.keys
|
|
|
|
|
for id in ids:
|
|
|
|
|
await subscriptions.unsubscribe(id)
|
|
|
|
|
|
2023-06-22 12:47:19 +02:00
|
|
|
proc getCallback(subscriptions: JsonRpcSubscriptions,
|
2024-11-28 14:48:10 +01:00
|
|
|
id: JsonNode): ?SubscriptionCallback {. raises:[].} =
|
2023-06-22 12:47:19 +02:00
|
|
|
try:
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
if not id.isNil and id in subscriptions.callbacks:
|
2024-11-28 14:48:10 +01:00
|
|
|
return subscriptions.callbacks[id].some
|
|
|
|
|
except: discard
|
2023-06-22 12:47:19 +02:00
|
|
|
|
|
|
|
|
# Web sockets
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
WebSocketSubscriptions = ref object of JsonRpcSubscriptions
|
|
|
|
|
|
2023-06-27 14:07:36 +02:00
|
|
|
proc new*(_: type JsonRpcSubscriptions,
|
|
|
|
|
client: RpcWebSocketClient): JsonRpcSubscriptions =
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
|
2023-06-27 14:07:36 +02:00
|
|
|
let subscriptions = WebSocketSubscriptions(client: client)
|
2023-12-12 09:24:37 +01:00
|
|
|
proc subscriptionHandler(arguments: JsonNode) {.raises:[].} =
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
let id = arguments{"subscription"} or newJString("")
|
|
|
|
|
if callback =? subscriptions.getCallback(id):
|
2024-11-28 14:48:10 +01:00
|
|
|
callback(id, success(arguments))
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
subscriptions.setMethodHandler("eth_subscription", subscriptionHandler)
|
2025-03-20 15:45:18 +01:00
|
|
|
|
2025-03-20 16:16:52 +01:00
|
|
|
when defined(ws_resubscribe):
|
2025-03-20 15:45:18 +01:00
|
|
|
subscriptions.resubscribeFut = resubscribeWebsocketEventsOnTimeout(subscriptions)
|
|
|
|
|
|
2023-06-27 14:07:36 +02:00
|
|
|
subscriptions
|
|
|
|
|
|
2023-06-22 12:47:19 +02:00
|
|
|
method subscribeBlocks(subscriptions: WebSocketSubscriptions,
|
|
|
|
|
onBlock: BlockHandler):
|
2023-06-28 11:02:21 +02:00
|
|
|
Future[JsonNode]
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [SubscriptionError, CancelledError]).} =
|
2024-11-28 14:48:10 +01:00
|
|
|
proc callback(id: JsonNode, argumentsResult: ?!JsonNode) {.raises: [].} =
|
|
|
|
|
without arguments =? argumentsResult, error:
|
|
|
|
|
onBlock(failure(Block, error.toErr(SubscriptionError)))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
let res = Block.fromJson(arguments{"result"}).mapFailure(SubscriptionError)
|
|
|
|
|
onBlock(res)
|
|
|
|
|
|
2024-12-09 16:22:25 +01:00
|
|
|
convertErrorsToSubscriptionError:
|
|
|
|
|
let id = await subscriptions.client.eth_subscribe("newHeads")
|
|
|
|
|
subscriptions.callbacks[id] = callback
|
|
|
|
|
return id
|
2023-06-22 12:47:19 +02:00
|
|
|
|
|
|
|
|
method subscribeLogs(subscriptions: WebSocketSubscriptions,
|
2023-07-20 15:51:28 +10:00
|
|
|
filter: EventFilter,
|
2023-06-22 12:47:19 +02:00
|
|
|
onLog: LogHandler):
|
2023-06-28 11:02:21 +02:00
|
|
|
Future[JsonNode]
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [SubscriptionError, CancelledError]).} =
|
2024-11-28 14:48:10 +01:00
|
|
|
proc callback(id: JsonNode, argumentsResult: ?!JsonNode) =
|
|
|
|
|
without arguments =? argumentsResult, error:
|
|
|
|
|
onLog(failure(Log, error.toErr(SubscriptionError)))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
let res = Log.fromJson(arguments{"result"}).mapFailure(SubscriptionError)
|
|
|
|
|
onLog(res)
|
|
|
|
|
|
2024-12-09 16:22:25 +01:00
|
|
|
convertErrorsToSubscriptionError:
|
|
|
|
|
let id = await subscriptions.client.eth_subscribe("logs", filter)
|
|
|
|
|
subscriptions.callbacks[id] = callback
|
2025-03-19 13:40:37 +01:00
|
|
|
subscriptions.logFilters[id] = filter
|
2024-12-09 16:22:25 +01:00
|
|
|
return id
|
2023-06-22 12:47:19 +02:00
|
|
|
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
method unsubscribe*(subscriptions: WebSocketSubscriptions,
|
2023-06-22 12:47:19 +02:00
|
|
|
id: JsonNode)
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [CancelledError]).} =
|
|
|
|
|
try:
|
|
|
|
|
subscriptions.callbacks.del(id)
|
|
|
|
|
discard await subscriptions.client.eth_unsubscribe(id)
|
|
|
|
|
except CancelledError as e:
|
|
|
|
|
raise e
|
|
|
|
|
except CatchableError:
|
|
|
|
|
# Ignore if uninstallation of the subscribiton fails.
|
|
|
|
|
discard
|
2023-06-22 12:47:19 +02:00
|
|
|
|
2025-03-26 14:55:42 +01:00
|
|
|
method close*(subscriptions: WebsocketSubscriptions) {.async.} =
|
|
|
|
|
await procCall JsonRpcSubscriptions(subscriptions).close()
|
|
|
|
|
if not subscriptions.resubscribeFut.isNil:
|
|
|
|
|
await subscriptions.resubscribeFut.cancelAndWait()
|
|
|
|
|
|
2023-06-22 12:47:19 +02:00
|
|
|
# Polling
|
|
|
|
|
|
|
|
|
|
type
|
fix: modify unsubscribe cleanup routine and tests (#84)
* fix: modify unsubscribe cleanup routine
Ignore exceptions (other than CancelledError) if uninstallation of the filter fails. If it's the last step in the subscription cleanup, then filter changes for this filter will no longer be polled so if the filter continues to live on in geth for whatever reason, then it doesn't matter.
This includes a number of fixes:
- `CancelledError` is now caught inside of `getChanges`. This was causing conditions during `subscriptions.close`, where the `CancelledError` would get consumed by the `except CatchableError`, if there was an ongoing `poll` happening at the time of close.
- After creating a new filter inside of `getChanges`, the new filter is polled for changes before returning.
- `getChanges` also does not swallow `CatchableError` by returning an empty array, and instead re-raises the error if it is not `filter not found`.
- The tests were simplified by accessing the private fields of `PollingSubscriptions`. That way, there wasn't a race condition for the `newFilterId` counter inside of the mock.
- The `MockRpcHttpServer` was simplified by keeping track of the active filters only, and invalidation simply removes the filter. The tests then only needed to rely on the fact that the filter id changed in the mapping.
- Because of the above changes, we no longer needed to sleep inside of the tests, so the sleeps were removed, and the polling interval could be changed to 1ms, which not only makes the tests faster, but would further highlight any race conditions if present.
* docs: rpc custom port documentation
---------
Co-authored-by: Adam Uhlíř <adam@uhlir.dev>
2024-10-25 14:58:45 +11:00
|
|
|
PollingSubscriptions* = ref object of JsonRpcSubscriptions
|
2023-06-27 16:45:38 +02:00
|
|
|
polling: Future[void]
|
2023-06-22 12:47:19 +02:00
|
|
|
|
2024-10-22 15:57:25 +02:00
|
|
|
# Used when filters are recreated to translate from the id that user
|
|
|
|
|
# originally got returned to new filter id
|
|
|
|
|
subscriptionMapping: Table[JsonNode, JsonNode]
|
|
|
|
|
|
2023-06-27 14:10:12 +02:00
|
|
|
proc new*(_: type JsonRpcSubscriptions,
|
2023-06-27 14:33:14 +02:00
|
|
|
client: RpcHttpClient,
|
|
|
|
|
pollingInterval = 4.seconds): JsonRpcSubscriptions =
|
2023-06-27 14:10:12 +02:00
|
|
|
|
|
|
|
|
let subscriptions = PollingSubscriptions(client: client)
|
|
|
|
|
|
2024-11-28 14:48:10 +01:00
|
|
|
proc resubscribe(id: JsonNode): Future[?!void] {.async: (raises: [CancelledError]).} =
|
2023-06-27 14:56:45 +02:00
|
|
|
try:
|
2024-11-11 13:59:39 +01:00
|
|
|
var newId: JsonNode
|
|
|
|
|
# Log filters are stored in logFilters, block filters are not persisted
|
|
|
|
|
# there is they do not need any specific data for their recreation.
|
|
|
|
|
# We use this to determine if the filter was log or block filter here.
|
|
|
|
|
if subscriptions.logFilters.hasKey(id):
|
|
|
|
|
let filter = subscriptions.logFilters[id]
|
|
|
|
|
newId = await subscriptions.client.eth_newFilter(filter)
|
fix: modify unsubscribe cleanup routine and tests (#84)
* fix: modify unsubscribe cleanup routine
Ignore exceptions (other than CancelledError) if uninstallation of the filter fails. If it's the last step in the subscription cleanup, then filter changes for this filter will no longer be polled so if the filter continues to live on in geth for whatever reason, then it doesn't matter.
This includes a number of fixes:
- `CancelledError` is now caught inside of `getChanges`. This was causing conditions during `subscriptions.close`, where the `CancelledError` would get consumed by the `except CatchableError`, if there was an ongoing `poll` happening at the time of close.
- After creating a new filter inside of `getChanges`, the new filter is polled for changes before returning.
- `getChanges` also does not swallow `CatchableError` by returning an empty array, and instead re-raises the error if it is not `filter not found`.
- The tests were simplified by accessing the private fields of `PollingSubscriptions`. That way, there wasn't a race condition for the `newFilterId` counter inside of the mock.
- The `MockRpcHttpServer` was simplified by keeping track of the active filters only, and invalidation simply removes the filter. The tests then only needed to rely on the fact that the filter id changed in the mapping.
- Because of the above changes, we no longer needed to sleep inside of the tests, so the sleeps were removed, and the polling interval could be changed to 1ms, which not only makes the tests faster, but would further highlight any race conditions if present.
* docs: rpc custom port documentation
---------
Co-authored-by: Adam Uhlíř <adam@uhlir.dev>
2024-10-25 14:58:45 +11:00
|
|
|
else:
|
2024-11-11 13:59:39 +01:00
|
|
|
newId = await subscriptions.client.eth_newBlockFilter()
|
|
|
|
|
subscriptions.subscriptionMapping[id] = newId
|
2024-11-28 14:48:10 +01:00
|
|
|
except CancelledError as e:
|
|
|
|
|
raise e
|
|
|
|
|
except CatchableError as e:
|
|
|
|
|
return failure(void, e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg))
|
|
|
|
|
|
|
|
|
|
return success()
|
2024-11-11 13:59:39 +01:00
|
|
|
|
2024-11-28 14:48:10 +01:00
|
|
|
proc getChanges(id: JsonNode): Future[?!JsonNode] {.async: (raises: [CancelledError]).} =
|
2024-11-12 08:44:51 +01:00
|
|
|
if mappedId =? subscriptions.subscriptionMapping.?[id]:
|
|
|
|
|
try:
|
|
|
|
|
let changes = await subscriptions.client.eth_getFilterChanges(mappedId)
|
|
|
|
|
if changes.kind == JArray:
|
2024-11-28 14:48:10 +01:00
|
|
|
return success(changes)
|
|
|
|
|
except JsonRpcError as e:
|
|
|
|
|
if error =? (await resubscribe(id)).errorOption:
|
|
|
|
|
return failure(JsonNode, error)
|
|
|
|
|
|
2024-11-12 08:44:51 +01:00
|
|
|
# TODO: we could still miss some events between losing the subscription
|
|
|
|
|
# and resubscribing. We should probably adopt a strategy like ethers.js,
|
|
|
|
|
# whereby we keep track of the latest block number that we've seen
|
|
|
|
|
# filter changes for:
|
|
|
|
|
# https://github.com/ethers-io/ethers.js/blob/f97b92bbb1bde22fcc44100af78d7f31602863ab/packages/providers/src.ts/base-provider.ts#L977
|
2024-11-28 14:48:10 +01:00
|
|
|
|
|
|
|
|
if not ("filter not found" in e.msg):
|
|
|
|
|
return failure(JsonNode, e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg))
|
|
|
|
|
except CancelledError as e:
|
|
|
|
|
raise e
|
|
|
|
|
except SubscriptionError as e:
|
|
|
|
|
return failure(JsonNode, e)
|
|
|
|
|
except CatchableError as e:
|
|
|
|
|
return failure(JsonNode, e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg))
|
|
|
|
|
return success(newJArray())
|
2023-06-27 14:56:45 +02:00
|
|
|
|
2024-11-11 13:59:39 +01:00
|
|
|
proc poll(id: JsonNode) {.async: (raises: [CancelledError]).} =
|
2024-11-28 14:48:10 +01:00
|
|
|
without callback =? subscriptions.getCallback(id):
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
without changes =? (await getChanges(id)), error:
|
|
|
|
|
callback(id, failure(JsonNode, error))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for change in changes:
|
|
|
|
|
callback(id, success(change))
|
2023-06-27 14:10:12 +02:00
|
|
|
|
2024-11-11 13:59:39 +01:00
|
|
|
proc poll {.async: (raises: []).} =
|
|
|
|
|
try:
|
|
|
|
|
while true:
|
|
|
|
|
for id in toSeq subscriptions.callbacks.keys:
|
|
|
|
|
await poll(id)
|
|
|
|
|
await sleepAsync(pollingInterval)
|
|
|
|
|
except CancelledError:
|
|
|
|
|
discard
|
2023-06-27 14:10:12 +02:00
|
|
|
|
2023-06-27 16:45:38 +02:00
|
|
|
subscriptions.polling = poll()
|
2024-11-11 13:59:39 +01:00
|
|
|
asyncSpawn subscriptions.polling
|
2023-06-27 14:10:12 +02:00
|
|
|
subscriptions
|
|
|
|
|
|
2023-06-27 16:45:38 +02:00
|
|
|
method close*(subscriptions: PollingSubscriptions) {.async.} =
|
|
|
|
|
await subscriptions.polling.cancelAndWait()
|
|
|
|
|
await procCall JsonRpcSubscriptions(subscriptions).close()
|
|
|
|
|
|
2023-06-27 14:10:12 +02:00
|
|
|
method subscribeBlocks(subscriptions: PollingSubscriptions,
|
|
|
|
|
onBlock: BlockHandler):
|
2023-06-28 11:02:21 +02:00
|
|
|
Future[JsonNode]
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [SubscriptionError, CancelledError]).} =
|
2023-06-27 14:10:12 +02:00
|
|
|
|
2024-11-28 14:48:10 +01:00
|
|
|
proc getBlock(hash: BlockHash) {.async: (raises:[]).} =
|
2023-06-27 15:24:29 +02:00
|
|
|
try:
|
|
|
|
|
if blck =? (await subscriptions.client.eth_getBlockByHash(hash, false)):
|
2024-11-28 14:48:10 +01:00
|
|
|
onBlock(success(blck))
|
|
|
|
|
except CancelledError:
|
2023-06-27 15:24:29 +02:00
|
|
|
discard
|
2024-11-28 14:48:10 +01:00
|
|
|
except CatchableError as e:
|
|
|
|
|
let error = e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription's block: " & e.msg)
|
|
|
|
|
onBlock(failure(Block, error))
|
|
|
|
|
|
|
|
|
|
proc callback(id: JsonNode, changeResult: ?!JsonNode) {.raises:[].} =
|
|
|
|
|
without change =? changeResult, e:
|
|
|
|
|
onBlock(failure(Block, e.toErr(SubscriptionError)))
|
|
|
|
|
return
|
2023-06-27 14:10:12 +02:00
|
|
|
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
if hash =? BlockHash.fromJson(change):
|
2023-06-27 14:10:12 +02:00
|
|
|
asyncSpawn getBlock(hash)
|
|
|
|
|
|
2024-12-09 16:22:25 +01:00
|
|
|
convertErrorsToSubscriptionError:
|
|
|
|
|
let id = await subscriptions.client.eth_newBlockFilter()
|
|
|
|
|
subscriptions.callbacks[id] = callback
|
|
|
|
|
subscriptions.subscriptionMapping[id] = id
|
|
|
|
|
return id
|
2023-06-27 14:10:12 +02:00
|
|
|
|
2023-06-27 15:56:57 +02:00
|
|
|
method subscribeLogs(subscriptions: PollingSubscriptions,
|
2023-07-20 15:51:28 +10:00
|
|
|
filter: EventFilter,
|
2023-06-27 15:56:57 +02:00
|
|
|
onLog: LogHandler):
|
2023-06-28 11:02:21 +02:00
|
|
|
Future[JsonNode]
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [SubscriptionError, CancelledError]).} =
|
2023-06-27 15:56:57 +02:00
|
|
|
|
2024-11-28 14:48:10 +01:00
|
|
|
proc callback(id: JsonNode, argumentsResult: ?!JsonNode) =
|
|
|
|
|
without arguments =? argumentsResult, error:
|
|
|
|
|
onLog(failure(Log, error.toErr(SubscriptionError)))
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
let res = Log.fromJson(arguments).mapFailure(SubscriptionError)
|
|
|
|
|
onLog(res)
|
2023-06-27 15:56:57 +02:00
|
|
|
|
2024-12-09 16:22:25 +01:00
|
|
|
convertErrorsToSubscriptionError:
|
|
|
|
|
let id = await subscriptions.client.eth_newFilter(filter)
|
|
|
|
|
subscriptions.callbacks[id] = callback
|
|
|
|
|
subscriptions.logFilters[id] = filter
|
|
|
|
|
subscriptions.subscriptionMapping[id] = id
|
|
|
|
|
return id
|
2023-06-27 15:56:57 +02:00
|
|
|
|
Upgrade to `nim-json-rpc` v0.4.2 and chronos v4 (#64)
* Add json de/serialization lib from codex to handle conversions
json-rpc now requires nim-json-serialization to convert types to/from json. Use the nim-json-serialization signatures to call the json serialization lib from nim-codex (should be moved to its own lib)
* Add ethers implementation for setMethodHandler
Was removed in json-rpc
* More json conversion updates
* Fix json_rpc.call returning JsonString instead of JsonNode
* Update exceptions
Use {.async: (raises: [...].} where needed
Annotate provider with {.push raises:[].}
Format signatures
* Start fixing tests (mainly conversion fixes)
* rename sender to `from`, update json error logging, add more conversions
* Refactor exceptions for providers and signers, fix more tests
- signer procs raise SignerError, provider procs raise ProviderError
- WalletError now inherits from SignerError
- move wallet module under signers
- create jsonrpo moudle under signers
- bump nim-json-rpc for null-handling fixes
- All jsonrpc provider tests passing, still need to fix others
* remove raises from async annotation for dynamic dispatch
- removes async: raises from getAddress and signTransaction because derived JsonRpcSigner methods were not being used when dynamically dispatched. Once `raises` was removed from the async annotation, the dynamic dispatch worked again. This is only the case for getAddress and signTransaction.
- add gcsafe annotation to wallet.provider so that it matches the base method
* Catch EstimateGasError before ProviderError
EstimateGasError is now a ProviderError (it is a SignerError, and SignerError is a ProviderError), so EstimateGasErrors were not being caught
* clean up - all tests passing
* support nim 2.0
* lock in chronos version
* Add serde options to the json util, along with tests
next step is to:
1. change back any ethers var names that were changed for serialization purposes, eg `from` and `type`
2. move the json util to its own lib
* bump json-rpc to 0.4.0 and fix test
* fix: specify raises for getAddress and sendTransaction
Fixes issue where getAddress and sendTransaction could not be found for MockSigner in tests. The problem was that the async: raises update had not been applied to the MockSigner.
* handle exceptions during jsonrpc init
There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution
* cleanup
* deserialize key defaults to serialize key
* Add more tests for OptIn/OptOut/Strict modes, fix logic
* use nim-serde instead of json util
Allows aliasing of de/serialized fields, so revert changes of sender to `from` and transactionType to `type`
* Move hash* shim to its own module
* address PR feedback
- add comments to hashes shim
- remove .catch from callback condition
- derive SignerError from EthersError instead of ProviderError. This allows Providers and Signers to be separate, as Ledger does it, to isolate functionality. Some signer functions now raise both ProviderError and SignerError
- Update reverts to check for SignerError
- Update ERC-20 method comment
* rename subscriptions.init > subscriptions.start
2024-02-19 16:50:46 +11:00
|
|
|
method unsubscribe*(subscriptions: PollingSubscriptions,
|
2023-06-27 14:10:12 +02:00
|
|
|
id: JsonNode)
|
2024-12-09 16:22:25 +01:00
|
|
|
{.async: (raises: [CancelledError]).} =
|
2024-11-28 15:32:55 +01:00
|
|
|
try:
|
|
|
|
|
subscriptions.logFilters.del(id)
|
|
|
|
|
subscriptions.callbacks.del(id)
|
|
|
|
|
if sub =? subscriptions.subscriptionMapping.?[id]:
|
|
|
|
|
subscriptions.subscriptionMapping.del(id)
|
2024-11-27 22:57:51 +01:00
|
|
|
discard await subscriptions.client.eth_uninstallFilter(sub)
|
2024-11-28 15:32:55 +01:00
|
|
|
except CancelledError as e:
|
|
|
|
|
raise e
|
|
|
|
|
except CatchableError:
|
|
|
|
|
# Ignore if uninstallation of the filter fails. If it's the last step in our
|
|
|
|
|
# cleanup, then filter changes for this filter will no longer be polled so
|
|
|
|
|
# if the filter continues to live on in geth for whatever reason then it
|
|
|
|
|
# doesn't matter.
|
|
|
|
|
discard
|