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 all 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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ $ forge --help
$ anvil --help
$ cast --help
```

***
*The IP in this repository was assigned to Mars SPC Limited in respect of the MarsOne SP*
12 changes: 0 additions & 12 deletions script/Counter.s.sol

This file was deleted.

14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

103 changes: 103 additions & 0 deletions src/SparkLendFreezerMom.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;

import { ISparkLendFreezerMom } from "src/interfaces/ISparkLendFreezerMom.sol";

interface AuthorityLike {
function canCall(address src, address dst, bytes4 sig) external view returns (bool);
}

interface PoolConfiguratorLike {
function setReserveFreeze(address asset, bool freeze) external;
}

interface PoolLike {
function getReservesList() external view returns (address[] memory);
}

contract SparkLendFreezerMom is ISparkLendFreezerMom {

/**********************************************************************************************/
/*** Declarations and Constructor ***/
/**********************************************************************************************/

address public immutable poolConfigurator;
address public immutable pool;

address public authority;
address public owner;

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

emit SetOwner(address(0), msg.sender);
}

/**********************************************************************************************/
/*** Modifiers ***/
/**********************************************************************************************/

modifier onlyOwner {
require(msg.sender == owner, "SparkLendFreezerMom/only-owner");
_;
}

modifier auth {
require(isAuthorized(msg.sender, msg.sig), "SparkLendFreezerMom/not-authorized");
_;
}

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

function setAuthority(address authority_) external override onlyOwner {
emit SetAuthority(authority, authority_);
authority = authority_;
}


function setOwner(address owner_) external override onlyOwner {
emit SetOwner(owner, owner_);
owner = owner_;
}

/**********************************************************************************************/
/*** Auth Functions ***/
/**********************************************************************************************/

function freezeAllMarkets() external override auth {
address[] memory reserves = PoolLike(pool).getReservesList();

for (uint256 i = 0; i < reserves.length; i++) {
address reserve = reserves[i];
PoolConfiguratorLike(poolConfigurator).setReserveFreeze(reserve, true);
emit FreezeMarket(reserve);
}
}

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

/**********************************************************************************************/
/*** Internal Functions ***/
/**********************************************************************************************/

function isAuthorized(address src, bytes4 sig) internal view returns (bool) {
if (src == address(this)) {
return true;
} else if (src == owner) {
return true;
} else if (authority == address(0)) {
return false;
} else {
return AuthorityLike(authority).canCall(src, address(this), sig);
}
}

}

96 changes: 96 additions & 0 deletions src/interfaces/ISparkLendFreezerMom.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.0;

interface ISparkLendFreezerMom {

/**********************************************************************************************/
/*** Events ***/
/**********************************************************************************************/

/**
* @dev Event to log the freezing of a given market in SparkLend.
* @dev NOTE: This event will fire even if the market is already frozen.
* @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.
* @param newOwner The address of the new owner.
*/
event SetOwner(address indexed oldOwner, address indexed newOwner);

/**
* @dev Event to log the setting of a new authority.
* @param oldAuthority The address of the previous authority.
* @param newAuthority The address of the new authority.
*/
event SetAuthority(address indexed oldAuthority, address indexed newAuthority);

/**********************************************************************************************/
/*** Storage Variables ***/
/**********************************************************************************************/

/**
* @dev Returns the address of the pool configurator.
* @return The address of the pool configurator.
*/
function poolConfigurator() external view returns (address);

/**
* @dev Returns the address of the pool.
* @return The address of the pool.
*/
function pool() external view returns (address);

/**
* @dev Returns the address of the authority.
* @return The address of the authority.
*/
function authority() external view returns (address);

/**
* @dev Returns the address of the owner.
* @return The address of the owner.
*/
function owner() external view returns (address);

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

/**
* @dev Function to set a new authority, permissioned to owner.
* @param authority The address of the new authority.
*/
function setAuthority(address authority) external;

/**
* @dev Function to set a new owner, permissioned to owner.
* @param owner The address of the new owner.
*/
function setOwner(address owner) external;

/**********************************************************************************************/
/*** Auth Functions ***/
/**********************************************************************************************/

/**
* @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. Note that the `authority` in this contract is assumed to be
* the Chief in the MakerDAO protocol.
* @param reserve The address of the market to freeze.
*/
function freezeMarket(address reserve) external;

/**
* @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. Note that the `authority` in this contract is assumed to be
* the Chief in the MakerDAO protocol.
*/
function freezeAllMarkets() external;

}
25 changes: 0 additions & 25 deletions test/Counter.t.sol

This file was deleted.

48 changes: 48 additions & 0 deletions test/Mocks.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity ^0.8.13;

contract AuthorityMock {

bool internal _allCanCall;

mapping (address => mapping (address => mapping (bytes4 => bool))) internal callAllowed;

function __setCanCall(address src, address dst, bytes4 sig, bool allowed) public {
callAllowed[src][dst][sig] = allowed;
}

function __setAllCanCall(bool allCanCall_) public {
_allCanCall = allCanCall_;
}

function canCall(address src, address dst, bytes4 sig) external view returns (bool) {
return callAllowed[src][dst][sig] || _allCanCall;
}

}

contract ConfiguratorMock {

function setReserveFreeze(address asset, bool freeze) external {}

}

contract PoolMock {

address[] internal assets;

function getReservesList() external view returns (address[] memory list) {
list = new address[](assets.length);

for (uint256 i; i < assets.length; ++i) {
list[i] = assets[i];
}
}

function __addAsset(address asset) public {
assets.push(asset);
}

}


Loading
Loading