[market] subscribe to offers for a certain request

This commit is contained in:
Mark Spanbroek 2022-03-29 10:53:05 +02:00 committed by markspanbroek
parent 0cee79b1a2
commit 5ea64522d8
2 changed files with 28 additions and 1 deletions

View File

@ -43,7 +43,8 @@ method subscribeOffers(market: OnChainMarket,
callback: OnOffer): callback: OnOffer):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: StorageOffered) {.upraises:[].} = proc onEvent(event: StorageOffered) {.upraises:[].} =
callback(event.offer) if event.requestId == requestId:
callback(event.offer)
let subscription = await market.contract.subscribe(StorageOffered, onEvent) let subscription = await market.contract.subscribe(StorageOffered, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) return OnChainMarketSubscription(eventSubscription: subscription)

View File

@ -93,3 +93,29 @@ ethersuite "On-Chain Market":
await market.offerStorage(offerWithoutHost) await market.offerStorage(offerWithoutHost)
check submitted.host == accounts[0] 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()