[market] Add market.getHost()
This commit is contained in:
parent
b414ecd67e
commit
03b92a2067
|
@ -39,6 +39,14 @@ method getRequest(market: OnChainMarket,
|
||||||
else:
|
else:
|
||||||
return none StorageRequest
|
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,
|
method fulfillRequest(market: OnChainMarket,
|
||||||
requestId: array[32, byte],
|
requestId: array[32, byte],
|
||||||
proof: seq[byte]) {.async.} =
|
proof: seq[byte]) {.async.} =
|
||||||
|
|
|
@ -31,6 +31,7 @@ proc balanceOf*(storage: Storage, account: Address): UInt256 {.contract, view.}
|
||||||
proc requestStorage*(storage: Storage, request: StorageRequest) {.contract.}
|
proc requestStorage*(storage: Storage, request: StorageRequest) {.contract.}
|
||||||
proc fulfillRequest*(storage: Storage, id: Id, proof: seq[byte]) {.contract.}
|
proc fulfillRequest*(storage: Storage, id: Id, proof: seq[byte]) {.contract.}
|
||||||
proc getRequest*(storage: Storage, id: Id): StorageRequest {.contract, view.}
|
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.}
|
proc finishContract*(storage: Storage, id: Id) {.contract.}
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,10 @@ method getRequest*(market: Market,
|
||||||
Future[?StorageRequest] {.base, async.} =
|
Future[?StorageRequest] {.base, async.} =
|
||||||
raiseAssert("not implemented")
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
|
method getHost*(market: Market,
|
||||||
|
requestId: array[32, byte]): Future[?Address] {.base, async.} =
|
||||||
|
raiseAssert("not implemented")
|
||||||
|
|
||||||
method fulfillRequest*(market: Market,
|
method fulfillRequest*(market: Market,
|
||||||
requestId: array[32, byte],
|
requestId: array[32, byte],
|
||||||
proof: seq[byte]) {.base, async.} =
|
proof: seq[byte]) {.base, async.} =
|
||||||
|
|
|
@ -11,6 +11,7 @@ type
|
||||||
Fulfillment* = object
|
Fulfillment* = object
|
||||||
requestId: array[32, byte]
|
requestId: array[32, byte]
|
||||||
proof: seq[byte]
|
proof: seq[byte]
|
||||||
|
host: Address
|
||||||
Subscriptions = object
|
Subscriptions = object
|
||||||
onRequest: seq[RequestSubscription]
|
onRequest: seq[RequestSubscription]
|
||||||
onFulfillment: seq[FulfillmentSubscription]
|
onFulfillment: seq[FulfillmentSubscription]
|
||||||
|
@ -37,14 +38,28 @@ method getRequest(market: MockMarket,
|
||||||
return some request
|
return some request
|
||||||
return none StorageRequest
|
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],
|
requestId: array[32, byte],
|
||||||
proof: seq[byte]) {.async.} =
|
proof: seq[byte],
|
||||||
market.fulfilled.add(Fulfillment(requestId: requestId, proof: proof))
|
host: Address) =
|
||||||
|
let fulfillment = Fulfillment(requestId: requestId, proof: proof, host: host)
|
||||||
|
market.fulfilled.add(fulfillment)
|
||||||
for subscription in market.subscriptions.onFulfillment:
|
for subscription in market.subscriptions.onFulfillment:
|
||||||
if subscription.requestId == requestId:
|
if subscription.requestId == requestId:
|
||||||
subscription.callback(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,
|
method subscribeRequests*(market: MockMarket,
|
||||||
callback: OnRequest):
|
callback: OnRequest):
|
||||||
Future[Subscription] {.async.} =
|
Future[Subscription] {.async.} =
|
||||||
|
|
|
@ -73,6 +73,13 @@ ethersuite "On-Chain Market":
|
||||||
discard await market.requestStorage(request)
|
discard await market.requestStorage(request)
|
||||||
await market.fulfillRequest(request.id, proof)
|
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":
|
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)
|
||||||
|
|
Loading…
Reference in New Issue