mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-01-10 17:43:06 +00:00
clean up - all tests passing
This commit is contained in:
parent
873124ccf7
commit
92b8ea028a
@ -45,7 +45,6 @@ proc raiseJsonRpcProviderError(
|
||||
if json =? JsonNode.fromJson(message):
|
||||
if "message" in json:
|
||||
message = json{"message"}.getStr
|
||||
echo "JsonRpcProviderError: ", message
|
||||
raise newException(JsonRpcProviderError, message)
|
||||
|
||||
template convertError(body) =
|
||||
@ -108,11 +107,8 @@ proc callImpl(
|
||||
args: JsonNode): Future[JsonNode] {.async: (raises: [JsonRpcProviderError]).} =
|
||||
|
||||
without response =? (await client.call(call, %args)).catch, error:
|
||||
echo "[jsonrpc.callImpl] error during call: ", error.msg
|
||||
raiseJsonRpcProviderError error.msg
|
||||
echo "[jsonrpc.callImpl] response: ", response.string
|
||||
without json =? JsonNode.fromJson(response.string), error:
|
||||
echo "[jsonrpc.callImpl] error after parsing json: ", error.msg
|
||||
raiseJsonRpcProviderError "Failed to parse response: " & error.msg
|
||||
json
|
||||
|
||||
@ -271,17 +267,14 @@ method unsubscribe*(
|
||||
convertError:
|
||||
let subscriptions = subscription.subscriptions
|
||||
let id = subscription.id
|
||||
echo "[jsonrpc.unsubscribe] subscription id: ", $id
|
||||
await subscriptions.unsubscribe(id)
|
||||
|
||||
method close*(
|
||||
provider: JsonRpcProvider) {.async: (raises:[ProviderError]).} =
|
||||
|
||||
convertError:
|
||||
echo "[JsonRpcProvider.close]"
|
||||
let client = await provider.client
|
||||
let subscriptions = await provider.subscriptions
|
||||
echo "[JsonRpcProvider.closing subscriptions]"
|
||||
await subscriptions.close()
|
||||
await client.close()
|
||||
|
||||
@ -334,7 +327,6 @@ method sendTransaction*(
|
||||
transaction: Transaction): Future[TransactionResponse] {.async.} =
|
||||
|
||||
convertSignerError:
|
||||
echo "[jsonrpc.sendTransaction]"
|
||||
if nonce =? transaction.nonce:
|
||||
signer.updateNonce(nonce)
|
||||
let
|
||||
|
||||
@ -40,11 +40,6 @@ template mapFailure*[T, V, E](
|
||||
|
||||
exp.mapErr(proc (e: V): ref CatchableError = (ref exc)(msg: e.msg))
|
||||
|
||||
proc expectFields(json: JsonNode, expectedFields: varargs[string]) =
|
||||
for fieldName in expectedFields:
|
||||
if not json.hasKey(fieldName):
|
||||
raiseSerializationError("'" & fieldName & "' field not found in " & $json)
|
||||
|
||||
# Address
|
||||
|
||||
func `%`*(address: Address): JsonNode =
|
||||
@ -57,45 +52,6 @@ func fromJson(_: type Address, json: JsonNode): ?!Address =
|
||||
"Failed to convert '" & $json & "' to Address: " & error.msg)
|
||||
success address
|
||||
|
||||
|
||||
# proc readValue*(
|
||||
# r: var JsonReader[JrpcConv],
|
||||
# result: var Address) {.raises: [SerializationError, IOError].} =
|
||||
|
||||
# let json = r.readValue(JsonNode)
|
||||
# result = Address.fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
# proc writeValue*(
|
||||
# writer: var JsonWriter[JrpcConv],
|
||||
# value: Address
|
||||
# ) {.raises:[IOError].} =
|
||||
# writer.writeValue(%value)
|
||||
|
||||
# Filter
|
||||
# func `%`*(filter: Filter): JsonNode =
|
||||
# %*{
|
||||
# "fromBlock": filter.fromBlock,
|
||||
# "toBlock": filter.toBlock
|
||||
# }
|
||||
|
||||
# proc writeValue*(
|
||||
# writer: var JsonWriter[JrpcConv],
|
||||
# value: Filter
|
||||
# ) {.raises:[IOError].} =
|
||||
# writer.writeValue(%value)
|
||||
|
||||
# EventFilter
|
||||
# func `%`*(filter: EventFilter): JsonNode =
|
||||
# %*{
|
||||
# "address": filter.address,
|
||||
# "topics": filter.topics
|
||||
# }
|
||||
# proc writeValue*(
|
||||
# writer: var JsonWriter[JrpcConv],
|
||||
# value: EventFilter
|
||||
# ) {.raises:[IOError].} =
|
||||
# writer.writeValue(%value)
|
||||
|
||||
# UInt256
|
||||
|
||||
func `%`*(integer: UInt256): JsonNode =
|
||||
@ -106,25 +62,6 @@ func fromJson*(_: type UInt256, json: JsonNode): ?!UInt256 =
|
||||
return UInt256.failure error.msg
|
||||
success result
|
||||
|
||||
# proc writeValue*(
|
||||
# w: var JsonWriter, value: StUint) {.inline, raises: [IOError].} =
|
||||
# echo "writing UInt256 value to hex: ", value.toString, ", in hex: ", value.toHex
|
||||
# w.writeValue %value
|
||||
|
||||
# proc readValue*(
|
||||
# r: var JsonReader, value: var StUint
|
||||
# ) {.inline, raises: [IOError, SerializationError].} =
|
||||
# let json = r.readValue(JsonNode)
|
||||
# value = typeof(value).fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
# TransactionHash
|
||||
|
||||
# proc readValue*(
|
||||
# r: var JsonReader, value: var TransactionHash
|
||||
# ) {.inline, raises: [IOError, SerializationError].} =
|
||||
# let json = r.readValue(JsonNode)
|
||||
# value = TransactionHash.fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
# Transaction
|
||||
|
||||
# TODO: add option that ignores none Option[T]
|
||||
@ -146,30 +83,8 @@ func `%`*(transaction: Transaction): JsonNode =
|
||||
if gasLimit =? transaction.gasLimit:
|
||||
result["gas"] = %gasLimit
|
||||
|
||||
# proc writeValue*(
|
||||
# writer: var JsonWriter[JrpcConv],
|
||||
# value: Transaction
|
||||
# ) {.raises:[IOError].} =
|
||||
# writer.writeValue(%value)
|
||||
|
||||
# Block
|
||||
|
||||
# proc readValue*(r: var JsonReader[JrpcConv], result: var Option[Block])
|
||||
# {.raises: [SerializationError, IOError].} =
|
||||
# var json = r.readValue(JsonNode)
|
||||
# if json.isNil or json.kind == JNull:
|
||||
# result = none Block
|
||||
|
||||
# result = Option[Block].fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
# BlockTag
|
||||
|
||||
# proc writeValue*(
|
||||
# writer: var JsonWriter[JrpcConv],
|
||||
# value: BlockTag
|
||||
# ) {.raises:[IOError].} =
|
||||
# writer.writeValue($value)
|
||||
|
||||
func `%`*(tag: BlockTag): JsonNode =
|
||||
% $tag
|
||||
|
||||
@ -202,24 +117,10 @@ proc fromJson*[E: TransactionStatus | TransactionType](
|
||||
let integer = ? fromHex[int](json.str).catch.mapFailure(SerializationError)
|
||||
success T(integer)
|
||||
|
||||
# proc readValue*(r: var JsonReader[JrpcConv],
|
||||
# result: var BlockTag) {.raises:[SerializationError, IOError].} =
|
||||
# var json = r.readValue(JsonNode)
|
||||
# result = BlockTag.fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
# PastTransaction
|
||||
|
||||
# proc readValue*(r: var JsonReader[JrpcConv], result: var Option[PastTransaction])
|
||||
# {.raises: [SerializationError, IOError].} =
|
||||
# var json = r.readValue(JsonNode)
|
||||
# result = Option[PastTransaction].fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
# TransactionReceipt
|
||||
|
||||
# proc readValue*(r: var JsonReader[JrpcConv], result: var Option[TransactionReceipt])
|
||||
# {.raises: [SerializationError, IOError].} =
|
||||
# var json = r.readValue(JsonNode)
|
||||
# result = Option[TransactionReceipt].fromJson(json).getOrRaise(SerializationError)
|
||||
# Generic conversions to use nim-json instead of nim-json-serialization for
|
||||
# json rpc serialization purposes
|
||||
# writeValue => `%`
|
||||
# readValue => fromJson
|
||||
|
||||
proc writeValue*[T: not JsonNode](
|
||||
writer: var JsonWriter[JrpcConv],
|
||||
@ -232,9 +133,4 @@ proc readValue*[T: not JsonNode](
|
||||
result: var T) {.raises: [SerializationError, IOError].} =
|
||||
|
||||
var json = r.readValue(JsonNode)
|
||||
# when T of JsonNode:
|
||||
# result = json
|
||||
# return
|
||||
# echo "[conversions.readValue] converting '", json, "' into ", T
|
||||
static: echo "[conversions.readValue] converting into ", T
|
||||
result = T.fromJson(json).getOrRaise(SerializationError)
|
||||
|
||||
@ -235,10 +235,8 @@ proc fromJson*[T: ref object or object](
|
||||
|
||||
proc parse*(json: string): ?!JsonNode =
|
||||
try:
|
||||
echo "[json.parse] json: ", json
|
||||
return parseJson(json).catch
|
||||
except Exception as e:
|
||||
echo "[json.parse] exception: ", e.msg
|
||||
return err newException(CatchableError, e.msg)
|
||||
|
||||
proc fromJson*[T: ref object or object](
|
||||
@ -252,14 +250,12 @@ proc fromJson*(
|
||||
_: type JsonNode,
|
||||
json: string
|
||||
): ?!JsonNode =
|
||||
echo "[JsonNode.fromJson] json: ", json
|
||||
return parse(json)
|
||||
|
||||
proc fromJson*[T: ref object or object](
|
||||
_: type T,
|
||||
json: string
|
||||
): ?!T =
|
||||
echo "[T.fromJson] T: ", T, ", json string: ", json
|
||||
let json = ? parse(json)
|
||||
T.fromJson(json)
|
||||
|
||||
|
||||
@ -27,18 +27,19 @@ func init*(subscriptions: JsonRpcSubscriptions) =
|
||||
subscriptions.client.onProcessMessage =
|
||||
proc(client: RpcClient,
|
||||
line: string): Result[bool, string] {.gcsafe, raises: [].} =
|
||||
# if json =? JrpcConv.decode(line, JsonNode).catch:
|
||||
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:
|
||||
echo "[subscriptions] processing method handler with params ", json{"params"}
|
||||
handler(json{"params"} or newJArray())
|
||||
return ok false # false = do not continue processing message using json_rpc's default processing handler
|
||||
# false = do not continue processing message using json_rpc's
|
||||
# default processing handler
|
||||
return ok false
|
||||
|
||||
return ok true # true = continue processing message using json_rpc's default message handler
|
||||
# true = continue processing message using json_rpc's default message handler
|
||||
return ok true
|
||||
|
||||
|
||||
proc setMethodHandler(
|
||||
@ -117,7 +118,6 @@ proc new*(_: type JsonRpcSubscriptions,
|
||||
proc subscriptionHandler(arguments: JsonNode) {.raises:[].} =
|
||||
if id =? arguments{"subscription"}.catch and
|
||||
callback =? subscriptions.getCallback(id):
|
||||
echo "[subscription handler] calling callback for id ", id
|
||||
callback(id, arguments)
|
||||
subscriptions.setMethodHandler("eth_subscription", subscriptionHandler)
|
||||
subscriptions
|
||||
@ -128,7 +128,6 @@ method subscribeBlocks(subscriptions: WebSocketSubscriptions,
|
||||
{.async.} =
|
||||
proc callback(id, arguments: JsonNode) {.raises: [].} =
|
||||
if blck =? Block.fromJson(arguments{"result"}):
|
||||
echo "[subscription.subscribeBlocks callback] calling onBlock callback with Block"
|
||||
onBlock(blck)
|
||||
let id = await subscriptions.client.eth_subscribe("newHeads")
|
||||
subscriptions.callbacks[id] = callback
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import ../../basics
|
||||
import ../../signer
|
||||
|
||||
type
|
||||
@ -7,7 +6,7 @@ type
|
||||
func raiseWalletError*(message: string) {.raises: [WalletError].}=
|
||||
raise newException(WalletError, message)
|
||||
|
||||
template convertError(body) =
|
||||
template convertError*(body) =
|
||||
try:
|
||||
body
|
||||
except CatchableError as error:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user