replaces deprecated generic methods from Market with methods for specific event types

This commit is contained in:
Marcin Czenko 2024-10-07 20:07:22 +02:00
parent 14d703a1a6
commit d07a5869ae
No known key found for this signature in database
GPG Key ID: 33DEA0C8E30937C0
4 changed files with 108 additions and 51 deletions

View File

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

View File

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

View File

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

View File

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