Skip to content

Commit d202680

Browse files
authored
feat: add events to Profile (#58)
1 parent 5412aea commit d202680

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

src/interfaces/IProfile.sol

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
pragma solidity 0.8.19;
44

55
interface IProfile {
6+
/**
7+
*
8+
* Events *
9+
*
10+
*/
11+
12+
/// @notice Emitted when a badge is attached.
13+
/// @param uid The id of the badge.
14+
event AttachBadge(bytes32 indexed uid);
15+
16+
/// @notice Emitted when a badge is detached.
17+
/// @param uid The id of the badge.
18+
event DetachBadge(bytes32 indexed uid);
19+
20+
/// @notice Emitted when the username is updated.
21+
event ChangeUsername(string oldUsername, string newUsername);
22+
23+
/// @notice Emitted when the avatar is updated.
24+
event ChangeAvatar(address oldToken, uint256 oldTokenId, address newToken, uint256 newTokenId);
25+
26+
/// @notice Emitted when the badge order is updated.
27+
event ReorderBadges(uint256 oldOrder, uint256 newOrder);
28+
629
/**
730
*
831
* Public Mutating Functions *

src/profile/Profile.sol

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,16 +240,23 @@ contract Profile is IProfile, Initializable, Multicall {
240240
function reorderBadges(uint256[] memory _orders) external onlyOwner {
241241
if (_orders.length != uids.length) revert LengthMismatch();
242242

243-
badgeOrderEncoding = _encodeOrder(_orders);
243+
uint256 oldOrder = badgeOrderEncoding;
244+
uint256 newOrder = _encodeOrder(_orders);
245+
badgeOrderEncoding = newOrder;
246+
247+
emit ReorderBadges(oldOrder, newOrder);
244248
}
245249

246250
/// @notice Change the username.
247251
/// @param newUsername The new username.
248252
function changeUsername(string memory newUsername) external onlyOwner {
249253
address _registry = registry;
250-
IProfileRegistry(_registry).unregisterUsername(username);
254+
string memory oldUsername = username;
255+
IProfileRegistry(_registry).unregisterUsername(oldUsername);
251256
IProfileRegistry(_registry).registerUsername(newUsername);
252257
username = newUsername;
258+
259+
emit ChangeUsername(oldUsername, newUsername);
253260
}
254261

255262
/// @notice Change the avatar.
@@ -260,7 +267,10 @@ contract Profile is IProfile, Initializable, Multicall {
260267
revert TokenNotOwnedByUser(token, tokenId);
261268
}
262269

270+
Avatar memory oldAvatar = avatar;
263271
avatar = Avatar(token, tokenId);
272+
273+
emit ChangeAvatar(oldAvatar.token, oldAvatar.tokenId, token, tokenId);
264274
}
265275

266276
/**
@@ -272,6 +282,8 @@ contract Profile is IProfile, Initializable, Multicall {
272282
/// @dev Internal function to attach one batch to this profile.
273283
/// @param uid The badge uid to attach.
274284
function _attachOne(bytes32 uid) private {
285+
// @note This will possible cause re-emit an existing badge, used for off-chain to index any attach tx.
286+
emit AttachBadge(uid);
275287
if (indexes[uid] > 0) return;
276288

277289
uids.push(uid);
@@ -292,6 +304,7 @@ contract Profile is IProfile, Initializable, Multicall {
292304
function _detachOne(bytes32 uid) private {
293305
uint256 valueIndex = indexes[uid];
294306
if (valueIndex == 0) return;
307+
emit DetachBadge(uid);
295308

296309
uint256 length = uids.length;
297310
uint256[] memory _oldOrders = _decodeOrder(badgeOrderEncoding, length);

test/Profile.t.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ contract ProfileRegistryTest is Test {
5757
error TokenNotOwnedByUser(address token, uint256 tokenId);
5858
error Unauthorized();
5959

60+
event AttachBadge(bytes32 indexed uid);
61+
event DetachBadge(bytes32 indexed uid);
62+
event ChangeUsername(string oldUsername, string newUsername);
63+
event ChangeAvatar(address oldToken, uint256 oldTokenId, address newToken, uint256 newTokenId);
64+
event ReorderBadges(uint256 oldOrder, uint256 newOrder);
65+
6066
address internal constant attester = address(1);
6167

6268
address private constant TREASURY_ADDRESS = 0x1000000000000000000000000000000000000000;
@@ -114,6 +120,10 @@ contract ProfileRegistryTest is Test {
114120
bytes32[] memory uids = new bytes32[](2);
115121
uids[0] = uid0;
116122
uids[1] = uid1;
123+
vm.expectEmit(false, false, false, true, address(profile));
124+
emit AttachBadge(uid0);
125+
vm.expectEmit(false, false, false, true, address(profile));
126+
emit AttachBadge(uid1);
117127
profile.attach(uids);
118128
bytes32[] memory badges = profile.getAttachedBadges();
119129
assertEq(badges.length, 2);
@@ -129,6 +139,10 @@ contract ProfileRegistryTest is Test {
129139
assertEq(orders[1], 2);
130140

131141
// attach again, no op
142+
vm.expectEmit(false, false, false, true, address(profile));
143+
emit AttachBadge(uid0);
144+
vm.expectEmit(false, false, false, true, address(profile));
145+
emit AttachBadge(uid1);
132146
profile.attach(uids);
133147
badges = profile.getAttachedBadges();
134148
assertEq(badges.length, 2);
@@ -231,6 +245,8 @@ contract ProfileRegistryTest is Test {
231245
// detach 6, order would be: 1, 2, 3, 4, 5, 9, 6, 7, 8
232246
bytes32[] memory detachBadges = new bytes32[](1);
233247
detachBadges[0] = originalBadges[5];
248+
vm.expectEmit(false, false, false, true, address(profile));
249+
emit DetachBadge(originalBadges[5]);
234250
profile.detach(detachBadges);
235251
bytes32[] memory badges = profile.getAttachedBadges();
236252
assertEq(badges.length, 9);
@@ -252,6 +268,14 @@ contract ProfileRegistryTest is Test {
252268
detachBadges[1] = badges[1];
253269
detachBadges[2] = badges[2];
254270
detachBadges[3] = badges[3];
271+
vm.expectEmit(false, false, false, true, address(profile));
272+
emit DetachBadge(badges[0]);
273+
vm.expectEmit(false, false, false, true, address(profile));
274+
emit DetachBadge(badges[1]);
275+
vm.expectEmit(false, false, false, true, address(profile));
276+
emit DetachBadge(badges[2]);
277+
vm.expectEmit(false, false, false, true, address(profile));
278+
emit DetachBadge(badges[3]);
255279
profile.detach(detachBadges);
256280
badges = profile.getAttachedBadges();
257281
assertEq(badges.length, 5);
@@ -353,6 +377,8 @@ contract ProfileRegistryTest is Test {
353377

354378
// succeed
355379
assertEq(profile.username(), "xxxxx");
380+
vm.expectEmit(false, false, false, true, address(profile));
381+
emit ChangeUsername("xxxxx", "zzzzz");
356382
profile.changeUsername("zzzzz");
357383
assertEq(profile.username(), "zzzzz");
358384
}
@@ -375,6 +401,8 @@ contract ProfileRegistryTest is Test {
375401

376402
// succeed
377403
assertEq(profile.getAvatar(), "123");
404+
vm.expectEmit(false, false, false, true, address(profile));
405+
emit ChangeAvatar(address(0), 0, address(token), 1);
378406
profile.changeAvatar(address(token), 1);
379407
assertEq(profile.getAvatar(), "testBaseURI/1");
380408

0 commit comments

Comments
 (0)