From 949a35962623c320b684a3e2e7616cb6c064fc48 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Fri, 12 Aug 2022 11:40:04 +1000 Subject: [PATCH] [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. --- contracts/Marketplace.sol | 10 ++++++++ test/Marketplace.test.js | 51 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/contracts/Marketplace.sol b/contracts/Marketplace.sol index c4697a1..4b8da05 100644 --- a/contracts/Marketplace.sol +++ b/contracts/Marketplace.sol @@ -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; } diff --git a/test/Marketplace.test.js b/test/Marketplace.test.js index 761563e..38b89de 100644 --- a/test/Marketplace.test.js +++ b/test/Marketplace.test.js @@ -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 + ) + }) + }) })