Skip to content

Commit

Permalink
rainbowkit wagmi adminRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Apr 4, 2024
1 parent fe6f60d commit d8b3f0d
Show file tree
Hide file tree
Showing 34 changed files with 25,083 additions and 20,991 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.vscode
.vscode
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
## Dependencies

### Backend

To install the backend dependencies, navigate to the backend directory and run:

`yarn install`

### Frontend

To install the frontend dependencies, navigate to the frontend directory and run:

`npm install`
## Dependencies

### Backend

To install the backend dependencies, navigate to the backend directory and run:

`yarn install`

### Frontend

To install the frontend dependencies, navigate to the frontend directory and run:

`npm install`
30 changes: 15 additions & 15 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
node_modules
.env

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json

node_modules
.env

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json

.yarn
2 changes: 1 addition & 1 deletion backend/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodeLinker: node-modules
nodeLinker: node-modules
70 changes: 35 additions & 35 deletions backend/a_launcher.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
#!/bin/bash

while true
do
echo "Welcome to the deployment menu"
echo "Please select an option:"

PS3='Choose a deployment option: '
options=("Tests" "Deploy Local" "Deploy Harmonie Testnet" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Tests")
echo "Testing the contracts on localhost network"
REPORT_GAS=true npx hardhat coverage
break
;;
"Deploy Local")
echo "Deploying contracts on localhost network"
npx hardhat run scripts/deploy.js --network localhost
break
;;
"Deploy Harmonie Testnet")
echo "Deploying factory on localhost network"
npx hardhat run scripts/deploy.js --network localhost
break
;;
"Quit")
echo "Exiting..."
exit 0
;;
*) echo "Invalid option $REPLY";;
esac
done
done
#!/bin/bash

while true
do
echo "Welcome to the deployment menu"
echo "Please select an option:"

