[market] offerStorage() returns submitted offer

This commit is contained in:
Mark Spanbroek 2022-03-31 11:07:39 +02:00 committed by markspanbroek
parent 94e34e7d28
commit 71f25d40c7
6 changed files with 39 additions and 38 deletions

View File

@ -35,10 +35,13 @@ method requestStorage(market: OnChainMarket,
await market.contract.requestStorage(request)
return request
method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} =
method offerStorage(market: OnChainMarket,
offer: StorageOffer):
Future[StorageOffer] {.async.} =
var offer = offer
offer.host = await market.signer.getAddress()
await market.contract.offerStorage(offer)
return offer
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
await market.contract.selectOffer(offerId)

View File

@ -19,7 +19,9 @@ method requestStorage*(market: Market,
Future[StorageRequest] {.base, async.} =
raiseAssert("not implemented")
method offerStorage*(market: Market, offer: StorageOffer) {.base, async.} =
method offerStorage*(market: Market,
offer: StorageOffer):
Future[StorageOffer] {.base, async.} =
raiseAssert("not implemented")
method selectOffer*(market: Market, id: array[32, byte]) {.base, async.} =

View File

@ -63,8 +63,8 @@ proc handleRequest(sales: Sales, request: StorageRequest) {.async.} =
sales.remove(availability)
let offer = sales.createOffer(request, availability)
await sales.market.offerStorage(offer)
var offer = sales.createOffer(request, availability)
offer = await sales.market.offerStorage(offer)
var subscription: ?Subscription
proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} =

View File

@ -61,34 +61,27 @@ ethersuite "On-Chain Market":
test "supports storage offers":
await token.approve(storage.address, request.maxPrice)
discard await market.requestStorage(request)
var submitted: seq[StorageOffer]
proc onOffer(offer: StorageOffer) =
submitted.add(offer)
let subscription = await market.subscribeOffers(request.id, onOffer)
await market.offerStorage(offer)
check submitted == @[offer]
await subscription.unsubscribe()
check (await market.offerStorage(offer)) == offer
test "sets host address when submitting storage offer":
var offerWithoutHost = offer
offerWithoutHost.host = Address.default
await token.approve(storage.address, request.maxPrice)
discard await market.requestStorage(request)
var submitted: StorageOffer
proc onOffer(offer: StorageOffer) =
submitted = offer
let subscription = await market.subscribeOffers(request.id, onOffer)
await market.offerStorage(offerWithoutHost)
var offerWithoutHost = offer
offerWithoutHost.host = Address.default
let submitted = await market.offerStorage(offerWithoutHost)
check submitted.host == accounts[0]
test "supports offer subscriptions":
await token.approve(storage.address, request.maxPrice)
discard await market.requestStorage(request)
var received: seq[StorageOffer]
proc onOffer(offer: StorageOffer) =
received.add(offer)
let subscription = await market.subscribeOffers(request.id, onOffer)
discard await market.offerStorage(offer)
check received == @[offer]
await subscription.unsubscribe()
test "subscribes only to offers for a certain request":
var otherRequest = StorageRequest.example
var otherOffer = StorageOffer.example
@ -108,8 +101,8 @@ ethersuite "On-Chain Market":
let subscription = await market.subscribeOffers(request.id, onOffer)
await market.offerStorage(offer)
await market.offerStorage(otherOffer)
discard await market.offerStorage(offer)
discard await market.offerStorage(otherOffer)
check submitted == @[offer]
@ -118,7 +111,7 @@ ethersuite "On-Chain Market":
test "supports selection of an offer":
await token.approve(storage.address, request.maxPrice)
discard await market.requestStorage(request)
await market.offerStorage(offer)
discard await market.offerStorage(offer)
var selected: seq[array[32, byte]]
proc onSelect(offerId: array[32, byte]) =
@ -141,10 +134,10 @@ ethersuite "On-Chain Market":
await token.approve(storage.address, request.maxPrice)
discard await market.requestStorage(request)
await market.offerStorage(offer)
discard await market.offerStorage(offer)
await token.approve(storage.address, otherRequest.maxPrice)
discard await market.requestStorage(otherRequest)
await market.offerStorage(otherOffer)
discard await market.offerStorage(otherOffer)
var selected: seq[array[32, byte]]
proc onSelect(offerId: array[32, byte]) =

View File

@ -41,12 +41,15 @@ method requestStorage*(market: MockMarket,
subscription.callback(request)
return request
method offerStorage*(market: MockMarket, offer: StorageOffer) {.async.} =
method offerStorage*(market: MockMarket,
offer: StorageOffer):
Future[StorageOffer] {.async.} =
market.offered.add(offer)
let subscriptions = market.subscriptions.onOffer
for subscription in subscriptions:
if subscription.requestId == offer.requestId:
subscription.callback(offer)
return offer
proc findOffer(market: MockMarket, id: array[32, byte]): ?StorageOffer =
for offer in market.offered:

View File

@ -79,8 +79,8 @@ suite "Purchasing":
var offer1, offer2 = createOffer(request)
offer1.price = 20.u256
offer2.price = 10.u256
await market.offerStorage(offer1)
await market.offerStorage(offer2)
discard await market.offerStorage(offer1)
discard await market.offerStorage(offer2)
market.advanceTimeTo(request.expiry)
await purchase.wait()
check market.selected[0] == offer2.id
@ -93,8 +93,8 @@ suite "Purchasing":
offer1.price = 20.u256
offer2.price = 10.u256
offer2.expiry = expired
await market.offerStorage(offer1)
await market.offerStorage(offer2)
discard await market.offerStorage(offer1)
discard await market.offerStorage(offer2)
market.advanceTimeTo(request.expiry)
await purchase.wait()
check market.selected[0] == offer1.id
@ -110,8 +110,8 @@ suite "Purchasing":
offer1.price = 20.u256
offer2.price = 10.u256
offer2.expiry = getTime().toUnix().u256 + expiryMargin - 1
await market.offerStorage(offer1)
await market.offerStorage(offer2)
discard await market.offerStorage(offer1)
discard await market.offerStorage(offer2)
market.advanceTimeTo(request.expiry)
await purchase.wait()
check market.selected[0] == offer1.id