Skip to content
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

added transferOwnership functionality in Owned contract #596

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
6 changes: 6 additions & 0 deletions contracts/Owned.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ contract Owned {
emit OwnerChanged(address(0), _owner);
}

function transferOwnership(address newOwner) external onlyOwner {
require(newOwner != address(0), "Owner address cannot be 0");
emit OwnerChanged(owner, newOwner);
owner = newOwner;
}

function nominateNewOwner(address _owner) external onlyOwner {
nominatedOwner = _owner;
emit OwnerNominated(_owner);
Expand Down
28 changes: 28 additions & 0 deletions test/contracts/Owned.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,33 @@ contract('Owned', accounts => {
assert.equal(owner, nominatedOwner);
assert.equal(nominatedOwnerFromContact, ZERO_ADDRESS);
});

it('should transfer ownership to another address by current contract owner', async () => {
const newOwner = account3;

const txn = await ownedContractInstance.transferOwnership(newOwner, { from: account1 });
assert.eventEqual(txn, 'OwnerChanged', { oldOwner: account1, newOwner });

const currentOwner = await ownedContractInstance.owner();
assert.equal(currentOwner, newOwner);
});

it('should not transfer ownership to another address when not invoked by current owner', async () => {
const newOwner = account3;

await assert.revert(ownedContractInstance.transferOwnership(newOwner, { from: newOwner }));

const currentOwner = await ownedContractInstance.owner();
assert.notEqual(currentOwner, newOwner);
});

it('should not transfer ownership to Zero address', async () => {
const newOwner = ZERO_ADDRESS;

await assert.revert(ownedContractInstance.transferOwnership(newOwner, { from: account1 }));

const currentOwner = await ownedContractInstance.owner();
assert.notEqual(currentOwner, newOwner);
});
});
});