Skip to content

Commit

Permalink
Merge pull request #182 from blackbeard002/cancel
Browse files Browse the repository at this point in the history
 refactor: drastically lowering cancelSwap gas consumption #181
  • Loading branch information
0xneves authored Jan 24, 2024
2 parents 41d7563 + 2876230 commit f9a1a26
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
10 changes: 4 additions & 6 deletions contracts/Swaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,15 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {
* @dev See {ISwaplace-cancelSwap}.
*/
function cancelSwap(uint256 swapId) public {
Swap memory swap = _swaps[swapId];
if (_swaps[swapId].owner != msg.sender) revert InvalidAddress(msg.sender);

if (swap.owner != msg.sender) revert InvalidAddress(msg.sender);

(address allowed, uint256 expiry) = parseData(swap.config);
(, uint256 expiry) = parseData(_swaps[swapId].config);

if (expiry < block.timestamp) revert InvalidExpiry(expiry);

_swaps[swapId].config = packData(allowed, 0);
_swaps[swapId].config = 0;

emit SwapCanceled(swapId, msg.sender);
emit SwapCanceled(swapId);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/ISwaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface ISwaplace {
/**
* @dev Emitted when a Swap is canceled.
*/
event SwapCanceled(uint256 indexed swapId, address indexed owner);
event SwapCanceled(uint256 indexed swapId);

/**
* @dev Allow users to create a Swap. Each new Swap self-increments its ID by one.
Expand Down
4 changes: 2 additions & 2 deletions test/TestSwaplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ describe("Swaplace", async function () {

it("Should be able to {cancelSwap} a Swap", async function () {
const lastSwap = await Swaplace.totalSwaps();
expect(await Swaplace.connect(owner).cancelSwap(lastSwap))
await expect(await Swaplace.connect(owner).cancelSwap(lastSwap))
.to.emit(Swaplace, "SwapCanceled")
.withArgs(lastSwap, swap.owner);
.withArgs(lastSwap);
});

it("Should not be able to {acceptSwap} a canceled a Swap", async function () {
Expand Down

0 comments on commit f9a1a26

Please sign in to comment.