Skip to content

Commit

Permalink
Add owner getter and setter functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-turek committed Dec 8, 2023
1 parent 5c5f118 commit ab74f87
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/asset/contracts/Asset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -384,5 +384,21 @@ contract Asset is
return "ASSET";
}

uint256[49] private __gap;
address private _owner;

/// @notice Returns the owner of the contract
/// @return address of the owner
function owner() external view returns (address) {
return _owner;
}

/// @notice Sets the owner of the contract
/// @param newOwner address of the new owner
function transferOwnership(address newOwner) external {
require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "Asset: Unauthorized");
_owner = newOwner;
emit OwnershipTransferred(_owner, newOwner);
}

uint256[48] private __gap;
}
18 changes: 17 additions & 1 deletion packages/asset/contracts/Catalyst.sol
Original file line number Diff line number Diff line change
Expand Up @@ -324,5 +324,21 @@ contract Catalyst is
return "CATALYST";
}

uint256[49] private __gap;
address private _owner;

/// @notice Returns the owner of the contract
/// @return address of the owner
function owner() external view returns (address) {
return _owner;
}

/// @notice Sets the owner of the contract
/// @param newOwner address of the new owner
function transferOwnership(address newOwner) external {
require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "Asset: Unauthorized");
_owner = newOwner;
emit OwnershipTransferred(_owner, newOwner);
}

uint256[48] private __gap;
}
1 change: 1 addition & 0 deletions packages/asset/contracts/interfaces/IAsset.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface IAsset {
}

event TrustedForwarderChanged(address indexed newTrustedForwarderAddress);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/// @notice Mint new tokens
/// @dev Only callable by the minter role
Expand Down
1 change: 1 addition & 0 deletions packages/asset/contracts/interfaces/ICatalyst.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface ICatalyst {
event DefaultRoyaltyChanged(address indexed newDefaultRoyaltyRecipient, uint256 newDefaultRoyaltyAmount);
event BaseURISet(string baseURI);
event OperatorRegistrySet(address indexed registry);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/// @notice Mints a new token, limited to MINTER_ROLE only
/// @param to The address that will own the minted token
Expand Down
28 changes: 28 additions & 0 deletions packages/asset/test/Asset.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ describe('Base Asset Contract (/packages/asset/contracts/Asset.sol)', function (
expect(await AssetContract.symbol()).to.be.equal('ASSET');
});
});
describe('Owner', function () {
it('should not have the owner set when initially deployed', async function () {
const {AssetContract} = await runAssetSetup();
expect(await AssetContract.owner()).to.be.equal(
ethers.constants.AddressZero
);
});
it('allows DEFAULT_ADMIN_ROLE to transfer the ownership', async function () {
const {AssetContract, assetAdmin} = await runAssetSetup();
await AssetContract.connect(assetAdmin).transferOwnership(
assetAdmin.address
);
expect(await AssetContract.owner()).to.be.equals(assetAdmin.address);
});
it('does not allow non-DEFAULT_ADMIN_ROLE account to transfer the ownership', async function () {
const {AssetContract, deployer} = await runAssetSetup();
await expect(
AssetContract.connect(deployer).transferOwnership(deployer.address)
).to.be.revertedWith('Asset: Unauthorized');
});
it('emits OwnershipTransferred event when DEFAULT_ADMIN_ROLE transfers the ownership', async function () {
const {AssetContract, assetAdmin} = await runAssetSetup();
const tx = await AssetContract.connect(assetAdmin).transferOwnership(
assetAdmin.address
);
await expect(tx).to.emit(AssetContract, 'OwnershipTransferred');
});
});
describe('Access Control', function () {
it('should have MINTER_ROLE defined', async function () {
const {AssetContract} = await runAssetSetup();
Expand Down
26 changes: 26 additions & 0 deletions packages/asset/test/Catalyst.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,32 @@ describe('Catalyst (/packages/asset/contracts/Catalyst.sol)', function () {
).to.revertedWith('Catalyst: CID cant be empty');
});
});
describe('Owner', function () {
it('should not have the owner set when initially deployed', async function () {
const {catalyst} = await runCatalystSetup();
expect(await catalyst.owner()).to.be.equal(ethers.constants.AddressZero);
});
it('allows DEFAULT_ADMIN_ROLE to transfer the ownership', async function () {
const {catalyst, catalystAdmin} = await runCatalystSetup();
await catalyst
.connect(catalystAdmin)
.transferOwnership(catalystAdmin.address);
expect(await catalyst.owner()).to.be.equals(catalystAdmin.address);
});
it('does not allow non-DEFAULT_ADMIN_ROLE account to transfer the ownership', async function () {
const {catalyst, deployer} = await runCatalystSetup();
await expect(
catalyst.connect(deployer).transferOwnership(deployer.address)
).to.be.revertedWith('Asset: Unauthorized');
});
it('emits OwnershipTransferred event when DEFAULT_ADMIN_ROLE transfers the ownership', async function () {
const {catalyst, catalystAdmin} = await runCatalystSetup();
const tx = await catalyst
.connect(catalystAdmin)
.transferOwnership(catalystAdmin.address);
await expect(tx).to.emit(catalyst, 'OwnershipTransferred');
});
});
describe('Admin Role', function () {
it('Admin can set minter', async function () {
const {catalystAsAdmin, user1, minterRole} = await runCatalystSetup();
Expand Down
2 changes: 2 additions & 0 deletions packages/asset/test/fixtures/asset/assetFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export function generateAssetId(

export async function runAssetSetup() {
const [
deployer,
assetAdmin,
owner,
secondOwner,
Expand Down Expand Up @@ -272,6 +273,7 @@ export async function runAssetSetup() {
AssetContractAsMinter,
AssetContractAsBurner,
AssetContractAsAdmin,
deployer,
owner,
assetAdmin,
minter,
Expand Down

0 comments on commit ab74f87

Please sign in to comment.