Skip to content

Commit ea641f0

Browse files
committed
getBalance
1 parent 0bada69 commit ea641f0

File tree

4 files changed

+40
-8
lines changed

4 files changed

+40
-8
lines changed

nimbus_verified_proxy/libverifproxy/example.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
static bool waitOver = true;
1616

1717
void onBlockNumber(Context *ctx, int status, char *res) {
18-
printf("response: %s\n", res);
18+
printf("Blocknumber: %s\n", res);
1919
freeResponse(res);
2020
}
2121

@@ -27,11 +27,17 @@ void onStart(Context *ctx, int status, char *res) {
2727
freeResponse(res);
2828
}
2929

30+
void onBalance(Context *ctx, int status, char *res) {
31+
printf("Balance: %s\n", res);
32+
freeResponse(res);
33+
}
34+
3035
void waitIsOver(Context *ctx, int status, char *res) {
3136
printf("waiting finished successfully\n");
3237
printf("status: %d\n", status);
3338

3439
eth_blockNumber(ctx, onBlockNumber);
40+
eth_getBalance(ctx, "0x954a86C613fd1fBaC9C7A43a071A68254C75E4AC", "latest", onBalance);
3541
waitOver = true;
3642

3743
freeResponse(res);
@@ -44,8 +50,8 @@ int main() {
4450
char* jsonConfig =
4551
"{"
4652
"\"eth2Network\": \"mainnet\","
47-
"\"trustedBlockRoot\": \"0xdd8db7bfd8c96c993a4cb78e0e6607cf1dcca3f379764388248c63d2bc40443b\","
48-
"\"backendUrl\": \"https://eth.llamarpc.com\","
53+
"\"trustedBlockRoot\": \"0xd9e4f5b2e7a8e50f9348a1890114ae522d3771ddfb44d8b7e7e2978c21869e91\","
54+
"\"backendUrl\": \"https://eth.blockrazor.xyz\","
4955
"\"beaconApiUrls\": \"http://testing.mainnet.beacon-api.nimbus.team,http://www.lightclientdata.org\","
5056
"\"logLevel\": \"FATAL\","
5157
"\"logStdout\": \"None\""

nimbus_verified_proxy/libverifproxy/verifproxy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ETH_RESULT_USE_CHECK Context *createAsyncTaskContext();
3636

3737
typedef void (*CallBackProc) (Context *ctx, int status, char *res);
3838

39+
void eth_getBalance(Context *ctx, char *address, char *blockTag, CallBackProc onBalance);
3940
void eth_blockNumber(Context *ctx, CallBackProc cb);
4041
void freeResponse(char *res);
4142
void freeContext(Context *ctx);

nimbus_verified_proxy/libverifproxy/verifproxy.nim

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
112137
proc 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

proxy_from_c

28.2 MB
Binary file not shown.

0 commit comments

Comments
 (0)