Skip to content

Commit f54793c

Browse files
authored
added strictAdd & strictRemove (#93)
1 parent 06971b1 commit f54793c

File tree

4 files changed

+161
-2
lines changed

4 files changed

+161
-2
lines changed

contracts/libs/arrays/SetHelper.sol

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ library SetHelper {
4242
}
4343
}
4444

45+
/**
46+
* @notice The function for the strict insertion of an array of elements into the address set
47+
* @param set the set to insert the elements into
48+
* @param array_ the elements to be inserted
49+
*/
50+
function strictAdd(EnumerableSet.AddressSet storage set, address[] memory array_) internal {
51+
for (uint256 i = 0; i < array_.length; i++) {
52+
require(set.add(array_[i]), "SetHelper: element already exists");
53+
}
54+
}
55+
56+
/**
57+
* @notice The function for the strict insertion of an array of elements into the uint256 set
58+
*/
59+
function strictAdd(EnumerableSet.UintSet storage set, uint256[] memory array_) internal {
60+
for (uint256 i = 0; i < array_.length; i++) {
61+
require(set.add(array_[i]), "SetHelper: element already exists");
62+
}
63+
}
64+
65+
/**
66+
* @notice The function for the strict insertion of an array of elements into the string set
67+
*/
68+
function strictAdd(StringSet.Set storage set, string[] memory array_) internal {
69+
for (uint256 i = 0; i < array_.length; i++) {
70+
require(set.add(array_[i]), "SetHelper: element already exists");
71+
}
72+
}
73+
4574
/**
4675
* @notice The function to remove an array of elements from the address set
4776
* @param set the set to remove the elements from
@@ -70,4 +99,33 @@ library SetHelper {
7099
set.remove(array_[i]);
71100
}
72101
}
102+
103+
/**
104+
* @notice The function for the strict removal of an array of elements from the address set
105+
* @param set the set to remove the elements from
106+
* @param array_ the elements to be removed
107+
*/
108+
function strictRemove(EnumerableSet.AddressSet storage set, address[] memory array_) internal {
109+
for (uint256 i = 0; i < array_.length; i++) {
110+
require(set.remove(array_[i]), "SetHelper: no such element");
111+
}
112+
}
113+
114+
/**
115+
* @notice The function for the strict removal of an array of elements from the uint256 set
116+
*/
117+
function strictRemove(EnumerableSet.UintSet storage set, uint256[] memory array_) internal {
118+
for (uint256 i = 0; i < array_.length; i++) {
119+
require(set.remove(array_[i]), "SetHelper: no such element");
120+
}
121+
}
122+
123+
/**
124+
* @notice The function for the strict removal of an array of elements from the string set
125+
*/
126+
function strictRemove(StringSet.Set storage set, string[] memory array_) internal {
127+
for (uint256 i = 0; i < array_.length; i++) {
128+
require(set.remove(array_[i]), "SetHelper: no such element");
129+
}
130+
}
73131
}

contracts/mock/libs/arrays/SetHelperMock.sol

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ contract SetHelperMock {
3030
stringSet.add(arr_);
3131
}
3232

33+
function strictAddToAddressSet(address[] memory arr_) external {
34+
addressSet.strictAdd(arr_);
35+
}
36+
37+
function strictAddToUintSet(uint256[] memory arr_) external {
38+
uintSet.strictAdd(arr_);
39+
}
40+
41+
function strictAddToStringSet(string[] memory arr_) external {
42+
stringSet.strictAdd(arr_);
43+
}
44+
3345
function removeFromAddressSet(address[] memory arr_) external {
3446
addressSet.remove(arr_);
3547
}
@@ -42,6 +54,18 @@ contract SetHelperMock {
4254
stringSet.remove(arr_);
4355
}
4456

