mirror of
https://github.com/codex-storage/codex-contracts-eth.git
synced 2025-01-10 11:55:44 +00:00
[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:
parent
3a97330e72
commit
949a359626
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user