[market] wait until request/offer expires
This commit is contained in:
parent
8ff748eff8
commit
75ec8c0bfd
|
@ -4,21 +4,28 @@ import pkg/questionable
|
||||||
import ../market
|
import ../market
|
||||||
import ./storage
|
import ./storage
|
||||||
|
|
||||||
|
export market
|
||||||
|
|
||||||
type
|
type
|
||||||
OnChainMarket* = ref object of Market
|
OnChainMarket* = ref object of Market
|
||||||
contract: Storage
|
contract: Storage
|
||||||
signer: Signer
|
signer: Signer
|
||||||
|
pollInterval*: Duration
|
||||||
MarketSubscription = market.Subscription
|
MarketSubscription = market.Subscription
|
||||||
EventSubscription = ethers.Subscription
|
EventSubscription = ethers.Subscription
|
||||||
OnChainMarketSubscription = ref object of MarketSubscription
|
OnChainMarketSubscription = ref object of MarketSubscription
|
||||||
eventSubscription: EventSubscription
|
eventSubscription: EventSubscription
|
||||||
|
|
||||||
export market
|
const DefaultPollInterval = 3.seconds
|
||||||
|
|
||||||
func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
|
func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
|
||||||
without signer =? contract.signer:
|
without signer =? contract.signer:
|
||||||
raiseAssert("Storage contract should have a 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.} =
|
method requestStorage(market: OnChainMarket, request: StorageRequest) {.async.} =
|
||||||
var request = request
|
var request = request
|
||||||
|
@ -33,6 +40,15 @@ method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} =
|
||||||
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
|
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
|
||||||
await market.contract.selectOffer(offerId)
|
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,
|
method subscribeRequests(market: OnChainMarket,
|
||||||
callback: OnRequest):
|
callback: OnRequest):
|
||||||
Future[MarketSubscription] {.async.} =
|
Future[MarketSubscription] {.async.} =
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import std/times
|
import pkg/chronos
|
||||||
import ./ethertest
|
|
||||||
import dagger/contracts
|
import dagger/contracts
|
||||||
import dagger/contracts/testtoken
|
import dagger/contracts/testtoken
|
||||||
|
import ./ethertest
|
||||||
import ./examples
|
import ./examples
|
||||||
|
import ./time
|
||||||
|
|
||||||
ethersuite "On-Chain Market":
|
ethersuite "On-Chain Market":
|
||||||
|
|
||||||
|
@ -163,3 +164,19 @@ ethersuite "On-Chain Market":
|
||||||
check selected == @[offer.id]
|
check selected == @[offer.id]
|
||||||
|
|
||||||
await subscription.unsubscribe()
|
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
|
||||||
|
|
Loading…
Reference in New Issue