[market] Add market.getHost()

This commit is contained in:
Mark Spanbroek 2022-07-04 12:11:18 +02:00 committed by markspanbroek
parent b414ecd67e
commit 03b92a2067
5 changed files with 39 additions and 4 deletions

View File

@ -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.} =

View File

@ -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.}

View File

@ -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.} =

View File

@ -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,
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]) {.async.} =
market.fulfilled.add(Fulfillment(requestId: requestId, proof: proof))
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.} =

View File

@ -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)