2022-03-30 15:28:56 +00:00
|
|
|
import std/times
|
2022-03-30 10:51:28 +00:00
|
|
|
import std/sequtils
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/upraises
|
|
|
|
import pkg/stint
|
|
|
|
import pkg/nimcrypto
|
|
|
|
import ./market
|
|
|
|
|
|
|
|
export stint
|
|
|
|
|
2022-03-30 15:28:56 +00:00
|
|
|
const DefaultOfferExpiryInterval = (10 * 60).u256
|
|
|
|
|
2022-03-30 10:51:28 +00:00
|
|
|
type
|
|
|
|
Sales* = ref object
|
|
|
|
market: Market
|
|
|
|
subscription: ?Subscription
|
2022-03-31 08:24:06 +00:00
|
|
|
available*: seq[Availability]
|
2022-03-30 15:28:56 +00:00
|
|
|
offerExpiryInterval*: UInt256
|
2022-03-31 08:24:06 +00:00
|
|
|
onSale*: OnSale
|
2022-03-30 10:51:28 +00:00
|
|
|
Availability* = object
|
|
|
|
id*: array[32, byte]
|
|
|
|
size*: uint64
|
|
|
|
duration*: uint64
|
|
|
|
minPrice*: UInt256
|
2022-03-31 08:24:06 +00:00
|
|
|
OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].}
|
2022-03-30 10:51:28 +00:00
|
|
|
|
|
|
|
func new*(_: type Sales, market: Market): Sales =
|
2022-03-30 15:28:56 +00:00
|
|
|
Sales(market: market, offerExpiryInterval: DefaultOfferExpiryInterval)
|
2022-03-30 10:51:28 +00:00
|
|
|
|
2022-03-30 15:07:03 +00:00
|
|
|
proc init*(_: type Availability,
|
2022-03-30 10:51:28 +00:00
|
|
|
size: uint64,
|
|
|
|
duration: uint64,
|
|
|
|
minPrice: UInt256): Availability =
|
|
|
|
var id: array[32, byte]
|
|
|
|
doAssert randomBytes(id) == 32
|
|
|
|
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
|
|
|
|
|
|
|
|
func add*(sales: Sales, availability: Availability) =
|
|
|
|
sales.available.add(availability)
|
|
|
|
|
|
|
|
func remove*(sales: Sales, availability: Availability) =
|
|
|
|
sales.available.keepItIf(it != availability)
|
|
|
|
|
|
|
|
func findAvailability(sales: Sales, request: StorageRequest): ?Availability =
|
|
|
|
for availability in sales.available:
|
|
|
|
if request.size <= availability.size.u256 and
|
|
|
|
request.duration <= availability.duration.u256 and
|
|
|
|
request.maxPrice >= availability.minPrice:
|
|
|
|
return some availability
|
|
|
|
|
2022-03-30 15:28:56 +00:00
|
|
|
proc createOffer(sales: Sales,
|
2022-03-30 10:51:28 +00:00
|
|
|
request: StorageRequest,
|
|
|
|
availability: Availability): StorageOffer =
|
|
|
|
StorageOffer(
|
|
|
|
requestId: request.id,
|
2022-03-30 15:28:56 +00:00
|
|
|
price: request.maxPrice,
|
|
|
|
expiry: getTime().toUnix().u256 + sales.offerExpiryInterval
|
2022-03-30 10:51:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc handleRequest(sales: Sales, request: StorageRequest) {.async.} =
|
2022-03-31 08:24:06 +00:00
|
|
|
without availability =? sales.findAvailability(request):
|
|
|
|
return
|
|
|
|
|
|
|
|
sales.remove(availability)
|
|
|
|
|
2022-03-31 09:07:39 +00:00
|
|
|
var offer = sales.createOffer(request, availability)
|
|
|
|
offer = await sales.market.offerStorage(offer)
|
2022-03-31 08:24:06 +00:00
|
|
|
|
|
|
|
var subscription: ?Subscription
|
|
|
|
proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} =
|
|
|
|
if subscription =? subscription:
|
|
|
|
asyncSpawn subscription.unsubscribe()
|
|
|
|
sales.onSale(offer)
|
|
|
|
subscription = some await sales.market.subscribeSelection(request.id, onSelect)
|
2022-03-30 10:51:28 +00:00
|
|
|
|
|
|
|
proc start*(sales: Sales) =
|
|
|
|
doAssert sales.subscription.isNone, "Sales already started"
|
|
|
|
|
|
|
|
proc onRequest(request: StorageRequest) {.gcsafe, upraises:[].} =
|
|
|
|
asyncSpawn sales.handleRequest(request)
|
|
|
|
|
|
|
|
proc subscribe {.async.} =
|
|
|
|
sales.subscription = some await sales.market.subscribeRequests(onRequest)
|
|
|
|
|
|
|
|
asyncSpawn subscribe()
|
|
|
|
|
|
|
|
proc stop*(sales: Sales) =
|
|
|
|
if subscription =? sales.subscription:
|
|
|
|
asyncSpawn subscription.unsubscribe()
|
|
|
|
sales.subscription = Subscription.none
|