[marketplace] Add tests for collateral locking

The collateral locking check was moved from Collateral
and AccountLocks to Marketplace. This tests the new
locking mechanism.
This commit is contained in:
Mark Spanbroek 2022-12-19 14:30:46 +01:00 committed by markspanbroek
parent d5d4dba442
commit dbc5e3738e
2 changed files with 28 additions and 27 deletions

View File

@ -1,5 +1,4 @@
const { expect } = require("chai")
const { exampleLock } = require("./examples")
describe("Collateral", function () {
let collateral, token
@ -90,25 +89,4 @@ describe("Collateral", function () {
expect(await collateral.balanceOf(account1.address)).to.equal(950)
})
})
describe("locking", function () {
let lock
beforeEach(async function () {
await token.approve(collateral.address, 42)
await collateral.deposit(42)
lock = await exampleLock()
await collateral.createLock(lock.id, lock.expiry)
await collateral.lock(account0.address, lock.id)
})
it("withdrawal fails when account is locked", async function () {
await expect(collateral.withdraw()).to.be.revertedWith("Account locked")
})
it("withdrawal succeeds when account is unlocked", async function () {
await collateral.unlock(lock.id)
await expect(collateral.withdraw()).not.to.be.reverted
})
})
})

View File

@ -126,11 +126,6 @@ describe("Marketplace", function () {
.withArgs(slot.request, slot.index, slotId(slot))
})
it("locks collateral of host", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof)
await expect(marketplace.withdraw()).to.be.revertedWith("Account locked")
})
it("starts requiring storage proofs", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof)
expect(await marketplace.proofEnd(slotId(slot))).to.be.gt(0)
@ -535,6 +530,34 @@ describe("Marketplace", function () {
})
})
describe("collateral locking", function () {
beforeEach(async function () {
switchAccount(client)
await token.approve(marketplace.address, price(request))
await marketplace.requestStorage(request)
switchAccount(host)
await token.approve(marketplace.address, collateral)
await marketplace.deposit(collateral)
})
it("locks collateral of host when it fills a slot", async function () {
await marketplace.fillSlot(slot.request, slot.index, proof)
await expect(marketplace.withdraw()).to.be.revertedWith("Account locked")
})
it("allows withdrawal when all slots are free", async function () {
let slot1 = { ...slot, index: 0 }
let slot2 = { ...slot, index: 1 }
await marketplace.fillSlot(slot1.request, slot1.index, proof)
await marketplace.fillSlot(slot2.request, slot2.index, proof)
await waitUntilFinished(marketplace, requestId(request))
await marketplace.freeSlot(slotId(slot1))
await expect(marketplace.withdraw()).to.be.revertedWith("Account locked")
await marketplace.freeSlot(slotId(slot2))
await expect(marketplace.withdraw()).not.to.be.reverted
})
})
describe("contract state", function () {
beforeEach(async function () {
switchAccount(client)