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

don't mint if wearer isn't changing #127

Merged
merged 2 commits into from
Nov 15, 2024
Merged
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
9 changes: 6 additions & 3 deletions contracts/autonomous-admin/DecentAutonomousAdminV1.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ contract DecentAutonomousAdminV1 is

hatsElectionModule.startNextTerm();

// This will burn the hat since wearer is no longer eligible
// This will burn the hat if wearer is no longer eligible
args.hatsProtocol.checkHatWearerStatus(args.hatId, args.currentWearer);
// This will mint the hat to the nominated wearer
args.hatsProtocol.mintHat(args.hatId, args.nominatedWearer);

// This will mint the hat to the nominated wearer, if necessary
if (args.nominatedWearer != args.currentWearer) {
args.hatsProtocol.mintHat(args.hatId, args.nominatedWearer);
}
}

function supportsInterface(
Expand Down
100 changes: 66 additions & 34 deletions test/autonomous-admin/DecentAutonomousAdminV1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('DecentAutonomousAdminHatV1', function () {
await hatsProtocol.createHat(
topHatId, // top hat id
'Details', // Hat details
100, // Max supply
1, // Max supply
'0x0000000000000000000000000000000000004a75', // Eligibility module (none)
'0x0000000000000000000000000000000000004a75', // Toggle module (none)
true, // Is mutable
Expand Down Expand Up @@ -86,55 +86,87 @@ describe('DecentAutonomousAdminHatV1', function () {

// Mint the role hat to currentWearer
await hatsProtocol.mintHat(roleHatId, await firstWearer.getAddress());

// set up the next election
nextTermEnd = firstTermEnd + 100;
await hatsElectionModule.setNextTerm(nextTermEnd);
await hatsElectionModule.elect(nextTermEnd, [await secondWearer.getAddress()]);
});

describe('triggerStartNextTerm', function () {
describe('before the first term is over', function () {
it('should have correct wearers', async () => {
expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(true);
expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(false);
describe('when the new wearer is different from the old wearer', function () {
beforeEach(async () => {
// set up the next election
nextTermEnd = firstTermEnd + 100;
await hatsElectionModule.setNextTerm(nextTermEnd);
await hatsElectionModule.elect(nextTermEnd, [await secondWearer.getAddress()]);
});
});

describe('after the first term is over', function () {
beforeEach(async () => {
// Wait until the first term is over
await setTime(firstTermEnd + 1);
describe('before the first term is over', function () {
it('should have correct wearers', async () => {
expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(true);
expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(false);
});
});

describe('with a valid current wearer', function () {
describe('after the first term is over', function () {
beforeEach(async () => {
await decentAutonomousAdminInstance.triggerStartNextTerm({
currentWearer: await firstWearer.getAddress(),
nominatedWearer: secondWearer.address,
hatsProtocol: await hatsProtocol.getAddress(),
hatId: roleHatId,
// Wait until the first term is over
await setTime(firstTermEnd + 1);
});

describe('with a valid current wearer', function () {
beforeEach(async () => {
await decentAutonomousAdminInstance.triggerStartNextTerm({
currentWearer: await firstWearer.getAddress(),
nominatedWearer: secondWearer.address,
hatsProtocol: await hatsProtocol.getAddress(),
hatId: roleHatId,
});
});

it('should have correct wearers after triggering next term', async () => {
expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(
false,
);
expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(
true,
);
});
});

it('should have correct wearers after triggering next term', async () => {
expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(false);
expect(await hatsProtocol.isWearerOfHat(secondWearer.address, roleHatId)).to.equal(true);
describe('with invalid current wearer', function () {
it('should revert if the current wearer is not the wearer of the hat', async () => {
await expect(
decentAutonomousAdminInstance.triggerStartNextTerm({
currentWearer: await randomUser.getAddress(),
nominatedWearer: secondWearer.address,
hatsProtocol: await hatsProtocol.getAddress(),
hatId: roleHatId,
}),
).to.be.revertedWithCustomError(hatsProtocol, 'AllHatsWorn');
});
});
});
});

describe('with invalid current wearer', function () {
it('should revert if the current wearer is not the wearer of the hat', async () => {
await expect(
decentAutonomousAdminInstance.triggerStartNextTerm({
currentWearer: await randomUser.getAddress(),
nominatedWearer: secondWearer.address,
hatsProtocol: await hatsProtocol.getAddress(),
hatId: roleHatId,
}),
).to.be.revertedWithCustomError(hatsProtocol, 'AllHatsWorn');
describe('when the new wearer is the same as the old wearer', function () {
beforeEach(async () => {
// set up the next election
nextTermEnd = firstTermEnd + 100;
await hatsElectionModule.setNextTerm(nextTermEnd);
await hatsElectionModule.elect(nextTermEnd, [await firstWearer.getAddress()]);

// Wait until the first term is over
await setTime(firstTermEnd + 1);

// trigger the next term
await decentAutonomousAdminInstance.triggerStartNextTerm({
currentWearer: await firstWearer.getAddress(),
nominatedWearer: firstWearer.address,
hatsProtocol: await hatsProtocol.getAddress(),
hatId: roleHatId,
});
});

it('should result in original wearer still wearing hat', async () => {
expect(await hatsProtocol.isWearerOfHat(firstWearer.address, roleHatId)).to.equal(true);
});
});
});
});