Skip to content

Commit 479f812

Browse files
committed
feat: add error messages and tests to reserve
1 parent 5250add commit 479f812

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/contracts/facilitators/gsm/GhoReserve.sol

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ contract GhoReserve is Ownable, VersionedInitializable, IGhoReserve {
2323
mapping(address => GhoUsage) private _ghoUsage;
2424

2525
/// Set of entities with a GHO limit available
26-
EnumerableSet.AddressSet private entities;
26+
EnumerableSet.AddressSet private _entities;
2727

2828
/**
2929
* @dev Constructor
@@ -68,30 +68,29 @@ contract GhoReserve is Ownable, VersionedInitializable, IGhoReserve {
6868

6969
/// @inheritdoc IGhoReserve
7070
function addEntity(address entity) external onlyOwner {
71-
entities.add(entity);
71+
require(_entities.add(entity), 'ENTITY_ALREADY_EXISTS');
7272
emit EntityAdded(entity);
7373
}
7474

7575
/// @inheritdoc IGhoReserve
7676
function removeEntity(address entity) external onlyOwner {
77-
GhoUsage memory usage = _ghoUsage[entity];
78-
require(usage.used == 0, 'CANNOT_REMOVE_ENTITY_WITH_BALANCE');
79-
entities.remove(entity);
77+
require(_ghoUsage[entity].used == 0, 'ENTITY_GHO_USED_NOT_ZERO');
78+
require(_entities.remove(entity), 'ENTITY_NOT_REMOVED');
8079

8180
emit EntityRemoved(entity);
8281
}
8382

8483
/// @inheritdoc IGhoReserve
8584
function setLimit(address entity, uint256 limit) external onlyOwner {
86-
require(entities.contains(entity), 'ENTITY_NOT_ALLOWED');
85+
require(_entities.contains(entity), 'ENTITY_DOES_NOT_EXIST');
8786
_ghoUsage[entity].limit = uint128(limit);
8887

8988
emit GhoLimitUpdated(entity, limit);
9089
}
9190

9291
/// @inheritdoc IGhoReserve
9392
function getEntities() external view returns (address[] memory) {
94-
return entities.values();
93+
return _entities.values();
9594
}
9695

9796
/// @inheritdoc IGhoReserve
@@ -112,12 +111,12 @@ contract GhoReserve is Ownable, VersionedInitializable, IGhoReserve {
112111

113112
/// @inheritdoc IGhoReserve
114113
function isEntity(address entity) external view returns (bool) {
115-
return entities.contains(entity);
114+
return _entities.contains(entity);
116115
}
117116

118117
/// @inheritdoc IGhoReserve
119118
function totalEntities() external view returns (uint256) {
120-
return entities.length();
119+
return _entities.length();
121120
}
122121

123122
/// @inheritdoc IGhoReserve

src/test/TestGhoReserve.t.sol

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,17 +104,19 @@ contract TestGhoReserve is TestGhoBase {
104104
}
105105

106106
function testAddEntityAlreadyInSet() public {
107+
uint256 entitiesCount = GHO_RESERVE.totalEntities();
107108
address alice = makeAddr('alice');
108109
vm.expectEmit(true, true, true, true, address(GHO_RESERVE));
109110
emit EntityAdded(alice);
110111
GHO_RESERVE.addEntity(address(alice));
111112

112113
// Set already contains two entities from constructor
113-
assertEq(GHO_RESERVE.totalEntities(), 3);
114+
assertEq(GHO_RESERVE.totalEntities(), entitiesCount + 1);
114115

116+
vm.expectRevert('ENTITY_ALREADY_EXISTS');
115117
GHO_RESERVE.addEntity(address(alice));
116118

117-
assertEq(GHO_RESERVE.totalEntities(), 3);
119+
assertEq(GHO_RESERVE.totalEntities(), entitiesCount + 1);
118120
}
119121

120122
function testRemoveEntity() public {
@@ -133,14 +135,16 @@ contract TestGhoReserve is TestGhoBase {
133135
}
134136

135137
function testRemoveEntityNotInSet() public {
138+
uint256 entitiesCount = GHO_RESERVE.totalEntities();
136139
address alice = makeAddr('alice');
137140
assertFalse(GHO_RESERVE.isEntity(alice));
138-
assertEq(GHO_RESERVE.totalEntities(), 2);
141+
assertEq(GHO_RESERVE.totalEntities(), entitiesCount);
139142

143+
vm.expectRevert('ENTITY_NOT_REMOVED');
140144
GHO_RESERVE.removeEntity(address(alice));
141145

142146
assertFalse(GHO_RESERVE.isEntity(alice));
143-
assertEq(GHO_RESERVE.totalEntities(), 2);
147+
assertEq(GHO_RESERVE.totalEntities(), entitiesCount);
144148
}
145149

146150
function testRevertRemoveEntityBalanceOutstanding() public {
@@ -152,7 +156,7 @@ contract TestGhoReserve is TestGhoBase {
152156
vm.prank(alice);
153157
GHO_RESERVE.use(5_000 ether);
154158

155-
vm.expectRevert('CANNOT_REMOVE_ENTITY_WITH_BALANCE');
159+
vm.expectRevert('ENTITY_GHO_USED_NOT_ZERO');
156160
GHO_RESERVE.removeEntity(alice);
157161
}
158162

@@ -166,6 +170,11 @@ contract TestGhoReserve is TestGhoBase {
166170
GHO_RESERVE.setLimit(alice, capacity);
167171
}
168172

173+
function testSetLimitEntityDoesNotExist() public {
174+
vm.expectRevert('ENTITY_DOES_NOT_EXIST');
175+
GHO_RESERVE.setLimit(makeAddr('no-entity'), 100_000 ether);
176+
}
177+
169178
function testTransfer() public {
170179
GhoReserve reserve = _deployReserve();
171180
address facilitator = makeAddr('facilitator');

0 commit comments

Comments
 (0)