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