Skip to content

Commit

Permalink
Finished Treasury
Browse files Browse the repository at this point in the history
  • Loading branch information
Sir-Deon committed Apr 4, 2024
1 parent 7be9dd5 commit c53b667
Showing 1 changed file with 19 additions and 101 deletions.
120 changes: 19 additions & 101 deletions contracts/Treasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ pragma solidity ^0.8.18;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
import "./interfaces/ITreasury.sol";

contract Treasury is ITreasury, Ownable, ReentrancyGuard {
contract Treasury is ITreasury, AccessControl, ReentrancyGuard {
bool public paused;
address[] multisig;
mapping(address => bool) public multisign;
address baki_vault;
mapping(string => address) currencies;

Expand All @@ -26,17 +25,12 @@ contract Treasury is ITreasury, Ownable, ReentrancyGuard {
);
event UnauthorizedAccess(address indexed _user, bool status);

constructor(
address _multisig1,
address _multisig2,
address _multisig3,
string memory _asset,
address _assetAddress
) Ownable(msg.sender) {
multisig.push(_multisig1);
multisig.push(_multisig2);
multisig.push(_multisig3);
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
bytes32 public constant VAULT_ROLE = keccak256("VAULT_ROLE");

constructor(string memory _asset, address _assetAddress) {
currencies[_asset] = _assetAddress;
grantRole(ADMIN_ROLE, msg.sender);
}

// Modifiers
Expand All @@ -51,10 +45,7 @@ contract Treasury is ITreasury, Ownable, ReentrancyGuard {
uint256 _amount,
string memory _asset
) external isPaused nonReentrant {
bool auth = authorized();
if (auth == false) return;
bool result = checkMutisig();
require(result == true, "This action requires multisig");
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
require(
IERC20(currencies[_asset]).balanceOf(address(this)) >= _amount,
"Insufficient balance"
Expand All @@ -66,7 +57,6 @@ contract Treasury is ITreasury, Ownable, ReentrancyGuard {
);

if (!transferSuccess) revert();
rollMultisig();
emit Withdraw(msg.sender, currencies[_asset], _amount);
}

Expand All @@ -76,10 +66,7 @@ contract Treasury is ITreasury, Ownable, ReentrancyGuard {
uint256 _amount,
string memory _asset
) external isPaused nonReentrant {
if (baki_vault != msg.sender) {
_pause();
revert();
}
require(hasRole(VAULT_ROLE, msg.sender), " Not vault");
require(
IERC20(currencies[_asset]).balanceOf(address(this)) >= _amount,
"Insufficient balance"
Expand All @@ -97,114 +84,45 @@ contract Treasury is ITreasury, Ownable, ReentrancyGuard {

// Add Baki vault address
function addBakiVault(address _vault) external isPaused nonReentrant {
bool auth = authorized();
if (auth == false) return;
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
require(_vault != address(0), "address cannot be a zero address");
baki_vault = _vault;
grantRole(ADMIN_ROLE, _vault);
}

function addAsset(address _address, string memory _asset)
external
isPaused
nonReentrant
{
bool auth = authorized();
if (auth == false) return;
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
require(_address != address(0), "address cannot be a zero address");
currencies[_asset] = _address;
}

function viewBalance(string memory _asset)
external
view
onlyOwner
isPaused
returns (uint256)
{
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
return IERC20(currencies[_asset]).balanceOf(address(this));
}

// Control functuons
function pauseVault() external onlyOwner nonReentrant {
_pause();
function pauseVault() external nonReentrant {
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
paused = true;
}

function unPauseVault() external nonReentrant {
bool auth = authorized();
if (auth == false) return;
bool result = checkMutisig();
require(result == true, "This action requires multisig");
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
paused = false;
rollMultisig();
}

function isTreasuryPaused() external view onlyOwner returns (bool) {
function isTreasuryPaused() external view returns (bool) {
require(hasRole(ADMIN_ROLE, msg.sender), "Not admin");
return paused;
}

function addMultisig(address _signer) external isPaused nonReentrant {
bool auth = authorized();
if (auth == false) return;
bool result = checkMutisig();
require(result == true, "This action requires multisig");
require(_signer != address(0), "address cannot be a zero address");
multisig.push(_signer);
multisign[_signer] = false;
rollMultisig();
}

function updateMultisig(address _signer, uint256 _index)
external
isPaused
nonReentrant
{
bool auth = authorized();
if (auth == false) return;
bool result = checkMutisig();
require(result == true, "This action requires multisig");
require(_signer != address(0), "address cannot be a zero address");
multisig[_index] = _signer;
rollMultisig();
}

function getMultisigNumber() external view onlyOwner returns (uint256) {
return multisig.length;
}

function validateAction() external nonReentrant {
for (uint256 i; i < multisig.length; i++) {
if (multisig[i] == msg.sender) {
multisign[msg.sender] = true;
}
}
}

function rollMultisig() internal {
for (uint256 i; i < multisig.length; i++) {
multisign[multisig[i]] = false;
}
}

function checkMutisig() internal view returns (bool) {
for (uint256 i; i < multisig.length; i++) {
if (multisign[multisig[i]] == false) {
return false;
}
}
return true;
}

function authorized() internal returns (bool) {
// Add your custom logic here
if (msg.sender == owner()) {
return true;
}
_pause();
emit UnauthorizedAccess(msg.sender, paused);
return false;
}

function _pause() internal {
paused = true;
}
}

0 comments on commit c53b667

Please sign in to comment.