[market] Update to latest dagger-contracts

This commit is contained in:
Mark Spanbroek 2022-06-14 09:47:05 +02:00 committed by markspanbroek
parent e352a181e4
commit b7ab9481d9
4 changed files with 60 additions and 182 deletions

View File

@ -31,16 +31,10 @@ method requestStorage(market: OnChainMarket,
await market.contract.requestStorage(request) await market.contract.requestStorage(request)
return request return request
method offerStorage(market: OnChainMarket, method fulfillRequest(market: OnChainMarket,
offer: StorageOffer): requestId: array[32, byte],
Future[StorageOffer] {.async.} = proof: seq[byte]) {.async.} =
var offer = offer await market.contract.fulfillRequest(requestId, proof)
offer.host = await market.signer.getAddress()
await market.contract.offerStorage(offer)
return offer
method selectOffer(market: OnChainMarket, offerId: array[32, byte]) {.async.} =
await market.contract.selectOffer(offerId)
method subscribeRequests(market: OnChainMarket, method subscribeRequests(market: OnChainMarket,
callback: OnRequest): callback: OnRequest):
@ -50,24 +44,14 @@ method subscribeRequests(market: OnChainMarket,
let subscription = await market.contract.subscribe(StorageRequested, onEvent) let subscription = await market.contract.subscribe(StorageRequested, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription) return OnChainMarketSubscription(eventSubscription: subscription)
method subscribeOffers(market: OnChainMarket, method subscribeFulfillment(market: OnChainMarket,
requestId: array[32, byte], requestId: array[32, byte],
callback: OnOffer): callback: OnFulfillment):
Future[MarketSubscription] {.async.} = Future[MarketSubscription] {.async.} =
proc onEvent(event: StorageOffered) {.upraises:[].} = proc onEvent(event: RequestFulfilled) {.upraises:[].} =
if event.requestId == requestId: if event.requestId == requestId:
callback(event.offer) callback(event.requestId)
let subscription = await market.contract.subscribe(StorageOffered, onEvent) let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
return OnChainMarketSubscription(eventSubscription: subscription)
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) return OnChainMarketSubscription(eventSubscription: subscription)
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} = method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =

View File

