[marketplace] Simplify myRequests()

This commit is contained in:
Mark Spanbroek 2022-11-23 12:39:49 +01:00 committed by markspanbroek
parent c0a1e11b87
commit c832dfbb7c
2 changed files with 9 additions and 34 deletions

View File

@ -25,7 +25,7 @@ contract Marketplace is Collateral, Proofs {
mapping(RequestId => Request) private requests; mapping(RequestId => Request) private requests;
mapping(RequestId => RequestContext) private requestContexts; mapping(RequestId => RequestContext) private requestContexts;
mapping(SlotId => Slot) private slots; mapping(SlotId => Slot) private slots;
SetMap.AddressBytes32SetMap private activeRequestsForClients; // purchasing mapping(address => EnumerableSet.Bytes32Set) private requestsPerClient; // purchasing
mapping(address => EnumerableSet.Bytes32Set) private slotsPerHost; // sales mapping(address => EnumerableSet.Bytes32Set) private slotsPerHost; // sales
mapping(SlotId => RequestId) private requestForSlot; mapping(SlotId => RequestId) private requestForSlot;
@ -44,19 +44,7 @@ contract Marketplace is Collateral, Proofs {
} }
function myRequests() public view returns (RequestId[] memory) { function myRequests() public view returns (RequestId[] memory) {
SetMap.AddressBytes32SetMapKey key = _toAddressSetMapKey(msg.sender); return _toRequestIds(requestsPerClient[msg.sender].values());
RequestId[] memory requestIds = _toRequestIds(
activeRequestsForClients.values(key)
);
RequestId[] memory result = new RequestId[](requestIds.length);
uint8 counter = 0;
for (uint8 i = 0; i < requestIds.length; i++) {
if (!_isCancelled(requestIds[i])) {
result[counter] = requestIds[i];
counter++;
}
}
return _toRequestIds(Utils._resize(_toBytes32s(result), counter));
} }
function isActive(bytes32 slot) private view returns (bool) { function isActive(bytes32 slot) private view returns (bool) {
@ -90,10 +78,7 @@ contract Marketplace is Collateral, Proofs {
context.endsAt = block.timestamp + request.ask.duration; context.endsAt = block.timestamp + request.ask.duration;
_setProofEnd(_toEndId(id), context.endsAt); _setProofEnd(_toEndId(id), context.endsAt);
activeRequestsForClients.add( requestsPerClient[request.client].add(RequestId.unwrap(id));
_toAddressSetMapKey(request.client),
RequestId.unwrap(id)
);
_createLock(_toLockId(id), request.expiry); _createLock(_toLockId(id), request.expiry);
@ -175,10 +160,6 @@ contract Marketplace is Collateral, Proofs {
context.state = RequestState.Failed; context.state = RequestState.Failed;
_setProofEnd(_toEndId(requestId), block.timestamp - 1); _setProofEnd(_toEndId(requestId), block.timestamp - 1);
context.endsAt = block.timestamp - 1; context.endsAt = block.timestamp - 1;
activeRequestsForClients.remove(
_toAddressSetMapKey(request.client),
RequestId.unwrap(requestId)
);
emit RequestFailed(requestId); emit RequestFailed(requestId);
// TODO: burn all remaining slot collateral (note: slot collateral not // TODO: burn all remaining slot collateral (note: slot collateral not
@ -195,10 +176,7 @@ contract Marketplace is Collateral, Proofs {
RequestContext storage context = _context(requestId); RequestContext storage context = _context(requestId);
Request storage request = _request(requestId); Request storage request = _request(requestId);
context.state = RequestState.Finished; context.state = RequestState.Finished;
activeRequestsForClients.remove( requestsPerClient[request.client].remove(RequestId.unwrap(requestId));
_toAddressSetMapKey(request.client),
RequestId.unwrap(requestId)
);
SlotId slotId = _toSlotId(requestId, slotIndex); SlotId slotId = _toSlotId(requestId, slotIndex);
Slot storage slot = _slot(slotId); Slot storage slot = _slot(slotId);
require(!slot.hostPaid, "Already paid"); require(!slot.hostPaid, "Already paid");
@ -225,10 +203,7 @@ contract Marketplace is Collateral, Proofs {
// Update request state to Cancelled. Handle in the withdraw transaction // Update request state to Cancelled. Handle in the withdraw transaction
// as there needs to be someone to pay for the gas to update the state // as there needs to be someone to pay for the gas to update the state
context.state = RequestState.Cancelled; context.state = RequestState.Cancelled;
activeRequestsForClients.remove( requestsPerClient[request.client].remove(RequestId.unwrap(requestId));
_toAddressSetMapKey(request.client),
RequestId.unwrap(requestId)
);
emit RequestCancelled(requestId); emit RequestCancelled(requestId);

View File

@ -705,10 +705,10 @@ describe("Marketplace", function () {
expect(await marketplace.myRequests()).to.deep.equal([requestId(request)]) expect(await marketplace.myRequests()).to.deep.equal([requestId(request)])
}) })
it("removes request from list when cancelled", async function () { it("keeps request in list when cancelled", async function () {
await marketplace.requestStorage(request) await marketplace.requestStorage(request)
await waitUntilCancelled(request) await waitUntilCancelled(request)
expect(await marketplace.myRequests()).to.deep.equal([]) expect(await marketplace.myRequests()).to.deep.equal([requestId(request)])
}) })
it("removes request from list when funds are withdrawn", async function () { it("removes request from list when funds are withdrawn", async function () {
@ -718,13 +718,13 @@ describe("Marketplace", function () {
expect(await marketplace.myRequests()).to.deep.equal([]) expect(await marketplace.myRequests()).to.deep.equal([])
}) })
it("removes request from list when request fails", async function () { it("keeps request in list when request fails", async function () {
await marketplace.requestStorage(request) await marketplace.requestStorage(request)
switchAccount(host) switchAccount(host)
await waitUntilStarted(marketplace, request, proof) await waitUntilStarted(marketplace, request, proof)
await waitUntilFailed(marketplace, request, slot) await waitUntilFailed(marketplace, request, slot)
switchAccount(client) switchAccount(client)
expect(await marketplace.myRequests()).to.deep.equal([]) expect(await marketplace.myRequests()).to.deep.equal([requestId(request)])
}) })
it("removes request from list when request finishes", async function () { it("removes request from list when request finishes", async function () {