[market] requestStorage() returns submitted request

This commit is contained in:
Mark Spanbroek 2022-03-31 10:46:03 +02:00 committed by markspanbroek
parent 2ffde32772
commit 94e34e7d28
6 changed files with 36 additions and 35 deletions

View File

@ -27,10 +27,13 @@ func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
pollInterval: DefaultPollInterval pollInterval: DefaultPollInterval
) )
method requestStorage(market: OnChainMarket, request: StorageRequest) {.async.} = method requestStorage(market: OnChainMarket,
request: StorageRequest):
Future[StorageRequest] {.async.} =
var request = request var request = request
request.client = await market.signer.getAddress() request.client = await market.signer.getAddress()
await market.contract.requestStorage(request) await market.contract.requestStorage(request)
return request
method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} = method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} =
var offer = offer var offer = offer

View File

@ -14,7 +14,9 @@ type
OnOffer* = proc(offer: StorageOffer) {.gcsafe, upraises:[].} OnOffer* = proc(offer: StorageOffer) {.gcsafe, upraises:[].}
OnSelect* = proc(offerId: array[32, byte]) {.gcsafe, upraises: [].} OnSelect* = proc(offerId: array[32, byte]) {.gcsafe, upraises: [].}
method requestStorage*(market: Market, request: StorageRequest) {.base, async.} = method requestStorage*(market: Market,
request: StorageRequest):
Future[StorageRequest] {.base, async.} =
raiseAssert("not implemented") raiseAssert("not implemented")
method offerStorage*(market: Market, offer: StorageOffer) {.base, async.} = method offerStorage*(market: Market, offer: StorageOffer) {.base, async.} =

View File

@ -72,10 +72,9 @@ proc run(purchase: Purchase) {.async.} =
proc onOffer(offer: StorageOffer) = proc onOffer(offer: StorageOffer) =
purchase.offers.add(offer) purchase.offers.add(offer)
let market = purchase.market let market = purchase.market
let request = purchase.request purchase.request = await market.requestStorage(purchase.request)
let subscription = await market.subscribeOffers(request.id, onOffer) let subscription = await market.subscribeOffers(purchase.request.id, onOffer)
await market.requestStorage(request) await market.waitUntil(purchase.request.expiry)
await market.waitUntil(request.expiry)
await purchase.selectOffer() await purchase.selectOffer()
await subscription.unsubscribe() await subscription.unsubscribe()

View File

@ -38,35 +38,29 @@ ethersuite "On-Chain Market":
discard OnChainMarket.new(storageWithoutSigner) discard OnChainMarket.new(storageWithoutSigner)
test "supports storage requests": test "supports storage requests":
var submitted: seq[StorageRequest]
proc onRequest(request: StorageRequest) =
submitted.add(request)
let subscription = await market.subscribeRequests(onRequest)
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
check (await market.requestStorage(request)) == request
await market.requestStorage(request)
check submitted == @[request]
await subscription.unsubscribe()
test "sets client address when submitting storage request": test "sets client address when submitting storage request":
var requestWithoutClient = request var requestWithoutClient = request
requestWithoutClient.client = Address.default requestWithoutClient.client = Address.default
await token.approve(storage.address, request.maxPrice)
let submitted = await market.requestStorage(requestWithoutClient)
check submitted.client == accounts[0]
var submitted: StorageRequest test "supports request subscriptions":
var received: seq[StorageRequest]
proc onRequest(request: StorageRequest) = proc onRequest(request: StorageRequest) =
submitted = request received.add(request)
let subscription = await market.subscribeRequests(onRequest) let subscription = await market.subscribeRequests(onRequest)
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
discard await market.requestStorage(request)
await market.requestStorage(requestWithoutClient) check received == @[request]
await subscription.unsubscribe()
check submitted.client == accounts[0]
test "supports storage offers": test "supports storage offers":
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
await market.requestStorage(request) discard await market.requestStorage(request)
var submitted: seq[StorageOffer] var submitted: seq[StorageOffer]
proc onOffer(offer: StorageOffer) = proc onOffer(offer: StorageOffer) =
@ -84,7 +78,7 @@ ethersuite "On-Chain Market":
offerWithoutHost.host = Address.default offerWithoutHost.host = Address.default
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
await market.requestStorage(request) discard await market.requestStorage(request)
var submitted: StorageOffer var submitted: StorageOffer
proc onOffer(offer: StorageOffer) = proc onOffer(offer: StorageOffer) =
@ -104,9 +98,9 @@ ethersuite "On-Chain Market":
otherOffer.price = otherRequest.maxPrice otherOffer.price = otherRequest.maxPrice
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
await market.requestStorage(request) discard await market.requestStorage(request)
await token.approve(storage.address, otherRequest.maxPrice) await token.approve(storage.address, otherRequest.maxPrice)
await market.requestStorage(otherRequest) discard await market.requestStorage(otherRequest)
var submitted: seq[StorageOffer] var submitted: seq[StorageOffer]
proc onOffer(offer: StorageOffer) = proc onOffer(offer: StorageOffer) =
@ -123,7 +117,7 @@ ethersuite "On-Chain Market":
test "supports selection of an offer": test "supports selection of an offer":
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
await market.requestStorage(request) discard await market.requestStorage(request)
await market.offerStorage(offer) await market.offerStorage(offer)
var selected: seq[array[32, byte]] var selected: seq[array[32, byte]]
@ -146,10 +140,10 @@ ethersuite "On-Chain Market":
otherOffer.price = otherRequest.maxPrice otherOffer.price = otherRequest.maxPrice
await token.approve(storage.address, request.maxPrice) await token.approve(storage.address, request.maxPrice)
await market.requestStorage(request) discard await market.requestStorage(request)
await market.offerStorage(offer) await market.offerStorage(offer)
await token.approve(storage.address, otherRequest.maxPrice) await token.approve(storage.address, otherRequest.maxPrice)
await market.requestStorage(otherRequest) discard await market.requestStorage(otherRequest)
await market.offerStorage(otherOffer) await market.offerStorage(otherOffer)
var selected: seq[array[32, byte]] var selected: seq[array[32, byte]]

