nim-codex/codex/purchasing.nim

57 lines
1.6 KiB
Nim
Raw Normal View History

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
import ./clock
import ./purchasing/purchase
2022-03-23 12:57:48 +00:00
export questionable
export market
export purchase
2022-03-23 12:57:48 +00:00
type
Purchasing* = ref object
market: Market
clock: Clock
purchases: Table[PurchaseId, Purchase]
2022-03-23 12:57:48 +00:00
proofProbability*: UInt256
requestExpiryInterval*: UInt256
PurchaseTimeout* = Timeout
2022-03-23 12:57:48 +00:00
const DefaultProofProbability = 100.u256
const DefaultRequestExpiryInterval = (10 * 60).u256
proc new*(_: type Purchasing, market: Market, clock: Clock): Purchasing =
2022-03-23 12:57:48 +00:00
Purchasing(
market: market,
clock: clock,
2022-03-23 12:57:48 +00:00
proofProbability: DefaultProofProbability,
requestExpiryInterval: DefaultRequestExpiryInterval,
2022-03-23 12:57:48 +00:00
)
proc populate*(purchasing: Purchasing, request: StorageRequest): StorageRequest =
result = request
if result.ask.proofProbability == 0.u256:
result.ask.proofProbability = purchasing.proofProbability
if result.expiry == 0.u256:
result.expiry = (purchasing.clock.now().u256 + purchasing.requestExpiryInterval)
if result.nonce == Nonce.default:
var id = result.nonce.toArray
doAssert randomBytes(id) == 32
result.nonce = Nonce(id)
proc purchase*(purchasing: Purchasing, request: StorageRequest): Purchase =
let request = purchasing.populate(request)
let purchase = newPurchase(request, purchasing.market, purchasing.clock)
purchase.start()
purchasing.purchases[purchase.id] = purchase
purchase
func getPurchase*(purchasing: Purchasing, id: PurchaseId): ?Purchase =
if purchasing.purchases.hasKey(id):
some purchasing.purchases[id]
else:
none Purchase