nim-nitro/nitro/wallet/balances.nim
Jacek Sieka a15660f70f
chore: fix import conflict
There's a new byteutils in newer versions of stew - also, remove
upraises and disable windows testing which requires SSL library install
2025-12-10 22:06:29 +01:00

45 lines
1.2 KiB
Nim

import std/tables
import std/sequtils
import ../basics
import ../protocol
{.push raises: [].}
export tables
type
Balances* = OrderedTable[Destination, UInt256]
func `balances`*(outcome: Outcome, asset: EthAddress): ?Balances =
for assetOutcome in seq[AssetOutcome](outcome):
if assetOutcome.assetHolder == asset:
if assetOutcome.kind == allocationType:
let allocation = assetOutcome.allocation
let items = seq[AllocationItem](allocation)
return items.toOrderedTable.some
Balances.none
func `update`*(outcome: var Outcome, asset: EthAddress, table: Balances) =
for assetOutcome in seq[AssetOutcome](outcome).mitems:
if assetOutcome.assetHolder == asset:
if assetOutcome.kind == allocationType:
assetOutcome.allocation = Allocation(toSeq(table.pairs))
func move*(balances: var Balances,
source: Destination,
destination: Destination,
amount: UInt256): ?!void =
try:
if balances[source] < amount:
return failure "insufficient funds"
balances[source] -= amount
if (balances.contains(destination)):
balances[destination] += amount
else:
balances[destination] = amount
success()
except KeyError:
failure "no funds"