From 71f25d40c79157a368c2cc4d981cc3a440b761de Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 31 Mar 2022 11:07:39 +0200 Subject: [PATCH] [market] offerStorage() returns submitted offer --- dagger/contracts/market.nim | 5 ++- dagger/market.nim | 4 ++- dagger/sales.nim | 4 +-- tests/contracts/testMarket.nim | 47 ++++++++++++----------------- tests/dagger/helpers/mockmarket.nim | 5 ++- tests/dagger/testpurchasing.nim | 12 ++++---- 6 files changed, 39 insertions(+), 38 deletions(-) diff --git a/dagger/contracts/market.nim b/dagger/contracts/market.nim index 4edf105f..7b95b014 100644 --- a/dagger/contracts/market.nim +++ b/dagger/contracts/market.nim @@ -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) diff --git a/dagger/market.nim b/dagger/market.nim index 285fdd56..afa6225a 100644 --- a/dagger/market.nim +++ b/dagger/market.nim @@ -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.} = diff --git a/dagger/sales.nim b/dagger/sales.nim index dc3002f0..b06b7421 100644 --- a/dagger/sales.nim +++ b/dagger/sales.nim @@ -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:[].} = diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 69eeafde..13fe763b 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -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]) = diff --git a/tests/dagger/helpers/mockmarket.nim b/tests/dagger/helpers/mockmarket.nim index 58fada00..de1f551b 100644 --- a/tests/dagger/helpers/mockmarket.nim +++ b/tests/dagger/helpers/mockmarket.nim @@ -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: diff --git a/tests/dagger/testpurchasing.nim b/tests/dagger/testpurchasing.nim index efbffa7b..775825bd 100644 --- a/tests/dagger/testpurchasing.nim +++ b/tests/dagger/testpurchasing.nim @@ -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