2022-05-09 13:15:23 +00:00
|
|
|
import std/json
|
|
|
|
import std/strutils
|
|
|
|
import pkg/stew/byteutils
|
|
|
|
import pkg/questionable/results
|
|
|
|
import ../sales
|
2022-05-11 08:51:59 +00:00
|
|
|
import ../purchasing
|
2022-05-09 13:15:23 +00:00
|
|
|
|
2022-05-10 12:13:39 +00:00
|
|
|
type
|
|
|
|
StorageRequestParams* = object
|
|
|
|
duration*: UInt256
|
|
|
|
maxPrice*: UInt256
|
2022-05-18 11:28:34 +00:00
|
|
|
expiry*: ?UInt256
|
2022-05-10 12:13:39 +00:00
|
|
|
|
2022-05-09 13:15:23 +00:00
|
|
|
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
|
|
|
|
let json = ?catch parseJson(string.fromBytes(bytes))
|
|
|
|
let size = ?catch UInt256.fromHex(json["size"].getStr)
|
2022-05-09 14:51:08 +00:00
|
|
|
let duration = ?catch UInt256.fromHex(json["duration"].getStr)
|
|
|
|
let minPrice = ?catch UInt256.fromHex(json["minPrice"].getStr)
|
2022-05-09 13:15:23 +00:00
|
|
|
success Availability.init(size, duration, minPrice)
|
2022-05-10 12:13:39 +00:00
|
|
|
|
|
|
|
proc fromJson*(_: type StorageRequestParams,
|
|
|
|
bytes: seq[byte]): ?! StorageRequestParams =
|
|
|
|
let json = ?catch parseJson(string.fromBytes(bytes))
|
|
|
|
let duration = ?catch UInt256.fromHex(json["duration"].getStr)
|
|
|
|
let maxPrice = ?catch UInt256.fromHex(json["maxPrice"].getStr)
|
2022-05-18 11:28:34 +00:00
|
|
|
let expiry = UInt256.fromHex(json["expiry"].getStr).catch.option
|
|
|
|
success StorageRequestParams(
|
|
|
|
duration: duration,
|
|
|
|
maxPrice: maxPrice,
|
|
|
|
expiry: expiry
|
|
|
|
)
|
2022-05-11 08:51:59 +00:00
|
|
|
|
|
|
|
func `%`*(address: Address): JsonNode =
|
|
|
|
% $address
|
|
|
|
|
|
|
|
func `%`*(stint: StInt|StUInt): JsonNode =
|
|
|
|
%("0x" & stint.toHex)
|
|
|
|
|
|
|
|
func `%`*(arr: openArray[byte]): JsonNode =
|
|
|
|
%("0x" & arr.toHex)
|
|
|
|
|
|
|
|
func `%`*(purchase: Purchase): JsonNode =
|
|
|
|
%*{
|
2022-05-12 11:43:42 +00:00
|
|
|
"finished": purchase.finished,
|
|
|
|
"error": purchase.error.?msg,
|
|
|
|
"request": purchase.request,
|
|
|
|
"selected": purchase.selected
|
2022-05-11 08:51:59 +00:00
|
|
|
}
|