From 40f9848f09a72a16f252f6120d0567f5b486a203 Mon Sep 17 00:00:00 2001 From: Joao Victor Pereira Santos Date: Thu, 21 Dec 2023 12:56:12 -0300 Subject: [PATCH] refactor: emit events #121 --- contracts/Swaplace.sol | 55 +++++------------------------- contracts/interfaces/ISwaplace.sol | 36 ++++++++++--------- docs/interfaces/ISwaplace.md | 2 +- test/TestSwaplace.test.ts | 18 +++++----- 4 files changed, 39 insertions(+), 72 deletions(-) diff --git a/contracts/Swaplace.sol b/contracts/Swaplace.sol index 0e851e0..5333911 100644 --- a/contracts/Swaplace.sol +++ b/contracts/Swaplace.sol @@ -17,27 +17,6 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { /// @dev Swap Identifier counter. uint256 private _totalSwaps; - /** - * @dev Emitted when a new Swap is created. - * keccak-256(SwapCreated(uint256,address,uint256,address)) - */ - bytes32 private constant EVENT_SWAP_CREATED_SIGNATURE = - 0x43a58bfac3282e5ce3bfd714c5bc0ddff8d6f2cd049db0b02a25d7cdd5026efb; - - /** - * @dev Emitted when a Swap is accepted. - * keccak-256(SwapAccepted(uint256,address)) - */ - bytes32 private constant EVENT_SWAP_ACCEPTED_SIGNATURE = - 0x38eced64b5c4ab50bb61d2f5fcace3c629a2d92f974374bf4a4f3e8a7c49caef; - - /** - * @dev Emitted when a Swap is canceled. - * keccak-256(SwapCanceled(uint256,address)) - */ - bytes32 private constant EVENT_SWAP_CANCELED_SIGNATURE = - 0x0a01e988a96145b1dd49cafc687666a0525e6b929299df4652cc646915e696d8; - /// @dev Mapping of Swap ID to Swap struct. See {ISwap-Swap}. mapping(uint256 => Swap) private _swaps; @@ -67,19 +46,7 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { _swaps[swapId] = swap; - uint256 swapExpiry = swap.expiry; - address allowed = swap.allowed; - assembly { - mstore(0x00, allowed) - log4( - 0x00, - 0x20, - EVENT_SWAP_CREATED_SIGNATURE, - swapId, - caller(), - swapExpiry - ) - } + emit SwapCreated(swapId, msg.sender, swap.allowed, swap.expiry); return swapId; } @@ -87,8 +54,8 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { /** * @dev See {ISwaplace-acceptSwap}. */ - function acceptSwap(uint256 id) public returns (bool) { - Swap memory swap = _swaps[id]; + function acceptSwap(uint256 swapId) public returns (bool) { + Swap memory swap = _swaps[swapId]; if (swap.allowed != address(0) && swap.allowed != msg.sender) { revert InvalidAddress(msg.sender); @@ -98,7 +65,7 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { revert InvalidExpiry(swap.expiry); } - _swaps[id].expiry = 0; + _swaps[swapId].expiry = 0; Asset[] memory assets = swap.asking; @@ -126,9 +93,7 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { } } - assembly { - log3(0x00, 0x00, EVENT_SWAP_ACCEPTED_SIGNATURE, id, caller()) - } + emit SwapAccepted(swapId, msg.sender); return true; } @@ -136,8 +101,8 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { /** * @dev See {ISwaplace-cancelSwap}. */ - function cancelSwap(uint256 id) public { - Swap memory swap = _swaps[id]; + function cancelSwap(uint256 swapId) public { + Swap memory swap = _swaps[swapId]; if (swap.owner != msg.sender) { revert InvalidAddress(msg.sender); @@ -147,11 +112,9 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 { revert InvalidExpiry(swap.expiry); } - _swaps[id].expiry = 0; + _swaps[swapId].expiry = 0; - assembly { - log3(0x00, 0x00, EVENT_SWAP_CANCELED_SIGNATURE, id, caller()) - } + emit SwapCanceled(swapId, msg.sender); } /** diff --git a/contracts/interfaces/ISwaplace.sol b/contracts/interfaces/ISwaplace.sol index c26bafd..af45e53 100644 --- a/contracts/interfaces/ISwaplace.sol +++ b/contracts/interfaces/ISwaplace.sol @@ -7,7 +7,25 @@ import {ISwap} from "./ISwap.sol"; * @dev Interface of the {Swaplace} implementation. */ interface ISwaplace { -<<<<<<< HEAD + /** + * @dev Emitted when a new Swap is created. + */ + event SwapCreated( + uint256 indexed swapId, + address indexed owner, + address indexed allowed, + uint256 expiry + ); + + /** + * @dev Emitted when a Swap is accepted. + */ + event SwapAccepted(uint256 indexed swapId, address indexed acceptee); + + /** + * @dev Emitted when a Swap is canceled. + */ + event SwapCanceled(uint256 indexed swapId, address indexed owner); /** * @dev Allow users to create a Swap. Each new Swap self-increments its ID by one. @@ -54,20 +72,6 @@ interface ISwaplace { * Emits a {SwapCanceled} event. */ function cancelSwap(uint256 swapId) external; -======= - /** - * @dev Allow users to create a Swap. Each new Swap self-increments its id by one. - * - * Requirements: - * - * - `owner` must be the caller address. - * - `expiry` should be bigger than timestamp. - * - `biding` and `asking` must not be empty. - * - * Emits a {SwapCreated} event. - */ - function createSwap(ISwap.Swap calldata Swap) external returns (uint256); ->>>>>>> a7ca232 (refactor: Remove unecessary import and move the declarion of events signatures) /** * @dev Retrieves the details of a Swap based on the `swapId` provided. @@ -77,4 +81,4 @@ interface ISwaplace { * If the `owner` is the zero address, then the Swap doesn't exist. */ function getSwap(uint256 swapId) external view returns (ISwap.Swap memory); -} +} \ No newline at end of file diff --git a/docs/interfaces/ISwaplace.md b/docs/interfaces/ISwaplace.md index 5e1481a..b3e08cc 100644 --- a/docs/interfaces/ISwaplace.md +++ b/docs/interfaces/ISwaplace.md @@ -7,7 +7,7 @@ Interface of the {Swaplace} implementation. ### SwapCreated ```solidity -event SwapCreated(uint256 swapId, address owner, uint256 expiry) +event SwapCreated(uint256 swapId, address owner, address allowed, uint256 expiry) ``` Emitted when a new Swap is created. diff --git a/test/TestSwaplace.test.ts b/test/TestSwaplace.test.ts index dc4cb94..4fd455d 100644 --- a/test/TestSwaplace.test.ts +++ b/test/TestSwaplace.test.ts @@ -72,7 +72,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); }); it("Should be able to create a 1-N swap with ERC20", async function () { @@ -98,7 +98,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); }); it("Should be able to create a N-N swap with ERC20", async function () { @@ -128,7 +128,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); }); it("Should be able to create a 1-1 swap with ERC721", async function () { @@ -150,7 +150,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); }); it("Should be able to create a 1-N swap with ERC721", async function () { @@ -176,7 +176,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); }); it("Should be able to create a N-N swap with ERC721", async function () { @@ -206,7 +206,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); }); }); @@ -302,7 +302,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); await expect( await Swaplace.connect(acceptee).acceptSwap( @@ -325,7 +325,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); await expect( await Swaplace.connect(acceptee).acceptSwap( @@ -390,7 +390,7 @@ describe("Swaplace", async function () { await expect(await Swaplace.connect(owner).createSwap(swap)) .to.emit(Swaplace, "SwapCreated") - .withArgs(await Swaplace.totalSwaps(), owner.address, swap.expiry); + .withArgs(await Swaplace.totalSwaps(), owner.address, swap.allowed, swap.expiry); await expect( Swaplace.connect(acceptee).acceptSwap(await Swaplace.totalSwaps()),