mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-05 23:43:07 +00:00
deploy: 7e6d1a4d0fa1fbb30cfa7f5aac45e521200dbfcb
This commit is contained in:
parent
b4f3eb6553
commit
3362cc9cdf
@ -1 +1 @@
|
|||||||
1614057306
|
1614221268
|
||||||
@ -7,7 +7,7 @@ import
|
|||||||
./v2/test_waku_filter,
|
./v2/test_waku_filter,
|
||||||
./v2/test_waku_pagination,
|
./v2/test_waku_pagination,
|
||||||
./v2/test_waku_payload,
|
./v2/test_waku_payload,
|
||||||
# ./v2/test_waku_swap,
|
./v2/test_waku_swap,
|
||||||
./v2/test_message_store,
|
./v2/test_message_store,
|
||||||
./v2/test_jsonrpc_waku,
|
./v2/test_jsonrpc_waku,
|
||||||
./v2/test_peer_manager,
|
./v2/test_peer_manager,
|
||||||
|
|||||||
@ -19,13 +19,20 @@ procSuite "Basic balance test":
|
|||||||
|
|
||||||
test "Get balance from running node":
|
test "Get balance from running node":
|
||||||
# NOTE: This corresponds to the first default account in Hardhat
|
# 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:
|
check:
|
||||||
contains(balance, "ETH")
|
balRes.isOk()
|
||||||
|
balance > 0
|
||||||
|
|
||||||
test "Setup Swap":
|
test "Setup Swap":
|
||||||
let json = waku_swap_contracts.setupSwap()
|
let res = waku_swap_contracts.setupSwap()
|
||||||
|
let json = res[]
|
||||||
|
|
||||||
var aliceAddress = json["aliceAddress"].getStr()
|
var aliceAddress = json["aliceAddress"].getStr()
|
||||||
aliceSwapAddress = json["aliceSwapAddress"].getStr()
|
aliceSwapAddress = json["aliceSwapAddress"].getStr()
|
||||||
@ -38,26 +45,31 @@ procSuite "Basic balance test":
|
|||||||
contains(aliceAddress, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
contains(aliceAddress, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
||||||
|
|
||||||
test "Sign Cheque":
|
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:
|
check:
|
||||||
|
sigRes.isOk()
|
||||||
contains(signature, "0x")
|
contains(signature, "0x")
|
||||||
|
|
||||||
test "Get ERC20 Balances":
|
test "Get ERC20 Balances":
|
||||||
let json = getERC20Balances(erc20address)
|
let res = waku_swap_contracts.getERC20Balances(erc20address)
|
||||||
|
|
||||||
check:
|
check:
|
||||||
json["bobBalance"].getInt() == 10000
|
res.isOk()
|
||||||
|
res[]["bobBalance"].getInt() == 10000
|
||||||
|
|
||||||
test "Redeem cheque and check balance":
|
test "Redeem cheque and check balance":
|
||||||
let json = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature)
|
let redeemRes = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature)
|
||||||
var resp = json["resp"].getStr()
|
var resp = redeemRes[]["resp"].getStr()
|
||||||
debug "json", json
|
debug "Redeem resp", resp
|
||||||
|
|
||||||
debug "Get balances"
|
let balRes = getERC20Balances(erc20address)
|
||||||
let json2 = getERC20Balances(erc20address)
|
|
||||||
debug "json", json2
|
|
||||||
|
|
||||||
# Balance for Bob has now increased
|
# Balance for Bob has now increased
|
||||||
check:
|
check:
|
||||||
json2["bobBalance"].getInt() == 10500
|
redeemRes.isOk()
|
||||||
|
balRes.isOk()
|
||||||
|
balRes[]["bobBalance"].getInt() == 10500
|
||||||
|
|||||||
@ -188,10 +188,10 @@ proc statusRawSender(peerOrResponder: Peer; options: StatusOptions;
|
|||||||
|
|
||||||
template status*(peer: Peer; options: StatusOptions;
|
template status*(peer: Peer; options: StatusOptions;
|
||||||
timeout: Duration = milliseconds(10000'i64)): Future[statusObj] =
|
timeout: Duration = milliseconds(10000'i64)): Future[statusObj] =
|
||||||
let peer_184525056 = peer
|
let peer_184785056 = peer
|
||||||
let sendingFuture`gensym184525057 = statusRawSender(peer, options)
|
let sendingFuture`gensym184785057 = statusRawSender(peer, options)
|
||||||
handshakeImpl(peer_184525056, sendingFuture`gensym184525057,
|
handshakeImpl(peer_184785056, sendingFuture`gensym184785057,
|
||||||
nextMsg(peer_184525056, statusObj), timeout)
|
nextMsg(peer_184785056, statusObj), timeout)
|
||||||
|
|
||||||
proc messages*(peerOrResponder: Peer; envelopes: openarray[Envelope]): Future[void] {.
|
proc messages*(peerOrResponder: Peer; envelopes: openarray[Envelope]): Future[void] {.
|
||||||
gcsafe.} =
|
gcsafe.} =
|
||||||
@ -272,9 +272,9 @@ proc p2pSyncResponse*(peerOrResponder: ResponderWithId[p2pSyncResponseObj]): Fut
|
|||||||
let msgBytes = finish(writer)
|
let msgBytes = finish(writer)
|
||||||
return sendMsg(peer, msgBytes)
|
return sendMsg(peer, msgBytes)
|
||||||
|
|
||||||
template send*(r`gensym184525072: ResponderWithId[p2pSyncResponseObj];
|
template send*(r`gensym184785072: ResponderWithId[p2pSyncResponseObj];
|
||||||
args`gensym184525073: varargs[untyped]): auto =
|
args`gensym184785073: varargs[untyped]): auto =
|
||||||
p2pSyncResponse(r`gensym184525072, args`gensym184525073)
|
p2pSyncResponse(r`gensym184785072, args`gensym184785073)
|
||||||
|
|
||||||
proc p2pSyncRequest*(peerOrResponder: Peer;
|
proc p2pSyncRequest*(peerOrResponder: Peer;
|
||||||
timeout: Duration = milliseconds(10000'i64)): Future[
|
timeout: Duration = milliseconds(10000'i64)): Future[
|
||||||
@ -458,71 +458,71 @@ proc p2pRequestCompleteUserHandler(peer: Peer; requestId: Hash;
|
|||||||
|
|
||||||
discard
|
discard
|
||||||
|
|
||||||
proc statusThunk(peer: Peer; _`gensym184525033: int; data`gensym184525034: Rlp) {.
|
proc statusThunk(peer: Peer; _`gensym184785033: int; data`gensym184785034: Rlp) {.
|
||||||
async, gcsafe.} =
|
async, gcsafe.} =
|
||||||
var rlp = data`gensym184525034
|
var rlp = data`gensym184785034
|
||||||
var msg {.noinit.}: statusObj
|
var msg {.noinit.}: statusObj
|
||||||
msg.options = checkedRlpRead(peer, rlp, StatusOptions)
|
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.} =
|
async, gcsafe.} =
|
||||||
var rlp = data`gensym184525059
|
var rlp = data`gensym184785059
|
||||||
var msg {.noinit.}: messagesObj
|
var msg {.noinit.}: messagesObj
|
||||||
msg.envelopes = checkedRlpRead(peer, rlp, openarray[Envelope])
|
msg.envelopes = checkedRlpRead(peer, rlp, openarray[Envelope])
|
||||||
await(messagesUserHandler(peer, msg.envelopes))
|
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.} =
|
async, gcsafe.} =
|
||||||
var rlp = data`gensym184525061
|
var rlp = data`gensym184785061
|
||||||
var msg {.noinit.}: statusOptionsObj
|
var msg {.noinit.}: statusOptionsObj
|
||||||
msg.options = checkedRlpRead(peer, rlp, StatusOptions)
|
msg.options = checkedRlpRead(peer, rlp, StatusOptions)
|
||||||
await(statusOptionsUserHandler(peer, msg.options))
|
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.} =
|
async, gcsafe.} =
|
||||||
var rlp = data`gensym184525063
|
var rlp = data`gensym184785063
|
||||||
var msg {.noinit.}: p2pRequestObj
|
var msg {.noinit.}: p2pRequestObj
|
||||||
msg.envelope = checkedRlpRead(peer, rlp, Envelope)
|
msg.envelope = checkedRlpRead(peer, rlp, Envelope)
|
||||||
await(p2pRequestUserHandler(peer, msg.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.} =
|
async, gcsafe.} =
|
||||||
var rlp = data`gensym184525065
|
var rlp = data`gensym184785065
|
||||||
var msg {.noinit.}: p2pMessageObj
|
var msg {.noinit.}: p2pMessageObj
|
||||||
msg.envelopes = checkedRlpRead(peer, rlp, openarray[Envelope])
|
msg.envelopes = checkedRlpRead(peer, rlp, openarray[Envelope])
|
||||||
await(p2pMessageUserHandler(peer, msg.envelopes))
|
await(p2pMessageUserHandler(peer, msg.envelopes))
|
||||||
|
|
||||||
proc batchAcknowledgedThunk(peer: Peer; _`gensym184525066: int;
|
proc batchAcknowledgedThunk(peer: Peer; _`gensym184785066: int;
|
||||||
data`gensym184525067: Rlp) {.async, gcsafe.} =
|
data`gensym184785067: Rlp) {.async, gcsafe.} =
|
||||||
var rlp = data`gensym184525067
|
var rlp = data`gensym184785067
|
||||||
var msg {.noinit.}: batchAcknowledgedObj
|
var msg {.noinit.}: batchAcknowledgedObj
|
||||||
await(batchAcknowledgedUserHandler(peer))
|
await(batchAcknowledgedUserHandler(peer))
|
||||||
|
|
||||||
proc messageResponseThunk(peer: Peer; _`gensym184525068: int;
|
proc messageResponseThunk(peer: Peer; _`gensym184785068: int;
|
||||||
data`gensym184525069: Rlp) {.async, gcsafe.} =
|
data`gensym184785069: Rlp) {.async, gcsafe.} =
|
||||||
var rlp = data`gensym184525069
|
var rlp = data`gensym184785069
|
||||||
var msg {.noinit.}: messageResponseObj
|
var msg {.noinit.}: messageResponseObj
|
||||||
await(messageResponseUserHandler(peer))
|
await(messageResponseUserHandler(peer))
|
||||||
|
|
||||||
proc p2pSyncResponseThunk(peer: Peer; _`gensym184525070: int;
|
proc p2pSyncResponseThunk(peer: Peer; _`gensym184785070: int;
|
||||||
data`gensym184525071: Rlp) {.async, gcsafe.} =
|
data`gensym184785071: Rlp) {.async, gcsafe.} =
|
||||||
var rlp = data`gensym184525071
|
var rlp = data`gensym184785071
|
||||||
var msg {.noinit.}: p2pSyncResponseObj
|
var msg {.noinit.}: p2pSyncResponseObj
|
||||||
let reqId = read(rlp, int)
|
let reqId = read(rlp, int)
|
||||||
await(p2pSyncResponseUserHandler(peer, reqId))
|
await(p2pSyncResponseUserHandler(peer, reqId))
|
||||||
resolveResponseFuture(peer, perPeerMsgId(peer, p2pSyncResponseObj), addr(msg),
|
resolveResponseFuture(peer, perPeerMsgId(peer, p2pSyncResponseObj), addr(msg),
|
||||||
reqId)
|
reqId)
|
||||||
|
|
||||||
proc p2pSyncRequestThunk(peer: Peer; _`gensym184525074: int;
|
proc p2pSyncRequestThunk(peer: Peer; _`gensym184785074: int;
|
||||||
data`gensym184525075: Rlp) {.async, gcsafe.} =
|
data`gensym184785075: Rlp) {.async, gcsafe.} =
|
||||||
var rlp = data`gensym184525075
|
var rlp = data`gensym184785075
|
||||||
var msg {.noinit.}: p2pSyncRequestObj
|
var msg {.noinit.}: p2pSyncRequestObj
|
||||||
let reqId = read(rlp, int)
|
let reqId = read(rlp, int)
|
||||||
await(p2pSyncRequestUserHandler(peer, reqId))
|
await(p2pSyncRequestUserHandler(peer, reqId))
|
||||||
|
|
||||||
proc p2pRequestCompleteThunk(peer: Peer; _`gensym184525076: int;
|
proc p2pRequestCompleteThunk(peer: Peer; _`gensym184785076: int;
|
||||||
data`gensym184525077: Rlp) {.async, gcsafe.} =
|
data`gensym184785077: Rlp) {.async, gcsafe.} =
|
||||||
var rlp = data`gensym184525077
|
var rlp = data`gensym184785077
|
||||||
var msg {.noinit.}: p2pRequestCompleteObj
|
var msg {.noinit.}: p2pRequestCompleteObj
|
||||||
tryEnterList(rlp)
|
tryEnterList(rlp)
|
||||||
msg.requestId = checkedRlpRead(peer, rlp, Hash)
|
msg.requestId = checkedRlpRead(peer, rlp, Hash)
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
##
|
##
|
||||||
|
|
||||||
import
|
import
|
||||||
std/[tables, options],
|
std/[tables, options, json],
|
||||||
bearssl,
|
bearssl,
|
||||||
chronos, chronicles, metrics, stew/results,
|
chronos, chronicles, metrics, stew/results,
|
||||||
libp2p/crypto/crypto,
|
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 We get this from the setup of swap setup, dynamic, should be part of setup
|
||||||
# TODO Add beneficiary, etc
|
# TODO Add beneficiary, etc
|
||||||
var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1"
|
var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1"
|
||||||
let signature = waku_swap_contracts.signCheque(aliceSwapAddress)
|
var signature: string
|
||||||
info "Signed Cheque", swapAddress = aliceSwapAddress, signature = signature
|
|
||||||
|
|
||||||
|
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)
|
let sigBytes = cast[seq[byte]](signature)
|
||||||
await connOpt.get().writeLP(Cheque(amount: 1, signature: sigBytes).encode().buffer)
|
await connOpt.get().writeLP(Cheque(amount: 1, signature: sigBytes).encode().buffer)
|
||||||
|
|
||||||
|
|||||||
@ -2,64 +2,86 @@
|
|||||||
#
|
#
|
||||||
# Assumes swap-contracts-module node is running.
|
# Assumes swap-contracts-module node is running.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
{.push raises: [Defect].}
|
||||||
|
|
||||||
import
|
import
|
||||||
std/[osproc, strutils, json],
|
std/[osproc, strutils, json],
|
||||||
chronicles
|
chronicles, stew/results
|
||||||
|
|
||||||
logScope:
|
logScope:
|
||||||
topics = "wakuswapcontracts"
|
topics = "wakuswapcontracts"
|
||||||
|
|
||||||
# XXX In general this is not a good API, more a collection of hacky glue code for PoC.
|
# TODO Richer error types than string, overkill for now...
|
||||||
#
|
type NodeTaskJsonResult = Result[JsonNode, string]
|
||||||
# TODO Error handling
|
|
||||||
|
# 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.
|
# Interacts with node in sibling path and interacts with a local Hardhat node.
|
||||||
const taskPrelude = "npx hardhat --network localhost "
|
const taskPrelude = "npx hardhat --network localhost "
|
||||||
const cmdPrelude = "cd ../swap-contracts-module; " & taskPrelude
|
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
|
let cmdString = $cmdPrelude & $taskStr
|
||||||
debug "execNodeTask", cmdString
|
debug "execNodeTask", cmdString
|
||||||
return osproc.execCmdEx(cmdString)
|
|
||||||
|
|
||||||
# TODO JSON?
|
try:
|
||||||
proc getBalance*(accountAddress: string): string =
|
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 task = "balance --account " & $accountAddress
|
||||||
let (output, errC) = execNodeTask(task)
|
let res = execNodeTaskJson(task)
|
||||||
debug "getBalance", output
|
return res
|
||||||
return output
|
|
||||||
|
|
||||||
proc setupSwap*(): JsonNode =
|
proc setupSwap*(): NodeTaskJsonResult =
|
||||||
let task = "setupSwap"
|
let task = "setupSwap"
|
||||||
let (output, errC) = execNodeTask(task)
|
let res = execNodeTaskJson(task)
|
||||||
|
return res
|
||||||
# XXX Assume succeeds
|
|
||||||
let json = parseJson(output)
|
|
||||||
return json
|
|
||||||
|
|
||||||
# TODO Signature
|
proc signCheque*(swapAddress: string): NodeTaskJsonResult =
|
||||||
proc signCheque*(swapAddress: string): string =
|
|
||||||
let task = "signCheque --swapaddress '" & $swapAddress & "'"
|
let task = "signCheque --swapaddress '" & $swapAddress & "'"
|
||||||
let (output, errC) = execNodeTask(task)
|
var res = execNodeTaskJson(task)
|
||||||
|
return res
|
||||||
|
|
||||||
# XXX Assume succeeds
|
proc getERC20Balances*(erc20address: string): NodeTaskJsonResult =
|
||||||
let json = parseJson(output)
|
|
||||||
let signature = json["signature"].getStr()
|
|
||||||
debug "signCheque", json=json, signature=signature
|
|
||||||
return signature
|
|
||||||
|
|
||||||
proc getERC20Balances*(erc20address: string): JsonNode =
|
|
||||||
let task = "getBalances --erc20address '" & $erc20address & "'"
|
let task = "getBalances --erc20address '" & $erc20address & "'"
|
||||||
let (output, errC) = execNodeTask(task)
|
let res = execNodeTaskJson(task)
|
||||||
|
debug "getERC20Balances", res
|
||||||
|
return res
|
||||||
|
|
||||||
# XXX Assume succeeds
|
proc redeemCheque*(swapAddress: string, signature: string): NodeTaskJsonResult =
|
||||||
let json = parseJson(output)
|
|
||||||
return json
|
|
||||||
|
|
||||||
proc redeemCheque*(swapAddress: string, signature: string): JsonNode =
|
|
||||||
let task = "redeemCheque --swapaddress '" & $swapAddress & "' --signature '" & $signature & "'"
|
let task = "redeemCheque --swapaddress '" & $swapAddress & "' --signature '" & $signature & "'"
|
||||||
let (output, errC) = execNodeTask(task)
|
let res = execNodeTaskJson(task)
|
||||||
|
return res
|
||||||
|
|
||||||
# XXX Assume succeeds
|
when isMainModule:
|
||||||
let json = parseJson(output)
|
var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1"
|
||||||
return json
|
var sigRes = signCheque(aliceSwapAddress)
|
||||||
|
if sigRes.isOk():
|
||||||
|
echo "All good"
|
||||||
|
echo "Signature ", sigRes[]
|
||||||
|
else:
|
||||||
|
echo sigRes
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user