[marketplace] Add isCancelled check for slot

Add cancelled check for slot state which checks the contract state and also the slot expiry time. This handles the case where the client may not have withdrawn funds yet (which sets the contract state to Cancelled).

Add tests for contract state.
This commit is contained in:
Eric Mastro 2022-08-12 11:40:04 +10:00 committed by Eric Mastro
parent 3a97330e72
commit 949a359626
2 changed files with 61 additions and 0 deletions

View File

@ -116,6 +116,16 @@ contract Marketplace is Collateral, Proofs {
emit RequestCancelled(requestId);
}
function isCancelled(bytes32 requestId) public view returns (bool) {
RequestContext storage context = requestContexts[requestId];
return
context.state == RequestState.Cancelled ||
(
context.state == RequestState.New &&
block.timestamp > requests[requestId].expiry
);
}
function _host(bytes32 slotId) internal view returns (address) {
return slots[slotId].host;
}

View File

@ -332,4 +332,55 @@ describe("Marketplace", function () {
expect(endBalance - startBalance).to.equal(price(request))
})
})
describe("contract state", 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)
})
const RequestState = {
New: 0,
Started: 1,
Cancelled: 2,
Finished: 3,
Failed: 4,
}
it("isCancelled is true once request is cancelled", async function () {
await expect(await marketplace.isCancelled(slot.request)).to.equal(false)
await waitUntilExpired(request.expiry)
await expect(await marketplace.isCancelled(slot.request)).to.equal(true)
})
it("state is Cancelled when client withdraws funds", async function () {
await expect(await marketplace.state(slot.request)).to.equal(
RequestState.New
)
await waitUntilExpired(request.expiry)
switchAccount(client)
await marketplace.withdrawFunds(slot.request)
await expect(await marketplace.state(slot.request)).to.equal(
RequestState.Cancelled
)
})
it("state is Started once all slots are filled", async function () {
await expect(await marketplace.state(slot.request)).to.equal(
RequestState.New
)
// fill all slots, should change state to RequestState.Started
const lastSlot = request.ask.slots - 1
for (let i = 0; i <= lastSlot; i++) {
await marketplace.fillSlot(slot.request, i, proof)
}
await expect(await marketplace.state(slot.request)).to.equal(
RequestState.Started
)
})
})
})