Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: drastically lowering cancelSwap gas consumption #181 #182

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading