[sales] Add REST API for sales availability

This commit is contained in:
Mark Spanbroek 2022-04-21 10:12:16 +02:00 committed by markspanbroek
parent d3dbbc75fa
commit e2959d4fe5
1 changed files with 35 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import pkg/libp2p/routing_record
import ../node import ../node
import ../blocktype import ../blocktype
import ../conf import ../conf
import ../contracts
proc validate( proc validate(
pattern: string, pattern: string,
@ -85,6 +86,12 @@ proc decodeString(T: type bool, value: string): Result[T, cstring] =
proc encodeString(value: bool): Result[string, cstring] = proc encodeString(value: bool): Result[string, cstring] =
ok($value) ok($value)
proc decodeString(_: type UInt256, value: string): Result[UInt256, cstring] =
try:
ok UInt256.fromHex(value)
except ValueError as e:
err e.msg.cstring
proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter = proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
var router = RestRouter.init(validate) var router = RestRouter.init(validate)
router.api( router.api(
@ -323,4 +330,32 @@ proc initRestApi*(node: DaggerNodeRef, conf: DaggerConf): RestRouter =
"\nAddrs: \n" & addrs & "\nAddrs: \n" & addrs &
"\nRoot Dir: " & $conf.dataDir) "\nRoot Dir: " & $conf.dataDir)
router.api(
MethodPost,
"/api/dagger/v1/sales/availability") do (
size: Option[uint64],
duration: Option[uint64],
minPrice: Option[UInt256]) -> RestApiResponse:
## Add available storage to sell
##
## size - size of available storage in bytes
## duration - maximum time the storage should be sold for (in seconds)
## minPrice - minimum price to be paid (in amount of tokens)
without size =? size.?get():
return RestApiResponse.error(Http400, "Missing or incorrect size")
without duration =? duration.?get():
return RestApiResponse.error(Http400, "Missing or incorrect duration")
without minPrice =? minPrice.?get():
return RestApiResponse.error(Http400, "Missing or incorrect minPrice")
without contracts =? node.contracts:
return RestApiResponse.error(Http503, "Sales unavailable")
let availability = Availability.init(size, duration, minPrice)
contracts.sales.add(availability)
return RestApiResponse.response(availability.id.toHex)
return router return router