From 0ca608996289a2b2a4ea9bba715bb9f3e99de869 Mon Sep 17 00:00:00 2001 From: Zahary Karadjov Date: Wed, 11 Mar 2020 11:03:09 +0200 Subject: [PATCH] Misc fixes * Allow BlockHash et al to be used as keys in hash tables * Switch from Exceptions to CatchableErrors * Export some helpers that needed in NBC --- web3.nim | 20 +++++++++++++++----- web3/ethtypes.nim | 5 ++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/web3.nim b/web3.nim index 01d201f..34e235c 100644 --- a/web3.nim +++ b/web3.nim @@ -50,7 +50,7 @@ proc handleSubscriptionNotification(w: Web3, j: JsonNode) = if s.historicalEventsProcessed: try: s.callback(j{"result"}) - except Exception as e: + except CatchableError as e: echo "Caught exception in handleSubscriptionNotification: ", e.msg echo e.getStackTrace() else: @@ -99,7 +99,7 @@ proc getHistoricalEvents(s: Subscription, options: JsonNode) {.async.} = s.callback(s.pendingEvents[i]) inc i s.pendingEvents = @[] - except Exception as e: + except CatchableError as e: echo "Caught exception in getHistoricalEvents: ", e.msg echo e.getStackTrace() @@ -154,11 +154,11 @@ func encode*[N](b: FixedBytes[N]): EncodeResult = fixedEncode(array[N, byte](b)) func encode*(b: Address): EncodeResult = fixedEncode(array[20, byte](b)) -proc skip0xPrefix(s: string): int = +proc skip0xPrefix*(s: string): int = if s.len > 1 and s[0] == '0' and s[1] in {'x', 'X'}: 2 else: 0 -proc strip0xPrefix(s: string): string = +proc strip0xPrefix*(s: string): string = let prefixLen = skip0xPrefix(s) if prefixLen != 0: s[prefixLen .. ^1] @@ -183,6 +183,9 @@ func fromHex*[N](x: type FixedBytes[N], s: string): FixedBytes[N] {.inline.} = func fromHex*(x: type Address, s: string): Address {.inline.} = fromHexAux(s, array[20, byte](result)) +template toHex*[N](x: FixedBytes[N]): string = + toHex(array[N, byte](x)) + func decodeFixed(input: string, offset: int, to: var openarray[byte]): int = let meaningfulLen = to.len * 2 var padding = to.len mod 32 @@ -326,6 +329,11 @@ func encode*(x: Bool): EncodeResult = encode(Int256(x)) func decode*[N](input: string, offset: int, to: var Bool): int {.inline.} = decode(input, offset, Stint(to)) +func decode*(input: string, offset: int, obj: var object): int = + var offset = offset + for field in fields(obj): + offset += decode(input, offset, field) + type Encodable = concept x encode(x) is EncodeResult @@ -371,7 +379,6 @@ func decode*[T; I: static int](input: string, to: array[0..I, T]): array[0..I, T for i in 0..I: result[i] = input[i*64 .. (i+1)*64].decode(T) - type InterfaceObjectKind = enum function, constructor, event @@ -681,6 +688,9 @@ macro contract*(cname: untyped, body: untyped): untyped = else: discard + when defined(debugMacros) or defined(debugWeb3Macros): + echo result.repr + proc getJsonLogs*(s: Sender, EventName: type, fromBlock, toBlock = none(RtBlockIdentifier), diff --git a/web3/ethtypes.nim b/web3/ethtypes.nim index c35de62..54ce625 100644 --- a/web3/ethtypes.nim +++ b/web3/ethtypes.nim @@ -1,4 +1,4 @@ -import options, json +import options, json, hashes import stint, stew/byteutils type @@ -190,3 +190,6 @@ func blockId*(n: BlockNumber): RtBlockIdentifier = func blockId*(a: string): RtBlockIdentifier = RtBlockIdentifier(kind: BlockIdentifierKind.alias, alias: a) +func hash*[N](bytes: FixedBytes[N]): Hash = + hash(array[N, byte](bytes)) +