[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) await market.contract.requestStorage(request)
return request return request
method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} = method offerStorage(market: OnChainMarket,
offer: StorageOffer):
Future[StorageOffer] {.async.} =
var offer = offer var offer = offer
offer.host = await market.signer.getAddress() offer.host = await market.signer.getAddress()
await market.contract.offerStorage(offer) await market.contract.offerStorage(offer)
return offer
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} = method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
await market.contract.selectOffer(offerId) await market.contract.selectOffer(offerId)

View File

@ -19,7 +19,9 @@ method requestStorage*(market: Market,
Future[StorageRequest] {.base, async.} = Future[StorageRequest] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method offerStorage*(market: Market, offer: StorageOffer) {.base, async.} = method offerStorage*(market: Market,
offer: StorageOffer):
Future[StorageOffer] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method selectOffer*(market: Market, id: array[32, byte]) {.base, async.} = 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) sales.remove(availability)
let offer = sales.createOffer(request, availability) var offer = sales.createOffer(request, availability)
await sales.market.offerStorage(offer) offer = await sales.market.offerStorage(offer)
var subscription: ?Subscription var subscription: ?Subscription
proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} = proc onSelect(offerId: array[32, byte]) {.gcsafe, upraises:[].} =

View File

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

View File

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

View File

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