mirror of
https://github.com/logos-messaging/logos-messaging-nim.git
synced 2026-01-06 07:53:12 +00:00
Swap redeem cheque and update accounting state accordingly (#397)
* Redeem cheque when recevived * Check balance, ish * conditional update accounting after redeem cheque * Add override for account update state w/o cheque * Update waku/v2/protocol/waku_swap/waku_swap.nim Co-authored-by: Sanaz Taheri Boshrooyeh <35961250+staheri14@users.noreply.github.com> Co-authored-by: Sanaz Taheri Boshrooyeh <35961250+staheri14@users.noreply.github.com>
This commit is contained in:
parent
52cb7f5448
commit
52d0ea6445
@ -40,6 +40,9 @@ procSuite "Waku SWAP Accounting":
|
|||||||
decodedCheque.isErr == false
|
decodedCheque.isErr == false
|
||||||
decodedCheque.get() == cheque
|
decodedCheque.get() == cheque
|
||||||
|
|
||||||
|
# TODO To do this reliably we need access to contract node
|
||||||
|
# With current logic state isn't updated because of bad cheque
|
||||||
|
# Consider moving this test to e2e test, and/or move swap module to be on by default
|
||||||
asyncTest "Update accounting state after store operations":
|
asyncTest "Update accounting state after store operations":
|
||||||
let
|
let
|
||||||
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
nodeKey1 = crypto.PrivateKey.random(Secp256k1, rng[])[]
|
||||||
|
|||||||
@ -122,7 +122,7 @@ proc sendCheque*(ws: WakuSwap) {.async.} =
|
|||||||
|
|
||||||
var res = waku_swap_contracts.signCheque(aliceSwapAddress)
|
var res = waku_swap_contracts.signCheque(aliceSwapAddress)
|
||||||
if res.isOk():
|
if res.isOk():
|
||||||
echo "signCheque ", res[]
|
info "signCheque ", res=res[]
|
||||||
let json = res[]
|
let json = res[]
|
||||||
signature = json["signature"].getStr()
|
signature = json["signature"].getStr()
|
||||||
else:
|
else:
|
||||||
@ -139,13 +139,56 @@ 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
|
||||||
let peerOpt = ws.peerManager.selectPeer(WakuSwapCodec)
|
let peerOpt = ws.peerManager.selectPeer(WakuSwapCodec)
|
||||||
let peerId = peerOpt.get().peerId
|
let peerId = peerOpt.get().peerId
|
||||||
ws.accounting[peerId] += int(cheque.amount)
|
|
||||||
|
# TODO Redeem cheque here
|
||||||
|
var signature = cast[string](cheque.signature)
|
||||||
|
# TODO Where should Alice Swap Address come from? Handshake probably?
|
||||||
|
# Hacky for now
|
||||||
|
var aliceSwapAddress = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1"
|
||||||
|
info "Redeeming cheque with", swapAddress=aliceSwapAddress, signature=signature
|
||||||
|
var res = waku_swap_contracts.redeemCheque(aliceSwapAddress, signature)
|
||||||
|
if res.isOk():
|
||||||
|
info "redeemCheque ok", redeem=res[]
|
||||||
|
else:
|
||||||
|
info "Unable to redeem cheque"
|
||||||
|
|
||||||
|
# Check balance here
|
||||||
|
# TODO How do we get ERC20 address here?
|
||||||
|
# XXX This one is wrong
|
||||||
|
# Normally this would be part of initial setup, otherwise we need some temp persistence here
|
||||||
|
# Possibly as part of handshake?
|
||||||
|
var erc20address = "0x6C3d502f1a97d4470b881015b83D9Dd1062172e1"
|
||||||
|
let balRes = waku_swap_contracts.getERC20Balances(erc20address)
|
||||||
|
if balRes.isOk():
|
||||||
|
# XXX: Assumes Alice and Bob here...
|
||||||
|
var bobBalance = balRes[]["bobBalance"].getInt()
|
||||||
|
info "New balance is", balance = bobBalance
|
||||||
|
else:
|
||||||
|
info "Problem getting Bob balance"
|
||||||
|
|
||||||
|
# TODO Could imagine scenario where you don't cash cheque but leave it as credit
|
||||||
|
# In that case, we would probably update accounting state, but keep track of cheques
|
||||||
|
|
||||||
|
# When this is true we update accounting state anyway when node is offline,
|
||||||
|
# makes waku_swap test pass for now
|
||||||
|
# Consider desired logic here
|
||||||
|
var stateUpdateOverRide = true
|
||||||
|
|
||||||
|
if res.isOk():
|
||||||
|
info "Updating accounting state with redeemed cheque"
|
||||||
|
ws.accounting[peerId] += int(cheque.amount)
|
||||||
|
else:
|
||||||
|
if stateUpdateOverRide:
|
||||||
|
info "Updating accounting state with even if cheque failed"
|
||||||
|
ws.accounting[peerId] += int(cheque.amount)
|
||||||
|
else:
|
||||||
|
info "Not updating accounting state with due to bad cheque"
|
||||||
|
|
||||||
info "New accounting state", accounting = ws.accounting[peerId]
|
info "New accounting state", accounting = ws.accounting[peerId]
|
||||||
|
|
||||||
proc init*(wakuSwap: WakuSwap) =
|
proc init*(wakuSwap: WakuSwap) =
|
||||||
@ -218,4 +261,3 @@ proc setPeer*(ws: WakuSwap, peer: PeerInfo) =
|
|||||||
waku_swap_peers.inc()
|
waku_swap_peers.inc()
|
||||||
|
|
||||||
# TODO End to end communication
|
# TODO End to end communication
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user