2022-03-29 14:17:20 +00:00
|
|
|
import pkg/chronos
|
2022-03-28 15:24:28 +00:00
|
|
|
import dagger/contracts
|
|
|
|
import dagger/contracts/testtoken
|
2022-03-29 14:17:20 +00:00
|
|
|
import ./ethertest
|
2022-03-28 15:24:28 +00:00
|
|
|
import ./examples
|
2022-03-29 14:17:20 +00:00
|
|
|
import ./time
|
2022-03-28 15:24:28 +00:00
|
|
|
|
|
|
|
ethersuite "On-Chain Market":
|
|
|
|
|
|
|
|
var market: OnChainMarket
|
|
|
|
var storage: Storage
|
|
|
|
var token: TestToken
|
2022-03-29 07:47:49 +00:00
|
|
|
var request: StorageRequest
|
|
|
|
var offer: StorageOffer
|
2022-03-28 15:24:28 +00:00
|
|
|
|
|
|
|
setup:
|
|
|
|
let deployment = deployment()
|
|
|
|
storage = Storage.new(!deployment.address(Storage), provider.getSigner())
|
|
|
|
token = TestToken.new(!deployment.address(TestToken), provider.getSigner())
|
|
|
|
await token.mint(accounts[0], 1000.u256)
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
let collateral = await storage.collateralAmount()
|
|
|
|
await token.approve(storage.address, collateral)
|
|
|
|
await storage.deposit(collateral)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
market = OnChainMarket.new(storage)
|
|
|
|
|
2022-03-29 07:47:49 +00:00
|
|
|
request = StorageRequest.example
|
|
|
|
offer = StorageOffer.example
|
|
|
|
request.client = accounts[0]
|
|
|
|
offer.host = accounts[0]
|
|
|
|
offer.requestId = request.id
|
2022-04-11 18:03:55 +00:00
|
|
|
offer.price = request.ask.maxPrice
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-03-28 15:28:51 +00:00
|
|
|
test "fails to instantiate when contract does not have a signer":
|
|
|
|
let storageWithoutSigner = storage.connect(provider)
|
|
|
|
expect AssertionError:
|
|
|
|
discard OnChainMarket.new(storageWithoutSigner)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
test "supports storage requests":
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
check (await market.requestStorage(request)) == request
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
test "sets client address when submitting storage request":
|
|
|
|
var requestWithoutClient = request
|
|
|
|
requestWithoutClient.client = Address.default
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
let submitted = await market.requestStorage(requestWithoutClient)
|
|
|
|
check submitted.client == accounts[0]
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-03-31 08:46:03 +00:00
|
|
|
test "supports request subscriptions":
|
2022-04-11 18:03:55 +00:00
|
|
|
var receivedIds: seq[array[32, byte]]
|
|
|
|
var receivedAsks: seq[StorageAsk]
|
|
|
|
proc onRequest(id: array[32, byte], ask: StorageAsk) =
|
|
|
|
receivedIds.add(id)
|
|
|
|
receivedAsks.add(ask)
|
2022-03-29 07:47:49 +00:00
|
|
|
let subscription = await market.subscribeRequests(onRequest)
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-04-11 18:03:55 +00:00
|
|
|
check receivedIds == @[request.id]
|
|
|
|
check receivedAsks == @[request.ask]
|
2022-03-31 08:46:03 +00:00
|
|
|
await subscription.unsubscribe()
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
test "supports storage offers":
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-31 09:07:39 +00:00
|
|
|
check (await market.offerStorage(offer)) == offer
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
test "sets host address when submitting storage offer":
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 09:07:39 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-29 07:47:49 +00:00
|
|
|
var offerWithoutHost = offer
|
|
|
|
offerWithoutHost.host = Address.default
|
2022-03-31 09:07:39 +00:00
|
|
|
let submitted = await market.offerStorage(offerWithoutHost)
|
|
|
|
check submitted.host == accounts[0]
|
2022-03-29 07:47:49 +00:00
|
|
|
|
2022-03-31 09:07:39 +00:00
|
|
|
test "supports offer subscriptions":
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-31 09:07:39 +00:00
|
|
|
var received: seq[StorageOffer]
|
2022-03-29 07:47:49 +00:00
|
|
|
proc onOffer(offer: StorageOffer) =
|
2022-03-31 09:07:39 +00:00
|
|
|
received.add(offer)
|
2022-03-29 07:47:49 +00:00
|
|
|
let subscription = await market.subscribeOffers(request.id, onOffer)
|
2022-03-31 09:07:39 +00:00
|
|
|
discard await market.offerStorage(offer)
|
|
|
|
check received == @[offer]
|
|
|
|
await subscription.unsubscribe()
|
2022-03-29 08:53:05 +00:00
|
|
|
|
|
|
|
test "subscribes only to offers for a certain request":
|
|
|
|
var otherRequest = StorageRequest.example
|
|
|
|
var otherOffer = StorageOffer.example
|
|
|
|
otherRequest.client = accounts[0]
|
|
|
|
otherOffer.host = accounts[0]
|
|
|
|
otherOffer.requestId = otherRequest.id
|
2022-04-11 18:03:55 +00:00
|
|
|
otherOffer.price = otherrequest.ask.maxPrice
|
2022-03-29 08:53:05 +00:00
|
|
|
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, otherrequest.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(otherRequest)
|
2022-03-29 08:53:05 +00:00
|
|
|
|
|
|
|
var submitted: seq[StorageOffer]
|
|
|
|
proc onOffer(offer: StorageOffer) =
|
|
|
|
submitted.add(offer)
|
|
|
|
|
|
|
|
let subscription = await market.subscribeOffers(request.id, onOffer)
|
|
|
|
|
2022-03-31 09:07:39 +00:00
|
|
|
discard await market.offerStorage(offer)
|
|
|
|
discard await market.offerStorage(otherOffer)
|
2022-03-29 08:53:05 +00:00
|
|
|
|
|
|
|
check submitted == @[offer]
|
|
|
|
|
|
|
|
await subscription.unsubscribe()
|
2022-03-29 09:20:07 +00:00
|
|
|
|
|
|
|
test "supports selection of an offer":
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-31 09:07:39 +00:00
|
|
|
discard await market.offerStorage(offer)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
|
|
|
var selected: seq[array[32, byte]]
|
|
|
|
proc onSelect(offerId: array[32, byte]) =
|
|
|
|
selected.add(offerId)
|
|
|
|
let subscription = await market.subscribeSelection(request.id, onSelect)
|
|
|
|
|
|
|
|
await market.selectOffer(offer.id)
|
|
|
|
|
|
|
|
check selected == @[offer.id]
|
|
|
|
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
|
|
|
test "subscribes only to selection for a certain request":
|
|
|
|
var otherRequest = StorageRequest.example
|
|
|
|
var otherOffer = StorageOffer.example
|
|
|
|
otherRequest.client = accounts[0]
|
|
|
|
otherOffer.host = accounts[0]
|
|
|
|
otherOffer.requestId = otherRequest.id
|
2022-04-11 18:03:55 +00:00
|
|
|
otherOffer.price = otherrequest.ask.maxPrice
|
2022-03-29 09:20:07 +00:00
|
|
|
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, request.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(request)
|
2022-03-31 09:07:39 +00:00
|
|
|
discard await market.offerStorage(offer)
|
2022-04-11 18:03:55 +00:00
|
|
|
await token.approve(storage.address, otherrequest.ask.maxPrice)
|
2022-03-31 08:46:03 +00:00
|
|
|
discard await market.requestStorage(otherRequest)
|
2022-03-31 09:07:39 +00:00
|
|
|
discard await market.offerStorage(otherOffer)
|
2022-03-29 09:20:07 +00:00
|
|
|
|
|
|
|
var selected: seq[array[32, byte]]
|
|
|
|
proc onSelect(offerId: array[32, byte]) =
|
|
|
|
selected.add(offerId)
|
|
|
|
|
|
|
|
let subscription = await market.subscribeSelection(request.id, onSelect)
|
|
|
|
|
|
|
|
await market.selectOffer(offer.id)
|
|
|
|
await market.selectOffer(otherOffer.id)
|
|
|
|
|
|
|
|
check selected == @[offer.id]
|
|
|
|
|
|
|
|
await subscription.unsubscribe()
|
2022-03-29 14:17:20 +00:00
|
|
|
|
2022-03-30 11:01:44 +00:00
|
|
|
test "can retrieve current block time":
|
|
|
|
let latestBlock = !await provider.getBlock(BlockTag.latest)
|
|
|
|
check (await market.getTime()) == latestBlock.timestamp
|
|
|
|
|
2022-03-29 14:17:20 +00:00
|
|
|
test "supports waiting for expiry of a request or offer":
|
2022-03-30 15:04:35 +00:00
|
|
|
let pollInterval = 200.milliseconds
|
2022-03-29 14:17:20 +00:00
|
|
|
market.pollInterval = pollInterval
|
|
|
|
|
|
|
|
proc waitForPoll {.async.} =
|
2022-03-30 15:04:35 +00:00
|
|
|
await sleepAsync(pollInterval * 2)
|
2022-03-29 14:17:20 +00:00
|
|
|
|
|
|
|
let future = market.waitUntil(request.expiry)
|
|
|
|
check not future.completed
|
|
|
|
await provider.advanceTimeTo(request.expiry - 1)
|
|
|
|
await waitForPoll()
|
|
|
|
check not future.completed
|
|
|
|
await provider.advanceTimeTo(request.expiry)
|
|
|
|
await waitForPoll()
|
|
|
|
check future.completed
|