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
|
|
|
|
|
|
2025-12-10 20:44:01 +01:00
|
|
|
{.push raises: [].}
|
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-17 12:22:00 +01:00
|
|
|
func init*(_: type Outcome,
|
2021-03-09 16:48:47 +01:00
|
|
|
asset: EthAddress,
|
|
|
|
|
allocation: openArray[AllocationItem]): Outcome =
|
|
|
|
|
let assetOutcome = AssetOutcome(
|
|
|
|
|
kind: allocationType,
|
|
|
|
|
assetHolder: asset,
|
|
|
|
|
allocation: Allocation(@allocation)
|
|
|
|
|
)
|
|
|
|
|
Outcome(@[assetOutcome])
|
|
|
|
|
|
2021-03-17 15:34:55 +01:00
|
|
|
proc `==`*(a, b: Allocation): bool {.borrow.}
|
2021-03-09 16:49:16 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func `==`*(a, b: AssetOutcome): bool =
|
2021-03-09 16:49:16 +01:00
|
|
|
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
|
|
|
|
|
|
2021-03-17 15:34:55 +01:00
|
|
|
proc `==`*(a, b: Outcome): bool {.borrow.}
|
2021-03-09 16:49:16 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func encode*(encoder: var AbiEncoder, guarantee: Guarantee) =
|
2021-12-06 15:39:27 +01:00
|
|
|
encoder.write:
|
|
|
|
|
( (guarantee.targetChannelId, guarantee.destinations), )
|
2021-02-22 15:32:48 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func encode*(encoder: var AbiEncoder, allocation: Allocation) =
|
2021-12-06 15:39:27 +01:00
|
|
|
encoder.write: (seq[AllocationItem](allocation),)
|
2021-02-22 15:32:48 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func encode*(encoder: var AbiEncoder, assetOutcome: AssetOutcome) =
|
2021-12-06 15:39:27 +01:00
|
|
|
var content: seq[byte]
|
2021-02-22 15:32:48 +01:00
|
|
|
case assetOutcome.kind:
|
|
|
|
|
of allocationType:
|
2021-12-06 15:39:27 +01:00
|
|
|
content = AbiEncoder.encode:
|
|
|
|
|
( (assetOutcome.kind, ABiEncoder.encode(assetOutcome.allocation)), )
|
2021-02-22 15:32:48 +01:00
|
|
|
of guaranteeType:
|
2021-12-06 15:39:27 +01:00
|
|
|
content = AbiEncoder.encode:
|
|
|
|
|
( (assetOutcome.kind, AbiEncoder.encode(assetOutcome.guarantee)), )
|
|
|
|
|
encoder.write( (assetOutcome.assetHolder, content) )
|
2021-02-25 09:11:46 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func encode*(encoder: var AbiEncoder, outcome: Outcome) =
|
2021-12-06 15:39:27 +01:00
|
|
|
encoder.write: (seq[AssetOutcome](outcome),)
|
2021-02-23 12:30:55 +01:00
|
|
|
|
2021-03-17 12:22:00 +01:00
|
|
|
func hashOutcome*(outcome: Outcome): array[32, byte] =
|
2021-02-25 09:11:46 +01:00
|
|
|
keccak256.digest(AbiEncoder.encode(outcome)).data
|