Skip to content

Commit

Permalink
update for the new roles functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Foivos committed Oct 31, 2023
1 parent fe2af9f commit a6216f0
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 56 deletions.
17 changes: 7 additions & 10 deletions contracts/token-manager/TokenManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ abstract contract TokenManager is ITokenManager, Operatable, FlowLimit, Implemen
operator_ = operatorBytes.toAddress();
}

uint8[] memory roles = new uint8[](2);
roles[0] = uint8(Roles.FLOW_LIMITER);
roles[1] = uint8(Roles.OPERATOR);
_addRoles(operator_, roles);
_addAccountRoles(operator_, 1 << uint8(Roles.FLOW_LIMITER) | 1 << uint8(Roles.OPERATOR));
_setup(params);
}

Expand Down Expand Up @@ -205,10 +202,10 @@ abstract contract TokenManager is ITokenManager, Operatable, FlowLimit, Implemen
*/
function addFlowLimiter(address flowLimiter) external onlyRole(uint8(Roles.OPERATOR)) {
if (flowLimiter == address(0)) revert ZeroAddress();

if (hasRole(flowLimiter, uint8(Roles.FLOW_LIMITER))) revert AlreadyFlowLimiter(flowLimiter);
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.FLOW_LIMITER);
_addRoles(flowLimiter, roles);

_addRole(flowLimiter, uint8(Roles.FLOW_LIMITER));
}

/**
Expand All @@ -217,10 +214,10 @@ abstract contract TokenManager is ITokenManager, Operatable, FlowLimit, Implemen
*/
function removeFlowLimiter(address flowLimiter) external onlyRole(uint8(Roles.OPERATOR)) {
if (flowLimiter == address(0)) revert ZeroAddress();

if (!hasRole(flowLimiter, uint8(Roles.FLOW_LIMITER))) revert NotFlowLimiter(flowLimiter);
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.FLOW_LIMITER);
_removeRoles(flowLimiter, roles);

_removeRole(flowLimiter, uint8(Roles.FLOW_LIMITER));
}

/**
Expand Down
16 changes: 4 additions & 12 deletions contracts/utils/Distributable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ contract Distributable is IDistributable, RolesBase, RolesConstants {
* @param distributor_ The address of the new distributor
*/
function _addDistributor(address distributor_) internal {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.DISTRIBUTOR);
_addRoles(distributor_, roles);
_addRole(distributor_, uint8(Roles.DISTRIBUTOR));
}

/**
Expand All @@ -30,9 +28,7 @@ contract Distributable is IDistributable, RolesBase, RolesConstants {
* @param distributor_ The address of the new distributor
*/
function transferDistributorship(address distributor_) external onlyRole(uint8(Roles.DISTRIBUTOR)) {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.DISTRIBUTOR);
_transferRoles(msg.sender, distributor_, roles);
_transferRole(msg.sender, distributor_, uint8(Roles.DISTRIBUTOR));
}

/**
Expand All @@ -41,19 +37,15 @@ contract Distributable is IDistributable, RolesBase, RolesConstants {
* @param distributor_ The address of the new distributor
*/
function proposeDistributorship(address distributor_) external onlyRole(uint8(Roles.DISTRIBUTOR)) {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.DISTRIBUTOR);
_proposeRoles(msg.sender, distributor_, roles);
_proposeRole(msg.sender, distributor_, uint8(Roles.DISTRIBUTOR));
}

/**
* @notice Accept a change of the distributor of the contract
* @dev Can only be called by the proposed distributor
*/
function acceptDistributorship(address fromDistributor) external {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.DISTRIBUTOR);
_acceptRoles(fromDistributor, msg.sender, roles);
_acceptRole(fromDistributor, msg.sender, uint8(Roles.DISTRIBUTOR));
}

/**
Expand Down
12 changes: 3 additions & 9 deletions contracts/utils/Operatable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ contract Operatable is IOperatable, RolesBase, RolesConstants {
* @param operator_ The address of the new operator
*/
function transferOperatorship(address operator_) external onlyRole(uint8(Roles.OPERATOR)) {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.OPERATOR);
_transferRoles(msg.sender, operator_, roles);
_transferRole(msg.sender, operator_, uint8(Roles.OPERATOR));
}

/**
Expand All @@ -41,19 +39,15 @@ contract Operatable is IOperatable, RolesBase, RolesConstants {
* @param operator_ The address of the new operator
*/
function proposeOperatorship(address operator_) external onlyRole(uint8(Roles.OPERATOR)) {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.OPERATOR);
_proposeRoles(msg.sender, operator_, roles);
_proposeRole(msg.sender, operator_, uint8(Roles.OPERATOR));
}

