From d07a5869aeaab1f18dd7dcbb1abc6f62157e94f9 Mon Sep 17 00:00:00 2001 From: Marcin Czenko Date: Mon, 7 Oct 2024 20:07:22 +0200 Subject: [PATCH] replaces deprecated generic methods from Market with methods for specific event types --- codex/contracts/market.nim | 76 +++++++++++++++++++----------- codex/market.nim | 26 +++++++--- tests/codex/helpers/mockmarket.nim | 47 ++++++++++++------ tests/contracts/testMarket.nim | 10 ++-- 4 files changed, 108 insertions(+), 51 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 2ba8adba..89218d3c 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -398,21 +398,10 @@ method subscribeProofSubmission*(market: OnChainMarket, method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} = await subscription.eventSubscription.unsubscribe() -method queryPastEvents*[T: MarketplaceEvent]( - market: OnChainMarket, - _: type T, - blocksAgo: int): Future[seq[T]] {.async.} = - - convertEthersError: - let contract = market.contract - let provider = contract.provider - - let head = await provider.getBlockNumber() - let fromBlock = BlockTag.init(head - blocksAgo.abs.u256) - - return await contract.queryFilter(T, - fromBlock, - BlockTag.latest) +proc blockNumberForBlocksEgo(provider: Provider, + blocksAgo: int): Future[BlockTag] {.async.} = + let head = await provider.getBlockNumber() + return BlockTag.init(head - blocksAgo.abs.u256) proc blockNumberAndTimestamp(provider: Provider, blockTag: BlockTag): Future[(UInt256, UInt256)] {.async.} = @@ -424,7 +413,7 @@ proc blockNumberAndTimestamp(provider: Provider, blockTag: BlockTag): (latestBlockNumber, latestBlock.timestamp) -proc blockNumberForEpoch(epochTime: int64, provider: Provider): Future[UInt256] +proc blockNumberForEpoch(provider: Provider, epochTime: int64): Future[BlockTag] {.async.} = let avgBlockTime = 13.u256 let epochTimeUInt256 = epochTime.u256 @@ -455,7 +444,7 @@ proc blockNumberForEpoch(epochTime: int64, provider: Provider): Future[UInt256] elif midBlockTimestamp > epochTimeUInt256: high = mid - 1 else: - return midBlockNumber + return BlockTag.init(midBlockNumber) let (_, lowTimestamp) = await blockNumberAndTimestamp( provider, BlockTag.init(low)) @@ -464,23 +453,56 @@ proc blockNumberForEpoch(epochTime: int64, provider: Provider): Future[UInt256] try: if abs(lowTimestamp.stint(256) - epochTimeUInt256.stint(256)) < abs(highTimestamp.stint(256) - epochTimeUInt256.stint(256)): - low + BlockTag.init(low) else: - high + BlockTag.init(high) except ValueError as e: raise newException(EthersError, fmt"Conversion error: {e.msg}") method queryPastSlotFilledEvents*( market: OnChainMarket, - fromTime: int64): Future[seq[SlotFilled]] {.async.} = + fromBlock: BlockTag): Future[seq[SlotFilled]] {.async.} = + convertEthersError: - let contract = market.contract - let provider = contract.provider + return await market.contract.queryFilter(SlotFilled, + fromBlock, + BlockTag.latest) - let blockNumberForEpoch = await blockNumberForEpoch(fromTime, provider) +method queryPastSlotFilledEvents*( + market: OnChainMarket, + blocksAgo: int): Future[seq[SlotFilled]] {.async.} = - let fromBlock = BlockTag.init(blockNumberForEpoch) + convertEthersError: + let fromBlock = + await blockNumberForBlocksEgo(market.contract.provider, blocksAgo) - return await contract.queryFilter(SlotFilled, - fromBlock, - BlockTag.latest) + return await market.queryPastSlotFilledEvents(fromBlock) + +method queryPastSlotFilledEvents*( + market: OnChainMarket, + fromTime: int64): Future[seq[SlotFilled]] {.async.} = + + convertEthersError: + let fromBlock = await blockNumberForEpoch(market.contract.provider, + fromTime) + + return await market.queryPastSlotFilledEvents(fromBlock) + +method queryPastStorageRequestedEvents*( + market: OnChainMarket, + fromBlock: BlockTag): Future[seq[StorageRequested]] {.async.} = + + convertEthersError: + return await market.contract.queryFilter(StorageRequested, + fromBlock, + BlockTag.latest) + +method queryPastStorageRequestedEvents*( + market: OnChainMarket, + blocksAgo: int): Future[seq[StorageRequested]] {.async.} = + + convertEthersError: + let fromBlock = + await blockNumberForBlocksEgo(market.contract.provider, blocksAgo) + + return await market.queryPastStorageRequestedEvents(fromBlock) diff --git a/codex/market.nim b/codex/market.nim index 73522c39..e3124984 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -243,13 +243,27 @@ method subscribeProofSubmission*(market: Market, method unsubscribe*(subscription: Subscription) {.base, async, upraises:[].} = raiseAssert("not implemented") -method queryPastEvents*[T: MarketplaceEvent]( - market: Market, - _: type T, - blocksAgo: int): Future[seq[T]] {.base, async.} = +method queryPastSlotFilledEvents*( + market: Market, + fromBlock: BlockTag): Future[seq[SlotFilled]] {.base, async.} = raiseAssert("not implemented") method queryPastSlotFilledEvents*( - market: Market, - fromTime: int64): Future[seq[SlotFilled]] {.base, async.} = + market: Market, + blocksAgo: int): Future[seq[SlotFilled]] {.base, async.} = + raiseAssert("not implemented") + +method queryPastStorageRequestedEvents*( + market: Market, + fromBlock: BlockTag): Future[seq[StorageRequested]] {.base, async.} = + raiseAssert("not implemented") + +method queryPastStorageRequestedEvents*( + market: Market, + blocksAgo: int): Future[seq[StorageRequested]] {.base, async.} = + raiseAssert("not implemented") + +method queryPastSlotFilledEvents*( + market: Market, + fromTime: int64): Future[seq[SlotFilled]] {.base, async.} = raiseAssert("not implemented") diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 928641c8..1d554102 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -8,6 +8,9 @@ import pkg/codex/market import pkg/codex/contracts/requests import pkg/codex/contracts/proofs import pkg/codex/contracts/config + +from pkg/ethers import BlockTag + import ../examples export market @@ -469,21 +472,37 @@ method subscribeProofSubmission*(mock: MockMarket, mock.subscriptions.onProofSubmitted.add(subscription) return subscription -method queryPastEvents*[T: MarketplaceEvent]( - market: MockMarket, - _: type T, - blocksAgo: int): Future[seq[T]] {.async.} = +method queryPastStorageRequestedEvents*( + market: MockMarket, + fromBlock: BlockTag): Future[seq[StorageRequested]] {.async.} = + return market.requested.map(request => + StorageRequested(requestId: request.id, + ask: request.ask, + expiry: request.expiry) + ) - if T of StorageRequested: - return market.requested.map(request => - StorageRequested(requestId: request.id, - ask: request.ask, - expiry: request.expiry) - ) - elif T of SlotFilled: - return market.filled.map(slot => - SlotFilled(requestId: slot.requestId, slotIndex: slot.slotIndex) - ) +method queryPastStorageRequestedEvents*( + market: MockMarket, + blocksAgo: int): Future[seq[StorageRequested]] {.async.} = + return market.requested.map(request => + StorageRequested(requestId: request.id, + ask: request.ask, + expiry: request.expiry) + ) + +method queryPastSlotFilledEvents*( + market: MockMarket, + fromBlock: BlockTag): Future[seq[SlotFilled]] {.async.} = + return market.filled.map(slot => + SlotFilled(requestId: slot.requestId, slotIndex: slot.slotIndex) + ) + +method queryPastSlotFilledEvents*( + market: MockMarket, + blocksAgo: int): Future[seq[SlotFilled]] {.async.} = + return market.filled.map(slot => + SlotFilled(requestId: slot.requestId, slotIndex: slot.slotIndex) + ) method queryPastSlotFilledEvents*( market: MockMarket, diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index 66088b71..bf0c9d37 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -413,7 +413,8 @@ ethersuite "On-Chain Market": # ago". proc getsPastRequest(): Future[bool] {.async.} = - let reqs = await market.queryPastEvents(StorageRequested, 5) + let reqs = + await market.queryPastStorageRequestedEvents(blocksAgo = 5) reqs.mapIt(it.requestId) == @[request.id, request1.id, request2.id] check eventually await getsPastRequest() @@ -432,7 +433,8 @@ ethersuite "On-Chain Market": # two PoA blocks per `fillSlot` call (6 blocks for 3 calls). We don't need # to check the `approve` for the first `fillSlot` call, so we only need to # check 5 "blocks ago". - let events = await market.queryPastEvents(SlotFilled, 5) + let events = + await market.queryPastSlotFilledEvents(blocksAgo = 5) check events == @[ SlotFilled(requestId: request.id, slotIndex: 0.u256), SlotFilled(requestId: request.id, slotIndex: 1.u256), @@ -443,8 +445,8 @@ ethersuite "On-Chain Market": await market.requestStorage(request) check eventually ( - (await market.queryPastEvents(StorageRequested, blocksAgo = -2)) == - (await market.queryPastEvents(StorageRequested, blocksAgo = 2)) + (await market.queryPastStorageRequestedEvents(blocksAgo = -2)) == + (await market.queryPastStorageRequestedEvents(blocksAgo = 2)) ) test "pays rewards and collateral to host":