From b4b6fdb7c6660a21674eeed29e139e008a9b57a9 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Thu, 28 Jul 2022 14:33:17 +0200 Subject: [PATCH] [contracts] subscribe to SlotFilled event --- codex/contracts/market.nim | 11 +++++++++++ codex/contracts/storage.nim | 5 +++++ tests/contracts/testMarket.nim | 28 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index d34720ed..a3723c81 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -66,6 +66,17 @@ method subscribeRequests(market: OnChainMarket, let subscription = await market.contract.subscribe(StorageRequested, onEvent) return OnChainMarketSubscription(eventSubscription: subscription) +method subscribeSlotFilled*(market: OnChainMarket, + requestId: array[32, byte], + slotIndex: UInt256, + callback: OnSlotFilled): + Future[MarketSubscription] {.async.} = + proc onEvent(event: SlotFilled) {.upraises:[].} = + if event.requestId == requestId and event.slotIndex == slotIndex: + callback(event.requestId, event.slotIndex) + let subscription = await market.contract.subscribe(SlotFilled, onEvent) + return OnChainMarketSubscription(eventSubscription: subscription) + method subscribeFulfillment(market: OnChainMarket, requestId: array[32, byte], callback: OnFulfillment): diff --git a/codex/contracts/storage.nim b/codex/contracts/storage.nim index 3b48df26..4f03aacf 100644 --- a/codex/contracts/storage.nim +++ b/codex/contracts/storage.nim @@ -13,12 +13,17 @@ type StorageRequested* = object of Event requestId*: Id ask*: StorageAsk + SlotFilled* = object of Event + requestId* {.indexed.}: Id + slotIndex* {.indexed.}: UInt256 + slotId* {.indexed.}: Id RequestFulfilled* = object of Event requestId* {.indexed.}: Id ProofSubmitted* = object of Event id*: Id proof*: seq[byte] + proc collateralAmount*(storage: Storage): UInt256 {.contract, view.} proc slashMisses*(storage: Storage): UInt256 {.contract, view.} proc slashPercentage*(storage: Storage): UInt256 {.contract, view.} diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 752bda88..9a731cd8 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -81,6 +81,34 @@ ethersuite "On-Chain Market": await market.fillSlot(request.id, slotIndex, proof) check (await market.getHost(request.id, slotIndex)) == some accounts[0] + test "support slot filled subscriptions": + await token.approve(storage.address, request.price) + discard await market.requestStorage(request) + var receivedIds: seq[array[32, byte]] + var receivedSlotIndices: seq[UInt256] + proc onSlotFilled(id: array[32, byte], slotIndex: UInt256) = + receivedIds.add(id) + receivedSlotIndices.add(slotIndex) + let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled) + await market.fillSlot(request.id, slotIndex, proof) + check receivedIds == @[request.id] + check receivedSlotIndices == @[slotIndex] + await subscription.unsubscribe() + + test "subscribes only to a certain slot": + var otherSlot = slotIndex - 1 + await token.approve(storage.address, request.price) + discard await market.requestStorage(request) + var receivedSlotIndices: seq[UInt256] + proc onSlotFilled(requestId: array[32, byte], slotIndex: UInt256) = + receivedSlotIndices.add(slotIndex) + let subscription = await market.subscribeSlotFilled(request.id, slotIndex, onSlotFilled) + await market.fillSlot(request.id, slotIndex - 1, proof) + check receivedSlotIndices.len == 0 + await market.fillSlot(request.id, slotIndex, proof) + check receivedSlotIndices == @[slotIndex] + await subscription.unsubscribe() + test "support fulfillment subscriptions": await token.approve(storage.address, request.price) discard await market.requestStorage(request)