Skip to content

Commit 0b637d3

Browse files
add clone to fix contract creation
1 parent 66e7737 commit 0b637d3

File tree

6 files changed

+71
-71
lines changed

6 files changed

+71
-71
lines changed

backend/contracts/Admins.sol

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.24;
33
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
4+
import "@openzeppelin/contracts/proxy/Clones.sol";
45
import "./Albums.sol";
56
/// @custom:security-contact [email protected]
67
/**
@@ -30,12 +31,15 @@ contract Admins is Ownable{
3031
mapping(address => address) public adminsContracts;
3132
address[] public adminsAccounts;
3233
address[] private superAdminsAccounts;
34+
35+
address private immutable _albumsTemplate;
3336

3437
/**
3538
* @dev Constructor that sets the deployer as the initial admin.
3639
*/
3740
constructor(address initialOwner) Ownable(initialOwner){
3841
adminRoles[initialOwner] = Role.SuperAdmin;
42+
_albumsTemplate = address(new Albums());
3943
}
4044

4145
/**
@@ -117,26 +121,17 @@ contract Admins is Ownable{
117121
require(ensureAdminDoNotExist(newAdmin), "admin exists");
118122
require(ensureSuperAdmin(msg.sender), "not super admin");
119123
require(adminRoles[newAdmin] == Role.None, "role already set");
120-
// todo factory call to deploy the contract and get the deployment address deployment address
124+
121125
adminRoles[newAdmin] = Role.Admin;
122-
bytes memory collectionBytecode = type(Albums).creationCode;
123-
bytes32 salt = keccak256(abi.encodePacked(newAdmin, block.timestamp));
124-
address collectionAddress;
125-
// assembly {
126-
// collectionAddress := create2(
127-
// 0,
128-
// add(collectionBytecode, 0x20),
129-
// mload(collectionBytecode),
130-
// salt
131-
// )
132-
// if iszero(extcodesize(collectionAddress)) {
133-
// // revert if something gone wrong (collectionAddress doesn't contain an address)
134-
// revert(0, 0)
135-
// }
136-
// }
137-
adminsContracts[newAdmin] = collectionAddress; // change this with deployed address
138-
adminsAccounts.push(collectionAddress);
139-
emit Granted(msg.sender,newAdmin, Role.Admin, collectionAddress);
126+
127+
address clone = Clones.clone(_albumsTemplate);
128+
Albums(clone).initialize(
129+
newAdmin
130+
);
131+
132+
adminsContracts[newAdmin] = clone;
133+
adminsAccounts.push(clone);
134+
emit Granted(msg.sender, newAdmin, Role.Admin, clone);
140135
}
141136

142137
function removeAdmin(address oldAdmin) external {

backend/contracts/Albums.sol

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.24;
33

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

6-
contract Albums is Ownable {
6+
contract Albums is OwnableUpgradeable {
77

88
struct Album {
99
uint16 id;
@@ -42,7 +42,11 @@ contract Albums is Ownable {
4242
uint16 song_id
4343
);
4444

45-
constructor(address initialOwner) Ownable(initialOwner) {}
45+
function initialize(
46+
address _user
47+
) external initializer {
48+
_transferOwnership(_user);
49+
}
4650

4751
function createAlbum(
4852
uint64 maxSupply,

backend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
1212
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
1313
"@nomicfoundation/hardhat-verify": "^2.0.0",
14+
"@openzeppelin/contracts-upgradeable": "^5.0.2",
1415
"@typechain/ethers-v6": "^0.5.0",
1516
"@typechain/hardhat": "^9.0.0",
1617
"chai": "^4.2.0",

backend/test/Admins.js

+40-46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const {
55
const { ethers } = require("hardhat");
66
const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
77
const { expect } = require("chai");
8+
const { ZeroAddress } = require("ethers");
89

910
describe("Admins", function () {
1011
let owner;
@@ -15,12 +16,10 @@ describe("Admins", function () {
1516

1617
beforeEach(async function () {
1718
const Admins = await ethers.getContractFactory("Admins");
18-
19+
1920
[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
2021
admins = await Admins.deploy(owner.address);
2122
//adminsAdd = admins.target;
22-
23-
2423
});
2524

2625
describe("Deployment", function () {
@@ -30,47 +29,49 @@ describe("Admins", function () {
3029
});
3130

3231
describe("addAdmin", function () {
33-
3432
it("Should not be used without super privilegies", async function () {
35-
await expect(admins.connect(addr1).addAdmin(addr2.address)).to.be.revertedWith("not super admin");
33+
await expect(
34+
admins.connect(addr1).addAdmin(addr2.address)
35+
).to.be.revertedWith("not super admin");
3636
});
3737

3838
it("Should not downgrade a super admin", async function () {
39-
await expect(admins.addAdmin(owner.address)).to.be.revertedWith("admin exists");
39+
await expect(admins.addAdmin(owner.address)).to.be.revertedWith(
40+
"admin exists"
41+
);
4042
});
4143

4244
it("Should fail if admin already exists", async function () {
4345
await admins.addAdmin(addr1.address);
44-
await expect(admins.addAdmin(addr1.address)).to.be.revertedWith("admin exists");
46+
await expect(admins.addAdmin(addr1.address)).to.be.revertedWith(
47+
"admin exists"
48+
);
4549
});
4650

4751
it("Should setup adminRoles and adminsContracts", async function () {
4852
await admins.addAdmin(addr1.address);
49-
await expect( await admins.adminRoles(addr1.address)).to.equal(1);
50-
await expect( await admins.adminsContracts(addr1.address) != 0 );
51-
// todo : fix with the deployed contract
53+
expect(await admins.adminRoles(addr1.address)).to.equal(1);
54+
expect(await admins.adminsContracts(addr1.address)).not.to.equal(ZeroAddress);
5255
});
53-
5456
});
5557

5658
describe("removeAdmin", function () {
57-
5859
it("Should not be used without super privilegies", async function () {
59-
await expect(admins.connect(addr1).removeAdmin(addr2.address)).to.be.reverted;
60+
await expect(admins.connect(addr1).removeAdmin(addr2.address)).to.be
61+
.reverted;
6062
});
6163

6264
it("Should clean adminRoles", async function () {
6365
await admins.addAdmin(addr1.address);
6466
await admins.removeAdmin(addr1.address);
65-
await expect( await admins.adminRoles(addr1.address)).to.equal(0);
67+
expect(await admins.adminRoles(addr1.address)).to.equal(ZeroAddress);
6668
});
67-
6869
});
6970

7071
describe("addSuperAdmin", function () {
71-
7272
it("Should not be used without super privilegies", async function () {
73-
await expect(admins.connect(addr1).addSuperAdmin(addr2.address)).to.be.reverted;
73+
await expect(admins.connect(addr1).addSuperAdmin(addr2.address)).to.be
74+
.reverted;
7475
});
7576

7677
it("Should fail if superadmin already exists", async function () {
@@ -80,57 +81,50 @@ describe("Admins", function () {
8081

8182
it("Should setup adminRoles", async function () {
8283
await admins.addSuperAdmin(addr1.address);
83-
await expect( await admins.adminRoles(addr1.address)).to.equal(2);
84-
// todo : fix with the deployed contract
84+
await expect(await admins.adminRoles(addr1.address)).to.equal(2);
85+
// todo : fix with the deployed contract
8586
});
86-
8787
});
8888

8989
describe("removeSuperAdmin", function () {
90-
9190
it("Should not be used without super privilegies", async function () {
92-
await expect(admins.connect(addr1).removeSuperAdmin(addr2.address)).to.be.reverted;
91+
await expect(admins.connect(addr1).removeSuperAdmin(addr2.address)).to.be
92+
.reverted;
9393
});
9494

9595
it("Should clean adminRoles", async function () {
9696
await admins.addSuperAdmin(addr1.address);
9797
await admins.removeSuperAdmin(addr1.address);
98-
await expect( await admins.adminRoles(addr1.address)).to.equal(0);
98+
await expect(await admins.adminRoles(addr1.address)).to.equal(0);
9999
});
100-
101100
});
102101

103-
104-
105102
describe("Events", function () {
106-
107103
it("Should emit an event on addAdmin", async function () {
108104
await expect(admins.addAdmin(addr1.address))
109-
.to.emit(admins, 'Granted')
110-
.withArgs( owner.address,addr1.address, 1, anyValue);
105+
.to.emit(admins, "Granted")
106+
.withArgs(owner.address, addr1.address, 1, anyValue);
111107
});
112108

113109
it("Should emit an event on addSuperAdmin", async function () {
114110
await expect(admins.addSuperAdmin(addr2.address))
115-
.to.emit(admins, 'Granted')
116-
.withArgs(owner.address,addr2.address, 2, anyValue );
111+
.to.emit(admins, "Granted")
112+
.withArgs(owner.address, addr2.address, 2, anyValue);
117113
});
114+
});
118115

116+
// describe("Transfers", function () {
117+
// it("Should transfer the funds to the owner", async function () {
118+
// const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
119+
// deployOneYearLockFixture
120+
// );
119121

120-
})
121-
122-
// describe("Transfers", function () {
123-
// it("Should transfer the funds to the owner", async function () {
124-
// const { lock, unlockTime, lockedAmount, owner } = await loadFixture(
125-
// deployOneYearLockFixture
126-
// );
127-
128-
// await time.increaseTo(unlockTime);
122+
// await time.increaseTo(unlockTime);
129123

130-
// await expect(lock.withdraw()).to.changeEtherBalances(
131-
// [owner, lock],
132-
// [lockedAmount, -lockedAmount]
133-
// );
134-
// });
135-
// });
124+
// await expect(lock.withdraw()).to.changeEtherBalances(
125+
// [owner, lock],
126+
// [lockedAmount, -lockedAmount]
127+
// );
128+
// });
129+
// });
136130
});

backend/test/Albums.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const {
22
loadFixture,
33
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
4-
const { ethers } = require("hardhat");
4+
const { ethers, deployments } = require("hardhat");
55
const { utils } = require("ethers");
66
const { expect } = require("chai");
77

@@ -10,7 +10,8 @@ describe("Albums test", async function () {
1010
const [owner, otherAccount1] = await ethers.getSigners();
1111

1212
const Albums = await ethers.getContractFactory("Albums");
13-
const albums = await Albums.deploy(owner.address);
13+
const albums = await Albums.deploy();
14+
await albums.initialize(owner.address);
1415

1516
return { albums, owner, otherAccount1 };
1617
};

backend/yarn.lock

+6-1
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,11 @@
682682
"@nomicfoundation/solidity-analyzer-win32-ia32-msvc" "0.1.1"
683683
"@nomicfoundation/solidity-analyzer-win32-x64-msvc" "0.1.1"
684684

685+
"@openzeppelin/contracts-upgradeable@^5.0.2":
686+
version "5.0.2"
687+
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts-upgradeable/-/contracts-upgradeable-5.0.2.tgz#3e5321a2ecdd0b206064356798c21225b6ec7105"
688+
integrity sha512-0MmkHSHiW2NRFiT9/r5Lu4eJq5UJ4/tzlOgYXNAIj/ONkQTVnz22pLxDvp4C4uZ9he7ZFvGn3Driptn1/iU7tQ==
689+
685690
"@openzeppelin/contracts@^5.0.2":
686691
version "5.0.2"
687692
resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-5.0.2.tgz#b1d03075e49290d06570b2fd42154d76c2a5d210"
@@ -1833,7 +1838,7 @@ ethers@^5.7.2:
18331838
"@ethersproject/web" "5.7.1"
18341839
"@ethersproject/wordlists" "5.7.0"
18351840

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

0 commit comments

Comments
 (0)