[market] Update to latest dagger-contracts
This commit is contained in:
parent
e352a181e4
commit
b7ab9481d9
|
@ -31,16 +31,10 @@ method requestStorage(market: OnChainMarket,
|
|||
await market.contract.requestStorage(request)
|
||||
return request
|
||||
|
||||
method offerStorage(market: OnChainMarket,
|
||||
offer: StorageOffer):
|
||||
Future[StorageOffer] {.async.} =
|
||||
var offer = offer
|
||||
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 fulfillRequest(market: OnChainMarket,
|
||||
requestId: array[32, byte],
|
||||
proof: seq[byte]) {.async.} =
|
||||
await market.contract.fulfillRequest(requestId, proof)
|
||||
|
||||
method subscribeRequests(market: OnChainMarket,
|
||||
callback: OnRequest):
|
||||
|
@ -50,24 +44,14 @@ method subscribeRequests(market: OnChainMarket,
|
|||
let subscription = await market.contract.subscribe(StorageRequested, onEvent)
|
||||
return OnChainMarketSubscription(eventSubscription: subscription)
|
||||
|
||||
method subscribeOffers(market: OnChainMarket,
|
||||
method subscribeFulfillment(market: OnChainMarket,
|
||||
requestId: array[32, byte],
|
||||
callback: OnOffer):
|
||||
callback: OnFulfillment):
|
||||
Future[MarketSubscription] {.async.} =
|
||||
proc onEvent(event: StorageOffered) {.upraises:[].} =
|
||||
proc onEvent(event: RequestFulfilled) {.upraises:[].} =
|
||||
if event.requestId == requestId:
|
||||
callback(event.offer)
|
||||
let subscription = await market.contract.subscribe(StorageOffered, 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)
|
||||
callback(event.requestId)
|
||||
let subscription = await market.contract.subscribe(RequestFulfilled, onEvent)
|
||||
return OnChainMarketSubscription(eventSubscription: subscription)
|
||||
|
||||
method unsubscribe*(subscription: OnChainMarketSubscription) {.async.} =
|
||||
|
|
|
@ -11,20 +11,16 @@ type
|
|||
Market* = ref object of RootObj
|
||||
Subscription* = ref object of RootObj
|
||||
OnRequest* = proc(id: array[32, byte], ask: StorageAsk) {.gcsafe, upraises:[].}
|
||||
OnOffer* = proc(offer: StorageOffer) {.gcsafe, upraises:[].}
|
||||
OnSelect* = proc(offerId: array[32, byte]) {.gcsafe, upraises: [].}
|
||||
OnFulfillment* = proc(requestId: array[32, byte]) {.gcsafe, upraises: [].}
|
||||
|
||||
method requestStorage*(market: Market,
|
||||
request: StorageRequest):
|
||||
Future[StorageRequest] {.base, async.} =
|
||||
raiseAssert("not implemented")
|
||||
|
||||
method offerStorage*(market: Market,
|
||||
offer: StorageOffer):
|
||||
Future[StorageOffer] {.base, async.} =
|
||||
raiseAssert("not implemented")
|
||||
|
||||
method selectOffer*(market: Market, id: array[32, byte]) {.base, async.} =
|
||||
method fulfillRequest*(market: Market,
|
||||
requestId: array[32, byte],
|
||||
proof: seq[byte]) {.base, async.} =
|
||||
raiseAssert("not implemented")
|
||||
|
||||
method subscribeRequests*(market: Market,
|
||||
|
@ -32,15 +28,9 @@ method subscribeRequests*(market: Market,
|
|||
Future[Subscription] {.base, async.} =
|
||||
raiseAssert("not implemented")
|
||||
|
||||
method subscribeOffers*(market: Market,
|
||||
method subscribeFulfillment*(market: Market,
|
||||
requestId: array[32, byte],
|
||||
callback: OnOffer):
|
||||
Future[Subscription] {.base, async.} =
|
||||
raiseAssert("not implemented")
|
||||
|
||||
method subscribeSelection*(market: Market,
|
||||
requestId: array[32, byte],
|
||||
callback: OnSelect):
|
||||
callback: OnFulfillment):
|
||||
Future[Subscription] {.base, async.} =
|
||||
raiseAssert("not implemented")
|
||||
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import std/sequtils
|
||||
import std/heapqueue
|
||||
import pkg/questionable
|
||||
import pkg/codex/market
|
||||
|
||||
export market
|
||||
|
@ -8,61 +6,37 @@ export market
|
|||
type
|
||||
MockMarket* = ref object of Market
|
||||
requested*: seq[StorageRequest]
|
||||
offered*: seq[StorageOffer]
|
||||
selected*: seq[array[32, byte]]
|
||||
fulfilled*: seq[Fulfillment]
|
||||
subscriptions: Subscriptions
|
||||
time: UInt256
|
||||
waiting: HeapQueue[Expiry]
|
||||
Fulfillment* = object
|
||||
requestId: array[32, byte]
|
||||
proof: seq[byte]
|
||||
Subscriptions = object
|
||||
onRequest: seq[RequestSubscription]
|
||||
onOffer: seq[OfferSubscription]
|
||||
onSelect: seq[SelectSubscription]
|
||||
onFulfillment: seq[FulfillmentSubscription]
|
||||
RequestSubscription* = ref object of Subscription
|
||||
market: MockMarket
|
||||
callback: OnRequest
|
||||
OfferSubscription* = ref object of Subscription
|
||||
FulfillmentSubscription* = ref object of Subscription
|
||||
market: MockMarket
|
||||
requestId: array[32, byte]
|
||||
callback: OnOffer
|
||||
SelectSubscription* = ref object of Subscription
|
||||
market: MockMarket
|
||||
requestId: array[32, byte]
|
||||
callback: OnSelect
|
||||
Expiry = object
|
||||
future: Future[void]
|
||||
expiry: UInt256
|
||||
callback: OnFulfillment
|
||||
|
||||
method requestStorage*(market: MockMarket,
|
||||
request: StorageRequest):
|
||||
Future[StorageRequest] {.async.} =
|
||||
market.requested.add(request)
|
||||
let subscriptions = market.subscriptions.onRequest
|
||||
for subscription in subscriptions:
|
||||
for subscription in market.subscriptions.onRequest:
|
||||
subscription.callback(request.id, request.ask)
|
||||
return request
|
||||
|
||||
method offerStorage*(market: MockMarket,
|
||||
offer: StorageOffer):
|
||||
Future[StorageOffer] {.async.} =
|
||||
market.offered.add(offer)
|
||||
let subscriptions = market.subscriptions.onOffer
|
||||
for subscription in subscriptions:
|
||||
if subscription.requestId == offer.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 fulfillRequest*(market: MockMarket,
|
||||
requestId: array[32, byte],
|
||||
proof: seq[byte]) {.async.} =
|
||||
market.fulfilled.add(Fulfillment(requestId: requestId, proof: proof))
|
||||
for subscription in market.subscriptions.onFulfillment:
|
||||
if subscription.requestId == requestId:
|
||||
subscription.callback(requestId)
|
||||
|
||||
method subscribeRequests*(market: MockMarket,
|
||||
callback: OnRequest):
|
||||
|
@ -74,35 +48,20 @@ method subscribeRequests*(market: MockMarket,
|
|||
market.subscriptions.onRequest.add(subscription)
|
||||
return subscription
|
||||
|
||||
method subscribeOffers*(market: MockMarket,
|
||||
method subscribeFulfillment*(market: MockMarket,
|
||||
requestId: array[32, byte],
|
||||
callback: OnOffer):
|
||||
callback: OnFulfillment):
|
||||
Future[Subscription] {.async.} =
|
||||
let subscription = OfferSubscription(
|
||||
let subscription = FulfillmentSubscription(
|
||||
market: market,
|
||||
requestId: requestId,
|
||||
callback: callback
|
||||
)
|
||||
market.subscriptions.onOffer.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)
|
||||
market.subscriptions.onFulfillment.add(subscription)
|
||||
return subscription
|
||||
|
||||
method unsubscribe*(subscription: RequestSubscription) {.async.} =
|
||||
subscription.market.subscriptions.onRequest.keepItIf(it != subscription)
|
||||
|
||||
method unsubscribe*(subscription: OfferSubscription) {.async.} =
|
||||
subscription.market.subscriptions.onOffer.keepItIf(it != subscription)
|
||||
|
||||
method unsubscribe*(subscription: SelectSubscription) {.async.} =
|
||||
subscription.market.subscriptions.onSelect.keepItIf(it != subscription)
|
||||
method unsubscribe*(subscription: FulfillmentSubscription) {.async.} =
|
||||
subscription.market.subscriptions.onFulfillment.keepItIf(it != subscription)
|
||||
|
|
|
@ -6,6 +6,7 @@ import ./examples
|
|||
import ./time
|
||||
|
||||
ethersuite "On-Chain Market":
|
||||
let proof = seq[byte].example
|
||||
|
||||
var market: OnChainMarket
|
||||
var storage: Storage
|
||||
|
@ -61,96 +62,40 @@ ethersuite "On-Chain Market":
|
|||
check receivedAsks == @[request.ask]
|
||||
await subscription.unsubscribe()
|
||||
|
||||
test "supports storage offers":
|
||||
test "supports fulfilling of requests":
|
||||
await token.approve(storage.address, request.ask.maxPrice)
|
||||
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)
|
||||
discard await market.requestStorage(request)
|
||||
var offerWithoutHost = offer
|
||||
offerWithoutHost.host = Address.default
|
||||
let submitted = await market.offerStorage(offerWithoutHost)
|
||||
check submitted.host == accounts[0]
|
||||
|
||||
test "supports offer subscriptions":
|
||||
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]
|
||||
var receivedIds: seq[array[32, byte]]
|
||||
proc onFulfillment(id: array[32, byte]) =
|
||||
receivedIds.add(id)
|
||||
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
||||
await market.fulfillRequest(request.id, proof)
|
||||
check receivedIds == @[request.id]
|
||||
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 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)
|
||||
await token.approve(storage.address, otherrequest.ask.maxPrice)
|
||||
discard await market.requestStorage(otherRequest)
|
||||
|
||||
var submitted: seq[StorageOffer]
|
||||
proc onOffer(offer: StorageOffer) =
|
||||
submitted.add(offer)
|
||||
var receivedIds: seq[array[32, byte]]
|
||||
proc onFulfillment(id: array[32, byte]) =
|
||||
receivedIds.add(id)
|
||||
|
||||
let subscription = await market.subscribeOffers(request.id, onOffer)
|
||||
let subscription = await market.subscribeFulfillment(request.id, onFulfillment)
|
||||
|
||||
discard await market.offerStorage(offer)
|
||||
discard await market.offerStorage(otherOffer)
|
||||
await market.fulfillRequest(request.id, proof)
|
||||
await market.fulfillRequest(otherRequest.id, proof)
|
||||
|
||||
check submitted == @[offer]
|
||||
|
||||
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]
|
||||
check receivedIds == @[request.id]
|
||||
|
||||
await subscription.unsubscribe()
|
||||
|
|
Loading…
Reference in New Issue