mirror of
https://github.com/logos-storage/nim-nitro.git
synced 2026-01-02 13:43:06 +00:00
There's a new byteutils in newer versions of stew - also, remove upraises and disable windows testing which requires SSL library install
45 lines
1.2 KiB
Nim
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"
|