/**
* @notice Accept a change of the operator of the contract
* @dev Can only be called by the proposed operator
*/
function acceptOperatorship(address fromOperator) external {
uint8[] memory roles = new uint8[](1);
roles[0] = uint8(Roles.OPERATOR);
_acceptRoles(fromOperator, msg.sender, roles);
_acceptRole(fromOperator, msg.sender, uint8(Roles.OPERATOR));
}

/**
Expand Down
8 changes: 4 additions & 4 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
Expand Up @@ -27,7 +27,7 @@
},
"dependencies": {
"@axelar-network/axelar-cgp-solidity": "6.1.2",
"@axelar-network/axelar-gmp-sdk-solidity": "^5.5.0"
"@axelar-network/axelar-gmp-sdk-solidity": "https://github.com/axelarnetwork/axelar-gmp-sdk-solidity.git#feat/roles-improvements"
},
"devDependencies": {
"@axelar-network/axelar-chains-config": "~0.1.2",
Expand Down
4 changes: 2 additions & 2 deletions test/TokenService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2075,7 +2075,7 @@ describe('Interchain Token Service', () => {
it('Should be able to add a flow limiter', async () => {
await expect(tokenManager.addFlowLimiter(otherWallet.address))
.to.emit(tokenManager, 'RolesAdded')
.withArgs(otherWallet.address, [FLOW_LIMITER_ROLE]);
.withArgs(otherWallet.address, 1 << FLOW_LIMITER_ROLE);

expect(await tokenManager.hasRole(wallet.address, FLOW_LIMITER_ROLE)).to.equal(true);
expect(await tokenManager.hasRole(otherWallet.address, FLOW_LIMITER_ROLE)).to.equal(true);
Expand All @@ -2084,7 +2084,7 @@ describe('Interchain Token Service', () => {
it('Should be able to remove a flow limiter', async () => {
await expect(tokenManager.removeFlowLimiter(wallet.address))
.to.emit(tokenManager, 'RolesRemoved')
.withArgs(wallet.address, [FLOW_LIMITER_ROLE]);
.withArgs(wallet.address, 1 << FLOW_LIMITER_ROLE);

expect(await tokenManager.hasRole(wallet.address, FLOW_LIMITER_ROLE)).to.equal(false);
expect(await tokenManager.hasRole(otherWallet.address, FLOW_LIMITER_ROLE)).to.equal(false);
Expand Down
12 changes: 6 additions & 6 deletions test/TokenServiceFullFlow.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ describe('Interchain Token Service Full Flow', () => {

await expect(token.transferDistributorship(newAddress))
.to.emit(token, 'RolesRemoved')
.withArgs(wallet.address, [DISTRIBUTOR_ROLE])
.withArgs(wallet.address, 1 << DISTRIBUTOR_ROLE)
.to.emit(token, 'RolesAdded')
.withArgs(newAddress, [DISTRIBUTOR_ROLE]);
.withArgs(newAddress, 1 << DISTRIBUTOR_ROLE);

await expectRevert((gasOptions) => token.mint(newAddress, amount, gasOptions), token, 'MissingRole');
await expectRevert((gasOptions) => token.burn(newAddress, amount, gasOptions), token, 'MissingRole');
Expand Down Expand Up @@ -235,9 +235,9 @@ describe('Interchain Token Service Full Flow', () => {

await expect(token.transferDistributorship(newAddress))
.to.emit(token, 'RolesRemoved')
.withArgs(wallet.address, [DISTRIBUTOR_ROLE])
.withArgs(wallet.address, 1 << DISTRIBUTOR_ROLE)
.to.emit(token, 'RolesAdded')
.withArgs(newAddress, [DISTRIBUTOR_ROLE]);
.withArgs(newAddress, 1 << DISTRIBUTOR_ROLE);

await expectRevert((gasOptions) => token.mint(newAddress, amount, gasOptions), token, 'MissingRole');
await expectRevert((gasOptions) => token.burn(newAddress, amount, gasOptions), token, 'MissingRole');
Expand Down Expand Up @@ -313,9 +313,9 @@ describe('Interchain Token Service Full Flow', () => {

await expect(token.transferDistributorship(tokenManager.address))
.to.emit(token, 'RolesRemoved')
.withArgs(wallet.address, [DISTRIBUTOR_ROLE])
.withArgs(wallet.address, 1 << DISTRIBUTOR_ROLE)
.to.emit(token, 'RolesAdded')
.withArgs(tokenManager.address, [DISTRIBUTOR_ROLE]);
.withArgs(tokenManager.address, 1 << DISTRIBUTOR_ROLE);

await expectRevert((gasOptions) => token.mint(newAddress, amount, gasOptions), token, 'MissingRole');
await expectRevert((gasOptions) => token.burn(newAddress, amount, gasOptions), token, 'MissingRole');
Expand Down
24 changes: 12 additions & 12 deletions test/UtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ describe('Operatable', () => {

await expect(test.transferOperatorship(otherWallet.address))
.to.emit(test, 'RolesRemoved')
.withArgs(ownerWallet.address, [operatorRole])
.withArgs(ownerWallet.address, 1 << operatorRole)
.to.emit(test, 'RolesAdded')
.withArgs(otherWallet.address, [operatorRole]);
.withArgs(otherWallet.address, 1 << operatorRole);

expect(await test.hasRole(otherWallet.address, operatorRole)).to.be.true;

Expand All @@ -67,7 +67,7 @@ describe('Operatable', () => {

await expect(test.connect(otherWallet).proposeOperatorship(ownerWallet.address))
.to.emit(test, 'RolesProposed')
.withArgs(otherWallet.address, ownerWallet.address, [operatorRole]);
.withArgs(otherWallet.address, ownerWallet.address, 1 << operatorRole);
});

it('Should be able to accept operatorship only as proposed operator', async () => {
Expand All @@ -81,9 +81,9 @@ describe('Operatable', () => {

await expect(test.connect(ownerWallet).acceptOperatorship(otherWallet.address))
.to.emit(test, 'RolesRemoved')
.withArgs(otherWallet.address, [operatorRole])
.withArgs(otherWallet.address, 1 << operatorRole)
.to.emit(test, 'RolesAdded')
.withArgs(ownerWallet.address, [operatorRole]);
.withArgs(ownerWallet.address, 1 << operatorRole);
});
});

Expand Down Expand Up @@ -115,9 +115,9 @@ describe('Distributable', () => {

await expect(test.transferDistributorship(otherWallet.address))
.to.emit(test, 'RolesRemoved')
.withArgs(ownerWallet.address, [distributorRole])
.withArgs(ownerWallet.address, 1 << distributorRole)
.to.emit(test, 'RolesAdded')
.withArgs(otherWallet.address, [distributorRole]);
.withArgs(otherWallet.address, 1 << distributorRole);

expect(await test.hasRole(otherWallet.address, distributorRole)).to.be.true;

Expand All @@ -135,7 +135,7 @@ describe('Distributable', () => {

await expect(test.connect(otherWallet).proposeDistributorship(ownerWallet.address))
.to.emit(test, 'RolesProposed')
.withArgs(otherWallet.address, ownerWallet.address, [distributorRole]);
.withArgs(otherWallet.address, ownerWallet.address, 1 << distributorRole);
});

it('Should be able to accept distributorship only as the proposed distributor', async () => {
Expand All @@ -149,9 +149,9 @@ describe('Distributable', () => {

await expect(test.connect(ownerWallet).acceptDistributorship(otherWallet.address))
.to.emit(test, 'RolesRemoved')
.withArgs(otherWallet.address, [distributorRole])
.withArgs(otherWallet.address, 1 << distributorRole)
.to.emit(test, 'RolesAdded')
.withArgs(ownerWallet.address, [distributorRole]);
.withArgs(ownerWallet.address, 1 << distributorRole);
});
});

Expand Down Expand Up @@ -419,9 +419,9 @@ describe('StandardizedTokenDeployer', () => {
.to.emit(token, 'Transfer')
.withArgs(AddressZero, mintTo, mintAmount)
.and.to.emit(token, 'RolesAdded')
.withArgs(tokenManager, [DISTRIBUTOR_ROLE])
.withArgs(tokenManager, 1 << DISTRIBUTOR_ROLE)
.to.emit(token, 'RolesAdded')
.withArgs(tokenManager, [DISTRIBUTOR_ROLE]);
.withArgs(tokenManager, 1 << DISTRIBUTOR_ROLE);

expect(await tokenProxy.implementation()).to.equal(standardizedToken.address);
expect(await token.name()).to.equal(name);
Expand Down

0 comments on commit a6216f0

Please sign in to comment.