57+
function strictRemoveFromAddressSet(address[] memory arr_) external {
58+
addressSet.strictRemove(arr_);
59+
}
60+
61+
function strictRemoveFromUintSet(uint256[] memory arr_) external {
62+
uintSet.strictRemove(arr_);
63+
}
64+
65+
function strictRemoveFromStringSet(string[] memory arr_) external {
66+
stringSet.strictRemove(arr_);
67+
}
68+
4569
function getAddressSet() external view returns (address[] memory) {
4670
return addressSet.values();
4771
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/libs/arrays/SetHelper.test.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ describe("SetHelper", () => {
4545
});
4646
});
4747

48+
describe("strictAdd", () => {
49+
it("should not strict add to address set if element already exists", async () => {
50+
await expect(mock.strictAddToAddressSet([FIRST.address, FIRST.address])).to.be.revertedWith(
51+
"SetHelper: element already exists",
52+
);
53+
});
54+
55+
it("should not strict add to uint set if element already exists", async () => {
56+
await expect(mock.strictAddToUintSet([1, 1])).to.be.revertedWith("SetHelper: element already exists");
57+
});
58+
59+
it("should strict add to string set if element already exists", async () => {
60+
await expect(mock.strictAddToStringSet(["1", "1"])).to.be.revertedWith("SetHelper: element already exists");
61+
});
62+
63+
it("should strict add to address set", async () => {
64+
await mock.strictAddToAddressSet([FIRST.address, SECOND.address]);
65+
66+
expect(await mock.getAddressSet()).to.deep.equal([FIRST.address, SECOND.address]);
67+
});
68+
69+
it("should strict add to uint set", async () => {
70+
await mock.strictAddToUintSet([1]);
71+
72+
expect(await mock.getUintSet()).to.deep.equal([1n]);
73+
});
74+
75+
it("should strict add to string set", async () => {
76+
await mock.strictAddToStringSet(["1", "2", "3"]);
77+
78+
expect(await mock.getStringSet()).to.deep.equal(["1", "2", "3"]);
79+
});
80+
});
81+
4882
describe("remove", () => {
4983
it("should remove from address set", async () => {
5084
await mock.addToAddressSet([FIRST.address, SECOND.address]);
@@ -67,4 +101,47 @@ describe("SetHelper", () => {
67101
expect(await mock.getStringSet()).to.deep.equal(["3", "2"]);
68102
});
69103
});
104+
105+
describe("remove", () => {
106+
it("should not strict remove from address set if no such element", async () => {
107+
await mock.strictAddToAddressSet([FIRST.address, SECOND.address]);
108+
109+
await expect(mock.strictRemoveFromAddressSet([SECOND.address, SECOND.address])).to.be.revertedWith(
110+
"SetHelper: no such element",
111+
);
112+
});
113+
114+
it("should not strict remove from uint set if no such element", async () => {
115+
await mock.strictAddToUintSet([1]);
116+
117+
await expect(mock.strictRemoveFromUintSet([1, 1])).to.be.revertedWith("SetHelper: no such element");
118+
});
119+
120+
it("should not strict remove from string set if no such element", async () => {
121+
await mock.strictAddToStringSet(["1", "2", "3"]);
122+
123+
await expect(mock.strictRemoveFromStringSet(["1", "1"])).to.be.revertedWith("SetHelper: no such element");
124+
});
125+
126+
it("should strict remove from address set", async () => {
127+
await mock.strictAddToAddressSet([FIRST.address, SECOND.address]);
128+
await mock.strictRemoveFromAddressSet([SECOND.address]);
129+
130+
expect(await mock.getAddressSet()).to.deep.equal([FIRST.address]);
131+
});
132+
133+
it("should strict remove from uint set", async () => {
134+
await mock.strictAddToUintSet([1]);
135+
await mock.strictRemoveFromUintSet([1]);
136+
137+
expect(await mock.getUintSet()).to.deep.equal([]);
138+
});
139+
140+
it("should strict remove from string set", async () => {
141+
await mock.strictAddToStringSet(["1", "2", "3"]);
142+
await mock.strictRemoveFromStringSet(["1"]);
143+
144+
expect(await mock.getStringSet()).to.deep.equal(["3", "2"]);
145+
});
146+
});
70147
});

0 commit comments

Comments
 (0)