mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-07 16:33:08 +00:00
Waku Swap: sign and send cheque (#386)
* fix balance test * move waku swap contracts into own module * Move more to separate waku swap contracts * move erc20address * Improve cmd/task util * fix redeem part too * sign cheque hardcode * add signature field, send cheque * echo->debug * disable waku swap test it calls contracts, which isnt integrated
This commit is contained in:
parent
e152ed349f
commit
b730d79438
@ -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,
|
||||||
|
|||||||
@ -2,7 +2,9 @@
|
|||||||
#
|
#
|
||||||
import
|
import
|
||||||
std/[unittest, options, tables, sets, osproc, strutils, strformat, json],
|
std/[unittest, options, tables, sets, osproc, strutils, strformat, json],
|
||||||
../test_helpers, ./utils
|
chronicles,
|
||||||
|
../test_helpers, ./utils,
|
||||||
|
../../waku/v2/protocol/waku_swap/waku_swap_contracts
|
||||||
|
|
||||||
procSuite "Basic balance test":
|
procSuite "Basic balance test":
|
||||||
var aliceSwapAddress = ""
|
var aliceSwapAddress = ""
|
||||||
@ -10,93 +12,51 @@ procSuite "Basic balance test":
|
|||||||
var erc20address = ""
|
var erc20address = ""
|
||||||
test "Get pwd of swap module":
|
test "Get pwd of swap module":
|
||||||
let (output, errC) = osproc.execCmdEx("(cd ../swap-contracts-module && pwd)")
|
let (output, errC) = osproc.execCmdEx("(cd ../swap-contracts-module && pwd)")
|
||||||
echo output
|
debug "output", output
|
||||||
|
|
||||||
check:
|
check:
|
||||||
contains(output, "swap-contracts-module")
|
contains(output, "swap-contracts-module")
|
||||||
|
|
||||||
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 taskString = "npx hardhat --network localhost balance --account 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
|
let balance = waku_swap_contracts.getBalance("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
||||||
let cmdString = "cd ../swap-contracts-module; " & &"{taskString}"
|
|
||||||
echo cmdString
|
|
||||||
let (output, errC) = osproc.execCmdEx(cmdString)
|
|
||||||
echo output
|
|
||||||
|
|
||||||
check:
|
check:
|
||||||
contains(output, "ETH")
|
contains(balance, "ETH")
|
||||||
|
|
||||||
test "Setup Swap":
|
test "Setup Swap":
|
||||||
let taskString = "npx hardhat --network localhost setupSwap"
|
let json = waku_swap_contracts.setupSwap()
|
||||||
let cmdString = "cd ../swap-contracts-module; " & &"{taskString}"
|
|
||||||
echo cmdString
|
|
||||||
let (output, errC) = osproc.execCmdEx(cmdString)
|
|
||||||
|
|
||||||
# XXX Assume succeeds
|
|
||||||
let json = parseJson(output)
|
|
||||||
var aliceAddress = json["aliceAddress"].getStr()
|
var aliceAddress = json["aliceAddress"].getStr()
|
||||||
aliceSwapAddress = json["aliceSwapAddress"].getStr()
|
aliceSwapAddress = json["aliceSwapAddress"].getStr()
|
||||||
erc20address = json["erc20address"].getStr()
|
erc20address = json["erc20address"].getStr()
|
||||||
echo erc20address
|
debug "erc20address", erc20address
|
||||||
echo json
|
debug "json", json
|
||||||
|
|
||||||
# Contains default Alice account
|
# Contains default Alice account
|
||||||
check:
|
check:
|
||||||
contains(aliceAddress, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
contains(aliceAddress, "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266")
|
||||||
|
|
||||||
test "Sign Cheque":
|
test "Sign Cheque":
|
||||||
#npx hardhat signCheque --swapaddress "0x94099942864EA81cCF197E9D71ac53310b1468D8"
|
signature = waku_swap_contracts.signCheque(aliceSwapAddress)
|
||||||
let taskString = "npx hardhat --network localhost signCheque --swapaddress '" & &"{aliceSwapAddress}" & "'"
|
|
||||||
let cmdString = "cd ../swap-contracts-module; " & &"{taskString}"
|
|
||||||
echo cmdString
|
|
||||||
let (output, errC) = osproc.execCmdEx(cmdString)
|
|
||||||
|
|
||||||
# XXX Assume succeeds
|
|
||||||
let json = parseJson(output)
|
|
||||||
signature = json["signature"].getStr()
|
|
||||||
echo json
|
|
||||||
echo signature
|
|
||||||
|
|
||||||
# Contains some signature
|
|
||||||
check:
|
check:
|
||||||
contains(signature, "0x")
|
contains(signature, "0x")
|
||||||
|
|
||||||
test "Get balances 1":
|
test "Get ERC20 Balances":
|
||||||
let taskString = "npx hardhat --network localhost getBalances --erc20address '" & &"{erc20address}" & "'"
|
let json = getERC20Balances(erc20address)
|
||||||
let cmdString = "cd ../swap-contracts-module; " & &"{taskString}"
|
|
||||||
echo cmdString
|
|
||||||
let (output, errC) = osproc.execCmdEx(cmdString)
|
|
||||||
|
|
||||||
# XXX Assume succeeds
|
|
||||||
let json = parseJson(output)
|
|
||||||
echo json
|
|
||||||
|
|
||||||
# Contains some signature
|
|
||||||
check:
|
check:
|
||||||
contains(signature, "0x")
|
json["bobBalance"].getInt() == 10000
|
||||||
|
|
||||||
test "Redeem cheque and check balance":
|
test "Redeem cheque and check balance":
|
||||||
# XXX Simplify string creation
|
let json = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature)
|
||||||
let taskString = "npx hardhat --network localhost redeemCheque --swapaddress '" & &"{aliceSwapAddress}" & "' --signature '" & &"{signature}" & "'"
|
|
||||||
let cmdString = "cd ../swap-contracts-module; " & &"{taskString}"
|
|
||||||
echo cmdString
|
|
||||||
let (output, errC) = osproc.execCmdEx(cmdString)
|
|
||||||
|
|
||||||
# XXX Assume succeeds
|
|
||||||
echo output
|
|
||||||
let json = parseJson(output)
|
|
||||||
var resp = json["resp"].getStr()
|
var resp = json["resp"].getStr()
|
||||||
echo json
|
debug "json", json
|
||||||
|
|
||||||
echo "Get balances"
|
debug "Get balances"
|
||||||
let taskString2 = "npx hardhat --network localhost getBalances --erc20address '" & &"{erc20address}" & "'"
|
let json2 = getERC20Balances(erc20address)
|
||||||
let cmdString2 = "cd ../swap-contracts-module; " & &"{taskString2}"
|
debug "json", json2
|
||||||
echo cmdString2
|
|
||||||
let (output2, errC2) = osproc.execCmdEx(cmdString2)
|
|
||||||
|
|
||||||
# XXX Assume succeeds
|
|
||||||
let json2 = parseJson(output2)
|
|
||||||
echo json2
|
|
||||||
|
|
||||||
# Balance for Bob has now increased
|
# Balance for Bob has now increased
|
||||||
check:
|
check:
|
||||||
|
|||||||
@ -31,7 +31,8 @@ import
|
|||||||
libp2p/stream/connection,
|
libp2p/stream/connection,
|
||||||
../../node/peer_manager,
|
../../node/peer_manager,
|
||||||
../message_notifier,
|
../message_notifier,
|
||||||
./waku_swap_types
|
./waku_swap_types,
|
||||||
|
../../waku/v2/protocol/waku_swap/waku_swap_contracts
|
||||||
|
|
||||||
export waku_swap_types
|
export waku_swap_types
|
||||||
|
|
||||||
@ -59,6 +60,7 @@ proc encode*(cheque: Cheque): ProtoBuffer =
|
|||||||
result.write(1, cheque.beneficiary)
|
result.write(1, cheque.beneficiary)
|
||||||
result.write(2, cheque.date)
|
result.write(2, cheque.date)
|
||||||
result.write(3, cheque.amount)
|
result.write(3, cheque.amount)
|
||||||
|
result.write(4, cheque.signature)
|
||||||
|
|
||||||
proc init*(T: type Handshake, buffer: seq[byte]): ProtoResult[T] =
|
proc init*(T: type Handshake, buffer: seq[byte]): ProtoResult[T] =
|
||||||
var beneficiary: seq[byte]
|
var beneficiary: seq[byte]
|
||||||
@ -73,12 +75,14 @@ proc init*(T: type Cheque, buffer: seq[byte]): ProtoResult[T] =
|
|||||||
var beneficiary: seq[byte]
|
var beneficiary: seq[byte]
|
||||||
var date: uint32
|
var date: uint32
|
||||||
var amount: uint32
|
var amount: uint32
|
||||||
|
var signature: seq[byte]
|
||||||
var cheque = Cheque()
|
var cheque = Cheque()
|
||||||
let pb = initProtoBuffer(buffer)
|
let pb = initProtoBuffer(buffer)
|
||||||
|
|
||||||
discard ? pb.getField(1, cheque.beneficiary)
|
discard ? pb.getField(1, cheque.beneficiary)
|
||||||
discard ? pb.getField(2, cheque.date)
|
discard ? pb.getField(2, cheque.date)
|
||||||
discard ? pb.getField(3, cheque.amount)
|
discard ? pb.getField(3, cheque.amount)
|
||||||
|
discard ? pb.getField(4, cheque.signature)
|
||||||
|
|
||||||
ok(cheque)
|
ok(cheque)
|
||||||
|
|
||||||
@ -89,6 +93,8 @@ proc init*(T: type Cheque, buffer: seq[byte]): ProtoResult[T] =
|
|||||||
|
|
||||||
# TODO Test for credit/debit operations in succession
|
# TODO Test for credit/debit operations in succession
|
||||||
|
|
||||||
|
|
||||||
|
# TODO Assume we calculated cheque
|
||||||
proc sendCheque*(ws: WakuSwap) {.async.} =
|
proc sendCheque*(ws: WakuSwap) {.async.} =
|
||||||
let peerOpt = ws.peerManager.selectPeer(WakuSwapCodec)
|
let peerOpt = ws.peerManager.selectPeer(WakuSwapCodec)
|
||||||
|
|
||||||
@ -109,9 +115,14 @@ proc sendCheque*(ws: WakuSwap) {.async.} =
|
|||||||
|
|
||||||
info "sendCheque"
|
info "sendCheque"
|
||||||
|
|
||||||
|
# TODO We get this from the setup of swap setup, dynamic, should be part of setup
|
||||||
# TODO Add beneficiary, etc
|
# TODO Add beneficiary, etc
|
||||||
# XXX Hardcoded amount for now
|
var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1"
|
||||||
await connOpt.get().writeLP(Cheque(amount: 1).encode().buffer)
|
let signature = waku_swap_contracts.signCheque(aliceSwapAddress)
|
||||||
|
info "Signed Cheque", swapAddress = aliceSwapAddress, signature = signature
|
||||||
|
|
||||||
|
let sigBytes = cast[seq[byte]](signature)
|
||||||
|
await connOpt.get().writeLP(Cheque(amount: 1, signature: sigBytes).encode().buffer)
|
||||||
|
|
||||||
# Set new balance
|
# Set new balance
|
||||||
let peerId = peer.peerId
|
let peerId = peer.peerId
|
||||||
@ -119,6 +130,7 @@ proc sendCheque*(ws: WakuSwap) {.async.} =
|
|||||||
info "New accounting state", accounting = ws.accounting[peerId]
|
info "New accounting state", accounting = ws.accounting[peerId]
|
||||||
|
|
||||||
# TODO Authenticate cheque, check beneficiary etc
|
# TODO Authenticate cheque, check beneficiary etc
|
||||||
|
# TODO Redeem cheque
|
||||||
proc handleCheque*(ws: WakuSwap, cheque: Cheque) =
|
proc handleCheque*(ws: WakuSwap, cheque: Cheque) =
|
||||||
info "handle incoming cheque"
|
info "handle incoming cheque"
|
||||||
# XXX Assume peerId is first peer
|
# XXX Assume peerId is first peer
|
||||||
@ -169,6 +181,7 @@ proc init*(wakuSwap: WakuSwap) =
|
|||||||
|
|
||||||
# TODO Isolate to policy function
|
# TODO Isolate to policy function
|
||||||
# TODO Tunable payment threshhold, hard code for PoC
|
# TODO Tunable payment threshhold, hard code for PoC
|
||||||
|
# XXX: Where should this happen? Apply policy...
|
||||||
let paymentThreshhold = 1
|
let paymentThreshhold = 1
|
||||||
if wakuSwap.accounting[peerId] >= paymentThreshhold:
|
if wakuSwap.accounting[peerId] >= paymentThreshhold:
|
||||||
info "Payment threshhold hit, send cheque"
|
info "Payment threshhold hit, send cheque"
|
||||||
|
|||||||
65
waku/v2/protocol/waku_swap/waku_swap_contracts.nim
Normal file
65
waku/v2/protocol/waku_swap/waku_swap_contracts.nim
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
# Glue code to interact with SWAP contracts module.
|
||||||
|
#
|
||||||
|
# Assumes swap-contracts-module node is running.
|
||||||
|
#
|
||||||
|
import
|
||||||
|
std/[osproc, strutils, json],
|
||||||
|
chronicles
|
||||||
|
|
||||||
|
logScope:
|
||||||
|
topics = "wakuswapcontracts"
|
||||||
|
|
||||||
|
# XXX In general this is not a good API, more a collection of hacky glue code for PoC.
|
||||||
|
#
|
||||||
|
# TODO Error handling
|
||||||
|
|
||||||
|
# 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] =
|
||||||
|
let cmdString = $cmdPrelude & $taskStr
|
||||||
|
debug "execNodeTask", cmdString
|
||||||
|
return osproc.execCmdEx(cmdString)
|
||||||
|
|
||||||
|
# TODO JSON?
|
||||||
|
proc getBalance*(accountAddress: string): string =
|
||||||
|
let task = "balance --account " & $accountAddress
|
||||||
|
let (output, errC) = execNodeTask(task)
|
||||||
|
debug "getBalance", output
|
||||||
|
return output
|
||||||
|
|
||||||
|
proc setupSwap*(): JsonNode =
|
||||||
|
let task = "setupSwap"
|
||||||
|
let (output, errC) = execNodeTask(task)
|
||||||
|
|
||||||
|
# XXX Assume succeeds
|
||||||
|
let json = parseJson(output)
|
||||||
|
return json
|
||||||
|
|
||||||
|
# TODO Signature
|
||||||
|
proc signCheque*(swapAddress: string): string =
|
||||||
|
let task = "signCheque --swapaddress '" & $swapAddress & "'"
|
||||||
|
let (output, errC) = execNodeTask(task)
|
||||||
|
|
||||||
|
# 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 =
|
||||||
|
let task = "getBalances --erc20address '" & $erc20address & "'"
|
||||||
|
let (output, errC) = execNodeTask(task)
|
||||||
|
|
||||||
|
# XXX Assume succeeds
|
||||||
|
let json = parseJson(output)
|
||||||
|
return json
|
||||||
|
|
||||||
|
proc redeemCheque*(swapAddress: string, signature: string): JsonNode =
|
||||||
|
let task = "redeemCheque --swapaddress '" & $swapAddress & "' --signature '" & $signature & "'"
|
||||||
|
let (output, errC) = execNodeTask(task)
|
||||||
|
|
||||||
|
# XXX Assume succeeds
|
||||||
|
let json = parseJson(output)
|
||||||
|
return json
|
||||||
@ -12,10 +12,13 @@ type
|
|||||||
Handshake* = object
|
Handshake* = object
|
||||||
beneficiary*: Beneficiary
|
beneficiary*: Beneficiary
|
||||||
|
|
||||||
|
# XXX I'm confused by lack of signature here, most important thing...
|
||||||
|
# TODO Look over these data structures again
|
||||||
Cheque* = object
|
Cheque* = object
|
||||||
beneficiary*: Beneficiary
|
beneficiary*: Beneficiary
|
||||||
date*: uint32
|
date*: uint32
|
||||||
amount*: uint32
|
amount*: uint32
|
||||||
|
signature*: seq[byte]
|
||||||
|
|
||||||
CreditHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.}
|
CreditHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.}
|
||||||
DebitHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.}
|
DebitHandler* = proc (peerId: PeerId, amount: int) {.gcsafe, closure.}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user