[contracts] subscribe to SlotFilled event

This commit is contained in:
Mark Spanbroek 2022-07-28 14:33:17 +02:00 committed by markspanbroek
parent 7995fd71d5
commit b4b6fdb7c6
3 changed files with 44 additions and 0 deletions

View File

@ -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):

View File

@ -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.}

View File

@ -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)