2022-03-30 15:28:56 +00:00
|
|
|
import std/times
|
2022-03-30 10:51:28 +00:00
|
|
|
import pkg/asynctest
|
|
|
|
import pkg/chronos
|
|
|
|
import pkg/dagger/sales
|
|
|
|
import ./helpers/mockmarket
|
|
|
|
import ./examples
|
|
|
|
|
|
|
|
suite "Sales":
|
|
|
|
|
2022-03-31 09:34:33 +00:00
|
|
|
let availability = Availability.init(size=100, duration=60, minPrice=42.u256)
|
|
|
|
let request = StorageRequest(duration: 60.u256, size: 100.u256, maxPrice:42.u256)
|
|
|
|
|
2022-03-30 10:51:28 +00:00
|
|
|
var sales: Sales
|
|
|
|
var market: MockMarket
|
|
|
|
|
|
|
|
setup:
|
|
|
|
market = MockMarket.new()
|
|
|
|
sales = Sales.new(market)
|
2022-03-31 09:34:33 +00:00
|
|
|
sales.start()
|
|
|
|
|
|
|
|
teardown:
|
|
|
|
sales.stop()
|
2022-03-30 10:51:28 +00:00
|
|
|
|
|
|
|
test "has no availability initially":
|
|
|
|
check sales.available.len == 0
|
|
|
|
|
|
|
|
test "can add available storage":
|
|
|
|
let availability1 = Availability.example
|
|
|
|
let availability2 = Availability.example
|
|
|
|
sales.add(availability1)
|
|
|
|
check sales.available.contains(availability1)
|
|
|
|
sales.add(availability2)
|
|
|
|
check sales.available.contains(availability1)
|
|
|
|
check sales.available.contains(availability2)
|
|
|
|
|
|
|
|
test "can remove available storage":
|
|
|
|
sales.add(availability)
|
|
|
|
sales.remove(availability)
|
|
|
|
check sales.available.len == 0
|
|
|
|
|
|
|
|
test "generates unique ids for storage availability":
|
2022-03-30 15:07:03 +00:00
|
|
|
let availability1 = Availability.init(size=1, duration=2, minPrice=3.u256)
|
|
|
|
let availability2 = Availability.init(size=1, duration=2, minPrice=3.u256)
|
2022-03-30 10:51:28 +00:00
|
|
|
check availability1.id != availability2.id
|
|
|
|
|
|
|
|
test "offers available storage when matching request comes in":
|
|
|
|
sales.add(availability)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-30 10:51:28 +00:00
|
|
|
check market.offered.len == 1
|
|
|
|
check market.offered[0].price == 42.u256
|
|
|
|
|
|
|
|
test "ignores request when no matching storage is available":
|
|
|
|
sales.add(availability)
|
2022-03-31 09:34:33 +00:00
|
|
|
var tooBig = request
|
|
|
|
tooBig.size = request.size + 1
|
|
|
|
discard await market.requestStorage(tooBig)
|
2022-03-30 10:51:28 +00:00
|
|
|
check market.offered.len == 0
|
|
|
|
|
|
|
|
test "makes storage unavailable when offer is submitted":
|
|
|
|
sales.add(availability)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-30 10:51:28 +00:00
|
|
|
check sales.available.len == 0
|
2022-03-30 15:28:56 +00:00
|
|
|
|
|
|
|
test "sets expiry time of offer":
|
|
|
|
sales.add(availability)
|
|
|
|
let now = getTime().toUnix().u256
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-30 15:28:56 +00:00
|
|
|
check market.offered[0].expiry == now + sales.offerExpiryInterval
|
2022-03-31 08:24:06 +00:00
|
|
|
|
2022-03-31 09:34:33 +00:00
|
|
|
test "calls onSale when offer is selected":
|
|
|
|
var sold: StorageOffer
|
2022-03-31 08:24:06 +00:00
|
|
|
sales.onSale = proc(offer: StorageOffer) =
|
2022-03-31 09:34:33 +00:00
|
|
|
sold = offer
|
|
|
|
sales.add(availability)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-31 08:24:06 +00:00
|
|
|
let offer = market.offered[0]
|
|
|
|
await market.selectOffer(offer.id)
|
2022-03-31 09:34:33 +00:00
|
|
|
check sold == offer
|
2022-03-31 09:20:18 +00:00
|
|
|
|
|
|
|
test "does not call onSale when a different offer is selected":
|
2022-03-31 09:34:33 +00:00
|
|
|
var didSell: bool
|
2022-03-31 09:20:18 +00:00
|
|
|
sales.onSale = proc(offer: StorageOffer) =
|
2022-03-31 09:34:33 +00:00
|
|
|
didSell = true
|
|
|
|
sales.add(availability)
|
|
|
|
let request = await market.requestStorage(request)
|
2022-03-31 09:20:18 +00:00
|
|
|
var otherOffer = StorageOffer(requestId: request.id, price: 1.u256)
|
|
|
|
otherOffer = await market.offerStorage(otherOffer)
|
|
|
|
await market.selectOffer(otherOffer.id)
|
2022-03-31 09:34:33 +00:00
|
|
|
check not didSell
|