View File

@ -32,11 +32,14 @@ type
future: Future[void] future: Future[void]
expiry: UInt256 expiry: UInt256
method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} = method requestStorage*(market: MockMarket,
request: StorageRequest):
Future[StorageRequest] {.async.} =
market.requested.add(request) market.requested.add(request)
let subscriptions = market.subscriptions.onRequest let subscriptions = market.subscriptions.onRequest
for subscription in subscriptions: for subscription in subscriptions:
subscription.callback(request) subscription.callback(request)
return request
method offerStorage*(market: MockMarket, offer: StorageOffer) {.async.} = method offerStorage*(market: MockMarket, offer: StorageOffer) {.async.} =
market.offered.add(offer) market.offered.add(offer)

View File

@ -42,7 +42,7 @@ suite "Sales":
sales.add(availability) sales.add(availability)
sales.start() sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
await market.requestStorage(request) discard await market.requestStorage(request)
check market.offered.len == 1 check market.offered.len == 1
check market.offered[0].price == 42.u256 check market.offered[0].price == 42.u256
sales.stop() sales.stop()
@ -52,7 +52,7 @@ suite "Sales":
sales.add(availability) sales.add(availability)
sales.start() sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
await market.requestStorage(request) discard await market.requestStorage(request)
check market.offered.len == 0 check market.offered.len == 0
sales.stop() sales.stop()
@ -61,7 +61,7 @@ suite "Sales":
sales.add(availability) sales.add(availability)
sales.start() sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
await market.requestStorage(request) discard await market.requestStorage(request)
check sales.available.len == 0 check sales.available.len == 0
sales.stop() sales.stop()
@ -71,7 +71,7 @@ suite "Sales":
sales.start() sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
let now = getTime().toUnix().u256 let now = getTime().toUnix().u256
await market.requestStorage(request) discard await market.requestStorage(request)
check market.offered[0].expiry == now + sales.offerExpiryInterval check market.offered[0].expiry == now + sales.offerExpiryInterval
sales.stop() sales.stop()
@ -83,7 +83,7 @@ suite "Sales":
selectedOffer = offer selectedOffer = offer
sales.start() sales.start()
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256) let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
await market.requestStorage(request) discard await market.requestStorage(request)
let offer = market.offered[0] let offer = market.offered[0]
await market.selectOffer(offer.id) await market.selectOffer(offer.id)
check selectedOffer == offer check selectedOffer == offer