Skip to content

Commit

Permalink
Merge pull request #8 from volkanguneri/fix/factory
Browse files Browse the repository at this point in the history
add clone to fix contract creation
  • Loading branch information
guillaumedebavelaere authored Apr 4, 2024
2 parents 66e7737 + 1a3bbed commit ebb9ef7
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 110 deletions.
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
33 changes: 14 additions & 19 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 "@openzeppelin/contracts/proxy/Clones.sol";
import "./Albums.sol";
/// @custom:security-contact [email protected]
/**
Expand Down Expand Up @@ -30,12 +31,15 @@ contract Admins is Ownable{
mapping(address => address) public adminsContracts;
address[] public adminsAccounts;
address[] private superAdminsAccounts;

address private immutable _albumsTemplate;

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

/**
Expand Down Expand Up @@ -117,26 +121,17 @@ contract Admins is Ownable{
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;
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);

address clone = Clones.clone(_albumsTemplate);
Albums(clone).initialize(
newAdmin
);

adminsContracts[newAdmin] = clone;
adminsAccounts.push(clone);
emit Granted(msg.sender, newAdmin, Role.Admin, clone);
}

function removeAdmin(address oldAdmin) external {
Expand Down
10 changes: 7 additions & 3 deletions backend/contracts/Albums.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract Albums is Ownable {
contract Albums is OwnableUpgradeable {

struct Album {
uint16 id;
Expand Down Expand Up @@ -42,7 +42,11 @@ contract Albums is Ownable {
uint16 song_id
);

constructor(address initialOwner) Ownable(initialOwner) {}
function initialize(
address _user
) external initializer {
_transferOwnership(_user);
}

function createAlbum(
uint64 maxSupply,
Expand Down
1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.0",
"@openzeppelin/contracts-upgradeable": "^5.0.2",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"chai": "^4.2.0",
Expand Down
91 changes: 41 additions & 50 deletions backend/test/Admins.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const {
time,
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { ethers } = require("hardhat");
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");
const { ZeroAddress } = require("ethers");

describe("Admins", function () {
let owner;
Expand All @@ -15,12 +13,10 @@ describe("Admins", function () {

beforeEach(async function () {
const Admins = await ethers.getContractFactory("Admins");

[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
admins = await Admins.deploy(owner.address);
//adminsAdd = admins.target;


});

describe("Deployment", function () {
Expand All @@ -30,47 +26,49 @@ describe("Admins", function () {
});

describe("addAdmin", function () {

it("Should not be used without super privilegies", async function () {
await expect(admins.connect(addr1).addAdmin(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)).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);
await expect(admins.addAdmin(addr1.address)).to.be.revertedWith("admin exists");
await expect(admins.addAdmin(addr1.address)).to.be.revertedWith(
"admin exists"
);
});

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

});

describe("removeAdmin", function () {

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

it("Should clean adminRoles", async function () {
await admins.addAdmin(addr1.address);
await admins.removeAdmin(addr1.address);
await expect( await admins.adminRoles(addr1.address)).to.equal(0);
expect(await admins.adminRoles(addr1.address)).to.equal(ZeroAddress);
});

});

describe("addSuperAdmin", function () {

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

it("Should fail if superadmin already exists", async function () {
Expand All @@ -80,57 +78,50 @@ describe("Admins", function () {

it("Should setup adminRoles", async function () {
await admins.addSuperAdmin(addr1.address);
await expect( await admins.adminRoles(addr1.address)).to.equal(2);
// todo : fix with the deployed contract
await expect(await admins.adminRoles(addr1.address)).to.equal(2);
// todo : fix with the deployed contract
});

});

describe("removeSuperAdmin", function () {

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

it("Should clean adminRoles", async function () {
await admins.addSuperAdmin(addr1.address);
await admins.removeSuperAdmin(addr1.address);
await expect( await admins.adminRoles(addr1.address)).to.equal(0);
await expect(await admins.adminRoles(addr1.address)).to.equal(0);
});

});



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);
.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 );
.to.emit(admins, "Granted")
.withArgs(owner.address, addr2.address, 2, anyValue);
});
});

// describe("Transfers", function () {
// it("Should transfer the funds to the owner", async function () {
// const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
// deployOneYearLockFixture
// );

})

// describe("Transfers", function () {
// it("Should transfer the funds to the owner", async function () {
// const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
// deployOneYearLockFixture
// );

// await time.increaseTo(unlockTime);
// await time.increaseTo(unlockTime);

// await expect(lock.withdraw()).to.changeEtherBalances(
// [owner, lock],
// [lockedAmount, -lockedAmount]
// );
// });
// });
// await expect(lock.withdraw()).to.changeEtherBalances(
// [owner, lock],
// [lockedAmount, -lockedAmount]
// );
// });
// });
});
5 changes: 3 additions & 2 deletions backend/test/Albums.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ const {
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
const { ethers } = require("hardhat");
const { utils } = require("ethers");
require("ethers");
const { expect } = require("chai");

describe("Albums test", async function () {
const deployAlbumsFixture = async () => {
const [owner, otherAccount1] = await ethers.getSigners();

const Albums = await ethers.getContractFactory("Albums");
const albums = await Albums.deploy(owner.address);
const albums = await Albums.deploy();
await albums.initialize(owner.address);

return { albums, owner, otherAccount1 };
};
Expand Down
7 changes: 6 additions & 1 deletion backend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,11 @@
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc" "0.1.1"
"@nomicfoundation/solidity-analyzer-win32-x64-msvc" "0.1.1"

"@openzeppelin/contracts-upgradeable@^5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-5.0.2.tgz#3e5321a2ecdd0b206064356798c21225b6ec7105"
integrity sha512-0MmkHSHiW2NRFiT9/r5Lu4eJq5UJ4/tzlOgYXNAIj/ONkQTVnz22pLxDvp4C4uZ9he7ZFvGn3Driptn1/iU7tQ==

"@openzeppelin/contracts@^5.0.2":
version "5.0.2"
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-5.0.2.tgz#b1d03075e49290d06570b2fd42154d76c2a5d210"
Expand Down Expand Up @@ -1833,7 +1838,7 @@ ethers@^5.7.2:
"@ethersproject/web" "5.7.1"
"@ethersproject/wordlists" "5.7.0"

ethers@^6.4.0, ethers@^6.7.0:
ethers@^6.11.1, ethers@^6.7.0:
version "6.11.1"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-6.11.1.tgz#96aae00b627c2e35f9b0a4d65c7ab658259ee6af"
integrity sha512-mxTAE6wqJQAbp5QAe/+o+rXOID7Nw91OZXvgpjDa1r4fAbq2Nu314oEZSbjoRLacuCzs7kUC3clEvkCQowffGg==
Expand Down

0 comments on commit ebb9ef7

Please sign in to comment.