[market] wait until request/offer expires

This commit is contained in:
Mark Spanbroek 2022-03-29 16:17:20 +02:00 committed by markspanbroek
parent 8ff748eff8
commit 75ec8c0bfd
2 changed files with 37 additions and 4 deletions

View File

@ -4,21 +4,28 @@ import pkg/questionable
import ../market
import ./storage
export market
type
OnChainMarket* = ref object of Market
contract: Storage
signer: Signer
pollInterval*: Duration
MarketSubscription = market.Subscription
EventSubscription = ethers.Subscription
OnChainMarketSubscription = ref object of MarketSubscription
eventSubscription: EventSubscription
export market
const DefaultPollInterval = 3.seconds
func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
without signer =? contract.signer:
raiseAssert("Storage contract should have a signer")
OnChainMarket(contract: contract, signer: signer)
OnChainMarket(
contract: contract,
signer: signer,
pollInterval: DefaultPollInterval
)
method requestStorage(market: OnChainMarket, request: StorageRequest) {.async.} =
var request = request
@ -33,6 +40,15 @@ method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} =
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
await market.contract.selectOffer(offerId)
proc getTime(market: OnChainMarket): Future[UInt256] {.async.} =
let provider = market.contract.provider
let blck = !await provider.getBlock(BlockTag.latest)
return blck.timestamp
method waitUntil*(market: OnChainMarket, expiry: UInt256) {.async.} =
while not ((time =? await market.getTime()) and (time >= expiry)):
await sleepAsync(market.pollInterval)
method subscribeRequests(market: OnChainMarket,
callback: OnRequest):
Future[MarketSubscription] {.async.} =

View File

@ -1,8 +1,9 @@
import std/times
import ./ethertest
import pkg/chronos
import dagger/contracts
import dagger/contracts/testtoken
import ./ethertest
import ./examples
import ./time
ethersuite "On-Chain Market":
@ -163,3 +164,19 @@ ethersuite "On-Chain Market":
check selected == @[offer.id]
await subscription.unsubscribe()
test "supports waiting for expiry of a request or offer":
let pollInterval = 100.milliseconds
market.pollInterval = pollInterval
proc waitForPoll {.async.} =
await sleepAsync(pollInterval + 10.milliseconds)
let future = market.waitUntil(request.expiry)
check not future.completed
await provider.advanceTimeTo(request.expiry - 1)
await waitForPoll()
check not future.completed
await provider.advanceTimeTo(request.expiry)
await waitForPoll()
check future.completed