From 3362cc9cdfdf7016514edc25a4590173ed451ca7 Mon Sep 17 00:00:00 2001 From: oskarth Date: Thu, 25 Feb 2021 03:10:43 +0000 Subject: [PATCH] deploy: 7e6d1a4d0fa1fbb30cfa7f5aac45e521200dbfcb --- .update.timestamp | 2 +- tests/all_tests_v2.nim | 2 +- tests/v2/test_waku_swap_contracts.nim | 38 +++++--- .../protocol/waku_protocol.nim.generated.nim | 64 ++++++------- waku/v2/protocol/waku_swap/waku_swap.nim | 15 ++- .../waku_swap/waku_swap_contracts.nim | 96 ++++++++++++------- 6 files changed, 130 insertions(+), 87 deletions(-) diff --git a/.update.timestamp b/.update.timestamp index eb2072ffc..a6d9a983a 100644 --- a/.update.timestamp +++ b/.update.timestamp @@ -1 +1 @@ -1614057306 \ No newline at end of file +1614221268 \ No newline at end of file diff --git a/tests/all_tests_v2.nim b/tests/all_tests_v2.nim index 0acf3892a..a4cdaa379 100644 --- a/tests/all_tests_v2.nim +++ b/tests/all_tests_v2.nim @@ -7,7 +7,7 @@ import ./v2/test_waku_filter, ./v2/test_waku_pagination, ./v2/test_waku_payload, -# ./v2/test_waku_swap, + ./v2/test_waku_swap, ./v2/test_message_store, ./v2/test_jsonrpc_waku, ./v2/test_peer_manager, diff --git a/tests/v2/test_waku_swap_contracts.nim b/tests/v2/test_waku_swap_contracts.nim index 29a27203e..a2675da03 100644 --- a/tests/v2/test_waku_swap_contracts.nim +++ b/tests/v2/test_waku_swap_contracts.nim @@ -19,13 +19,20 @@ procSuite "Basic balance test": test "Get balance from running node": # NOTE: This corresponds to the first default account in Hardhat - let balance = waku_swap_contracts.getBalance("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266") + let balRes = waku_swap_contracts.getBalance("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266") + var balance: float + if balRes.isOk(): + let json = balRes[] + let balanceStr = json["balance"].getStr() + balance = parseFloat(balanceStr) check: - contains(balance, "ETH") + balRes.isOk() + balance > 0 test "Setup Swap": - let json = waku_swap_contracts.setupSwap() + let res = waku_swap_contracts.setupSwap() + let json = res[] var aliceAddress = json["aliceAddress"].getStr() aliceSwapAddress = json["aliceSwapAddress"].getStr() @@ -38,26 +45,31 @@ procSuite "Basic balance test": contains(aliceAddress, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266") test "Sign Cheque": - signature = waku_swap_contracts.signCheque(aliceSwapAddress) + var sigRes = waku_swap_contracts.signCheque(aliceSwapAddress) + if sigRes.isOk(): + let json = sigRes[] + signature = json["signature"].getStr() check: + sigRes.isOk() contains(signature, "0x") test "Get ERC20 Balances": - let json = getERC20Balances(erc20address) + let res = waku_swap_contracts.getERC20Balances(erc20address) check: - json["bobBalance"].getInt() == 10000 + res.isOk() + res[]["bobBalance"].getInt() == 10000 test "Redeem cheque and check balance": - let json = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature) - var resp = json["resp"].getStr() - debug "json", json + let redeemRes = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature) + var resp = redeemRes[]["resp"].getStr() + debug "Redeem resp", resp - debug "Get balances" - let json2 = getERC20Balances(erc20address) - debug "json", json2 + let balRes = getERC20Balances(erc20address) # Balance for Bob has now increased check: - json2["bobBalance"].getInt() == 10500 + redeemRes.isOk() + balRes.isOk() + balRes[]["bobBalance"].getInt() == 10500 diff --git a/waku/v1/protocol/waku_protocol.nim.generated.nim b/waku/v1/protocol/waku_protocol.nim.generated.nim index 9f7a4ce64..4559feb54 100644 --- a/waku/v1/protocol/waku_protocol.nim.generated.nim +++ b/waku/v1/protocol/waku_protocol.nim.generated.nim @@ -188,10 +188,10 @@ proc statusRawSender(peerOrResponder: Peer; options: StatusOptions; template status*(peer: Peer; options: StatusOptions; timeout: Duration = milliseconds(10000'i64)): Future[statusObj] = - let peer_184525056 = peer - let sendingFuture`gensym184525057 = statusRawSender(peer, options) - handshakeImpl(peer_184525056, sendingFuture`gensym184525057, - nextMsg(peer_184525056, statusObj), timeout) + let peer_184785056 = peer + let sendingFuture`gensym184785057 = statusRawSender(peer, options) + handshakeImpl(peer_184785056, sendingFuture`gensym184785057, + nextMsg(peer_184785056, statusObj), timeout) proc messages*(peerOrResponder: Peer; envelopes: openarray[Envelope]): Future[void] {. gcsafe.} = @@ -272,9 +272,9 @@ proc p2pSyncResponse*(peerOrResponder: ResponderWithId[p2pSyncResponseObj]): Fut let msgBytes = finish(writer) return sendMsg(peer, msgBytes) -template send*(r`gensym184525072: ResponderWithId[p2pSyncResponseObj]; - args`gensym184525073: varargs[untyped]): auto = - p2pSyncResponse(r`gensym184525072, args`gensym184525073) +template send*(r`gensym184785072: ResponderWithId[p2pSyncResponseObj]; + args`gensym184785073: varargs[untyped]): auto = + p2pSyncResponse(r`gensym184785072, args`gensym184785073) proc p2pSyncRequest*(peerOrResponder: Peer; timeout: Duration = milliseconds(10000'i64)): Future[ @@ -458,71 +458,71 @@ proc p2pRequestCompleteUserHandler(peer: Peer; requestId: Hash; discard -proc statusThunk(peer: Peer; _`gensym184525033: int; data`gensym184525034: Rlp) {. +proc statusThunk(peer: Peer; _`gensym184785033: int; data`gensym184785034: Rlp) {. async, gcsafe.} = - var rlp = data`gensym184525034 + var rlp = data`gensym184785034 var msg {.noinit.}: statusObj msg.options = checkedRlpRead(peer, rlp, StatusOptions) -proc messagesThunk(peer: Peer; _`gensym184525058: int; data`gensym184525059: Rlp) {. +proc messagesThunk(peer: Peer; _`gensym184785058: int; data`gensym184785059: Rlp) {. async, gcsafe.} = - var rlp = data`gensym184525059 + var rlp = data`gensym184785059 var msg {.noinit.}: messagesObj msg.envelopes = checkedRlpRead(peer, rlp, openarray[Envelope]) await(messagesUserHandler(peer, msg.envelopes)) -proc statusOptionsThunk(peer: Peer; _`gensym184525060: int; data`gensym184525061: Rlp) {. +proc statusOptionsThunk(peer: Peer; _`gensym184785060: int; data`gensym184785061: Rlp) {. async, gcsafe.} = - var rlp = data`gensym184525061 + var rlp = data`gensym184785061 var msg {.noinit.}: statusOptionsObj msg.options = checkedRlpRead(peer, rlp, StatusOptions) await(statusOptionsUserHandler(peer, msg.options)) -proc p2pRequestThunk(peer: Peer; _`gensym184525062: int; data`gensym184525063: Rlp) {. +proc p2pRequestThunk(peer: Peer; _`gensym184785062: int; data`gensym184785063: Rlp) {. async, gcsafe.} = - var rlp = data`gensym184525063 + var rlp = data`gensym184785063 var msg {.noinit.}: p2pRequestObj msg.envelope = checkedRlpRead(peer, rlp, Envelope) await(p2pRequestUserHandler(peer, msg.envelope)) -proc p2pMessageThunk(peer: Peer; _`gensym184525064: int; data`gensym184525065: Rlp) {. +proc p2pMessageThunk(peer: Peer; _`gensym184785064: int; data`gensym184785065: Rlp) {. async, gcsafe.} = - var rlp = data`gensym184525065 + var rlp = data`gensym184785065 var msg {.noinit.}: p2pMessageObj msg.envelopes = checkedRlpRead(peer, rlp, openarray[Envelope]) await(p2pMessageUserHandler(peer, msg.envelopes)) -proc batchAcknowledgedThunk(peer: Peer; _`gensym184525066: int; - data`gensym184525067: Rlp) {.async, gcsafe.} = - var rlp = data`gensym184525067 +proc batchAcknowledgedThunk(peer: Peer; _`gensym184785066: int; + data`gensym184785067: Rlp) {.async, gcsafe.} = + var rlp = data`gensym184785067 var msg {.noinit.}: batchAcknowledgedObj await(batchAcknowledgedUserHandler(peer)) -proc messageResponseThunk(peer: Peer; _`gensym184525068: int; - data`gensym184525069: Rlp) {.async, gcsafe.} = - var rlp = data`gensym184525069 +proc messageResponseThunk(peer: Peer; _`gensym184785068: int; + data`gensym184785069: Rlp) {.async, gcsafe.} = + var rlp = data`gensym184785069 var msg {.noinit.}: messageResponseObj await(messageResponseUserHandler(peer)) -proc p2pSyncResponseThunk(peer: Peer; _`gensym184525070: int; - data`gensym184525071: Rlp) {.async, gcsafe.} = - var rlp = data`gensym184525071 +proc p2pSyncResponseThunk(peer: Peer; _`gensym184785070: int; + data`gensym184785071: Rlp) {.async, gcsafe.} = + var rlp = data`gensym184785071 var msg {.noinit.}: p2pSyncResponseObj let reqId = read(rlp, int) await(p2pSyncResponseUserHandler(peer, reqId)) resolveResponseFuture(peer, perPeerMsgId(peer, p2pSyncResponseObj), addr(msg), reqId) -proc p2pSyncRequestThunk(peer: Peer; _`gensym184525074: int; - data`gensym184525075: Rlp) {.async, gcsafe.} = - var rlp = data`gensym184525075 +proc p2pSyncRequestThunk(peer: Peer; _`gensym184785074: int; + data`gensym184785075: Rlp) {.async, gcsafe.} = + var rlp = data`gensym184785075 var msg {.noinit.}: p2pSyncRequestObj let reqId = read(rlp, int) await(p2pSyncRequestUserHandler(peer, reqId)) -proc p2pRequestCompleteThunk(peer: Peer; _`gensym184525076: int; - data`gensym184525077: Rlp) {.async, gcsafe.} = - var rlp = data`gensym184525077 +proc p2pRequestCompleteThunk(peer: Peer; _`gensym184785076: int; + data`gensym184785077: Rlp) {.async, gcsafe.} = + var rlp = data`gensym184785077 var msg {.noinit.}: p2pRequestCompleteObj tryEnterList(rlp) msg.requestId = checkedRlpRead(peer, rlp, Hash) diff --git a/waku/v2/protocol/waku_swap/waku_swap.nim b/waku/v2/protocol/waku_swap/waku_swap.nim index 83854adb5..b4385ae50 100644 --- a/waku/v2/protocol/waku_swap/waku_swap.nim +++ b/waku/v2/protocol/waku_swap/waku_swap.nim @@ -22,7 +22,7 @@ ## import - std/[tables, options], + std/[tables, options, json], bearssl, chronos, chronicles, metrics, stew/results, libp2p/crypto/crypto, @@ -118,9 +118,18 @@ proc sendCheque*(ws: WakuSwap) {.async.} = # TODO We get this from the setup of swap setup, dynamic, should be part of setup # TODO Add beneficiary, etc var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1" - let signature = waku_swap_contracts.signCheque(aliceSwapAddress) - info "Signed Cheque", swapAddress = aliceSwapAddress, signature = signature + var signature: string + var res = waku_swap_contracts.signCheque(aliceSwapAddress) + if res.isOk(): + echo "signCheque ", res[] + let json = res[] + signature = json["signature"].getStr() + else: + # To test code paths, this should look different in a production setting + warn "Something went wrong when signing cheque, sending anyway" + + info "Signed Cheque", swapAddress = aliceSwapAddress, signature = signature let sigBytes = cast[seq[byte]](signature) await connOpt.get().writeLP(Cheque(amount: 1, signature: sigBytes).encode().buffer) diff --git a/waku/v2/protocol/waku_swap/waku_swap_contracts.nim b/waku/v2/protocol/waku_swap/waku_swap_contracts.nim index 87c3b101c..ba818a554 100644 --- a/waku/v2/protocol/waku_swap/waku_swap_contracts.nim +++ b/waku/v2/protocol/waku_swap/waku_swap_contracts.nim @@ -2,64 +2,86 @@ # # Assumes swap-contracts-module node is running. # + +{.push raises: [Defect].} + import std/[osproc, strutils, json], - chronicles + chronicles, stew/results logScope: topics = "wakuswapcontracts" -# XXX In general this is not a good API, more a collection of hacky glue code for PoC. -# -# TODO Error handling +# TODO Richer error types than string, overkill for now... +type NodeTaskJsonResult = Result[JsonNode, string] + +# XXX In general this is not a great API, more a collection of hacky glue code for PoC. # Interacts with node in sibling path and interacts with a local Hardhat node. const taskPrelude = "npx hardhat --network localhost " const cmdPrelude = "cd ../swap-contracts-module; " & taskPrelude -proc execNodeTask(taskStr: string): tuple[output: TaintedString, exitCode: int] = +# proc execNodeTask(taskStr: string): tuple[output: TaintedString, exitCode: int] = +# let cmdString = $cmdPrelude & $taskStr +# debug "execNodeTask", cmdString +# return osproc.execCmdEx(cmdString) + +proc execNodeTaskJson(taskStr: string): NodeTaskJsonResult = let cmdString = $cmdPrelude & $taskStr debug "execNodeTask", cmdString - return osproc.execCmdEx(cmdString) -# TODO JSON? -proc getBalance*(accountAddress: string): string = + try: + let (output, errC) = osproc.execCmdEx(cmdString) + if errC>0: + error "Error executing node task", output + return err(output) + + debug "Command executed", output + + try: + let json = parseJson(output) + return ok(json) + except JsonParsingError: + return err("Unable to parse JSON:" & $output) + except Exception: + return err("Unable to parse JSON:" & $output) + + except OSError: + return err("Unable to execute command, OSError:" & $taskStr) + except Exception: + return err("Unable to execute command:" & $taskStr) + +proc getBalance*(accountAddress: string): NodeTaskJsonResult = let task = "balance --account " & $accountAddress - let (output, errC) = execNodeTask(task) - debug "getBalance", output - return output + let res = execNodeTaskJson(task) + return res -proc setupSwap*(): JsonNode = +proc setupSwap*(): NodeTaskJsonResult = let task = "setupSwap" - let (output, errC) = execNodeTask(task) - - # XXX Assume succeeds - let json = parseJson(output) - return json + let res = execNodeTaskJson(task) + return res -# TODO Signature -proc signCheque*(swapAddress: string): string = +proc signCheque*(swapAddress: string): NodeTaskJsonResult = let task = "signCheque --swapaddress '" & $swapAddress & "'" - let (output, errC) = execNodeTask(task) + var res = execNodeTaskJson(task) + return res - # XXX Assume succeeds - let json = parseJson(output) - let signature = json["signature"].getStr() - debug "signCheque", json=json, signature=signature - return signature - -proc getERC20Balances*(erc20address: string): JsonNode = +proc getERC20Balances*(erc20address: string): NodeTaskJsonResult = let task = "getBalances --erc20address '" & $erc20address & "'" - let (output, errC) = execNodeTask(task) + let res = execNodeTaskJson(task) + debug "getERC20Balances", res + return res - # XXX Assume succeeds - let json = parseJson(output) - return json - -proc redeemCheque*(swapAddress: string, signature: string): JsonNode = +proc redeemCheque*(swapAddress: string, signature: string): NodeTaskJsonResult = let task = "redeemCheque --swapaddress '" & $swapAddress & "' --signature '" & $signature & "'" - let (output, errC) = execNodeTask(task) + let res = execNodeTaskJson(task) + return res - # XXX Assume succeeds - let json = parseJson(output) - return json +when isMainModule: + var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1" + var sigRes = signCheque(aliceSwapAddress) + if sigRes.isOk(): + echo "All good" + echo "Signature ", sigRes[] + else: + echo sigRes