[market] requestStorage() returns submitted request
This commit is contained in:
parent
2ffde32772
commit
94e34e7d28
|
@ -27,10 +27,13 @@ func new*(_: type OnChainMarket, contract: Storage): OnChainMarket =
|
|||
pollInterval: DefaultPollInterval
|
||||
)
|
||||
|
||||
method requestStorage(market: OnChainMarket, request: StorageRequest) {.async.} =
|
||||
method requestStorage(market: OnChainMarket,
|
||||
request: StorageRequest):
|
||||
Future[StorageRequest] {.async.} =
|
||||
var request = request
|
||||
request.client = await market.signer.getAddress()
|
||||
await market.contract.requestStorage(request)
|
||||
return request
|
||||
|
||||
method offerStorage(market: OnChainMarket, offer: StorageOffer) {.async.} =
|
||||
var offer = offer
|
||||
|
|
|
@ -14,7 +14,9 @@ type
|
|||
OnOffer* = proc(offer: StorageOffer) {.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")
|
||||
|
||||
method offerStorage*(market: Market, offer: StorageOffer) {.base, async.} =
|
||||
|
|
|
@ -72,10 +72,9 @@ proc run(purchase: Purchase) {.async.} =
|
|||
proc onOffer(offer: StorageOffer) =
|
||||
purchase.offers.add(offer)
|
||||
let market = purchase.market
|
||||
let request = purchase.request
|
||||
let subscription = await market.subscribeOffers(request.id, onOffer)
|
||||
await market.requestStorage(request)
|
||||
await market.waitUntil(request.expiry)
|
||||
purchase.request = await market.requestStorage(purchase.request)
|
||||
let subscription = await market.subscribeOffers(purchase.request.id, onOffer)
|
||||
await market.waitUntil(purchase.request.expiry)
|
||||
await purchase.selectOffer()
|
||||
await subscription.unsubscribe()
|
||||
|
||||
|
|
|
@ -38,35 +38,29 @@ ethersuite "On-Chain Market":
|
|||
discard OnChainMarket.new(storageWithoutSigner)
|
||||
|
||||
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 market.requestStorage(request)
|
||||
|
||||
check submitted == @[request]
|
||||
|
||||
await subscription.unsubscribe()
|
||||
check (await market.requestStorage(request)) == request
|
||||
|
||||
test "sets client address when submitting storage request":
|
||||
var requestWithoutClient = request
|
||||
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) =
|
||||
submitted = request
|
||||
received.add(request)
|
||||
let subscription = await market.subscribeRequests(onRequest)
|
||||
await token.approve(storage.address, request.maxPrice)
|
||||
|
||||
await market.requestStorage(requestWithoutClient)
|
||||
|
||||
check submitted.client == accounts[0]
|
||||
discard await market.requestStorage(request)
|
||||
check received == @[request]
|
||||
await subscription.unsubscribe()
|
||||
|
||||
test "supports storage offers":
|
||||
await token.approve(storage.address, request.maxPrice)
|
||||
await market.requestStorage(request)
|
||||
discard await market.requestStorage(request)
|
||||
|
||||
var submitted: seq[StorageOffer]
|
||||
proc onOffer(offer: StorageOffer) =
|
||||
|
@ -84,7 +78,7 @@ ethersuite "On-Chain Market":
|
|||
offerWithoutHost.host = Address.default
|
||||
|
||||
await token.approve(storage.address, request.maxPrice)
|
||||
await market.requestStorage(request)
|
||||
discard await market.requestStorage(request)
|
||||
|
||||
var submitted: StorageOffer
|
||||
proc onOffer(offer: StorageOffer) =
|
||||
|
@ -104,9 +98,9 @@ ethersuite "On-Chain Market":
|
|||
otherOffer.price = otherRequest.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 market.requestStorage(otherRequest)
|
||||
discard await market.requestStorage(otherRequest)
|
||||
|
||||
var submitted: seq[StorageOffer]
|
||||
proc onOffer(offer: StorageOffer) =
|
||||
|
@ -123,7 +117,7 @@ ethersuite "On-Chain Market":
|
|||
|
||||
test "supports selection of an offer":
|
||||
await token.approve(storage.address, request.maxPrice)
|
||||
await market.requestStorage(request)
|
||||
discard await market.requestStorage(request)
|
||||
await market.offerStorage(offer)
|
||||
|
||||
var selected: seq[array[32, byte]]
|
||||
|
@ -146,10 +140,10 @@ ethersuite "On-Chain Market":
|
|||
otherOffer.price = otherRequest.maxPrice
|
||||
|
||||
await token.approve(storage.address, request.maxPrice)
|
||||
await market.requestStorage(request)
|
||||
discard await market.requestStorage(request)
|
||||
await market.offerStorage(offer)
|
||||
await token.approve(storage.address, otherRequest.maxPrice)
|
||||
await market.requestStorage(otherRequest)
|
||||
discard await market.requestStorage(otherRequest)
|
||||
await market.offerStorage(otherOffer)
|
||||
|
||||
var selected: seq[array[32, byte]]
|
||||
|
|
|
@ -32,11 +32,14 @@ type
|
|||
future: Future[void]
|
||||
expiry: UInt256
|
||||
|
||||
method requestStorage*(market: MockMarket, request: StorageRequest) {.async.} =
|
||||
method requestStorage*(market: MockMarket,
|
||||
request: StorageRequest):
|
||||
Future[StorageRequest] {.async.} =
|
||||
market.requested.add(request)
|
||||
let subscriptions = market.subscriptions.onRequest
|
||||
for subscription in subscriptions:
|
||||
subscription.callback(request)
|
||||
return request
|
||||
|
||||
method offerStorage*(market: MockMarket, offer: StorageOffer) {.async.} =
|
||||
market.offered.add(offer)
|
||||
|
|
|
@ -42,7 +42,7 @@ suite "Sales":
|
|||
sales.add(availability)
|
||||
sales.start()
|
||||
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[0].price == 42.u256
|
||||
sales.stop()
|
||||
|
@ -52,7 +52,7 @@ suite "Sales":
|
|||
sales.add(availability)
|
||||
sales.start()
|
||||
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
|
||||
sales.stop()
|
||||
|
||||
|
@ -61,7 +61,7 @@ suite "Sales":
|
|||
sales.add(availability)
|
||||
sales.start()
|
||||
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
|
||||
sales.stop()
|
||||
|
||||
|
@ -71,7 +71,7 @@ suite "Sales":
|
|||
sales.start()
|
||||
let request = StorageRequest(duration:60.u256, size:100.u256, maxPrice:42.u256)
|
||||
let now = getTime().toUnix().u256
|
||||
await market.requestStorage(request)
|
||||
discard await market.requestStorage(request)
|
||||
check market.offered[0].expiry == now + sales.offerExpiryInterval
|
||||
sales.stop()
|
||||
|
||||
|
@ -83,7 +83,7 @@ suite "Sales":
|
|||
selectedOffer = offer
|
||||
sales.start()
|
||||
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]
|
||||
await market.selectOffer(offer.id)
|
||||
check selectedOffer == offer
|
||||
|
|
Loading…
Reference in New Issue