mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-07 16:13:09 +00:00
Return updated signed state after payment
This commit is contained in:
parent
40cfe54144
commit
40b4782f9d
@ -108,24 +108,24 @@ func pay*(wallet: var Wallet,
|
|||||||
channel: ChannelId,
|
channel: ChannelId,
|
||||||
asset: EthAddress,
|
asset: EthAddress,
|
||||||
receiver: Destination,
|
receiver: Destination,
|
||||||
amount: UInt256): ?!void =
|
amount: UInt256): ?!SignedState =
|
||||||
if var state =? wallet.state(channel):
|
if var state =? wallet.state(channel):
|
||||||
if var balances =? state.outcome.balances(asset):
|
if var balances =? state.outcome.balances(asset):
|
||||||
?balances.move(wallet.destination, receiver, amount)
|
?balances.move(wallet.destination, receiver, amount)
|
||||||
try:
|
try:
|
||||||
state.outcome.update(asset, balances)
|
state.outcome.update(asset, balances)
|
||||||
wallet.updateChannel(SignedState(state: state))
|
wallet.updateChannel(SignedState(state: state))
|
||||||
ok()
|
ok(wallet.channels[channel])
|
||||||
except KeyError as error:
|
except KeyError as error:
|
||||||
void.failure error
|
SignedState.failure error
|
||||||
else:
|
else:
|
||||||
void.failure "asset not found"
|
SignedState.failure "asset not found"
|
||||||
else:
|
else:
|
||||||
void.failure "channel not found"
|
SignedState.failure "channel not found"
|
||||||
|
|
||||||
func pay*(wallet: var Wallet,
|
func pay*(wallet: var Wallet,
|
||||||
channel: ChannelId,
|
channel: ChannelId,
|
||||||
asset: EthAddress,
|
asset: EthAddress,
|
||||||
receiver: EthAddress,
|
receiver: EthAddress,
|
||||||
amount: UInt256): ?!void =
|
amount: UInt256): ?!SignedState =
|
||||||
wallet.pay(channel, asset, receiver.toDestination, amount)
|
wallet.pay(channel, asset, receiver.toDestination, amount)
|
||||||
|
|||||||
@ -106,6 +106,13 @@ suite "wallet: making payments":
|
|||||||
let expectedSignature = key.sign(wallet.state(channel).get)
|
let expectedSignature = key.sign(wallet.state(channel).get)
|
||||||
check wallet.signature(channel, wallet.address) == expectedSignature.some
|
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":
|
test "payment fails when channel not found":
|
||||||
wallet = Wallet.init(key)
|
wallet = Wallet.init(key)
|
||||||
check wallet.pay(channel, asset, hub, 1.u256).isErr
|
check wallet.pay(channel, asset, hub, 1.u256).isErr
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user