Skip to content

Commit

Permalink
Merge pull request #127 from decentdao/mint-only-wearer-is-different
Browse files Browse the repository at this point in the history
don't mint if wearer isn't changing
  • Loading branch information
adamgall authored Nov 15, 2024
2 parents 3b7a1af + ec67ec6 commit f26571a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 37 deletions.
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);
});
});
});
});

0 comments on commit f26571a

Please sign in to comment.