Skip to content

Commit

Permalink
added transferOwnership functionality in Owned contract
Browse files Browse the repository at this point in the history
  • Loading branch information
dev1644 committed Jul 16, 2020
1 parent ffb64c6 commit 4e4ef78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
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);
});
});
});

0 comments on commit 4e4ef78

Please sign in to comment.