PS3='Choose a deployment option: '
options=("Tests" "Deploy Local" "Deploy Harmonie Testnet" "Quit")
select opt in "${options[@]}"
do
case $opt in
"Tests")
echo "Testing the contracts on localhost network"
REPORT_GAS=true npx hardhat coverage
break
;;
"Deploy Local")
echo "Deploying contracts on localhost network"
npx hardhat run scripts/deploy.js --network localhost
break
;;
"Deploy Harmonie Testnet")
echo "Deploying factory on localhost network"
npx hardhat run scripts/deploy.js --network localhost
break
;;
"Quit")
echo "Exiting..."
exit 0
;;
*) echo "Invalid option $REPLY";;
esac
done
done
229 changes: 135 additions & 94 deletions backend/contracts/Admins.sol
Original file line number Diff line number Diff line change
@@ -1,94 +1,135 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
/// @custom:security-contact [email protected]
contract Admins is Ownable{

event Granted (
/// Granter of the role.
address from,
/// Grantee of the role.
address to,
/// The role granted.
Role role,
address contractt
);

enum Role {
None,
Admin,
SuperAdmin
}
// Mapping of the user roles.
mapping(address => Role) public adminRoles;
mapping(address => address) public adminsContracts;
address[] public adminsAccounts;
address[] private superAdminsAccounts;
// Owner of the smart contract.
constructor(address initialOwner) Ownable(initialOwner){
adminRoles[initialOwner] = Role.SuperAdmin;
}

function ensureSuperAdmin(address _addr) internal view returns (bool){
return uint(adminRoles[_addr]) == uint(Role.SuperAdmin);
}

function ensureAdminDoNotExist(address _addr) internal view returns (bool){
for (uint256 i = 0; i < superAdminsAccounts.length; i++) {
if (adminsAccounts[i] == _addr) {
return false;
}
}
return true;
}

function ensureSuperAdminDoNotExist(address _addr) internal view returns (bool){
for (uint256 i = 0; i < superAdminsAccounts.length; i++) {
if (superAdminsAccounts[i] == _addr) {
return false;
}
}
return true;
}


/// @dev Allow a superAdmin.
function addSuperAdmin(address _addr) external onlyOwner {
require(ensureSuperAdminDoNotExist(_addr));
require(ensureAdminDoNotExist(_addr));
// todo factory call to deploy the contract and get the deployment address deployment address
address _deployedTO = address(0);
adminRoles[_addr] = Role.SuperAdmin;
superAdminsAccounts.push(_addr);
emit Granted(msg.sender,_addr, Role.SuperAdmin, _deployedTO);
}

/// @dev Remove a superAdmin.
function removeSuperAdmin(address _addr) external onlyOwner {
for (uint256 i = 0; i < superAdminsAccounts.length; i++) {
if (superAdminsAccounts[i] == _addr) {
delete superAdminsAccounts[i];
}
}
//address _deployedTO = address(0);
adminRoles[_addr] = Role.None;
superAdminsAccounts.push(_addr);
emit Granted(msg.sender,_addr, Role.None, _addr);
}

/// @dev Allow an Admin.
function addAdmin(address new_admin, address newContract) external {
require(ensureSuperAdmin(msg.sender), "not super admin");
require(ensureAdminDoNotExist(new_admin), "admin exists");
require(adminRoles[new_admin] == Role.None, "role already set");
// todo factory call to deploy the contract and get the deployment address deployment address
adminRoles[new_admin] = Role.Admin;
adminsContracts[new_admin] = new_admin; // change this with deployed adress
adminsAccounts.push(new_admin);
emit Granted(msg.sender,new_admin, Role.Admin , newContract);
}



}

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
/// @custom:security-contact [email protected]
/**
* @title Admins Contract
* @dev Manages administrative privileges in a smart contract system.
* @notice This contract allows for the addition and removal of admin addresses.
*/
contract Admins is Ownable{

event Granted (
/// Granter of the role.
address from,
/// Grantee of the role.
address to,
/// The role granted.
Role role,
address contractt
);

enum Role {
None,
Admin,
SuperAdmin
}
// Mapping of the user roles.
mapping(address => Role) public adminRoles;
mapping(address => address) public adminsContracts;
address[] public adminsAccounts;
address[] private superAdminsAccounts;

/**
* @dev Constructor that sets the deployer as the initial admin.
*/
constructor(address initialOwner) Ownable(initialOwner){
adminRoles[initialOwner] = Role.SuperAdmin;
}

/**
* @dev Checks if the given address is assigned the SuperAdmin role.
* This function is internal and can be used within the contract to ensure that
* an operation is being performed by a SuperAdmin.
* @param _addr The address to check for the SuperAdmin role.
* @return bool Returns true if the address is a SuperAdmin, false otherwise.
*/
function ensureSuperAdmin(address _addr) internal view returns (bool){
return uint(adminRoles[_addr]) == uint(Role.SuperAdmin);
}

/**
* @dev Ensures that the given address is not already present in the `adminsAccounts` array.
* This check helps in avoiding duplicate admin entries. Since this function iterates
* over the `adminsAccounts` array, be cautious of gas costs when the array size grows.
* @param _addr The address to verify against the admin accounts mapping.
* @return bool Returns true if the address is not an admin, false if it already exists as an admin.
*/
function ensureAdminDoNotExist(address _addr) internal view returns (bool){
return uint(adminRoles[_addr]) == uint(Role.None);
}

/**
* @dev Verifies that the given address is not already listed in the `superAdminsAccounts` array.
* Useful for maintaining a clean list of SuperAdmins without duplicates. As with `ensureAdminDoNotExist`,
* be mindful of potential gas costs due to array iteration.
* @param _addr The address to check against the super admin accounts list.
* @return bool Returns true if the address is not a super admin, false if it already exists as a super admin.
*/
function ensureSuperAdminDoNotExist(address _addr) internal view returns (bool){
return uint(adminRoles[_addr]) == uint(Role.None);
}

/**
* @dev Adds a new SuperAdmin to the contract.
* This function can only be called by the contract owner. It ensures that the address being added does not already exist as either a SuperAdmin or Admin. After verification, it assigns the SuperAdmin role to the address and emits an event.
* @param _addr Address to be added as a SuperAdmin.
* @notice This action is irreversible through this function and can only be performed by the contract owner.
*/
function addSuperAdmin(address _addr) external onlyOwner {
require(ensureSuperAdminDoNotExist(_addr) && ensureAdminDoNotExist(_addr), "role already set");
// todo factory call to deploy the contract and get the deployment address deployment address
address _deployedTO = address(0);
adminRoles[_addr] = Role.SuperAdmin;
superAdminsAccounts.push(_addr);
emit Granted(msg.sender,_addr, Role.SuperAdmin, _deployedTO);
}

/**
* @dev Removes a super administrator from the contract.
* This function allows the contract owner to remove an address from the list of super administrators.
* It updates the admin's role to 'None' and emits a 'Granted' event indicating the role change.
* Note: This function should be called only by the contract owner.
* @param _addr The address to be removed from the super administrators list.
* @notice Ensure that the address being removed is indeed a super administrator and that
* you have the necessary permissions to perform this action.
*/
function removeSuperAdmin(address _addr) external onlyOwner {
for (uint256 i = 0; i < superAdminsAccounts.length; i++) {
if (superAdminsAccounts[i] == _addr) {
delete superAdminsAccounts[i];
}
}
//address _deployedTO = address(0);
adminRoles[_addr] = Role.None;
superAdminsAccounts.push(_addr);
emit Granted(msg.sender,_addr, Role.None, _addr);
}

/**
* @dev Adds a new admin.
* @param newAdmin Address to be added as admin.
* @notice Only existing superadmins can add new admins.
//TODO newContract will be removed to be used after deploy
*/
function addAdmin(address newAdmin, address newContract) external {
require(ensureAdminDoNotExist(newAdmin), "admin exists");
require(ensureSuperAdmin(msg.sender), "not super admin");
require(adminRoles[newAdmin] == Role.None, "role already set");
// todo factory call to deploy the contract and get the deployment address deployment address
adminRoles[newAdmin] = Role.Admin;
adminsContracts[newAdmin] = newAdmin; // change this with deployed adress
adminsAccounts.push(newAdmin);
emit Granted(msg.sender,newAdmin, Role.Admin , newContract);
}

function removeAdmin(address oldAdmin) external {
require(ensureSuperAdmin(msg.sender), "not super admin caller");
require(adminRoles[oldAdmin] == Role.Admin, "not an admin");
// todo factory call to deploy the contract and get the deployment address deployment address
adminRoles[oldAdmin] = Role.None;
// deal with adminsContracts[oldAdmin] ?
// remove it ? adminsAccounts.push(oldAdmin);
emit Granted(msg.sender,oldAdmin, Role.None , oldAdmin);
}
}
Loading

0 comments on commit d8b3f0d

Please sign in to comment.