Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Build initial implementation with unit testing (SC-204) #1

Merged
merged 21 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
8 changes: 4 additions & 4 deletions src/SparkLendFreezerMom.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ contract SparkLendFreezerMom is ISparkLendFreezerMom {
address public authority;
address public owner;

constructor(address poolConfigurator_, address pool_, address authority_) {
constructor(address poolConfigurator_, address pool_) {
poolConfigurator = poolConfigurator_;
pool = pool_;
authority = authority_;
owner = msg.sender;

emit SetOwner(address(0), msg.sender);
Expand All @@ -51,7 +50,7 @@ contract SparkLendFreezerMom is ISparkLendFreezerMom {
}

/**********************************************************************************************/
/*** Wards Functions ***/
/*** Owner Functions ***/
/**********************************************************************************************/

function setAuthority(address authority_) external override onlyOwner {
Expand All @@ -73,13 +72,14 @@ contract SparkLendFreezerMom is ISparkLendFreezerMom {
address[] memory reserves = PoolLike(pool).getReservesList();

for (uint256 i = 0; i < reserves.length; i++) {
if (reserves[i] == address(0)) continue;
PoolConfiguratorLike(poolConfigurator).setReserveFreeze(reserves[i], true);
hexonaut marked this conversation as resolved.
Show resolved Hide resolved
emit FreezeMarket(reserves[i]);
hexonaut marked this conversation as resolved.
Show resolved Hide resolved
}
}

function freezeMarket(address reserve) external override auth {
PoolConfiguratorLike(poolConfigurator).setReserveFreeze(reserve, true);
emit FreezeMarket(reserve);
}

/**********************************************************************************************/
Expand Down
14 changes: 12 additions & 2 deletions src/interfaces/ISparkLendFreezerMom.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ interface ISparkLendFreezerMom {
/*** Events ***/
/**********************************************************************************************/

/**
* @dev Event to log the freezing of a given market in SparkLend.
* @param reserve The address of the market reserve.
*/
event FreezeMarket(address indexed reserve);

/**
* @dev Event to log the setting of a new owner.
* @param oldOwner The address of the previous owner.
Expand Down Expand Up @@ -70,13 +76,17 @@ interface ISparkLendFreezerMom {
/**********************************************************************************************/

/**
* @dev Function to freeze a specified market. Permissioned to the `hat` in the Chief.
* @dev Function to freeze a specified market. Permissioned using the isAuthorized function
* which allows the owner, the freezer contract itself, or the `hat` in the Chief
* to call the function.
* @param reserve The address of the market to freeze.
*/
function freezeMarket(address reserve) external;

/**
* @dev Function to freeze all markets. Permissioned to the `hat` in the Chief.
* @dev Function to freeze all markets. Permissioned using the isAuthorized function
* which allows the owner, the freezer contract itself, or the `hat` in the Chief
* to call the function.
hexonaut marked this conversation as resolved.
Show resolved Hide resolved
*/
function freezeAllMarkets() external;

Expand Down
85 changes: 78 additions & 7 deletions test/SparkLendFreezerMom.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ contract SparkLendFreezerMomUnitTestBase is Test {
authority = new AuthorityMock();
configurator = address(new ConfiguratorMock());
pool = address(new PoolMock());
freezer = new SparkLendFreezerMom(configurator, pool, address(authority));
freezer = new SparkLendFreezerMom(configurator, pool);

freezer.setAuthority(address(authority));
freezer.setOwner(owner);
}

Expand All @@ -33,19 +34,18 @@ contract SparkLendFreezerMomUnitTestBase is Test {
contract ConstructorTests is SparkLendFreezerMomUnitTestBase {

function test_constructor() public {
freezer = new SparkLendFreezerMom(configurator, pool, address(authority));
freezer = new SparkLendFreezerMom(configurator, pool);

assertEq(freezer.poolConfigurator(), configurator);
assertEq(freezer.pool(), pool);
assertEq(freezer.authority(), address(authority));
assertEq(freezer.owner(), address(this));
}

}

contract SetOwnerTests is SparkLendFreezerMomUnitTestBase {

function test_setOwner_no_auth() public {
function test_setOwner_noAuth() public {
vm.expectRevert("SparkLendFreezerMom/only-owner");
freezer.setOwner(address(1));
}
Expand All @@ -64,7 +64,7 @@ contract SetOwnerTests is SparkLendFreezerMomUnitTestBase {

contract SetAuthorityTests is SparkLendFreezerMomUnitTestBase {

function test_setAuthority_no_auth() public {
function test_setAuthority_noAuth() public {
vm.expectRevert("SparkLendFreezerMom/only-owner");
freezer.setAuthority(makeAddr("newAuthority"));
}
Expand All @@ -89,8 +89,10 @@ contract FreezeAllMarketsTests is SparkLendFreezerMomUnitTestBase {
}

function test_freezeAllMarkets() public {
address caller = makeAddr("caller");

authority.__setCanCall(
address(this),
caller,
address(freezer),
freezer.freezeAllMarkets.selector,
true
Expand All @@ -105,6 +107,7 @@ contract FreezeAllMarketsTests is SparkLendFreezerMomUnitTestBase {
bytes4 poolSig = PoolMock.getReservesList.selector;
bytes4 configSig = ConfiguratorMock.setReserveFreeze.selector;

vm.prank(caller);
vm.expectCall(pool, abi.encodePacked(poolSig));
vm.expectCall(configurator, abi.encodePacked(configSig, abi.encode(asset1, true)));
vm.expectCall(configurator, abi.encodePacked(configSig, abi.encode(asset2, true)));
Expand All @@ -123,19 +126,87 @@ contract FreezeMarketTests is SparkLendFreezerMomUnitTestBase {
}

function test_freezeMarket() public {
address caller = makeAddr("caller");

authority.__setCanCall(
address(this),
caller,
address(freezer),
freezer.freezeMarket.selector,
true
);

bytes4 configSig = ConfiguratorMock.setReserveFreeze.selector;

vm.prank(caller);
vm.expectCall(configurator, abi.encodePacked(configSig, abi.encode(reserve, true)));
freezer.freezeMarket(reserve);
}

}

contract EventTests is SparkLendFreezerMomUnitTestBase {

event FreezeMarket(address indexed reserve);
event SetOwner(address indexed oldOwner, address indexed newOwner);
event SetAuthority(address indexed oldAuthority, address indexed newAuthority);

function test_setAuthority_eventData() external {
address newAuthority = makeAddr("newAuthority");

vm.prank(owner);
vm.expectEmit(address(freezer));
emit SetAuthority(address(authority), newAuthority);
freezer.setAuthority(newAuthority);
}

function test_setOwner_eventData() external {
address newOwner = makeAddr("newOwner");

vm.prank(owner);
vm.expectEmit(address(freezer));
emit SetOwner(owner, newOwner);
freezer.setOwner(newOwner);
}

function test_freezeMarket_eventData() public {
address caller = makeAddr("caller");
address asset = makeAddr("asset");

authority.__setCanCall(
caller,
address(freezer),
freezer.freezeMarket.selector,
true
);

vm.prank(caller);
emit FreezeMarket(asset);
freezer.freezeMarket(asset);
}

function test_freezeAllMarkets_eventData() public {
address caller = makeAddr("caller");

authority.__setCanCall(
caller,
address(freezer),
freezer.freezeAllMarkets.selector,
true
);

address asset1 = makeAddr("asset1");
address asset2 = makeAddr("asset2");

PoolMock(pool).__addAsset(asset1);
PoolMock(pool).__addAsset(asset2);

vm.prank(caller);
vm.expectEmit(address(freezer));
emit FreezeMarket(asset1);
vm.expectEmit(address(freezer));
emit FreezeMarket(asset2);
freezer.freezeAllMarkets();
}
}


Loading