[sales] Set offer expiry timestamp

This commit is contained in:
Mark Spanbroek 2022-03-30 17:28:56 +02:00 committed by markspanbroek
parent 76be728f61
commit 71baedb5ec
2 changed files with 19 additions and 4 deletions

View File

@ -1,3 +1,4 @@
import std/times
import std/sequtils
import pkg/questionable
import pkg/upraises
@ -7,12 +8,14 @@ import ./market
export stint
const DefaultOfferExpiryInterval = (10 * 60).u256
type
Sales* = ref object
market: Market
available*: seq[Availability]
subscription: ?Subscription
offerExpiryInterval*: UInt256
Availability* = object
id*: array[32, byte]
size*: uint64
@ -20,7 +23,7 @@ type
minPrice*: UInt256
func new*(_: type Sales, market: Market): Sales =
Sales(market: market)
Sales(market: market, offerExpiryInterval: DefaultOfferExpiryInterval)
proc init*(_: type Availability,
size: uint64,
@ -43,12 +46,13 @@ func findAvailability(sales: Sales, request: StorageRequest): ?Availability =
request.maxPrice >= availability.minPrice:
return some availability
func createOffer(sales: Sales,
proc createOffer(sales: Sales,
request: StorageRequest,
availability: Availability): StorageOffer =
StorageOffer(
requestId: request.id,
price: request.maxPrice
price: request.maxPrice,
expiry: getTime().toUnix().u256 + sales.offerExpiryInterval
)
proc handleRequest(sales: Sales, request: StorageRequest) {.async.} =

View File

@ -1,3 +1,4 @@
import std/times
import pkg/asynctest
import pkg/chronos
import pkg/dagger/sales
@ -63,3 +64,13 @@ suite "Sales":
await market.requestStorage(request)
check sales.available.len == 0
sales.stop()
test "sets expiry time of offer":
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
sales.add(availability)
sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
let now = getTime().toUnix().u256
await market.requestStorage(request)
check market.offered[0].expiry == now + sales.offerExpiryInterval
sales.stop()