replaces deprecated generic methods from Market with methods for specific event types
This commit is contained in:
parent
14d703a1a6
commit
d07a5869ae
|
@ -398,21 +398,10 @@ method subscribeProofSubmission*(market: OnChainMarket,
|
||||||
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
|
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
|
||||||
await subscription.eventSubscription.unsubscribe()
|
await subscription.eventSubscription.unsubscribe()
|
||||||
|
|
||||||
method queryPastEvents*[T: MarketplaceEvent](
|
proc blockNumberForBlocksEgo(provider: Provider,
|
||||||
market: OnChainMarket,
|
blocksAgo: int): Future[BlockTag] {.async.} =
|
||||||
_: type T,
|
let head = await provider.getBlockNumber()
|
||||||
blocksAgo: int): Future[seq[T]] {.async.} =
|
return BlockTag.init(head - blocksAgo.abs.u256)
|
||||||
|
|
||||||
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 blockNumberAndTimestamp(provider: Provider, blockTag: BlockTag):
|
proc blockNumberAndTimestamp(provider: Provider, blockTag: BlockTag):
|
||||||
Future[(UInt256, UInt256)] {.async.} =
|
Future[(UInt256, UInt256)] {.async.} =
|
||||||
|
@ -424,7 +413,7 @@ proc blockNumberAndTimestamp(provider: Provider, blockTag: BlockTag):
|
||||||
|
|
||||||
(latestBlockNumber, latestBlock.timestamp)
|
(latestBlockNumber, latestBlock.timestamp)
|
||||||
|
|
||||||
proc blockNumberForEpoch(epochTime: int64, provider: Provider): Future[UInt256]
|
proc blockNumberForEpoch(provider: Provider, epochTime: int64): Future[BlockTag]
|
||||||
{.async.} =
|
{.async.} =
|
||||||
let avgBlockTime = 13.u256
|
let avgBlockTime = 13.u256
|
||||||
let epochTimeUInt256 = epochTime.u256
|
let epochTimeUInt256 = epochTime.u256
|
||||||
|
@ -455,7 +444,7 @@ proc blockNumberForEpoch(epochTime: int64, provider: Provider): Future[UInt256]
|
||||||
elif midBlockTimestamp > epochTimeUInt256:
|
elif midBlockTimestamp > epochTimeUInt256:
|
||||||
high = mid - 1
|
high = mid - 1
|
||||||
else:
|
else:
|
||||||
return midBlockNumber
|
return BlockTag.init(midBlockNumber)
|
||||||
|
|
||||||
let (_, lowTimestamp) = await blockNumberAndTimestamp(
|
let (_, lowTimestamp) = await blockNumberAndTimestamp(
|
||||||
provider, BlockTag.init(low))
|
provider, BlockTag.init(low))
|
||||||
|
@ -464,23 +453,56 @@ proc blockNumberForEpoch(epochTime: int64, provider: Provider): Future[UInt256]
|
||||||
try:
|
try:
|
||||||
if abs(lowTimestamp.stint(256) - epochTimeUInt256.stint(256)) <
|
if abs(lowTimestamp.stint(256) - epochTimeUInt256.stint(256)) <
|
||||||
abs(highTimestamp.stint(256) - epochTimeUInt256.stint(256)):
|
abs(highTimestamp.stint(256) - epochTimeUInt256.stint(256)):
|
||||||
low
|
BlockTag.init(low)
|
||||||
else:
|
else:
|
||||||
high
|
BlockTag.init(high)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
raise newException(EthersError, fmt"Conversion error: {e.msg}")
|
raise newException(EthersError, fmt"Conversion error: {e.msg}")
|
||||||
|
|
||||||
method queryPastSlotFilledEvents*(
|
method queryPastSlotFilledEvents*(
|
||||||
market: OnChainMarket,
|
market: OnChainMarket,
|
||||||
fromTime: int64): Future[seq[SlotFilled]] {.async.} =
|
fromBlock: BlockTag): Future[seq[SlotFilled]] {.async.} =
|
||||||
|
|
||||||
convertEthersError:
|
convertEthersError:
|
||||||
let contract = market.contract
|
return await market.contract.queryFilter(SlotFilled,
|
||||||
let provider = contract.provider
|
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,
|
return await market.queryPastSlotFilledEvents(fromBlock)
|
||||||
fromBlock,
|
|
||||||
BlockTag.latest)
|
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)
|
||||||
|
|
|
@ -243,13 +243,27 @@ method subscribeProofSubmission*(market: Market,
|
||||||
method unsubscribe*(subscription: Subscription) {.base, async, upraises:[].} =
|
method unsubscribe*(subscription: Subscription) {.base, async, upraises:[].} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method queryPastEvents*[T: MarketplaceEvent](
|
method queryPastSlotFilledEvents*(
|
||||||
market: Market,
|
market: Market,
|
||||||
_: type T,
|
fromBlock: BlockTag): Future[seq[SlotFilled]] {.base, async.} =
|
||||||
blocksAgo: int): Future[seq[T]] {.base, async.} =
|
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method queryPastSlotFilledEvents*(
|
method queryPastSlotFilledEvents*(
|
||||||
market: Market,
|
market: Market,
|
||||||
fromTime: int64): Future[seq[SlotFilled]] {.base, async.} =
|
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")
|
raiseAssert("not implemented")
|
||||||
|
|
|
@ -8,6 +8,9 @@ import pkg/codex/market
|
||||||
import pkg/codex/contracts/requests
|
import pkg/codex/contracts/requests
|
||||||
import pkg/codex/contracts/proofs
|
import pkg/codex/contracts/proofs
|
||||||
import pkg/codex/contracts/config
|
import pkg/codex/contracts/config
|
||||||
|
|
||||||
|
from pkg/ethers import BlockTag
|
||||||
|
|
||||||
import ../examples
|
import ../examples
|
||||||
|
|
||||||
export market
|
export market
|
||||||
|
@ -469,21 +472,37 @@ method subscribeProofSubmission*(mock: MockMarket,
|
||||||
mock.subscriptions.onProofSubmitted.add(subscription)
|
mock.subscriptions.onProofSubmitted.add(subscription)
|
||||||
return subscription
|
return subscription
|
||||||
|
|
||||||
method queryPastEvents*[T: MarketplaceEvent](
|
method queryPastStorageRequestedEvents*(
|
||||||
market: MockMarket,
|
market: MockMarket,
|
||||||
_: type T,
|
fromBlock: BlockTag): Future[seq[StorageRequested]] {.async.} =
|
||||||
blocksAgo: int): Future[seq[T]] {.async.} =
|
return market.requested.map(request =>
|
||||||
|
StorageRequested(requestId: request.id,
|
||||||
|
ask: request.ask,
|
||||||
|
expiry: request.expiry)
|
||||||
|
)
|
||||||
|
|
||||||
if T of StorageRequested:
|
method queryPastStorageRequestedEvents*(
|
||||||
return market.requested.map(request =>
|
market: MockMarket,
|
||||||
StorageRequested(requestId: request.id,
|
blocksAgo: int): Future[seq[StorageRequested]] {.async.} =
|
||||||
ask: request.ask,
|
return market.requested.map(request =>
|
||||||
expiry: request.expiry)
|
StorageRequested(requestId: request.id,
|
||||||
)
|
ask: request.ask,
|
||||||
elif T of SlotFilled:
|
expiry: request.expiry)
|
||||||
return market.filled.map(slot =>
|
)
|
||||||
SlotFilled(requestId: slot.requestId, slotIndex: slot.slotIndex)
|
|
||||||
)
|
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*(
|
method queryPastSlotFilledEvents*(
|
||||||
market: MockMarket,
|
market: MockMarket,
|
||||||
|
|
|
@ -413,7 +413,8 @@ ethersuite "On-Chain Market":
|
||||||
# ago".
|
# ago".
|
||||||
|
|
||||||
proc getsPastRequest(): Future[bool] {.async.} =
|
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]
|
reqs.mapIt(it.requestId) == @[request.id, request1.id, request2.id]
|
||||||
|
|
||||||
check eventually await getsPastRequest()
|
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
|
# 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
|
# to check the `approve` for the first `fillSlot` call, so we only need to
|
||||||
# check 5 "blocks ago".
|
# check 5 "blocks ago".
|
||||||
let events = await market.queryPastEvents(SlotFilled, 5)
|
let events =
|
||||||
|
await market.queryPastSlotFilledEvents(blocksAgo = 5)
|
||||||
check events == @[
|
check events == @[
|
||||||
SlotFilled(requestId: request.id, slotIndex: 0.u256),
|
SlotFilled(requestId: request.id, slotIndex: 0.u256),
|
||||||
SlotFilled(requestId: request.id, slotIndex: 1.u256),
|
SlotFilled(requestId: request.id, slotIndex: 1.u256),
|
||||||
|
@ -443,8 +445,8 @@ ethersuite "On-Chain Market":
|
||||||
await market.requestStorage(request)
|
await market.requestStorage(request)
|
||||||
|
|
||||||
check eventually (
|
check eventually (
|
||||||
(await market.queryPastEvents(StorageRequested, blocksAgo = -2)) ==
|
(await market.queryPastStorageRequestedEvents(blocksAgo = -2)) ==
|
||||||
(await market.queryPastEvents(StorageRequested, blocksAgo = 2))
|
(await market.queryPastStorageRequestedEvents(blocksAgo = 2))
|
||||||
)
|
)
|
||||||
|
|
||||||
test "pays rewards and collateral to host":
|
test "pays rewards and collateral to host":
|
||||||
|
|
Loading…
Reference in New Issue