Skip to content

MultiOwnable primary owner #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions contracts/access/MultiOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ abstract contract MultiOwnable is IMultiOwnable, Initializable {
return _owners.values();
}

/**
* @notice The function to get the address of the primary owner.
* @dev The function ensures compatibility with protocols like OpenSea where a single owner is required
* @return the address of the primary owner
*/
function owner() public view virtual returns (address) {
return _owners.at(0);
}

/**
* @notice The function to check the ownership of a user
* @param address_ the user to check
Expand All @@ -89,10 +98,16 @@ abstract contract MultiOwnable is IMultiOwnable, Initializable {
* @param newOwners_ the array of addresses to add to _owners
*/
function _addOwners(address[] memory newOwners_) private {
bool isSetEmpty_ = _owners.length() == 0;

_owners.add(newOwners_);

require(!_owners.contains(address(0)), "MultiOwnable: zero address can not be added");

if (isSetEmpty_) {
emit OwnershipTransferred(address(0), _owners.at(0));
}

emit OwnersAdded(newOwners_);
}

Expand All @@ -106,8 +121,16 @@ abstract contract MultiOwnable is IMultiOwnable, Initializable {
* @param oldOwners_ the array of addresses to remove from _owners
*/
function _removeOwners(address[] memory oldOwners_) private {
address previousOwner_ = _owners.at(0);

_owners.remove(oldOwners_);

address newOwner_ = _owners.length() > 0 ? _owners.at(0) : address(0);

if (newOwner_ != previousOwner_) {
emit OwnershipTransferred(previousOwner_, newOwner_);
}

emit OwnersRemoved(oldOwners_);
}

Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/access/IMultiOwnable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pragma solidity ^0.8.4;
interface IMultiOwnable {
event OwnersAdded(address[] newOwners);
event OwnersRemoved(address[] removedOwners);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

/**
* @notice The function to add equally rightful owners to the contract
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/solidity-lib",
"version": "2.7.6",
"version": "2.7.7",
"license": "MIT",
"author": "Distributed Lab",
"readme": "README.md",
Expand Down
35 changes: 35 additions & 0 deletions test/access/MultiOwnable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ describe("MultiOwnable", () => {
expect(await multiOwnable.isOwner(THIRD.address)).to.be.true;
});

it("should emit OwnershipTransferred event when init function is called", async () => {
const MultiOwnableMock = await ethers.getContractFactory("MultiOwnableMock");
const multiOwnable = await MultiOwnableMock.deploy();

await expect(multiOwnable.connect(SECOND).__MultiOwnableMock_init())
.to.emit(multiOwnable, "OwnershipTransferred")
.withArgs(ZERO_ADDR, SECOND.address);

await expect(multiOwnable.connect(SECOND).addOwners([SECOND.address, THIRD.address])).not.to.emit(
multiOwnable,
"OwnershipTransferred",
);
});

it("should not add null address", async () => {
await expect(multiOwnable.addOwners([ZERO_ADDR])).to.be.revertedWith(
"MultiOwnable: zero address can not be added",
Expand All @@ -75,6 +89,21 @@ describe("MultiOwnable", () => {
expect(await multiOwnable.isOwner(SECOND.address)).to.be.false;
expect(await multiOwnable.isOwner(FIRST.address)).to.be.true;
});

it("should emit OwnershipTransferred event only when primary owner is changed", async () => {
await multiOwnable.addOwners([SECOND.address]);

await expect(multiOwnable.connect(SECOND).removeOwners([FIRST.address]))
.to.emit(multiOwnable, "OwnershipTransferred")
.withArgs(FIRST.address, SECOND.address);

await multiOwnable.connect(SECOND).addOwners([FIRST.address]);
await expect(multiOwnable.removeOwners([FIRST.address])).not.to.emit(multiOwnable, "OwnershipTransferred");

await expect(multiOwnable.connect(SECOND).removeOwners([SECOND.address]))
.to.emit(multiOwnable, "OwnershipTransferred")
.withArgs(SECOND.address, ZERO_ADDR);
});
});

describe("renounceOwnership()", () => {
Expand All @@ -93,6 +122,12 @@ describe("MultiOwnable", () => {
});
});

describe("owner()", () => {
it("should correctly get the primary owner", async () => {
expect(await multiOwnable.owner()).to.be.equal(FIRST.address);
});
});

describe("isOwner()", () => {
it("should correctly check the initial owner", async () => {
expect(await multiOwnable.isOwner(FIRST.address)).to.be.true;
Expand Down
Loading