Return updated signed state after payment

This commit is contained in:
Mark Spanbroek 2021-03-22 09:23:33 +01:00
parent 40cfe54144
commit 40b4782f9d
2 changed files with 13 additions and 6 deletions

View File

@ -108,24 +108,24 @@ func pay*(wallet: var Wallet,
channel: ChannelId,
asset: EthAddress,
receiver: Destination,
amount: UInt256): ?!void =
amount: UInt256): ?!SignedState =
if var state =? wallet.state(channel):
if var balances =? state.outcome.balances(asset):
?balances.move(wallet.destination, receiver, amount)
try:
state.outcome.update(asset, balances)
wallet.updateChannel(SignedState(state: state))
ok()
ok(wallet.channels[channel])
except KeyError as error:
void.failure error
SignedState.failure error
else:
void.failure "asset not found"
SignedState.failure "asset not found"
else:
void.failure "channel not found"
SignedState.failure "channel not found"
func pay*(wallet: var Wallet,
channel: ChannelId,
asset: EthAddress,
receiver: EthAddress,
amount: UInt256): ?!void =
amount: UInt256): ?!SignedState =
wallet.pay(channel, asset, receiver.toDestination, amount)

View File

@ -106,6 +106,13 @@ suite "wallet: making payments":
let expectedSignature = key.sign(wallet.state(channel).get)
check wallet.signature(channel, wallet.address) == expectedSignature.some
test "pay returns the updated signed state":
wallet = Wallet.init(key)
channel = wallet.openLedgerChannel(hub, chainId, nonce, asset, 42.u256)
let updated = wallet.pay(channel, asset, hub, 1.u256).option
check updated?.state == wallet.state(channel)
check updated?.signatures == wallet.signatures(channel)
test "payment fails when channel not found":
wallet = Wallet.init(key)
check wallet.pay(channel, asset, hub, 1.u256).isErr