2022-05-11 07:14:43 +00:00
|
|
|
import std/tables
|
2022-03-23 12:57:48 +00:00
|
|
|
import pkg/stint
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/questionable
|
|
|
|
import pkg/nimcrypto
|
|
|
|
import ./market
|
2022-05-17 14:51:29 +00:00
|
|
|
import ./clock
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
export questionable
|
2022-03-24 09:00:40 +00:00
|
|
|
export market
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
Purchasing* = ref object
|
|
|
|
market: Market
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: Clock
|
2022-05-11 07:14:43 +00:00
|
|
|
purchases: Table[array[32, byte], Purchase]
|
2022-03-23 12:57:48 +00:00
|
|
|
proofProbability*: UInt256
|
|
|
|
requestExpiryInterval*: UInt256
|
2022-03-28 13:16:39 +00:00
|
|
|
offerExpiryMargin*: UInt256
|
2022-03-23 12:57:48 +00:00
|
|
|
Purchase* = ref object
|
2022-03-24 13:22:45 +00:00
|
|
|
future: Future[void]
|
|
|
|
market: Market
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: Clock
|
2022-03-28 13:16:39 +00:00
|
|
|
offerExpiryMargin: UInt256
|
2022-03-24 13:22:45 +00:00
|
|
|
request*: StorageRequest
|
|
|
|
offers*: seq[StorageOffer]
|
|
|
|
selected*: ?StorageOffer
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
const DefaultProofProbability = 100.u256
|
|
|
|
const DefaultRequestExpiryInterval = (10 * 60).u256
|
2022-03-28 13:16:39 +00:00
|
|
|
const DefaultOfferExpiryMargin = (8 * 60).u256
|
2022-03-23 12:57:48 +00:00
|
|
|
|
2022-03-24 13:22:45 +00:00
|
|
|
proc start(purchase: Purchase) {.gcsafe.}
|
2022-05-11 07:14:43 +00:00
|
|
|
func id*(purchase: Purchase): array[32, byte]
|
2022-03-24 13:22:45 +00:00
|
|
|
|
2022-05-17 14:51:29 +00:00
|
|
|
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
|
2022-03-23 12:57:48 +00:00
|
|
|
Purchasing(
|
|
|
|
market: market,
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: clock,
|
2022-03-23 12:57:48 +00:00
|
|
|
proofProbability: DefaultProofProbability,
|
2022-03-28 13:16:39 +00:00
|
|
|
requestExpiryInterval: DefaultRequestExpiryInterval,
|
|
|
|
offerExpiryMargin: DefaultOfferExpiryMargin
|
2022-03-23 12:57:48 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 13:22:45 +00:00
|
|
|
proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest =
|
|
|
|
result = request
|
2022-04-11 18:03:55 +00:00
|
|
|
if result.ask.proofProbability == 0.u256:
|
|
|
|
result.ask.proofProbability = purchasing.proofProbability
|
2022-03-24 13:22:45 +00:00
|
|
|
if result.expiry == 0.u256:
|
2022-05-17 14:51:29 +00:00
|
|
|
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
|
2022-03-24 13:22:45 +00:00
|
|
|
if result.nonce == array[32, byte].default:
|
|
|
|
doAssert randomBytes(result.nonce) == 32
|
|
|
|
|
2022-03-24 09:00:40 +00:00
|
|
|
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
|
2022-03-24 13:22:45 +00:00
|
|
|
let request = purchasing.populate(request)
|
2022-03-28 13:16:39 +00:00
|
|
|
let purchase = Purchase(
|
|
|
|
request: request,
|
|
|
|
market: purchasing.market,
|
2022-05-17 14:51:29 +00:00
|
|
|
clock: purchasing.clock,
|
2022-03-28 13:16:39 +00:00
|
|
|
offerExpiryMargin: purchasing.offerExpiryMargin
|
|
|
|
)
|
2022-03-24 13:22:45 +00:00
|
|
|
purchase.start()
|
2022-05-11 07:14:43 +00:00
|
|
|
purchasing.purchases[purchase.id] = purchase
|
2022-03-24 13:22:45 +00:00
|
|
|
purchase
|
|
|
|
|
2022-05-11 07:14:43 +00:00
|
|
|
func getPurchase*(purchasing: Purchasing, id: array[32, byte]): ?Purchase =
|
|
|
|
if purchasing.purchases.hasKey(id):
|
|
|
|
some purchasing.purchases[id]
|
|
|
|
else:
|
|
|
|
none Purchase
|
|
|
|
|
2022-03-28 10:28:22 +00:00
|
|
|
proc selectOffer(purchase: Purchase) {.async.} =
|
|
|
|
var cheapest: ?StorageOffer
|
|
|
|
for offer in purchase.offers:
|
2022-05-17 14:51:29 +00:00
|
|
|
without purchase.clock.now().u256 < offer.expiry - purchase.offerExpiryMargin:
|
2022-03-28 12:40:41 +00:00
|
|
|
continue
|
|
|
|
without current =? cheapest:
|
|
|
|
cheapest = some offer
|
|
|
|
continue
|
|
|
|
if current.price > offer.price:
|
2022-03-28 10:28:22 +00:00
|
|
|
cheapest = some offer
|
|
|
|
if cheapest =? cheapest:
|
|
|
|
await purchase.market.selectOffer(cheapest.id)
|
|
|
|
|
2022-03-24 13:22:45 +00:00
|
|
|
proc run(purchase: Purchase) {.async.} =
|
2022-03-28 10:28:22 +00:00
|
|
|
proc onOffer(offer: StorageOffer) =
|
|
|
|
purchase.offers.add(offer)
|
|
|
|
let market = purchase.market
|
2022-03-31 08:46:03 +00:00
|
|
|
purchase.request = await market.requestStorage(purchase.request)
|
|
|
|
let subscription = await market.subscribeOffers(purchase.request.id, onOffer)
|
|
|
|
await market.waitUntil(purchase.request.expiry)
|
2022-03-28 10:28:22 +00:00
|
|
|
await purchase.selectOffer()
|
|
|
|
await subscription.unsubscribe()
|
2022-03-24 13:22:45 +00:00
|
|
|
|
|
|
|
proc start(purchase: Purchase) =
|
|
|
|
purchase.future = purchase.run()
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
proc wait*(purchase: Purchase) {.async.} =
|
2022-03-24 13:22:45 +00:00
|
|
|
await purchase.future
|
2022-05-11 07:01:31 +00:00
|
|
|
|
|
|
|
func id*(purchase: Purchase): array[32, byte] =
|
|
|
|
purchase.request.id
|
2022-05-12 11:43:42 +00:00
|
|
|
|
|
|
|
func finished*(purchase: Purchase): bool =
|
|
|
|
purchase.future.finished
|
|
|
|
|
|
|
|
func error*(purchase: Purchase): ?(ref CatchableError) =
|
|
|
|
if purchase.future.failed:
|
|
|
|
some purchase.future.error
|
|
|
|
else:
|
|
|
|
none (ref CatchableError)
|