@ -11,20 +11,16 @@ type
Market* = ref object of RootObj Market* = ref object of RootObj
Subscription* = ref object of RootObj Subscription* = ref object of RootObj
OnRequest* = proc(id: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].} OnRequest* = proc(id: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].}
OnOffer* = proc(offer: StorageOffer) {.gcsafe, upraises:[].} OnFulfillment* = proc(requestId: array[32, byte]) {.gcsafe, upraises: [].}
OnSelect* = proc(offerId: array[32, byte]) {.gcsafe, upraises: [].}
method requestStorage*(market: Market, method requestStorage*(market: Market,
request: StorageRequest): request: StorageRequest):
Future[StorageRequest] {.base, async.} = Future[StorageRequest] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method offerStorage*(market: Market, method fulfillRequest*(market: Market,
offer: StorageOffer): requestId: array[32, byte],
Future[StorageOffer] {.base, async.} = proof: seq[byte]) {.base, async.} =
raiseAssert("not implemented")
method selectOffer*(market: Market, id: array[32, byte]) {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method subscribeRequests*(market: Market, method subscribeRequests*(market: Market,
@ -32,15 +28,9 @@ method subscribeRequests*(market: Market,
Future[Subscription] {.base, async.} = Future[Subscription] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method subscribeOffers*(market: Market, method subscribeFulfillment*(market: Market,
requestId: array[32, byte], requestId: array[32, byte],
callback: OnOffer): callback: OnFulfillment):
Future[Subscription] {.base, async.} =
raiseAssert("not implemented")
method subscribeSelection*(market: Market,
requestId: array[32, byte],
callback: OnSelect):
Future[Subscription] {.base, async.} = Future[Subscription] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")

View File

@ -1,6 +1,4 @@
import std/sequtils import std/sequtils
import std/heapqueue
import pkg/questionable
import pkg/codex/market import pkg/codex/market
export market export market
@ -8,61 +6,37 @@ export market
type type
MockMarket* = ref object of Market MockMarket* = ref object of Market
requested*: seq[StorageRequest] requested*: seq[StorageRequest]
offered*: seq[StorageOffer] fulfilled*: seq[Fulfillment]
selected*: seq[array[32, byte]]
subscriptions: Subscriptions subscriptions: Subscriptions
time: UInt256 Fulfillment* = object
waiting: HeapQueue[Expiry] requestId: array[32, byte]
proof: seq[byte]
Subscriptions = object Subscriptions = object
onRequest: seq[RequestSubscription] onRequest: seq[RequestSubscription]
onOffer: seq[OfferSubscription] onFulfillment: seq[FulfillmentSubscription]
onSelect: seq[SelectSubscription]
RequestSubscription* = ref object of Subscription RequestSubscription* = ref object of Subscription
market: MockMarket market: MockMarket
callback: OnRequest callback: OnRequest
OfferSubscription* = ref object of Subscription FulfillmentSubscription* = ref object of Subscription
market: MockMarket market: MockMarket
requestId: array[32, byte] requestId: array[32, byte]
callback: OnOffer callback: OnFulfillment
SelectSubscription* = ref object of Subscription
market: MockMarket
requestId: array[32, byte]
callback: OnSelect
Expiry = object
future: Future[void]
expiry: UInt256
method requestStorage*(market: MockMarket, method requestStorage*(market: MockMarket,
request: StorageRequest): request: StorageRequest):
Future[StorageRequest] {.async.} = Future[StorageRequest] {.async.} =
market.requested.add(request) market.requested.add(request)
let subscriptions = market.subscriptions.onRequest for subscription in market.subscriptions.onRequest:
for subscription in subscriptions:
subscription.callback(request.id, request.ask) subscription.callback(request.id, request.ask)
return request return request
method offerStorage*(market: MockMarket, method fulfillRequest*(market: MockMarket,
offer: StorageOffer): requestId: array[32, byte],
Future[StorageOffer] {.async.} = proof: seq[byte]) {.async.} =
market.offered.add(offer) market.fulfilled.add(Fulfillment(requestId: requestId, proof: proof))
let subscriptions = market.subscriptions.onOffer for subscription in market.subscriptions.onFulfillment:
for subscription in subscriptions: if subscription.requestId == requestId:
if subscription.requestId == offer.requestId: subscription.callback(requestId)
subscription.callback(offer)
return offer
proc findOffer(market: MockMarket, id: array[32, byte]): ?StorageOffer =
for offer in market.offered:
if offer.id == id:
return some offer
method selectOffer*(market: MockMarket, id: array[32, byte]) {.async.} =
market.selected.add(id)
let subscriptions = market.subscriptions.onSelect
for subscription in subscriptions:
if offer =? market.findOffer(id):
if subscription.requestId == offer.requestId:
subscription.callback(id)
method subscribeRequests*(market: MockMarket, method subscribeRequests*(market: MockMarket,
callback: OnRequest): callback: OnRequest):
@ -74,35 +48,20 @@ method subscribeRequests*(market: MockMarket,
market.subscriptions.onRequest.add(subscription) market.subscriptions.onRequest.add(subscription)
return subscription return subscription
method subscribeOffers*(market: MockMarket, method subscribeFulfillment*(market: MockMarket,
requestId: array[32, byte], requestId: array[32, byte],
callback: OnOffer): callback: OnFulfillment):
Future[Subscription] {.async.} = Future[Subscription] {.async.} =
let subscription = OfferSubscription( let subscription = FulfillmentSubscription(
market: market, market: market,
requestId: requestId, requestId: requestId,
callback: callback callback: callback
) )
market.subscriptions.onOffer.add(subscription) market.subscriptions.onFulfillment.add(subscription)
return subscription
method subscribeSelection*(market: MockMarket,
requestId: array[32, byte],
callback: OnSelect):
Future[Subscription] {.async.} =
let subscription = SelectSubscription(
market: market,
requestId: requestId,
callback: callback
)
market.subscriptions.onSelect.add(subscription)
return subscription return subscription
method unsubscribe*(subscription: RequestSubscription) {.async.} = method unsubscribe*(subscription: RequestSubscription) {.async.} =
subscription.market.subscriptions.onRequest.keepItIf(it != subscription) subscription.market.subscriptions.onRequest.keepItIf(it != subscription)
method unsubscribe*(subscription: OfferSubscription) {.async.} = method unsubscribe*(subscription: FulfillmentSubscription) {.async.} =
subscription.market.subscriptions.onOffer.keepItIf(it != subscription) subscription.market.subscriptions.onFulfillment.keepItIf(it != subscription)
method unsubscribe*(subscription: SelectSubscription) {.async.} =
subscription.market.subscriptions.onSelect.keepItIf(it != subscription)

View File

@ -6,6 +6,7 @@ import ./examples
import ./time import ./time
ethersuite "On-Chain Market": ethersuite "On-Chain Market":
let proof = seq[byte].example
var market: OnChainMarket var market: OnChainMarket
var storage: Storage var storage: Storage
@ -61,96 +62,40 @@ ethersuite "On-Chain Market":
check receivedAsks == @[request.ask] check receivedAsks == @[request.ask]
await subscription.unsubscribe() await subscription.unsubscribe()
test "supports storage offers": test "supports fulfilling of requests":
await token.approve(storage.address, request.ask.maxPrice) await token.approve(storage.address, request.ask.maxPrice)
discard await market.requestStorage(request) discard await market.requestStorage(request)
check (await market.offerStorage(offer)) == offer await market.fulfillRequest(request.id, proof)
test "sets host address when submitting storage offer": test "support fulfillment subscriptions":
await token.approve(storage.address, request.ask.maxPrice) await token.approve(storage.address, request.ask.maxPrice)
discard await market.requestStorage(request) discard await market.requestStorage(request)
var offerWithoutHost = offer var receivedIds: seq[array[32, byte]]
offerWithoutHost.host = Address.default proc onFulfillment(id: array[32, byte]) =
let submitted = await market.offerStorage(offerWithoutHost) receivedIds.add(id)
check submitted.host == accounts[0] let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
await market.fulfillRequest(request.id, proof)
test "supports offer subscriptions": check receivedIds == @[request.id]
await token.approve(storage.address, request.ask.maxPrice)
discard await market.requestStorage(request)
var received: seq[StorageOffer]
proc onOffer(offer: StorageOffer) =
received.add(offer)
let subscription = await market.subscribeOffers(request.id, onOffer)
discard await market.offerStorage(offer)
check received == @[offer]
await subscription.unsubscribe() await subscription.unsubscribe()
test "subscribes only to offers for a certain request": test "subscribes only to fulfillmentof a certain request":
var otherRequest = StorageRequest.example var otherRequest = StorageRequest.example
var otherOffer = StorageOffer.example
otherRequest.client = accounts[0] otherRequest.client = accounts[0]
otherOffer.host = accounts[0]
otherOffer.requestId = otherRequest.id
otherOffer.price = otherrequest.ask.maxPrice
await token.approve(storage.address, request.ask.maxPrice) await token.approve(storage.address, request.ask.maxPrice)
discard await market.requestStorage(request) discard await market.requestStorage(request)
await token.approve(storage.address, otherrequest.ask.maxPrice) await token.approve(storage.address, otherrequest.ask.maxPrice)
discard await market.requestStorage(otherRequest) discard await market.requestStorage(otherRequest)
var submitted: seq[StorageOffer] var receivedIds: seq[array[32, byte]]
proc onOffer(offer: StorageOffer) = proc onFulfillment(id: array[32, byte]) =
submitted.add(offer) receivedIds.add(id)
let subscription = await market.subscribeOffers(request.id, onOffer) let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
discard await market.offerStorage(offer) await market.fulfillRequest(request.id, proof)
discard await market.offerStorage(otherOffer) await market.fulfillRequest(otherRequest.id, proof)
check submitted == @[offer] check receivedIds == @[request.id]
await subscription.unsubscribe()
test "supports selection of an offer":
await token.approve(storage.address, request.ask.maxPrice)
discard await market.requestStorage(request)
discard await market.offerStorage(offer)
var selected: seq[array[32, byte]]
proc onSelect(offerId: array[32, byte]) =
selected.add(offerId)
let subscription = await market.subscribeSelection(request.id, onSelect)
await market.selectOffer(offer.id)
check selected == @[offer.id]
await subscription.unsubscribe()
test "subscribes only to selection for a certain request":
var otherRequest = StorageRequest.example
var otherOffer = StorageOffer.example
otherRequest.client = accounts[0]
otherOffer.host = accounts[0]
otherOffer.requestId = otherRequest.id
otherOffer.price = otherrequest.ask.maxPrice
await token.approve(storage.address, request.ask.maxPrice)
discard await market.requestStorage(request)
discard await market.offerStorage(offer)
await token.approve(storage.address, otherrequest.ask.maxPrice)
discard await market.requestStorage(otherRequest)
discard await market.offerStorage(otherOffer)
var selected: seq[array[32, byte]]
proc onSelect(offerId: array[32, byte]) =
selected.add(offerId)
let subscription = await market.subscribeSelection(request.id, onSelect)
await market.selectOffer(offer.id)
await market.selectOffer(otherOffer.id)
check selected == @[offer.id]
await subscription.unsubscribe() await subscription.unsubscribe()