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-09-27 08:28:36 +00:00
|
|
|
import ./purchasing/purchase
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
export questionable
|
2022-11-08 07:10:17 +00:00
|
|
|
export chronos
|
2022-03-24 09:00:40 +00:00
|
|
|
export market
|
2022-09-27 08:28:36 +00:00
|
|
|
export purchase
|
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-08-17 02:29:44 +00:00
|
|
|
purchases: Table[PurchaseId, Purchase]
|
2022-03-23 12:57:48 +00:00
|
|
|
proofProbability*: UInt256
|
|
|
|
requestExpiryInterval*: UInt256
|
2022-06-14 13:25:48 +00:00
|
|
|
PurchaseTimeout* = Timeout
|
2022-03-23 12:57:48 +00:00
|
|
|
|
|
|
|
const DefaultProofProbability = 100.u256
|
|
|
|
const DefaultRequestExpiryInterval = (10 * 60).u256
|
|
|
|
|
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,
|
2022-03-23 12:57:48 +00:00
|
|
|
)
|
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
proc load*(purchasing: Purchasing) {.async.} =
|
|
|
|
let market = purchasing.market
|
|
|
|
let requestIds = await market.myRequests()
|
|
|
|
for requestId in requestIds:
|
|
|
|
let purchase = Purchase.new(requestId, purchasing.market, purchasing.clock)
|
|
|
|
purchase.load()
|
|
|
|
purchasing.purchases[purchase.id] = purchase
|
|
|
|
|
|
|
|
proc start*(purchasing: Purchasing) {.async.} =
|
|
|
|
await purchasing.load()
|
|
|
|
|
|
|
|
proc stop*(purchasing: Purchasing) {.async.} =
|
|
|
|
discard
|
|
|
|
|
|
|
|
proc populate*(purchasing: Purchasing,
|
2023-06-22 15:11:18 +00:00
|
|
|
request: StorageRequest
|
|
|
|
): Future[StorageRequest] {.async.} =
|
2022-03-24 13:22:45 +00:00
|
|
|
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-08-17 02:29:44 +00:00
|
|
|
if result.nonce == Nonce.default:
|
2022-08-18 05:55:53 +00:00
|
|
|
var id = result.nonce.toArray
|
|
|
|
doAssert randomBytes(id) == 32
|
|
|
|
result.nonce = Nonce(id)
|
2022-11-08 07:10:17 +00:00
|
|
|
result.client = await purchasing.market.getSigner()
|
2022-03-24 13:22:45 +00:00
|
|
|
|
2022-11-08 07:10:17 +00:00
|
|
|
proc purchase*(purchasing: Purchasing,
|
2023-06-22 15:11:18 +00:00
|
|
|
request: StorageRequest
|
|
|
|
): Future[Purchase] {.async.} =
|
2022-11-08 07:10:17 +00:00
|
|
|
let request = await purchasing.populate(request)
|
|
|
|
let purchase = Purchase.new(request, purchasing.market, purchasing.clock)
|
2022-03-24 13:22:45 +00:00
|
|
|
purchase.start()
|
2022-05-11 07:14:43 +00:00
|
|
|
purchasing.purchases[purchase.id] = purchase
|
2022-11-08 07:10:17 +00:00
|
|
|
return purchase
|
2022-03-24 13:22:45 +00:00
|
|
|
|
2022-08-17 02:29:44 +00:00
|
|
|
func getPurchase*(purchasing: Purchasing, id: PurchaseId): ?Purchase =
|
2022-05-11 07:14:43 +00:00
|
|
|
if purchasing.purchases.hasKey(id):
|
|
|
|
some purchasing.purchases[id]
|
|
|
|
else:
|
|
|
|
none Purchase
|
2023-11-13 11:30:27 +00:00
|
|
|
|
|
|
|
func getPurchaseIds*(purchasing: Purchasing): seq[PurchaseId] =
|
|
|
|
var pIds: seq[PurchaseId] = @[]
|
|
|
|
for key in purchasing.purchases.keys:
|
|
|
|
pIds.add(key)
|
|
|
|
return pIds
|
|
|
|
|