From 5ea64522d8bb79bcc01a0ddedf10dcc46ec69d80 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 29 Mar 2022 10:53:05 +0200 Subject: [PATCH] [market] subscribe to offers for a certain request --- dagger/contracts/market.nim | 3 ++- tests/contracts/testMarket.nim | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/dagger/contracts/market.nim b/dagger/contracts/market.nim index 008d2726..770e5687 100644 --- a/dagger/contracts/market.nim +++ b/dagger/contracts/market.nim @@ -43,7 +43,8 @@ method subscribeOffers(market: OnChainMarket, callback: OnOffer): Future[MarketSubscription] {.async.} = proc onEvent(event: StorageOffered) {.upraises:[].} = - callback(event.offer) + if event.requestId == requestId: + callback(event.offer) let subscription = await market.contract.subscribe(StorageOffered, onEvent) return OnChainMarketSubscription(eventSubscription: subscription) diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 6c86ca9b..4cee3e67 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -93,3 +93,29 @@ ethersuite "On-Chain Market": await market.offerStorage(offerWithoutHost) check submitted.host == accounts[0] + + test "subscribes only to offers for a certain request": + var otherRequest = StorageRequest.example + var otherOffer = StorageOffer.example + otherRequest.client = accounts[0] + otherOffer.host = accounts[0] + otherOffer.requestId = otherRequest.id + otherOffer.price = otherRequest.maxPrice + + await token.approve(storage.address, request.maxPrice) + await market.requestStorage(request) + await token.approve(storage.address, otherRequest.maxPrice) + await market.requestStorage(otherRequest) + + var submitted: seq[StorageOffer] + proc onOffer(offer: StorageOffer) = + submitted.add(offer) + + let subscription = await market.subscribeOffers(request.id, onOffer) + + await market.offerStorage(offer) + await market.offerStorage(otherOffer) + + check submitted == @[offer] + + await subscription.unsubscribe()