2021-04-07 10:19:00 +00:00
|
|
|
import pkg/protobuf_serialization
|
|
|
|
import pkg/nitro
|
|
|
|
import pkg/questionable
|
2021-04-07 10:23:31 +00:00
|
|
|
import pkg/upraises
|
2021-04-08 07:22:41 +00:00
|
|
|
import ./bitswap
|
2021-04-07 10:19:00 +00:00
|
|
|
|
|
|
|
export PricingMessage
|
2021-04-07 12:30:33 +00:00
|
|
|
export StateChannelUpdate
|
2021-04-07 10:19:00 +00:00
|
|
|
|
|
|
|
export nitro
|
|
|
|
|
2021-04-07 10:23:31 +00:00
|
|
|
push: {.upraises: [].}
|
|
|
|
|
2021-04-07 10:19:00 +00:00
|
|
|
type
|
|
|
|
Pricing* = object
|
|
|
|
address*: EthAddress
|
|
|
|
asset*: EthAddress
|
|
|
|
price*: UInt256
|
|
|
|
|
|
|
|
func init*(_: type PricingMessage, pricing: Pricing): PricingMessage =
|
|
|
|
PricingMessage(
|
|
|
|
address: @(pricing.address.toArray),
|
|
|
|
asset: @(pricing.asset.toArray),
|
|
|
|
price: @(pricing.price.toBytesBE)
|
|
|
|
)
|
|
|
|
|
|
|
|
func parse(_: type EthAddress, bytes: seq[byte]): ?EthAddress =
|
|
|
|
var address: array[20, byte]
|
|
|
|
if bytes.len != address.len:
|
|
|
|
return EthAddress.none
|
|
|
|
for i in 0..<address.len:
|
|
|
|
address[i] = bytes[i]
|
|
|
|
EthAddress(address).some
|
|
|
|
|
|
|
|
func parse(_: type UInt256, bytes: seq[byte]): ?UInt256 =
|
|
|
|
if bytes.len > 32:
|
|
|
|
return UInt256.none
|
|
|
|
UInt256.fromBytesBE(bytes).some
|
|
|
|
|
|
|
|
func init*(_: type Pricing, message: PricingMessage): ?Pricing =
|
|
|
|
let address = EthAddress.parse(message.address)
|
|
|
|
let asset = EThAddress.parse(message.asset)
|
|
|
|
let price = UInt256.parse(message.price)
|
|
|
|
if address.isNone or asset.isNone or price.isNone:
|
|
|
|
return Pricing.none
|
|
|
|
Pricing(address: address.get, asset: asset.get, price: price.get).some
|
2021-04-07 12:30:33 +00:00
|
|
|
|
|
|
|
func init*(_: type StateChannelUpdate, state: SignedState): StateChannelUpdate =
|
|
|
|
StateChannelUpdate(update: state.toJson.toBytes)
|
|
|
|
|
|
|
|
proc init*(_: type SignedState, update: StateChannelUpdate): ?SignedState =
|
|
|
|
SignedState.fromJson(string.fromBytes(update.update))
|