2021-02-22 15:32:48 +01:00
|
|
|
import pkg/nimcrypto
|
2021-03-09 11:07:35 +01:00
|
|
|
import ../basics
|
2021-02-22 15:32:48 +01:00
|
|
|
import ./abi
|
|
|
|
|
|
2021-03-09 09:37:27 +01:00
|
|
|
include questionable/errorban
|
2021-03-03 10:30:07 +01:00
|
|
|
|
2021-03-09 11:07:35 +01:00
|
|
|
export basics
|
2021-02-22 15:32:48 +01:00
|
|
|
|
|
|
|
|
type
|
2021-02-23 12:30:55 +01:00
|
|
|
Outcome* = distinct seq[AssetOutcome]
|
2021-02-22 15:32:48 +01:00
|
|
|
AssetOutcomeType* = enum
|
|
|
|
|
allocationType = 0
|
|
|
|
|
guaranteeType = 1
|
|
|
|
|
AssetOutcome* = object
|
|
|
|
|
assetHolder*: EthAddress
|
|
|
|
|
case kind*: AssetOutcomeType
|
|
|
|
|
of allocationType:
|
|
|
|
|
allocation*: Allocation
|
|
|
|
|
of guaranteeType:
|
|
|
|
|
guarantee*: Guarantee
|
2021-02-25 09:11:46 +01:00
|
|
|
Allocation* = distinct seq[AllocationItem]
|
2021-03-09 16:48:47 +01:00
|
|
|
AllocationItem* = tuple
|
|
|
|
|
destination: Destination
|
|
|
|
|
amount: UInt256
|
2021-02-22 15:32:48 +01:00
|
|
|
Guarantee* = object
|
2021-03-09 14:02:01 +01:00
|
|
|
targetChannelId*: Destination
|
|
|
|
|
destinations*: seq[Destination]
|
2021-02-22 15:32:48 +01:00
|
|
|
|
2021-03-09 16:48:47 +01:00
|
|
|
proc init*(_: type Outcome,
|
|
|
|
|
asset: EthAddress,
|
|
|
|
|
allocation: openArray[AllocationItem]): Outcome =
|
|
|
|
|
let assetOutcome = AssetOutcome(
|
|
|
|
|
kind: allocationType,
|
|
|
|
|
assetHolder: asset,
|
|
|
|
|
allocation: Allocation(@allocation)
|
|
|
|
|
)
|
|
|
|
|
Outcome(@[assetOutcome])
|
|
|
|
|
|
2021-03-09 16:49:16 +01:00
|
|
|
proc `==`*(a, b: Allocation): bool {.borrow.}
|
|
|
|
|
|
|
|
|
|
proc `==`*(a, b: AssetOutcome): bool =
|
|
|
|
|
if a.kind != b.kind:
|
|
|
|
|
return false
|
|
|
|
|
if a.assetHolder != b.assetHolder:
|
|
|
|
|
return false
|
|
|
|
|
case a.kind:
|
|
|
|
|
of allocationType:
|
|
|
|
|
a.allocation == b.allocation
|
|
|
|
|
of guaranteeType:
|
|
|
|
|
a.guarantee == b.guarantee
|
|
|
|
|
|
|
|
|
|
proc `==`*(a, b: Outcome): bool {.borrow.}
|
|
|
|
|
|
2021-02-25 09:11:46 +01:00
|
|
|
proc encode*(encoder: var AbiEncoder, guarantee: Guarantee) =
|
|
|
|
|
encoder.startTuple()
|
|
|
|
|
encoder.startTuple()
|
|
|
|
|
encoder.write(guarantee.targetChannelId)
|
|
|
|
|
encoder.write(guarantee.destinations)
|
|
|
|
|
encoder.finishTuple()
|
|
|
|
|
encoder.finishTuple()
|
2021-02-22 15:32:48 +01:00
|
|
|
|
2021-02-25 09:11:46 +01:00
|
|
|
proc encode*(encoder: var AbiEncoder, item: AllocationItem) =
|
|
|
|
|
encoder.startTuple()
|
|
|
|
|
encoder.write(item.destination)
|
|
|
|
|
encoder.write(item.amount)
|
|
|
|
|
encoder.finishTuple()
|
2021-02-22 15:32:48 +01:00
|
|
|
|
2021-02-25 09:11:46 +01:00
|
|
|
proc encode*(encoder: var AbiEncoder, allocation: Allocation) =
|
|
|
|
|
encoder.startTuple()
|
|
|
|
|
encoder.write(seq[AllocationItem](allocation))
|
|
|
|
|
encoder.finishTuple()
|
2021-02-22 15:32:48 +01:00
|
|
|
|
2021-02-25 09:11:46 +01:00
|
|
|
proc encode*(encoder: var AbiEncoder, assetOutcome: AssetOutcome) =
|
|
|
|
|
var content= AbiEncoder.init()
|
|
|
|
|
content.startTuple()
|
2021-02-22 15:32:48 +01:00
|
|
|
content.startTuple()
|
|
|
|
|
content.write(assetOutcome.kind)
|
|
|
|
|
case assetOutcome.kind:
|
|
|
|
|
of allocationType:
|
2021-02-25 09:11:46 +01:00
|
|
|
content.write(AbiEncoder.encode(assetOutcome.allocation))
|
2021-02-22 15:32:48 +01:00
|
|
|
of guaranteeType:
|
2021-02-25 09:11:46 +01:00
|
|
|
content.write(AbiEncoder.encode(assetOutcome.guarantee))
|
|
|
|
|
content.finishTuple()
|
2021-02-22 15:32:48 +01:00
|
|
|
content.finishTuple()
|
|
|
|
|
|
2021-02-25 09:11:46 +01:00
|
|
|
encoder.startTuple()
|
|
|
|
|
encoder.write(assetOutcome.assetHolder)
|
|
|
|
|
encoder.write(content.finish())
|
|
|
|
|
encoder.finishTuple()
|
|
|
|
|
|
|
|
|
|
proc encode*(encoder: var AbiEncoder, outcome: Outcome) =
|
|
|
|
|
encoder.startTuple()
|
|
|
|
|
encoder.write(seq[AssetOutcome](outcome))
|
|
|
|
|
encoder.finishTuple()
|
2021-02-23 12:30:55 +01:00
|
|
|
|
2021-02-22 15:32:48 +01:00
|
|
|
proc hashOutcome*(outcome: Outcome): array[32, byte] =
|
2021-02-25 09:11:46 +01:00
|
|
|
keccak256.digest(AbiEncoder.encode(outcome)).data
|