nim-codex/codex/sales.nim

162 lines
4.7 KiB
Nim
Raw Normal View History

2022-03-30 10:51:28 +00:00
import std/sequtils
import pkg/questionable
import pkg/upraises
import pkg/stint
import pkg/nimcrypto
import pkg/chronicles
2022-03-30 10:51:28 +00:00
import ./market
2022-05-17 15:02:03 +00:00
import ./clock
2022-03-30 10:51:28 +00:00
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
2022-05-17 15:02:03 +00:00
clock: Clock
2022-03-30 10:51:28 +00:00
subscription: ?Subscription
available*: seq[Availability]
2022-03-30 15:28:56 +00:00
offerExpiryInterval*: UInt256
2022-03-31 09:41:45 +00:00
onSale: ?OnSale
2022-03-30 10:51:28 +00:00
Availability* = object
id*: array[32, byte]
size*: UInt256
duration*: UInt256
2022-03-30 10:51:28 +00:00
minPrice*: UInt256
2022-03-31 12:35:53 +00:00
Negotiation = ref object
sales: Sales
requestId: array[32, byte]
ask: StorageAsk
2022-03-31 12:35:53 +00:00
availability: Availability
offer: ?StorageOffer
subscription: ?Subscription
waiting: ?Future[void]
finished: bool
OnSale = proc(offer: StorageOffer) {.gcsafe, upraises: [].}
2022-03-30 10:51:28 +00:00
2022-05-17 15:02:03 +00:00
func new*(_: type Sales, market: Market, clock: Clock): Sales =
Sales(
market: market,
clock: clock,
offerExpiryInterval: DefaultOfferExpiryInterval
)
2022-03-30 10:51:28 +00:00
proc init*(_: type Availability,
size: UInt256,
duration: UInt256,
2022-03-30 10:51:28 +00:00
minPrice: UInt256): Availability =
var id: array[32, byte]
doAssert randomBytes(id) == 32
Availability(id: id, size: size, duration: duration, minPrice: minPrice)
2022-03-31 09:41:45 +00:00
proc `onSale=`*(sales: Sales, callback: OnSale) =
sales.onSale = some callback
2022-03-30 10:51:28 +00:00
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, ask: StorageAsk): ?Availability =
2022-03-30 10:51:28 +00:00
for availability in sales.available:
if ask.size <= availability.size and
ask.duration <= availability.duration and
ask.maxPrice >= availability.minPrice:
2022-03-30 10:51:28 +00:00
return some availability
2022-03-31 12:35:53 +00:00
proc createOffer(negotiation: Negotiation): StorageOffer =
2022-05-17 15:02:03 +00:00
let sales = negotiation.sales
2022-03-30 10:51:28 +00:00
StorageOffer(
requestId: negotiation.requestId,
price: negotiation.ask.maxPrice,
2022-05-17 15:02:03 +00:00
expiry: sales.clock.now().u256 + sales.offerExpiryInterval
2022-03-30 10:51:28 +00:00
)
2022-03-31 12:35:53 +00:00
proc sendOffer(negotiation: Negotiation) {.async.} =
let offer = negotiation.createOffer()
negotiation.offer = some await negotiation.sales.market.offerStorage(offer)
proc finish(negotiation: Negotiation, success: bool) =
if negotiation.finished:
return
negotiation.finished = true
2022-03-31 12:35:53 +00:00
if subscription =? negotiation.subscription:
asyncSpawn subscription.unsubscribe()
if waiting =? negotiation.waiting:
waiting.cancel()
if success and offer =? negotiation.offer:
2022-03-31 12:35:53 +00:00
if onSale =? negotiation.sales.onSale:
onSale(offer)
else:
negotiation.sales.add(negotiation.availability)
proc onSelect(negotiation: Negotiation, offerId: array[32, byte]) =
if offer =? negotiation.offer and offer.id == offerId:
negotiation.finish(success = true)
else:
negotiation.finish(success = false)
2022-03-31 12:35:53 +00:00
proc subscribeSelect(negotiation: Negotiation) {.async.} =
without offer =? negotiation.offer:
return
2022-03-31 12:35:53 +00:00
proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} =
negotiation.onSelect(offerId)
let market = negotiation.sales.market
let subscription = await market.subscribeSelection(offer.requestId, onSelect)
negotiation.subscription = some subscription
2022-03-31 12:35:53 +00:00
proc waitForExpiry(negotiation: Negotiation) {.async.} =
without offer =? negotiation.offer:
return
await negotiation.sales.clock.waitUntil(offer.expiry.truncate(int64))
negotiation.finish(success = false)
2022-03-31 12:35:53 +00:00
proc start(negotiation: Negotiation) {.async.} =
try:
let sales = negotiation.sales
let availability = negotiation.availability
sales.remove(availability)
await negotiation.sendOffer()
await negotiation.subscribeSelect()
negotiation.waiting = some negotiation.waitForExpiry()
except CatchableError as e:
error "Negotiation failed", msg = e.msg
proc handleRequest(sales: Sales, requestId: array[32, byte], ask: StorageAsk) =
without availability =? sales.findAvailability(ask):
2022-03-31 12:35:53 +00:00
return
2022-03-31 12:35:53 +00:00
let negotiation = Negotiation(
sales: sales,
requestId: requestId,
ask: ask,
2022-03-31 12:35:53 +00:00
availability: availability
)
asyncSpawn negotiation.start()
2022-03-30 10:51:28 +00:00
proc start*(sales: Sales) {.async.} =
2022-03-30 10:51:28 +00:00
doAssert sales.subscription.isNone, "Sales already started"
proc onRequest(requestId: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} =
sales.handleRequest(requestId, ask)
2022-03-30 10:51:28 +00:00
try:
sales.subscription = some await sales.market.subscribeRequests(onRequest)
except CatchableError as e:
error "Unable to start sales", msg = e.msg
2022-03-30 10:51:28 +00:00
proc stop*(sales: Sales) {.async.} =
2022-03-30 10:51:28 +00:00
if subscription =? sales.subscription:
sales.subscription = Subscription.none
try:
await subscription.unsubscribe()
except CatchableError as e:
warn "Unsubscribe failed", msg = e.msg