Skip to content

Commit

Permalink
test: Add comprehensive test suite for MultisigFactory contract
Browse files Browse the repository at this point in the history
  • Loading branch information
SigismundSchlomo committed Nov 18, 2024
1 parent a9caa7c commit bf6624f
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 1 deletion.
2 changes: 1 addition & 1 deletion contracts/multisig/MultisigFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract MultisigFactory is IMultisigFactory, UUPSUpgradeable, AccessControlUpgr
string memory name = _names[i];
address multisigAddress = _multisigs[i];
require(multisigAddress != address(0), "Invalid multisig address");
require(multisigs[name] != address(0), "Already registered");
require(multisigs[name] == address(0), "Already registered");

multisigs[name] = multisigAddress;

Expand Down
112 changes: 112 additions & 0 deletions test/multisig/MultisigFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import { expect } from "chai";
import { ethers, upgrades } from "hardhat";
import { MultisigFactory, Multisig } from "../../typechain-types";
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";

describe("MultisigFactory", function () {
let multisigFactory: MultisigFactory;
let owner: SignerWithAddress;
let addr1: SignerWithAddress;
let addr2: SignerWithAddress;
let ecosystemMaster: SignerWithAddress;
let commonMaster: SignerWithAddress;

beforeEach(async function () {
[owner, addr1, addr2, ecosystemMaster, commonMaster] = await ethers.getSigners();

const MultisigFactoryFactory = await ethers.getContractFactory("MultisigFactory");
multisigFactory = await upgrades.deployProxy(
MultisigFactoryFactory,
[ecosystemMaster.address, commonMaster.address]
) as MultisigFactory;

// Grant CREATOR_ROLE to owner
const CREATOR_ROLE = await multisigFactory.CREATOR_ROLE();
await multisigFactory.grantRole(CREATOR_ROLE, owner.address);
});

it("should initialize correctly", async function () {
const CREATOR_ROLE = await multisigFactory.CREATOR_ROLE();
expect(await multisigFactory.hasRole(CREATOR_ROLE, owner.address)).to.be.true;
});

it("should create new multisig", async function () {
const settings = {
signers: [owner.address, addr1.address],
isInitiatorFlags: [true, false],
threshold: 100,
owner: owner.address
};

const multisigName = "TestMultisig";
const tx = await multisigFactory.createMultisig(multisigName, settings);
const receipt = await tx.wait();

// Check event emission
const event = receipt.events?.find(e => e.event === "MultisigCreated");
expect(event).to.not.be.undefined;

// Verify the multisig was stored
const multisigAddress = await multisigFactory.getMultisigAddress(multisigName);
expect(multisigAddress).to.not.equal(ethers.constants.AddressZero);

// Verify the multisig configuration
const multisig = await ethers.getContractAt("Multisig", multisigAddress);
const [signers, flags] = await multisig.getSigners();
expect(signers).to.deep.equal([owner.address, addr1.address]);
expect(flags).to.deep.equal([true, false]);
});

it("should register existing multisigs", async function () {
// First create a multisig directly
const Multisig = await ethers.getContractFactory("Multisig");
const multisig = await Multisig.deploy(
[owner.address, addr1.address],
[true, false],
100,
owner.address
);

// Register it in the factory
const multisigName = "ExistingMultisig";
await multisigFactory.registerMultisigs(
[multisig.address],
[multisigName]
);

// Verify registration
expect(await multisigFactory.getMultisigAddress(multisigName))
.to.equal(multisig.address);
});

it("should not allow non-CREATOR_ROLE to create multisigs", async function () {
const settings = {
signers: [owner.address, addr1.address],
isInitiatorFlags: [true, false],
threshold: 100,
owner: owner.address
};

await expect(
multisigFactory.connect(addr1).createMultisig("TestMultisig", settings)
).to.be.reverted;
});

it("should not allow non-CREATOR_ROLE to register multisigs", async function () {
await expect(
multisigFactory.connect(addr1).registerMultisigs(
[ethers.constants.AddressZero],
["TestMultisig"]
)
).to.be.reverted;
});

it("should not allow registering zero address multisigs", async function () {
await expect(
multisigFactory.registerMultisigs(
[ethers.constants.AddressZero],
["TestMultisig"]
)
).to.be.revertedWith("Invalid multisig address");
});
});

0 comments on commit bf6624f

Please sign in to comment.