2022-02-16 09:50:00 +00:00
|
|
|
const { ethers } = require("hardhat")
|
|
|
|
const { expect } = require("chai")
|
2022-02-16 13:38:19 +00:00
|
|
|
const { exampleRequest, exampleOffer } = require("./examples")
|
|
|
|
const { now, hours } = require("./time")
|
2022-02-16 09:50:00 +00:00
|
|
|
const { keccak256, defaultAbiCoder } = ethers.utils
|
|
|
|
|
|
|
|
describe("Marketplace", function () {
|
2022-02-16 13:38:19 +00:00
|
|
|
const collateral = 100
|
2022-02-16 09:50:00 +00:00
|
|
|
|
|
|
|
let marketplace
|
|
|
|
let token
|
2022-02-17 10:00:18 +00:00
|
|
|
let client, host
|
|
|
|
let request, offer
|
2022-02-16 09:50:00 +00:00
|
|
|
|
|
|
|
beforeEach(async function () {
|
2022-02-17 10:00:18 +00:00
|
|
|
;[client, host] = await ethers.getSigners()
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
const TestToken = await ethers.getContractFactory("TestToken")
|
|
|
|
token = await TestToken.deploy()
|
2022-02-17 10:00:18 +00:00
|
|
|
await token.mint(client.address, 1000)
|
|
|
|
await token.mint(host.address, 1000)
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
const Marketplace = await ethers.getContractFactory("Marketplace")
|
2022-02-16 13:38:19 +00:00
|
|
|
marketplace = await Marketplace.deploy(token.address, collateral)
|
2022-02-17 10:00:18 +00:00
|
|
|
|
|
|
|
request = exampleRequest()
|
|
|
|
request.client = client.address
|
|
|
|
|
|
|
|
offer = exampleOffer()
|
|
|
|
offer.host = host.address
|
|
|
|
offer.requestId = requestId(request)
|
2022-02-16 09:50:00 +00:00
|
|
|
})
|
|
|
|
|
2022-02-17 10:00:18 +00:00
|
|
|
function switchAccount(account) {
|
|
|
|
token = token.connect(account)
|
|
|
|
marketplace = marketplace.connect(account)
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
describe("requesting storage", function () {
|
2022-02-17 10:00:18 +00:00
|
|
|
beforeEach(function () {
|
|
|
|
switchAccount(client)
|
|
|
|
})
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
it("emits event when storage is requested", async function () {
|
|
|
|
await token.approve(marketplace.address, request.maxPrice)
|
|
|
|
await expect(marketplace.requestStorage(request))
|
|
|
|
.to.emit(marketplace, "StorageRequested")
|
|
|
|
.withArgs(requestId(request), requestToArray(request))
|
|
|
|
})
|
|
|
|
|
2022-02-17 10:00:18 +00:00
|
|
|
it("rejects request with invalid client address", async function () {
|
|
|
|
let invalid = { ...request, client: host.address }
|
|
|
|
await token.approve(marketplace.address, invalid.maxPrice)
|
|
|
|
await expect(marketplace.requestStorage(invalid)).to.be.revertedWith(
|
|
|
|
"Invalid client address"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
it("rejects request with insufficient payment", async function () {
|
|
|
|
let insufficient = request.maxPrice - 1
|
|
|
|
await token.approve(marketplace.address, insufficient)
|
|
|
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
|
|
|
"ERC20: transfer amount exceeds allowance"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects resubmission of request", async function () {
|
|
|
|
await token.approve(marketplace.address, request.maxPrice * 2)
|
|
|
|
await marketplace.requestStorage(request)
|
|
|
|
await expect(marketplace.requestStorage(request)).to.be.revertedWith(
|
|
|
|
"Request already exists"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2022-02-16 13:38:19 +00:00
|
|
|
|
|
|
|
describe("offering storage", function () {
|
|
|
|
beforeEach(async function () {
|
2022-02-17 10:00:18 +00:00
|
|
|
switchAccount(client)
|
2022-02-16 13:38:19 +00:00
|
|
|
await token.approve(marketplace.address, request.maxPrice)
|
|
|
|
await marketplace.requestStorage(request)
|
2022-02-17 10:00:18 +00:00
|
|
|
switchAccount(host)
|
2022-02-16 13:38:19 +00:00
|
|
|
await token.approve(marketplace.address, collateral)
|
|
|
|
await marketplace.deposit(collateral)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("emits event when storage is offered", async function () {
|
|
|
|
await expect(marketplace.offerStorage(offer))
|
|
|
|
.to.emit(marketplace, "StorageOffered")
|
|
|
|
.withArgs(offerId(offer), offerToArray(offer))
|
|
|
|
})
|
|
|
|
|
2022-02-17 10:06:14 +00:00
|
|
|
it("rejects offer with invalid host address", async function () {
|
|
|
|
let invalid = { ...offer, host: client.address }
|
|
|
|
await expect(marketplace.offerStorage(invalid)).to.be.revertedWith(
|
|
|
|
"Invalid host address"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
it("rejects offer for unknown request", async function () {
|
|
|
|
let unknown = exampleRequest()
|
|
|
|
let invalid = { ...offer, requestId: requestId(unknown) }
|
|
|
|
await expect(marketplace.offerStorage(invalid)).to.be.revertedWith(
|
|
|
|
"Unknown request"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects an offer that exceeds the maximum price", async function () {
|
|
|
|
let invalid = { ...offer, price: request.maxPrice + 1 }
|
|
|
|
await expect(marketplace.offerStorage(invalid)).to.be.revertedWith(
|
|
|
|
"Price too high"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects resubmission of offer", async function () {
|
|
|
|
await marketplace.offerStorage(offer)
|
|
|
|
await expect(marketplace.offerStorage(offer)).to.be.revertedWith(
|
|
|
|
"Offer already exists"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("rejects offer with insufficient collateral", async function () {
|
|
|
|
let insufficient = collateral - 1
|
|
|
|
await marketplace.withdraw()
|
|
|
|
await token.approve(marketplace.address, insufficient)
|
|
|
|
await marketplace.deposit(insufficient)
|
|
|
|
await expect(marketplace.offerStorage(offer)).to.be.revertedWith(
|
|
|
|
"Insufficient collateral"
|
|
|
|
)
|
|
|
|
})
|
|
|
|
})
|
2022-02-16 09:50:00 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
function requestId(request) {
|
|
|
|
return keccak256(
|
|
|
|
defaultAbiCoder.encode(
|
|
|
|
[
|
2022-02-17 10:00:18 +00:00
|
|
|
"address",
|
2022-02-16 09:50:00 +00:00
|
|
|
"uint256",
|
|
|
|
"uint256",
|
|
|
|
"bytes32",
|
|
|
|
"uint256",
|
|
|
|
"uint256",
|
|
|
|
"uint256",
|
2022-02-17 11:24:27 +00:00
|
|
|
"uint256",
|
2022-02-16 09:50:00 +00:00
|
|
|
"bytes32",
|
|
|
|
],
|
|
|
|
requestToArray(request)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-16 13:38:19 +00:00
|
|
|
function offerId(offer) {
|
|
|
|
return keccak256(
|
|
|
|
defaultAbiCoder.encode(
|
2022-02-17 10:06:14 +00:00
|
|
|
["address", "bytes32", "uint256", "uint256"],
|
2022-02-16 13:38:19 +00:00
|
|
|
offerToArray(offer)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-02-16 09:50:00 +00:00
|
|
|
function requestToArray(request) {
|
|
|
|
return [
|
2022-02-17 10:00:18 +00:00
|
|
|
request.client,
|
2022-02-16 09:50:00 +00:00
|
|
|
request.duration,
|
|
|
|
request.size,
|
|
|
|
request.contentHash,
|
|
|
|
request.proofPeriod,
|
|
|
|
request.proofTimeout,
|
|
|
|
request.maxPrice,
|
2022-02-17 11:24:27 +00:00
|
|
|
request.expiry,
|
2022-02-16 09:50:00 +00:00
|
|
|
request.nonce,
|
|
|
|
]
|
|
|
|
}
|
2022-02-16 13:38:19 +00:00
|
|
|
|
|
|
|
function offerToArray(offer) {
|
2022-02-17 10:06:14 +00:00
|
|
|
return [offer.host, offer.requestId, offer.price, offer.expiry]
|
2022-02-16 13:38:19 +00:00
|
|
|
}
|