nim-codex/codex/rest/json.nim

57 lines
1.7 KiB
Nim
Raw Permalink Normal View History

import std/json
import std/strutils
import pkg/stew/byteutils
import pkg/questionable/results
import ../sales
import ../purchasing
type
StorageRequestParams* = object
duration*: UInt256
2022-07-20 12:11:00 +00:00
reward*: UInt256
expiry*: ?UInt256
nodes*: ?uint
tolerance*: ?uint
proc fromJson*(_: type Availability, bytes: seq[byte]): ?!Availability =
let json = ?catch parseJson(string.fromBytes(bytes))
let size = ?catch UInt256.fromHex(json["size"].getStr)
let duration = ?catch UInt256.fromHex(json["duration"].getStr)
let minPrice = ?catch UInt256.fromHex(json["minPrice"].getStr)
success Availability.init(size, duration, minPrice)
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)
let expiry = UInt256.fromHex(json["expiry"].getStr).catch.option
let nodes = strutils.fromHex[uint](json["nodes"].getStr).catch.option
let tolerance = strutils.fromHex[uint](json["tolerance"].getStr).catch.option
success StorageRequestParams(
duration: duration,
2022-07-20 12:11:00 +00:00
reward: reward,
expiry: expiry,
nodes: nodes,
tolerance: tolerance
)
func `%`*(address: Address): JsonNode =
% $address
func `%`*(stint: StInt|StUInt): JsonNode =
%("0x" & stint.toHex)
func `%`*(arr: openArray[byte]): JsonNode =
%("0x" & arr.toHex)
func `%`*(id: RequestId | SlotId | Nonce): JsonNode =
% id.toArray
func `%`*(purchase: Purchase): JsonNode =
%*{
"finished": purchase.finished,
"error": purchase.error.?msg,
"request": purchase.request,
}