Skip to content

Commit

Permalink
refactor: emit events #121
Browse files Browse the repository at this point in the history
  • Loading branch information
0xjoaovpsantos committed Dec 21, 2023
1 parent 4c42b68 commit 40f9848
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 72 deletions.
55 changes: 9 additions & 46 deletions contracts/Swaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -67,28 +46,16 @@ 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;
}

/**
* @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);
Expand All @@ -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;

Expand Down Expand Up @@ -126,18 +93,16 @@ contract Swaplace is SwapFactory, ISwaplace, IERC165 {
}
}

assembly {
log3(0x00, 0x00, EVENT_SWAP_ACCEPTED_SIGNATURE, id, caller())
}
emit SwapAccepted(swapId, msg.sender);

return true;
}

/**
* @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);
Expand All @@ -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);
}

/**
Expand Down
36 changes: 20 additions & 16 deletions contracts/interfaces/ISwaplace.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion docs/interfaces/ISwaplace.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 9 additions & 9 deletions test/TestSwaplace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand All @@ -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 () {
Expand All @@ -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 () {
Expand Down Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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()),
Expand Down

0 comments on commit 40f9848

Please sign in to comment.