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
This commit is contained in:
Zahary Karadjov 2020-03-11 11:03:09 +02:00
parent d172765114
commit 0ca6089962
No known key found for this signature in database
GPG Key ID: C8936F8A3073D609
2 changed files with 19 additions and 6 deletions

View File

@ -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),

View File

@ -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))