From 03b92a20671fd8a2d872a661af783d810b31caf8 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Mon, 4 Jul 2022 12:11:18 +0200 Subject: [PATCH] [market] Add market.getHost() --- codex/contracts/market.nim | 8 ++++++++ codex/contracts/storage.nim | 1 + codex/market.nim | 4 ++++ tests/codex/helpers/mockmarket.nim | 23 +++++++++++++++++++---- tests/contracts/testMarket.nim | 7 +++++++ 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/codex/contracts/market.nim b/codex/contracts/market.nim index 128c9d63..1a8445f6 100644 --- a/codex/contracts/market.nim +++ b/codex/contracts/market.nim @@ -39,6 +39,14 @@ method getRequest(market: OnChainMarket, else: return none StorageRequest +method getHost(market: OnChainMarket, + id: array[32, byte]): Future[?Address] {.async.} = + let address = await market.contract.getHost(id) + if address != Address.default: + return some address + else: + return none Address + method fulfillRequest(market: OnChainMarket, requestId: array[32, byte], proof: seq[byte]) {.async.} = diff --git a/codex/contracts/storage.nim b/codex/contracts/storage.nim index 94e77065..9e1f30ae 100644 --- a/codex/contracts/storage.nim +++ b/codex/contracts/storage.nim @@ -31,6 +31,7 @@ proc balanceOf*(storage: Storage, account: Address): UInt256 {.contract, view.} proc requestStorage*(storage: Storage, request: StorageRequest) {.contract.} proc fulfillRequest*(storage: Storage, id: Id, proof: seq[byte]) {.contract.} proc getRequest*(storage: Storage, id: Id): StorageRequest {.contract, view.} +proc getHost*(storage: Storage, id: Id): Address {.contract, view.} proc finishContract*(storage: Storage, id: Id) {.contract.} diff --git a/codex/market.nim b/codex/market.nim index 1d048f09..91a19299 100644 --- a/codex/market.nim +++ b/codex/market.nim @@ -25,6 +25,10 @@ method getRequest*(market: Market, Future[?StorageRequest] {.base, async.} = raiseAssert("not implemented") +method getHost*(market: Market, + requestId: array[32, byte]): Future[?Address] {.base, async.} = + raiseAssert("not implemented") + method fulfillRequest*(market: Market, requestId: array[32, byte], proof: seq[byte]) {.base, async.} = diff --git a/tests/codex/helpers/mockmarket.nim b/tests/codex/helpers/mockmarket.nim index 67faa7c4..4e534ba3 100644 --- a/tests/codex/helpers/mockmarket.nim +++ b/tests/codex/helpers/mockmarket.nim @@ -11,6 +11,7 @@ type Fulfillment* = object requestId: array[32, byte] proof: seq[byte] + host: Address Subscriptions = object onRequest: seq[RequestSubscription] onFulfillment: seq[FulfillmentSubscription] @@ -37,14 +38,28 @@ method getRequest(market: MockMarket, return some request return none StorageRequest -method fulfillRequest*(market: MockMarket, - requestId: array[32, byte], - proof: seq[byte]) {.async.} = - market.fulfilled.add(Fulfillment(requestId: requestId, proof: proof)) +method getHost(market: MockMarket, + id: array[32, byte]): Future[?Address] {.async.} = + for fulfillment in market.fulfilled: + if fulfillment.requestId == id: + return some fulfillment.host + return none Address + +proc fulfillRequest*(market: MockMarket, + requestId: array[32, byte], + proof: seq[byte], + host: Address) = + let fulfillment = Fulfillment(requestId: requestId, proof: proof, host: host) + market.fulfilled.add(fulfillment) for subscription in market.subscriptions.onFulfillment: if subscription.requestId == requestId: subscription.callback(requestId) +method fulfillRequest*(market: MockMarket, + requestId: array[32, byte], + proof: seq[byte]) {.async.} = + market.fulfillRequest(requestid, proof, Address.default) + method subscribeRequests*(market: MockMarket, callback: OnRequest): Future[Subscription] {.async.} = diff --git a/tests/contracts/testMarket.nim b/tests/contracts/testMarket.nim index f39c629a..9f75e286 100644 --- a/tests/contracts/testMarket.nim +++ b/tests/contracts/testMarket.nim @@ -73,6 +73,13 @@ ethersuite "On-Chain Market": discard await market.requestStorage(request) await market.fulfillRequest(request.id, proof) + test "can retrieve host that fulfilled request": + await token.approve(storage.address, request.ask.maxPrice) + discard await market.requestStorage(request) + check (await market.getHost(request.id)) == none Address + await market.fulfillRequest(request.id, proof) + check (await market.getHost(request.id)) == some accounts[0] + test "support fulfillment subscriptions": await token.approve(storage.address, request.ask.maxPrice) discard await market.requestStorage(request)