mirror of
https://github.com/logos-storage/nim-ethers.git
synced 2026-05-18 07:59:52 +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 json =? JsonNode.fromJson(message):
|
||||||
if "message" in json:
|
if "message" in json:
|
||||||
message = json{"message"}.getStr
|
message = json{"message"}.getStr
|
||||||
echo "JsonRpcProviderError: ", message
|
|
||||||
raise newException(JsonRpcProviderError, message)
|
raise newException(JsonRpcProviderError, message)
|
||||||
|
|
||||||
template convertError(body) =
|
template convertError(body) =
|
||||||
@ -108,11 +107,8 @@ proc callImpl(
|
|||||||
args: JsonNode): Future[JsonNode] {.async: (raises: [JsonRpcProviderError]).} =
|
args: JsonNode): Future[JsonNode] {.async: (raises: [JsonRpcProviderError]).} =
|
||||||
|
|
||||||
without response =? (await client.call(call, %args)).catch, error:
|
without response =? (await client.call(call, %args)).catch, error:
|
||||||
echo "[jsonrpc.callImpl] error during call: ", error.msg
|
|
||||||
raiseJsonRpcProviderError error.msg
|
raiseJsonRpcProviderError error.msg
|
||||||
echo "[jsonrpc.callImpl] response: ", response.string
|
|
||||||
without json =? JsonNode.fromJson(response.string), error:
|
without json =? JsonNode.fromJson(response.string), error:
|
||||||
echo "[jsonrpc.callImpl] error after parsing json: ", error.msg
|
|
||||||
raiseJsonRpcProviderError "Failed to parse response: " & error.msg
|
raiseJsonRpcProviderError "Failed to parse response: " & error.msg
|
||||||
json
|
json
|
||||||
|
|
||||||
@ -271,17 +267,14 @@ method unsubscribe*(
|
|||||||
convertError:
|
convertError:
|
||||||
let subscriptions = subscription.subscriptions
|
let subscriptions = subscription.subscriptions
|
||||||
let id = subscription.id
|
let id = subscription.id
|
||||||
echo "[jsonrpc.unsubscribe] subscription id: ", $id
|
|
||||||
await subscriptions.unsubscribe(id)
|
await subscriptions.unsubscribe(id)
|
||||||
|
|
||||||
method close*(
|
method close*(
|
||||||
provider: JsonRpcProvider) {.async: (raises:[ProviderError]).} =
|
provider: JsonRpcProvider) {.async: (raises:[ProviderError]).} =
|
||||||
|
|
||||||
convertError:
|
convertError:
|
||||||
echo "[JsonRpcProvider.close]"
|
|
||||||
let client = await provider.client
|
let client = await provider.client
|
||||||
let subscriptions = await provider.subscriptions
|
let subscriptions = await provider.subscriptions
|
||||||
echo "[JsonRpcProvider.closing subscriptions]"
|
|
||||||
await subscriptions.close()
|
await subscriptions.close()
|
||||||
await client.close()
|
await client.close()
|
||||||
|
|
||||||
@ -334,7 +327,6 @@ method sendTransaction*(
|
|||||||
transaction: Transaction): Future[TransactionResponse] {.async.} =
|
transaction: Transaction): Future[TransactionResponse] {.async.} =
|
||||||
|
|
||||||
convertSignerError:
|
convertSignerError:
|
||||||
echo "[jsonrpc.sendTransaction]"
|
|
||||||
if nonce =? transaction.nonce:
|
if nonce =? transaction.nonce:
|
||||||
signer.updateNonce(nonce)
|
signer.updateNonce(nonce)
|
||||||
let
|
let
|
||||||
|
|||||||
@ -40,11 +40,6 @@ template mapFailure*[T, V, E](
|
|||||||
|
|
||||||
exp.mapErr(proc (e: V): ref CatchableError = (ref exc)(msg: e.msg))
|
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
|
# Address
|
||||||
|
|
||||||
func `%`*(address: Address): JsonNode =
|
func `%`*(address: Address): JsonNode =
|
||||||
@ -57,45 +52,6 @@ func fromJson(_: type Address, json: JsonNode): ?!Address =
|
|||||||
"Failed to convert '" & $json & "' to Address: " & error.msg)
|
"Failed to convert '" & $json & "' to Address: " & error.msg)
|
||||||
success address
|
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
|
# UInt256
|
||||||
|
|
||||||
func `%`*(integer: UInt256): JsonNode =
|
func `%`*(integer: UInt256): JsonNode =
|
||||||
@ -106,25 +62,6 @@ func fromJson*(_: type UInt256, json: JsonNode): ?!UInt256 =
|
|||||||
return UInt256.failure error.msg
|
return UInt256.failure error.msg
|
||||||
success result
|
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
|
# Transaction
|
||||||
|
|
||||||
# TODO: add option that ignores none Option[T]
|
# TODO: add option that ignores none Option[T]
|
||||||
@ -146,30 +83,8 @@ func `%`*(transaction: Transaction): JsonNode =
|
|||||||
if gasLimit =? transaction.gasLimit:
|
if gasLimit =? transaction.gasLimit:
|
||||||
result["gas"] = %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
|
# BlockTag
|
||||||
|
|
||||||
# proc writeValue*(
|
|
||||||
# writer: var JsonWriter[JrpcConv],
|
|
||||||
# value: BlockTag
|
|
||||||
# ) {.raises:[IOError].} =
|
|
||||||
# writer.writeValue($value)
|
|
||||||
|
|
||||||
func `%`*(tag: BlockTag): JsonNode =
|
func `%`*(tag: BlockTag): JsonNode =
|
||||||
% $tag
|
% $tag
|
||||||
|
|
||||||
@ -202,24 +117,10 @@ proc fromJson*[E: TransactionStatus | TransactionType](
|
|||||||
let integer = ? fromHex[int](json.str).catch.mapFailure(SerializationError)
|
let integer = ? fromHex[int](json.str).catch.mapFailure(SerializationError)
|
||||||
success T(integer)
|
success T(integer)
|
||||||
|
|
||||||
# proc readValue*(r: var JsonReader[JrpcConv],
|
# Generic conversions to use nim-json instead of nim-json-serialization for
|
||||||
# result: var BlockTag) {.raises:[SerializationError, IOError].} =
|
# json rpc serialization purposes
|
||||||
# var json = r.readValue(JsonNode)
|
# writeValue => `%`
|
||||||
# result = BlockTag.fromJson(json).getOrRaise(SerializationError)
|
# readValue => fromJson
|
||||||
|
|
||||||
# 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)
|
|
||||||
|
|
||||||
proc writeValue*[T: not JsonNode](
|
proc writeValue*[T: not JsonNode](
|
||||||
writer: var JsonWriter[JrpcConv],
|
writer: var JsonWriter[JrpcConv],
|
||||||
@ -232,9 +133,4 @@ proc readValue*[T: not JsonNode](
|
|||||||
result: var T) {.raises: [SerializationError, IOError].} =
|
result: var T) {.raises: [SerializationError, IOError].} =
|
||||||
|
|
||||||
var json = r.readValue(JsonNode)
|
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)
|
result = T.fromJson(json).getOrRaise(SerializationError)
|
||||||
|
|||||||
@ -235,10 +235,8 @@ proc fromJson*[T: ref object or object](
|
|||||||
|
|
||||||
proc parse*(json: string): ?!JsonNode =
|
proc parse*(json: string): ?!JsonNode =
|
||||||
try:
|
try:
|
||||||
echo "[json.parse] json: ", json
|
|
||||||
return parseJson(json).catch
|
return parseJson(json).catch
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
echo "[json.parse] exception: ", e.msg
|
|
||||||
return err newException(CatchableError, e.msg)
|
return err newException(CatchableError, e.msg)
|
||||||
|
|
||||||
proc fromJson*[T: ref object or object](
|
proc fromJson*[T: ref object or object](
|
||||||
@ -252,14 +250,12 @@ proc fromJson*(
|
|||||||
_: type JsonNode,
|
_: type JsonNode,
|
||||||
json: string
|
json: string
|
||||||
): ?!JsonNode =
|
): ?!JsonNode =
|
||||||
echo "[JsonNode.fromJson] json: ", json
|
|
||||||
return parse(json)
|
return parse(json)
|
||||||
|
|
||||||
proc fromJson*[T: ref object or object](
|
proc fromJson*[T: ref object or object](
|
||||||
_: type T,
|
_: type T,
|
||||||
json: string
|
json: string
|
||||||
): ?!T =
|
): ?!T =
|
||||||
echo "[T.fromJson] T: ", T, ", json string: ", json
|
|
||||||
let json = ? parse(json)
|
let json = ? parse(json)
|
||||||
T.fromJson(json)
|
T.fromJson(json)
|
||||||
|
|
||||||
|
|||||||
@ -27,18 +27,19 @@ func init*(subscriptions: JsonRpcSubscriptions) =
|
|||||||
subscriptions.client.onProcessMessage =
|
subscriptions.client.onProcessMessage =
|
||||||
proc(client: RpcClient,
|
proc(client: RpcClient,
|
||||||
line: string): Result[bool, string] {.gcsafe, raises: [].} =
|
line: string): Result[bool, string] {.gcsafe, raises: [].} =
|
||||||
# if json =? JrpcConv.decode(line, JsonNode).catch:
|
|
||||||
if json =? JsonNode.fromJson(line):
|
if json =? JsonNode.fromJson(line):
|
||||||
if "method" in json:
|
if "method" in json:
|
||||||
let methodName = json{"method"}.getStr()
|
let methodName = json{"method"}.getStr()
|
||||||
if methodName in subscriptions.methodHandlers:
|
if methodName in subscriptions.methodHandlers:
|
||||||
let handler = subscriptions.methodHandlers.getOrDefault(methodName)
|
let handler = subscriptions.methodHandlers.getOrDefault(methodName)
|
||||||
if not handler.isNil:
|
if not handler.isNil:
|
||||||
echo "[subscriptions] processing method handler with params ", json{"params"}
|
|
||||||
handler(json{"params"} or newJArray())
|
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(
|
proc setMethodHandler(
|
||||||
@ -117,7 +118,6 @@ proc new*(_: type JsonRpcSubscriptions,
|
|||||||
proc subscriptionHandler(arguments: JsonNode) {.raises:[].} =
|
proc subscriptionHandler(arguments: JsonNode) {.raises:[].} =
|
||||||
if id =? arguments{"subscription"}.catch and
|
if id =? arguments{"subscription"}.catch and
|
||||||
callback =? subscriptions.getCallback(id):
|
callback =? subscriptions.getCallback(id):
|
||||||
echo "[subscription handler] calling callback for id ", id
|
|
||||||
callback(id, arguments)
|
callback(id, arguments)
|
||||||
subscriptions.setMethodHandler("eth_subscription", subscriptionHandler)
|
subscriptions.setMethodHandler("eth_subscription", subscriptionHandler)
|
||||||
subscriptions
|
subscriptions
|
||||||
@ -128,7 +128,6 @@ method subscribeBlocks(subscriptions: WebSocketSubscriptions,
|
|||||||
{.async.} =
|
{.async.} =
|
||||||
proc callback(id, arguments: JsonNode) {.raises: [].} =
|
proc callback(id, arguments: JsonNode) {.raises: [].} =
|
||||||
if blck =? Block.fromJson(arguments{"result"}):
|
if blck =? Block.fromJson(arguments{"result"}):
|
||||||
echo "[subscription.subscribeBlocks callback] calling onBlock callback with Block"
|
|
||||||
onBlock(blck)
|
onBlock(blck)
|
||||||
let id = await subscriptions.client.eth_subscribe("newHeads")
|
let id = await subscriptions.client.eth_subscribe("newHeads")
|
||||||
subscriptions.callbacks[id] = callback
|
subscriptions.callbacks[id] = callback
|
||||||
|
|||||||
@ -1,4 +1,3 @@
|
|||||||
import ../../basics
|
|
||||||
import ../../signer
|
import ../../signer
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -7,7 +6,7 @@ type
|
|||||||
func raiseWalletError*(message: string) {.raises: [WalletError].}=
|
func raiseWalletError*(message: string) {.raises: [WalletError].}=
|
||||||
raise newException(WalletError, message)
|
raise newException(WalletError, message)
|
||||||
|
|
||||||
template convertError(body) =
|
template convertError*(body) =
|
||||||
try:
|
try:
|
||||||
body
|
body
|
||||||
except CatchableError as error:
|
except CatchableError as error:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user