@uniswap/v4-core
Version:
🦄 Core smart contracts of Uniswap v4
1,169 lines (1,024 loc) • 114 kB
JavaScript
const { web3 } = require('hardhat');
const { constants, expectEvent, time, expectRevert } = require('@openzeppelin/test-helpers');
const { expectRevertCustomError } = require('../../helpers/customError');
const { selector } = require('../../helpers/methods');
const { clockFromReceipt } = require('../../helpers/time');
const {
buildBaseRoles,
formatAccess,
EXPIRATION,
MINSETBACK,
EXECUTION_ID_STORAGE_SLOT,
CONSUMING_SCHEDULE_STORAGE_SLOT,
} = require('../../helpers/access-manager');
const {
// COMMON PATHS
COMMON_SCHEDULABLE_PATH,
COMMON_SCHEDULABLE_PATH_IF_ZERO_DELAY,
// MODE HELPERS
shouldBehaveLikeClosable,
// DELAY HELPERS
shouldBehaveLikeDelay,
// OPERATION HELPERS
shouldBehaveLikeSchedulableOperation,
// METHOD HELPERS
shouldBehaveLikeCanCall,
shouldBehaveLikeGetAccess,
shouldBehaveLikeHasRole,
// ADMIN OPERATION HELPERS
shouldBehaveLikeDelayedAdminOperation,
shouldBehaveLikeNotDelayedAdminOperation,
shouldBehaveLikeRoleAdminOperation,
// RESTRICTED OPERATION HELPERS
shouldBehaveLikeAManagedRestrictedOperation,
// HELPERS
scheduleOperation,
} = require('./AccessManager.behavior');
const { default: Wallet } = require('ethereumjs-wallet');
const {
mine,
time: { setNextBlockTimestamp },
getStorageAt,
} = require('@nomicfoundation/hardhat-network-helpers');
const { MAX_UINT48 } = require('../../helpers/constants');
const { impersonate } = require('../../helpers/account');
const AccessManager = artifacts.require('$AccessManager');
const AccessManagedTarget = artifacts.require('$AccessManagedTarget');
const Ownable = artifacts.require('$Ownable');
const someAddress = Wallet.generate().getChecksumAddressString();
contract('AccessManager', function (accounts) {
const [admin, manager, guardian, member, user, other] = accounts;
beforeEach(async function () {
this.roles = buildBaseRoles();
// Add members
this.roles.ADMIN.members = [admin];
this.roles.SOME_ADMIN.members = [manager];
this.roles.SOME_GUARDIAN.members = [guardian];
this.roles.SOME.members = [member];
this.roles.PUBLIC.members = [admin, manager, guardian, member, user, other];
this.manager = await AccessManager.new(admin);
this.target = await AccessManagedTarget.new(this.manager.address);
for (const { id: roleId, admin, guardian, members } of Object.values(this.roles)) {
if (roleId === this.roles.PUBLIC.id) continue; // Every address belong to public and is locked
if (roleId === this.roles.ADMIN.id) continue; // Admin set during construction and is locked
// Set admin role avoiding default
if (admin.id !== this.roles.ADMIN.id) {
await this.manager.$_setRoleAdmin(roleId, admin.id);
}
// Set guardian role avoiding default
if (guardian.id !== this.roles.ADMIN.id) {
await this.manager.$_setRoleGuardian(roleId, guardian.id);
}
// Grant role to members
for (const member of members) {
await this.manager.$_grantRole(roleId, member, 0, 0);
}
}
});
describe('during construction', function () {
it('grants admin role to initialAdmin', async function () {
const manager = await AccessManager.new(other);
expect(await manager.hasRole(this.roles.ADMIN.id, other).then(formatAccess)).to.be.deep.equal([true, '0']);
});
it('rejects zero address for initialAdmin', async function () {
await expectRevertCustomError(AccessManager.new(constants.ZERO_ADDRESS), 'AccessManagerInvalidInitialAdmin', [
constants.ZERO_ADDRESS,
]);
});
it('initializes setup roles correctly', async function () {
for (const { id: roleId, admin, guardian, members } of Object.values(this.roles)) {
expect(await this.manager.getRoleAdmin(roleId)).to.be.bignumber.equal(admin.id);
expect(await this.manager.getRoleGuardian(roleId)).to.be.bignumber.equal(guardian.id);
for (const user of this.roles.PUBLIC.members) {
expect(await this.manager.hasRole(roleId, user).then(formatAccess)).to.be.deep.equal([
members.includes(user),
'0',
]);
}
}
});
});
describe('getters', function () {
describe('#canCall', function () {
beforeEach('set calldata', function () {
this.calldata = '0x12345678';
this.role = { id: web3.utils.toBN(379204) };
});
shouldBehaveLikeCanCall({
closed() {
it('should return false and no delay', async function () {
const { immediate, delay } = await this.manager.canCall(
someAddress,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal('0');
});
},
open: {
callerIsTheManager: {
executing() {
it('should return true and no delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(true);
expect(delay).to.be.bignumber.equal('0');
});
},
notExecuting() {
it('should return false and no delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal('0');
});
},
},
callerIsNotTheManager: {
publicRoleIsRequired() {
it('should return true and no delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(true);
expect(delay).to.be.bignumber.equal('0');
});
},
specificRoleIsRequired: {
requiredRoleIsGranted: {
roleGrantingIsDelayed: {
callerHasAnExecutionDelay: {
beforeGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('should return false and no execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal('0');
});
},
afterGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
this.scheduleIn = this.executionDelay; // For shouldBehaveLikeSchedulableOperation
});
shouldBehaveLikeSchedulableOperation({
scheduled: {
before() {
beforeEach('consume previously set delay', async function () {
// Consume previously set delay
await mine();
});
it('should return false and execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal(this.executionDelay);
});
},
after() {
beforeEach('consume previously set delay', async function () {
// Consume previously set delay
await mine();
});
it('should return false and execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal(this.executionDelay);
});
},
expired() {
beforeEach('consume previously set delay', async function () {
// Consume previously set delay
await mine();
});
it('should return false and execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal(this.executionDelay);
});
},
},
notScheduled() {
it('should return false and execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal(this.executionDelay);
});
},
});
},
},
callerHasNoExecutionDelay: {
beforeGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('should return false and no execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal('0');
});
},
afterGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('should return true and no execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(true);
expect(delay).to.be.bignumber.equal('0');
});
},
},
},
roleGrantingIsNotDelayed: {
callerHasAnExecutionDelay() {
it('should return false and execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal(this.executionDelay);
});
},
callerHasNoExecutionDelay() {
it('should return true and no execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(true);
expect(delay).to.be.bignumber.equal('0');
});
},
},
},
requiredRoleIsNotGranted() {
it('should return false and no execution delay', async function () {
const { immediate, delay } = await this.manager.canCall(
this.caller,
this.target.address,
this.calldata.substring(0, 10),
);
expect(immediate).to.be.equal(false);
expect(delay).to.be.bignumber.equal('0');
});
},
},
},
},
});
});
describe('#expiration', function () {
it('has a 7 days default expiration', async function () {
expect(await this.manager.expiration()).to.be.bignumber.equal(EXPIRATION);
});
});
describe('#minSetback', function () {
it('has a 5 days default minimum setback', async function () {
expect(await this.manager.minSetback()).to.be.bignumber.equal(MINSETBACK);
});
});
describe('#isTargetClosed', function () {
shouldBehaveLikeClosable({
closed() {
it('returns true', async function () {
expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(true);
});
},
open() {
it('returns false', async function () {
expect(await this.manager.isTargetClosed(this.target.address)).to.be.equal(false);
});
},
});
});
describe('#getTargetFunctionRole', function () {
const methodSelector = selector('something(address,bytes)');
it('returns the target function role', async function () {
const roleId = web3.utils.toBN(21498);
await this.manager.$_setTargetFunctionRole(this.target.address, methodSelector, roleId);
expect(await this.manager.getTargetFunctionRole(this.target.address, methodSelector)).to.be.bignumber.equal(
roleId,
);
});
it('returns the ADMIN role if not set', async function () {
expect(await this.manager.getTargetFunctionRole(this.target.address, methodSelector)).to.be.bignumber.equal(
this.roles.ADMIN.id,
);
});
});
describe('#getTargetAdminDelay', function () {
describe('when the target admin delay is setup', function () {
beforeEach('set target admin delay', async function () {
this.oldDelay = await this.manager.getTargetAdminDelay(this.target.address);
this.newDelay = time.duration.days(10);
await this.manager.$_setTargetAdminDelay(this.target.address, this.newDelay);
this.delay = MINSETBACK; // For shouldBehaveLikeDelay
});
shouldBehaveLikeDelay('effect', {
before() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns the old target admin delay', async function () {
expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal(this.oldDelay);
});
},
after() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns the new target admin delay', async function () {
expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal(this.newDelay);
});
},
});
});
it('returns the 0 if not set', async function () {
expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal('0');
});
});
describe('#getRoleAdmin', function () {
const roleId = web3.utils.toBN(5234907);
it('returns the role admin', async function () {
const adminId = web3.utils.toBN(789433);
await this.manager.$_setRoleAdmin(roleId, adminId);
expect(await this.manager.getRoleAdmin(roleId)).to.be.bignumber.equal(adminId);
});
it('returns the ADMIN role if not set', async function () {
expect(await this.manager.getRoleAdmin(roleId)).to.be.bignumber.equal(this.roles.ADMIN.id);
});
});
describe('#getRoleGuardian', function () {
const roleId = web3.utils.toBN(5234907);
it('returns the role guardian', async function () {
const guardianId = web3.utils.toBN(789433);
await this.manager.$_setRoleGuardian(roleId, guardianId);
expect(await this.manager.getRoleGuardian(roleId)).to.be.bignumber.equal(guardianId);
});
it('returns the ADMIN role if not set', async function () {
expect(await this.manager.getRoleGuardian(roleId)).to.be.bignumber.equal(this.roles.ADMIN.id);
});
});
describe('#getRoleGrantDelay', function () {
const roleId = web3.utils.toBN(9248439);
describe('when the grant admin delay is setup', function () {
beforeEach('set grant admin delay', async function () {
this.oldDelay = await this.manager.getRoleGrantDelay(roleId);
this.newDelay = time.duration.days(11);
await this.manager.$_setGrantDelay(roleId, this.newDelay);
this.delay = MINSETBACK; // For shouldBehaveLikeDelay
});
shouldBehaveLikeDelay('grant', {
before() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns the old role grant delay', async function () {
expect(await this.manager.getRoleGrantDelay(roleId)).to.be.bignumber.equal(this.oldDelay);
});
},
after() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns the new role grant delay', async function () {
expect(await this.manager.getRoleGrantDelay(roleId)).to.be.bignumber.equal(this.newDelay);
});
},
});
});
it('returns 0 if delay is not set', async function () {
expect(await this.manager.getTargetAdminDelay(this.target.address)).to.be.bignumber.equal('0');
});
});
describe('#getAccess', function () {
beforeEach('set role', function () {
this.role = { id: web3.utils.toBN(9452) };
this.caller = user;
});
shouldBehaveLikeGetAccess({
requiredRoleIsGranted: {
roleGrantingIsDelayed: {
callerHasAnExecutionDelay: {
beforeGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('role is not in effect and execution delay is set', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
// Not in effect yet
expect(await time.latest()).to.be.bignumber.lt(access[0]);
});
},
afterGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('access has role in effect and execution delay is set', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
// Already in effect
expect(await time.latest()).to.be.bignumber.equal(access[0]);
});
},
},
callerHasNoExecutionDelay: {
beforeGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('access has role not in effect without execution delay', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
// Not in effect yet
expect(await time.latest()).to.be.bignumber.lt(access[0]);
});
},
afterGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('role is in effect without execution delay', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal(this.delayEffect); // inEffectSince
expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
// Already in effect
expect(await time.latest()).to.be.bignumber.equal(access[0]);
});
},
},
},
roleGrantingIsNotDelayed: {
callerHasAnExecutionDelay() {
it('access has role in effect and execution delay is set', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal(await time.latest()); // inEffectSince
expect(access[1]).to.be.bignumber.equal(this.executionDelay); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
// Already in effect
expect(await time.latest()).to.be.bignumber.equal(access[0]);
});
},
callerHasNoExecutionDelay() {
it('access has role in effect without execution delay', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal(await time.latest()); // inEffectSince
expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
// Already in effect
expect(await time.latest()).to.be.bignumber.equal(access[0]);
});
},
},
},
requiredRoleIsNotGranted() {
it('has empty access', async function () {
const access = await this.manager.getAccess(this.role.id, this.caller);
expect(access[0]).to.be.bignumber.equal('0'); // inEffectSince
expect(access[1]).to.be.bignumber.equal('0'); // currentDelay
expect(access[2]).to.be.bignumber.equal('0'); // pendingDelay
expect(access[3]).to.be.bignumber.equal('0'); // pendingDelayEffect
});
},
});
});
describe('#hasRole', function () {
beforeEach('setup shouldBehaveLikeHasRole', function () {
this.role = { id: web3.utils.toBN(49832) };
this.calldata = '0x1234';
this.caller = user;
});
shouldBehaveLikeHasRole({
publicRoleIsRequired() {
it('has PUBLIC role', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.true;
expect(executionDelay).to.be.bignumber.eq('0');
});
},
specificRoleIsRequired: {
requiredRoleIsGranted: {
roleGrantingIsDelayed: {
callerHasAnExecutionDelay: {
beforeGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('does not have role but execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.false;
expect(executionDelay).to.be.bignumber.eq(this.executionDelay);
});
},
afterGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('has role and execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.true;
expect(executionDelay).to.be.bignumber.eq(this.executionDelay);
});
},
},
callerHasNoExecutionDelay: {
beforeGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('does not have role nor execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.false;
expect(executionDelay).to.be.bignumber.eq('0');
});
},
afterGrantDelay() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('has role and no execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.true;
expect(executionDelay).to.be.bignumber.eq('0');
});
},
},
},
roleGrantingIsNotDelayed: {
callerHasAnExecutionDelay() {
it('has role and execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.true;
expect(executionDelay).to.be.bignumber.eq(this.executionDelay);
});
},
callerHasNoExecutionDelay() {
it('has role and no execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.true;
expect(executionDelay).to.be.bignumber.eq('0');
});
},
},
},
requiredRoleIsNotGranted() {
it('has no role and no execution delay', async function () {
const { isMember, executionDelay } = await this.manager.hasRole(this.role.id, this.caller);
expect(isMember).to.be.false;
expect(executionDelay).to.be.bignumber.eq('0');
});
},
},
});
});
describe('#getSchedule', function () {
beforeEach('set role and calldata', async function () {
const method = 'fnRestricted()';
this.caller = user;
this.role = { id: web3.utils.toBN(493590) };
await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
this.calldata = await this.target.contract.methods[method]().encodeABI();
this.scheduleIn = time.duration.days(10); // For shouldBehaveLikeSchedulableOperation
});
shouldBehaveLikeSchedulableOperation({
scheduled: {
before() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns schedule in the future', async function () {
const schedule = await this.manager.getSchedule(this.operationId);
expect(schedule).to.be.bignumber.equal(this.scheduledAt.add(this.scheduleIn));
expect(schedule).to.be.bignumber.gt(await time.latest());
});
},
after() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns schedule', async function () {
const schedule = await this.manager.getSchedule(this.operationId);
expect(schedule).to.be.bignumber.equal(this.scheduledAt.add(this.scheduleIn));
expect(schedule).to.be.bignumber.eq(await time.latest());
});
},
expired() {
beforeEach('consume previously set grant delay', async function () {
// Consume previously set delay
await mine();
});
it('returns 0', async function () {
expect(await this.manager.getSchedule(this.operationId)).to.be.bignumber.equal('0');
});
},
},
notScheduled() {
it('defaults to 0', async function () {
expect(await this.manager.getSchedule(this.operationId)).to.be.bignumber.equal('0');
});
},
});
});
describe('#getNonce', function () {
describe('when operation is scheduled', function () {
beforeEach('schedule operation', async function () {
const method = 'fnRestricted()';
this.caller = user;
this.role = { id: web3.utils.toBN(4209043) };
await this.manager.$_setTargetFunctionRole(this.target.address, selector(method), this.role.id);
await this.manager.$_grantRole(this.role.id, this.caller, 0, 1); // nonzero execution delay
this.calldata = await this.target.contract.methods[method]().encodeABI();
this.delay = time.duration.days(10);
const { operationId } = await scheduleOperation(this.manager, {
caller: this.caller,
target: this.target.address,
calldata: this.calldata,
delay: this.delay,
});
this.operationId = operationId;
});
it('returns nonce', async function () {
expect(await this.manager.getNonce(this.operationId)).to.be.bignumber.equal('1');
});
});
describe('when is not scheduled', function () {
it('returns default 0', async function () {
expect(await this.manager.getNonce(web3.utils.keccak256('operation'))).to.be.bignumber.equal('0');
});
});
});
describe('#hashOperation', function () {
it('returns an operationId', async function () {
const calldata = '0x123543';
const address = someAddress;
const args = [user, address, calldata];
expect(await this.manager.hashOperation(...args)).to.be.bignumber.eq(
await web3.utils.keccak256(web3.eth.abi.encodeParameters(['address', 'address', 'bytes'], args)),
);
});
});
});
describe('admin operations', function () {
beforeEach('set required role', function () {
this.role = this.roles.ADMIN;
});
describe('subject to a delay', function () {
describe('#labelRole', function () {
describe('restrictions', function () {
beforeEach('set method and args', function () {
const method = 'labelRole(uint64,string)';
const args = [123443, 'TEST'];
this.calldata = this.manager.contract.methods[method](...args).encodeABI();
});
shouldBehaveLikeDelayedAdminOperation();
});
it('emits an event with the label', async function () {
expectEvent(await this.manager.labelRole(this.roles.SOME.id, 'Some label', { from: admin }), 'RoleLabel', {
roleId: this.roles.SOME.id,
label: 'Some label',
});
});
it('updates label on a second call', async function () {
await this.manager.labelRole(this.roles.SOME.id, 'Some label', { from: admin });
expectEvent(await this.manager.labelRole(this.roles.SOME.id, 'Updated label', { from: admin }), 'RoleLabel', {
roleId: this.roles.SOME.id,
label: 'Updated label',
});
});
it('reverts labeling PUBLIC_ROLE', async function () {
await expectRevertCustomError(
this.manager.labelRole(this.roles.PUBLIC.id, 'Some label', { from: admin }),
'AccessManagerLockedRole',
[this.roles.PUBLIC.id],
);
});
it('reverts labeling ADMIN_ROLE', async function () {
await expectRevertCustomError(
this.manager.labelRole(this.roles.ADMIN.id, 'Some label', { from: admin }),
'AccessManagerLockedRole',
[this.roles.ADMIN.id],
);
});
});
describe('#setRoleAdmin', function () {
describe('restrictions', function () {
beforeEach('set method and args', function () {
const method = 'setRoleAdmin(uint64,uint64)';
const args = [93445, 84532];
this.calldata = this.manager.contract.methods[method](...args).encodeABI();
});
shouldBehaveLikeDelayedAdminOperation();
});
it("sets any role's admin if called by an admin", async function () {
expect(await this.manager.getRoleAdmin(this.roles.SOME.id)).to.be.bignumber.equal(this.roles.SOME_ADMIN.id);
const { receipt } = await this.manager.setRoleAdmin(this.roles.SOME.id, this.roles.ADMIN.id, { from: admin });
expectEvent(receipt, 'RoleAdminChanged', { roleId: this.roles.SOME.id, admin: this.roles.ADMIN.id });
expect(await this.manager.getRoleAdmin(this.roles.SOME.id)).to.be.bignumber.equal(this.roles.ADMIN.id);
});
it('reverts setting PUBLIC_ROLE admin', async function () {
await expectRevertCustomError(
this.manager.setRoleAdmin(this.roles.PUBLIC.id, this.roles.ADMIN.id, { from: admin }),
'AccessManagerLockedRole',
[this.roles.PUBLIC.id],
);
});
it('reverts setting ADMIN_ROLE admin', async function () {
await expectRevertCustomError(
this.manager.setRoleAdmin(this.roles.ADMIN.id, this.roles.ADMIN.id, { from: admin }),
'AccessManagerLockedRole',
[this.roles.ADMIN.id],
);
});
});
describe('#setRoleGuardian', function () {
describe('restrictions', function () {
beforeEach('set method and args', function () {
const method = 'setRoleGuardian(uint64,uint64)';
const args = [93445, 84532];
this.calldata = this.manager.contract.methods[method](...args).encodeABI();
});
shouldBehaveLikeDelayedAdminOperation();
});
it("sets any role's guardian if called by an admin", async function () {
expect(await this.manager.getRoleGuardian(this.roles.SOME.id)).to.be.bignumber.equal(
this.roles.SOME_GUARDIAN.id,
);
const { receipt } = await this.manager.setRoleGuardian(this.roles.SOME.id, this.roles.ADMIN.id, {
from: admin,
});
expectEvent(receipt, 'RoleGuardianChanged', { roleId: this.roles.SOME.id, guardian: this.roles.ADMIN.id });
expect(await this.manager.getRoleGuardian(this.roles.SOME.id)).to.be.bignumber.equal(this.roles.ADMIN.id);
});
it('reverts setting PUBLIC_ROLE admin', async function () {
await expectRevertCustomError(
this.manager.setRoleGuardian(this.roles.PUBLIC.id, this.roles.ADMIN.id, { from: admin }),
'AccessManagerLockedRole',
[this.roles.PUBLIC.id],
);
});
it('reverts setting ADMIN_ROLE admin', async function () {
await expectRevertCustomError(
this.manager.setRoleGuardian(this.roles.ADMIN.id, this.roles.ADMIN.id, { from: admin }),
'AccessManagerLockedRole',
[this.roles.ADMIN.id],
);
});
});
describe('#setGrantDelay', function () {
describe('restrictions', function () {
beforeEach('set method and args', function () {
const method = 'setGrantDelay(uint64,uint32)';
const args = [984910, time.duration.days(2)];
this.calldata = this.manager.contract.methods[method](...args).encodeABI();
});
shouldBehaveLikeDelayedAdminOperation();
});
it('reverts setting grant delay for the PUBLIC_ROLE', async function () {
await expectRevertCustomError(
this.manager.setGrantDelay(this.roles.PUBLIC.id, web3.utils.toBN(69), { from: admin }),
'AccessManagerLockedRole',
[this.roles.PUBLIC.id],
);
});
describe('when increasing the delay', function () {
const oldDelay = web3.utils.toBN(10);
const newDelay = web3.utils.toBN(100);
beforeEach('sets old delay', async function () {
this.role = this.roles.SOME;
await this.manager.$_setGrantDelay(this.role.id, oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
});
it('increases the delay after minsetback', async function () {
const { receipt } = await this.manager.setGrantDelay(this.role.id, newDelay, { from: admin });
const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
expectEvent(receipt, 'RoleGrantDelayChanged', {
roleId: this.role.id,
delay: newDelay,
since: timestamp.add(MINSETBACK),
});
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(newDelay);
});
});
describe('when reducing the delay', function () {
const oldDelay = time.duration.days(10);
beforeEach('sets old delay', async function () {
this.role = this.roles.SOME;
await this.manager.$_setGrantDelay(this.role.id, oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
});
describe('when the delay difference is shorter than minimum setback', function () {
const newDelay = oldDelay.subn(1);
it('increases the delay after minsetback', async function () {
const { receipt } = await this.manager.setGrantDelay(this.role.id, newDelay, { from: admin });
const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
expectEvent(receipt, 'RoleGrantDelayChanged', {
roleId: this.role.id,
delay: newDelay,
since: timestamp.add(MINSETBACK),
});
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(newDelay);
});
});
describe('when the delay difference is longer than minimum setback', function () {
const newDelay = web3.utils.toBN(1);
beforeEach('assert delay difference is higher than minsetback', function () {
expect(oldDelay.sub(newDelay)).to.be.bignumber.gt(MINSETBACK);
});
it('increases the delay after delay difference', async function () {
const setback = oldDelay.sub(newDelay);
const { receipt } = await this.manager.setGrantDelay(this.role.id, newDelay, { from: admin });
const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
expectEvent(receipt, 'RoleGrantDelayChanged', {
roleId: this.role.id,
delay: newDelay,
since: timestamp.add(setback),
});
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(oldDelay);
await time.increase(setback);
expect(await this.manager.getRoleGrantDelay(this.role.id)).to.be.bignumber.equal(newDelay);
});
});
});
});
describe('#setTargetAdminDelay', function () {
describe('restrictions', function () {
beforeEach('set method and args', function () {
const method = 'setTargetAdminDelay(address,uint32)';
const args = [someAddress, time.duration.days(3)];
this.calldata = this.manager.contract.methods[method](...args).encodeABI();
});
shouldBehaveLikeDelayedAdminOperation();
});
describe('when increasing the delay', function () {
const oldDelay = time.duration.days(10);
const newDelay = time.duration.days(11);
const target = someAddress;
beforeEach('sets old delay', async function () {
await this.manager.$_setTargetAdminDelay(target, oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
});
it('increases the delay after minsetback', async function () {
const { receipt } = await this.manager.setTargetAdminDelay(target, newDelay, { from: admin });
const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
expectEvent(receipt, 'TargetAdminDelayUpdated', {
target,
delay: newDelay,
since: timestamp.add(MINSETBACK),
});
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(newDelay);
});
});
describe('when reducing the delay', function () {
const oldDelay = time.duration.days(10);
const target = someAddress;
beforeEach('sets old delay', async function () {
await this.manager.$_setTargetAdminDelay(target, oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
});
describe('when the delay difference is shorter than minimum setback', function () {
const newDelay = oldDelay.subn(1);
it('increases the delay after minsetback', async function () {
const { receipt } = await this.manager.setTargetAdminDelay(target, newDelay, { from: admin });
const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
expectEvent(receipt, 'TargetAdminDelayUpdated', {
target,
delay: newDelay,
since: timestamp.add(MINSETBACK),
});
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
await time.increase(MINSETBACK);
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(newDelay);
});
});
describe('when the delay difference is longer than minimum setback', function () {
const newDelay = web3.utils.toBN(1);
beforeEach('assert delay difference is higher than minsetback', function () {
expect(oldDelay.sub(newDelay)).to.be.bignumber.gt(MINSETBACK);
});
it('increases the delay after delay difference', async function () {
const setback = oldDelay.sub(newDelay);
const { receipt } = await this.manager.setTargetAdminDelay(target, newDelay, { from: admin });
const timestamp = await clockFromReceipt.timestamp(receipt).then(web3.utils.toBN);
expectEvent(receipt, 'TargetAdminDelayUpdated', {
target,
delay: newDelay,
since: timestamp.add(setback),
});
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(oldDelay);
await time.increase(setback);
expect(await this.manager.getTargetAdminDelay(target)).to.be.bignumber.equal(newDelay);
});
});
});
});
});
describe('not subject to a delay', function () {
describe('#updateAuthority', function () {
beforeEach('create a target and a new authority', async function () {
this.newAuthority = await AccessManager.new(admin);
this.newManagedTarget = await AccessManagedTarget.new(this.manager.address);
});
describe('restrictions', function () {
beforeEach('set method and args', function () {
const method = 'updateAuthority(address,address)';
const args = [this.newManagedTarget.address, this.newAuthority.address];
this.calldata = this.manager.contract.methods[method](...args).encodeABI();
});
shouldBehaveLikeNotDelayedAdminOperation();
});
it('changes the authority', async function () {
expect(await this.newManagedTarget.authority()).to.be.equal(this.manager.address);
const { tx } = await this.manager.updateAuthority(this.newManagedTarget.address, this.newAuthority.address, {
from: admin,
});
// Managed contract is responsible of notifying the change through an event
await expectEvent.inTransaction(tx, this.newManagedTarget, 'AuthorityUpdated', {
authority: this.newAuthority.address,
});
expect(await this.newManagedTarget.authority()).to.be.equ