diff --git a/web3.nim b/web3.nim index 377b13d..b1cb022 100644 --- a/web3.nim +++ b/web3.nim @@ -87,28 +87,18 @@ func getValue(params: RequestParamsRx, field: string, FieldType: type): except CatchableError as exc: return err(exc.msg) -func toJsonString(params: RequestParamsRx): - Result[JsonString, string] {.gcsafe, raises: [].} = - try: - let res = JrpcSys.encode(params.toTx) - return ok(res.JsonString) - except CatchableError as exc: - return err(exc.msg) - proc handleSubscriptionNotification(w: Web3, params: RequestParamsRx): Result[void, string] {.gcsafe, raises: [].} = let subs = params.getValue("subscription", string).valueOr: return err(error) let s = w.subscriptions.getOrDefault(subs) if not s.isNil and not s.removed: + let res = params.getValue("result", JsonString).valueOr: + return err(error) if s.historicalEventsProcessed: - let res = params.getValue("result", JsonString).valueOr: - return err(error) s.eventHandler(res) else: - let par = params.toJsonString().valueOr: - return err(error) - s.pendingEvents.add(par) + s.pendingEvents.add(res) ok() diff --git a/web3/contract_dsl.nim b/web3/contract_dsl.nim index 4a1db4f..b39115b 100644 --- a/web3/contract_dsl.nim +++ b/web3/contract_dsl.nim @@ -2,6 +2,7 @@ import std/[macros, strutils], json_serialization, ./[encoding, eth_api_types], + ./conversions, stint, stew/byteutils @@ -40,6 +41,12 @@ type of constructor: constructorObject: ConstructorObject of event: eventObject: EventObject + EventData* = object + data*: seq[byte] + topics*: seq[Bytes32] + +EventData.useDefaultSerializationIn JrpcConv + proc joinStrings(s: varargs[string]): string = join(s) proc unknownType() = discard # Used for informative errors @@ -233,7 +240,7 @@ proc genEvent(cname: NimNode, eventObject: EventObject): NimNode = if not eventObject.anonymous: let callbackIdent = ident "callback" let jsonIdent = ident "j" - let jsonData = ident "jsonData" + let eventData = ident "eventData" var params = nnkFormalParams.newTree(newEmptyNode()) paramsWithRawData = nnkFormalParams.newTree(newEmptyNode()) @@ -243,10 +250,9 @@ proc genEvent(cname: NimNode, eventObject: EventObject): NimNode = call = nnkCall.newTree(callbackIdent) callWithRawData = nnkCall.newTree(callbackIdent) offset = ident "offset" - inputData = ident "inputData" argParseBody.add quote do: - let `jsonData` = JrpcConv.decode(`jsonIdent`.string, JsonNode) + let `eventData` = JrpcConv.decode(`jsonIdent`.string, EventData) var offsetInited = false @@ -264,19 +270,18 @@ proc genEvent(cname: NimNode, eventObject: EventObject): NimNode = if input.indexed: argParseBody.add quote do: var `argument`: `kind` - discard decode(hexToSeqByte(`jsonData`["topics"][`i`].getStr), 0, 0, `argument`) + discard decode(`eventData`.topics[`i`].data, 0, 0, `argument`) i += 1 else: if not offsetInited: argParseBody.add quote do: - var `inputData` = hexToSeqByte(`jsonData`["data"].getStr) var `offset` = 0 offsetInited = true argParseBody.add quote do: var `argument`: `kind` - `offset` += decode(`inputData`, 0, `offset`, `argument`) + `offset` += decode(`eventData`.data, 0, `offset`, `argument`) call.add argument callWithRawData.add argument let