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
|
2022-07-20 12:11:00 +00:00
|
|
|
reward*: UInt256
|
2022-05-18 11:28:34 +00:00
|
|
|
expiry*: ?UInt256
|
2022-08-02 15:50:57 +00:00
|
|
|
nodes*: ?uint
|
|
|
|
tolerance*: ?uint
|
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)
|
2022-07-20 12:11:00 +00:00
|
|
|
let reward = ?catch UInt256.fromHex(json["reward"].getStr)
|
2022-05-18 11:28:34 +00:00
|
|
|
let expiry = UInt256.fromHex(json["expiry"].getStr).catch.option
|
2022-08-02 15:50:57 +00:00
|
|
|
let nodes = strutils.fromHex[uint](json["nodes"].getStr).catch.option
|
|
|
|
let tolerance = strutils.fromHex[uint](json["tolerance"].getStr).catch.option
|
2022-05-18 11:28:34 +00:00
|
|
|
success StorageRequestParams(
|
|
|
|
duration: duration,
|
2022-07-20 12:11:00 +00:00
|
|
|
reward: reward,
|
2022-08-02 15:50:57 +00:00
|
|
|
expiry: expiry,
|
|
|
|
nodes: nodes,
|
|
|
|
tolerance: tolerance
|
2022-05-18 11:28:34 +00:00
|
|
|
)
|
2022-05-11 08:51:59 +00:00
|
|
|
|
|
|
|
func `%`*(address: Address): JsonNode =
|
|
|
|
% $address
|
|
|
|
|
2023-03-10 07:02:54 +00:00
|
|
|
func `%`*(stint: StInt|StUint): JsonNode =
|
2022-05-11 08:51:59 +00:00
|
|
|
%("0x" & stint.toHex)
|
|
|
|
|
|
|
|
func `%`*(arr: openArray[byte]): JsonNode =
|
|
|
|
%("0x" & arr.toHex)
|
|
|
|
|
2022-08-18 05:55:53 +00:00
|
|
|
func `%`*(id: RequestId | SlotId | Nonce): JsonNode =
|
|
|
|
% id.toArray
|
|
|
|
|
2022-05-11 08:51:59 +00:00
|
|
|
func `%`*(purchase: Purchase): JsonNode =
|
|
|
|
%*{
|
2022-11-08 07:10:17 +00:00
|
|
|
"state": (purchase.state as PurchaseState).?description |? "none",
|
2022-05-12 11:43:42 +00:00
|
|
|
"error": purchase.error.?msg,
|
|
|
|
"request": purchase.request,
|
2022-05-11 08:51:59 +00:00
|
|
|
}
|