Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/contracts/facilitators/gsm/GhoReserve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ contract GhoReserve is Ownable, VersionedInitializable, IGhoReserve {

/// @inheritdoc IGhoReserve
function use(uint256 amount) external {
require(_entities.contains(msg.sender), "NOT_ENTITY");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this check actually needed? same in restore

  • when removing the entity, we check used and limit is 0.
  • when adding an entity, it can start with 0 limit.
  • use check limit, so only existent entities with non-zero limit are allowed
  • restore check used, so only existent entities with non-zero used are allowed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

apart from this, we are doing a similar check in setLimit with different error. We should be consistent

  function setLimit(address entity, uint256 limit) external onlyOwner {
    require(_entities.contains(entity), 'ENTITY_DOES_NOT_EXIST');


GhoUsage storage entity = _ghoUsage[msg.sender];
require(entity.limit >= entity.used + amount, 'LIMIT_EXCEEDED');

Expand All @@ -57,6 +59,8 @@ contract GhoReserve is Ownable, VersionedInitializable, IGhoReserve {

/// @inheritdoc IGhoReserve
function restore(uint256 amount) external {
require(_entities.contains(msg.sender), "NOT_ENTITY");

_ghoUsage[msg.sender].used -= amount.toUint128();
IERC20(GHO_TOKEN).transferFrom(msg.sender, address(this), amount);
emit GhoRestored(msg.sender, amount);
Expand All @@ -79,6 +83,8 @@ contract GhoReserve is Ownable, VersionedInitializable, IGhoReserve {
require(_ghoUsage[entity].used == 0, 'ENTITY_GHO_USED_NOT_ZERO');
require(_entities.remove(entity), 'ENTITY_NOT_REMOVED');

delete _ghoUsage[entity];

emit EntityRemoved(entity);
}

Expand Down
19 changes: 17 additions & 2 deletions tests/unit/TestGhoReserve.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ contract TestGhoReserve is TestGhoBase {
}

function testRevertUseNoCapacity() public {
GHO_RESERVE.addEntity(address(this));
vm.expectRevert('LIMIT_EXCEEDED');
GHO_RESERVE.use(100 ether);
}
Expand Down Expand Up @@ -74,6 +75,11 @@ contract TestGhoReserve is TestGhoBase {
GHO_RESERVE.use(value + 1);
}

function testUseNotEntity() public {
vm.expectRevert("NOT_ENTITY");
GHO_RESERVE.use(1_000 ether);
}

function testRevertRestoreNoWithdrawnAmount() public {
GHO_RESERVE.addEntity(address(this));
GHO_RESERVE.setLimit(address(this), 10_000 ether);
Expand Down Expand Up @@ -123,6 +129,11 @@ contract TestGhoReserve is TestGhoBase {
GHO_RESERVE.restore(value + 1);
}

function testRestoreNotEntity() public {
vm.expectRevert("NOT_ENTITY");
GHO_RESERVE.use(1_000 ether);
}

function testAddEntity() public {
address alice = makeAddr('alice');
vm.expectEmit(true, true, true, true, address(GHO_RESERVE));
Expand All @@ -149,18 +160,22 @@ contract TestGhoReserve is TestGhoBase {
}

function testRemoveEntity() public {
uint256 limit = 1_000_000 ether;
address alice = makeAddr('alice');
vm.expectEmit(true, true, true, true, address(GHO_RESERVE));
emit EntityAdded(alice);
GHO_RESERVE.addEntity(address(alice));
GHO_RESERVE.addEntity(alice);
GHO_RESERVE.setLimit(alice, limit);

assertTrue(GHO_RESERVE.isEntity(alice));
assertEq(GHO_RESERVE.getLimit(alice), limit);

vm.expectEmit(true, true, true, true, address(GHO_RESERVE));
emit EntityRemoved(alice);
GHO_RESERVE.removeEntity(address(alice));
GHO_RESERVE.removeEntity(alice);

assertFalse(GHO_RESERVE.isEntity(alice));
assertEq(GHO_RESERVE.getLimit(alice), 0);
}

function testRemoveEntityNotInSet() public {
Expand Down