From 71baedb5ecd0e7cd42bb6bd4cdfa3e50377eefff Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 30 Mar 2022 17:28:56 +0200 Subject: [PATCH] [sales] Set offer expiry timestamp --- dagger/sales.nim | 12 ++++++++---- tests/dagger/testsales.nim | 11 +++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/dagger/sales.nim b/dagger/sales.nim index d614ea67..864da0be 100644 --- a/dagger/sales.nim +++ b/dagger/sales.nim @@ -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.} = diff --git a/tests/dagger/testsales.nim b/tests/dagger/testsales.nim index 5aa9d329..1b3e1e09 100644 --- a/tests/dagger/testsales.nim +++ b/tests/dagger/testsales.nim @@ -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()