@@ -10,9 +10,10 @@ import
1010 json_serialization,
1111 chronos,
1212 eth/ net/ nat,
13- std/ [atomics, locks, json, net],
13+ std/ [atomics, locks, json, net, strutils ],
1414 beacon_chain/ spec/ [digest, network],
1515 beacon_chain/ nimbus_binary_common,
16+ web3/ [eth_api_types, conversions],
1617 ../ engine/ types,
1718 ../ engine/ engine,
1819 ../ lc/ lc,
@@ -88,12 +89,12 @@ proc alloc(str: string): cstring =
8889 ret[str.len] = '\0 '
8990 return ret
9091
91- proc eth_blockNumber (ctx: ptr Context , cb: CallBackProc ) {.exported .} =
92+ # NOTE: this is not the C callback. This is just a callback for the future
93+ template callbackToC (ctx: ptr Context , cb: CallBackProc , asyncCall: untyped ) =
9294 let task = createTask (cb)
93-
9495 ctx.tasks.add (task)
9596
96- let fut = ctx.frontend. eth_blockNumber ()
97+ let fut = asyncCall
9798
9899 fut.addCallback proc (_: pointer ) {.gcsafe .} =
99100 if fut.cancelled ():
@@ -109,6 +110,30 @@ proc eth_blockNumber(ctx: ptr Context, cb: CallBackProc) {.exported.} =
109110 task.status = 0
110111 task.finished = true
111112
113+ proc eth_blockNumber (ctx: ptr Context , cb: CallBackProc ) {.exported .} =
114+ callbackToC (ctx, cb):
115+ ctx.frontend.eth_blockNumber ()
116+
117+ proc eth_getBalance (
118+ ctx: ptr Context , address: cstring , blockTag: cstring , cb: CallBackProc
119+ ) {.exported .} =
120+ let
121+ addressTyped =
122+ try :
123+ Address .fromHex ($ address)
124+ except ValueError as e:
125+ cb (ctx, - 3 , alloc (e.msg))
126+ return
127+
128+ blockTagTyped =
129+ try :
130+ BlockTag (kind: bidNumber, number: Quantity (parseBiggestUInt ($ blockTag)))
131+ except ValueError :
132+ BlockTag (kind: bidAlias, alias: $ blockTag)
133+
134+ callbackToC (ctx, cb):
135+ ctx.frontend.eth_getBalance (addressTyped, blockTagTyped)
136+
112137proc pollAsyncTaskEngine (ctx: ptr Context ) {.exported .} =
113138 var delList: seq [int ] = @ []
114139
@@ -254,7 +279,7 @@ proc startVerifProxy(
254279 task.finished = true
255280 task.status = - 1
256281 else :
257- task.response = " success"
282+ task.response = " success" # result is void hence we just provide a string
258283 task.status = 0
259284 task.finished = true
260285
0 commit comments