Skip to content

Commit

Permalink
Add factory - part 1 (#6)
Browse files Browse the repository at this point in the history
* add Test on Granted events
* remove useless argument
  • Loading branch information
adrien-vieilleribiere authored Apr 4, 2024
1 parent 27f5b65 commit 14f395a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
30 changes: 20 additions & 10 deletions backend/contracts/Admins.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import "./Albums.sol";
/// @custom:security-contact [email protected]
/**
* @title Admins Contract
Expand Down Expand Up @@ -67,12 +68,6 @@ contract Admins is Ownable{
* @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){
// for (uint256 i = 0; i < superAdminsAccounts.length; i++) {
// if (superAdminsAccounts[i] == _addr) {
// return false;
// }
// }
// return true;
return uint(adminRoles[_addr]) == uint(Role.None);
}

Expand Down Expand Up @@ -118,15 +113,30 @@ contract Admins is Ownable{
* @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 {
function addAdmin(address newAdmin) 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);
bytes memory collectionBytecode = type(Albums).creationCode;
bytes32 salt = keccak256(abi.encodePacked(newAdmin, block.timestamp));
address collectionAddress;
// assembly {
// collectionAddress := create2(
// 0,
// add(collectionBytecode, 0x20),
// mload(collectionBytecode),
// salt
// )
// if iszero(extcodesize(collectionAddress)) {
// // revert if something gone wrong (collectionAddress doesn't contain an address)
// revert(0, 0)
// }
// }
adminsContracts[newAdmin] = collectionAddress; // change this with deployed address
adminsAccounts.push(collectionAddress);
emit Granted(msg.sender,newAdmin, Role.Admin, collectionAddress);
}

function removeAdmin(address oldAdmin) external {
Expand Down
37 changes: 23 additions & 14 deletions backend/test/Admins.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ describe("Admins", function () {
describe("addAdmin", function () {

it("Should not be used without super privilegies", async function () {
await expect(admins.connect(addr1).addAdmin(addr2.address, addr2.address)).to.be.revertedWith("not super admin");
await expect(admins.connect(addr1).addAdmin(addr2.address)).to.be.revertedWith("not super admin");
});

it("Should not downgrade a super admin", async function () {
await expect(admins.addAdmin(owner.address, owner.address)).to.be.revertedWith("admin exists");
await expect(admins.addAdmin(owner.address)).to.be.revertedWith("admin exists");
});

it("Should fail if admin already exists", async function () {
await admins.addAdmin(addr1.address, addr1.address);
await expect(admins.addAdmin(addr1.address, addr1.address)).to.be.revertedWith("admin exists");
await admins.addAdmin(addr1.address);
await expect(admins.addAdmin(addr1.address)).to.be.revertedWith("admin exists");
});

it("Should setup adminRoles and adminsContracts", async function () {
await admins.addAdmin(addr1.address, addr1.address);
await admins.addAdmin(addr1.address);
await expect( await admins.adminRoles(addr1.address)).to.equal(1);
await expect( await admins.adminsContracts(addr1.address)).to.equal(addr1.address);
await expect( await admins.adminsContracts(addr1.address) != 0 );
// todo : fix with the deployed contract
});

Expand All @@ -60,7 +60,7 @@ describe("Admins", function () {
});

it("Should clean adminRoles", async function () {
await admins.addAdmin(addr1.address,addr1.address );
await admins.addAdmin(addr1.address);
await admins.removeAdmin(addr1.address);
await expect( await admins.adminRoles(addr1.address)).to.equal(0);
});
Expand Down Expand Up @@ -102,13 +102,22 @@ describe("Admins", function () {



// describe("Events", function () {
// it("Should emit an event on addAdmin", async function () {
// await expect(lock.withdraw())
// .to.emit(lock, "Withdrawal")
// .withArgs(lockedAmount, anyValue); // We accept any value as `when` arg
// });
// });
describe("Events", function () {

it("Should emit an event on addAdmin", async function () {
await expect(admins.addAdmin(addr1.address))
.to.emit(admins, 'Granted')
.withArgs( owner.address,addr1.address, 1, anyValue);
});

it("Should emit an event on addSuperAdmin", async function () {
await expect(admins.addSuperAdmin(addr2.address))
.to.emit(admins, 'Granted')
.withArgs(owner.address,addr2.address, 2, anyValue );
});


})

// describe("Transfers", function () {
// it("Should transfer the funds to the owner", async function () {
Expand Down

0 comments on commit 14f395a

Please sign in to comment.