2022-03-28 15:24:28 +00:00
|
|
|
import pkg/ethers
|
|
|
|
import pkg/upraises
|
2022-03-28 15:28:51 +00:00
|
|
|
import pkg/questionable
|
2022-03-28 15:24:28 +00:00
|
|
|
import ../market
|
|
|
|
import ./storage
|
|
|
|
|
2022-03-29 14:17:20 +00:00
|
|
|
export market
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
type
|
|
|
|
OnChainMarket* = ref object of Market
|
|
|
|
contract: Storage
|
|
|
|
signer: Signer
|
2022-03-29 14:17:20 +00:00
|
|
|
pollInterval*: Duration
|
2022-03-28 15:24:28 +00:00
|
|
|
MarketSubscription = market.Subscription
|
|
|
|
EventSubscription = ethers.Subscription
|
|
|
|
OnChainMarketSubscription = ref object of MarketSubscription
|
|
|
|
eventSubscription: EventSubscription
|
|
|
|
|
2022-03-29 14:17:20 +00:00
|
|
|
const DefaultPollInterval = 3.seconds
|
2022-03-28 15:24:28 +00:00
|
|
|
|
|
|
|
func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
|
2022-03-28 15:28:51 +00:00
|
|
|
without signer =? contract.signer:
|
|
|
|
raiseAssert("Storage contract should have a signer")
|
2022-03-29 14:17:20 +00:00
|
|
|
OnChainMarket(
|
|
|
|
contract: contract,
|
|
|
|
signer: signer,
|
|
|
|
pollInterval: DefaultPollInterval
|
|
|
|
)
|
2022-03-28 15:24:28 +00:00
|
|
|
|
2022-03-31 08:46:03 +00:00
|
|
|
method requestStorage(market: OnChainMarket,
|
|
|
|
request: StorageRequest):
|
|
|
|
Future[StorageRequest] {.async.} =
|
2022-03-29 07:47:49 +00:00
|
|
|
var request = request
|
|
|
|
request.client = await market.signer.getAddress()
|
|
|
|
await market.contract.requestStorage(request)
|
2022-03-31 08:46:03 +00:00
|
|
|
return request
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} =
|
|
|
|
var offer = offer
|
|
|
|
offer.host = await market.signer.getAddress()
|
|
|
|
await market.contract.offerStorage(offer)
|
|
|
|
|
2022-03-29 09:20:07 +00:00
|
|
|
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
|
|
|
|
await market.contract.selectOffer(offerId)
|
|
|
|
|
2022-03-30 11:01:44 +00:00
|
|
|
method getTime(market: OnChainMarket): Future[UInt256] {.async.} =
|
2022-03-29 14:17:20 +00:00
|
|
|
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)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
method subscribeRequests(market: OnChainMarket,
|
|
|
|
callback: OnRequest):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: StorageRequested) {.upraises:[].} =
|
|
|
|
callback(event.request)
|
|
|
|
let subscription = await market.contract.subscribe(StorageRequested, onEvent)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-03-29 07:47:49 +00:00
|
|
|
method subscribeOffers(market: OnChainMarket,
|
|
|
|
requestId: array[32, byte],
|
|
|
|
callback: OnOffer):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onEvent(event: StorageOffered) {.upraises:[].} =
|
2022-03-29 08:53:05 +00:00
|
|
|
if event.requestId == requestId:
|
|
|
|
callback(event.offer)
|
2022-03-29 07:47:49 +00:00
|
|
|
let subscription = await market.contract.subscribe(StorageOffered, onEvent)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
2022-03-28 15:24:28 +00:00
|
|
|
|
2022-03-29 09:20:07 +00:00
|
|
|
method subscribeSelection(market: OnChainMarket,
|
|
|
|
requestId: array[32, byte],
|
|
|
|
callback: OnSelect):
|
|
|
|
Future[MarketSubscription] {.async.} =
|
|
|
|
proc onSelect(event: OfferSelected) {.upraises: [].} =
|
|
|
|
if event.requestId == requestId:
|
|
|
|
callback(event.offerId)
|
|
|
|
let subscription = await market.contract.subscribe(OfferSelected, onSelect)
|
|
|
|
return OnChainMarketSubscription(eventSubscription: subscription)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
|
|
|
|
await subscription.eventSubscription.unsubscribe()
|