2022-03-29 07:47:49 +00:00
|
|
|
import std/times
|
2022-03-28 15:24:28 +00:00
|
|
|
import ./ethertest
|
|
|
|
import dagger/contracts
|
|
|
|
import dagger/contracts/testtoken
|
|
|
|
import ./examples
|
|
|
|
|
|
|
|
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
|
|
|
|
offer.price = request.maxPrice
|
|
|
|
|
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":
|
|
|
|
var submitted: seq[StorageRequest]
|
|
|
|
proc onRequest(request: StorageRequest) =
|
|
|
|
submitted.add(request)
|
|
|
|
let subscription = await market.subscribeRequests(onRequest)
|
2022-03-29 07:47:49 +00:00
|
|
|
await token.approve(storage.address, request.maxPrice)
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
await market.requestStorage(request)
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
check submitted == @[request]
|
|
|
|
|
2022-03-28 15:24:28 +00:00
|
|
|
await subscription.unsubscribe()
|
2022-03-29 07:47:49 +00:00
|
|
|
|
|
|
|
test "sets client address when submitting storage request":
|
|
|
|
var requestWithoutClient = request
|
|
|
|
requestWithoutClient.client = Address.default
|
|
|
|
|
|
|
|
var submitted: StorageRequest
|
|
|
|
proc onRequest(request: StorageRequest) =
|
|
|
|
submitted = request
|
|
|
|
let subscription = await market.subscribeRequests(onRequest)
|
|
|
|
await token.approve(storage.address, request.maxPrice)
|
|
|
|
|
|
|
|
await market.requestStorage(requestWithoutClient)
|
|
|
|
|
|
|
|
check submitted.client == accounts[0]
|
|
|
|
|
|
|
|
test "supports storage offers":
|
|
|
|
await token.approve(storage.address, request.maxPrice)
|
|
|
|
await market.requestStorage(request)
|
|
|
|
|
|
|
|
var submitted: seq[StorageOffer]
|
|
|
|
proc onOffer(offer: StorageOffer) =
|
|
|
|
submitted.add(offer)
|
|
|
|
let subscription = await market.subscribeOffers(request.id, onOffer)
|
|
|
|
|
|
|
|
await market.offerStorage(offer)
|
|
|
|
|
|
|
|
check submitted == @[offer]
|
|
|
|
|
|
|
|
await subscription.unsubscribe()
|
|
|
|
|
|
|
|
test "sets host address when submitting storage offer":
|
|
|
|
var offerWithoutHost = offer
|
|
|
|
offerWithoutHost.host = Address.default
|
|
|
|
|
|
|
|
await token.approve(storage.address, request.maxPrice)
|
|
|
|
await market.requestStorage(request)
|
|
|
|
|
|
|
|
var submitted: StorageOffer
|
|
|
|
proc onOffer(offer: StorageOffer) =
|
|
|
|
submitted = offer
|
|
|
|
let subscription = await market.subscribeOffers(request.id, onOffer)
|
|
|
|
|
|
|
|
await market.offerStorage(offerWithoutHost)
|
|
|
|
|
|
|
|
check submitted.host == accounts[0]
|