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

Improve unit tests #60

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
272 changes: 210 additions & 62 deletions test/Baal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,39 @@ const revertMessages = {
submitProposalFlag: "!flag",
submitVoteTimeEnded: "ended",
proposalMisnumbered: "!exist",
callFailure: "call failure",
initializableAlreadyInitialized: "Initializable: contract is already initialized",
};

const zeroAddress = "0x0000000000000000000000000000000000000000";

const deploymentConfig = {
GRACE_PERIOD_IN_SECONDS: 43200,
MIN_VOTING_PERIOD_IN_SECONDS: 172800,
MAX_VOTING_PERIOD_IN_SECONDS: 432000,
PROPOSAL_OFFERING: 0,
TOKEN_NAME: "wrapped ETH",
TOKEN_SYMBOL: "WETH",
};

const deploymentConfigVoteTimeError = {
GRACE_PERIOD_IN_SECONDS: 43200,
MIN_VOTING_PERIOD_IN_SECONDS: 432000,
MAX_VOTING_PERIOD_IN_SECONDS: 172800,
PROPOSAL_OFFERING: 0,
TOKEN_NAME: "wrapped ETH",
TOKEN_SYMBOL: "WETH",
};

const deploymentConfigGracePeriodError = {
GRACE_PERIOD_IN_SECONDS: 0,
MIN_VOTING_PERIOD_IN_SECONDS: 432000,
MAX_VOTING_PERIOD_IN_SECONDS: 172800,
PROPOSAL_OFFERING: 0,
TOKEN_NAME: "wrapped ETH",
TOKEN_SYMBOL: "WETH",
};

async function blockTime() {
const block = await ethers.provider.getBlock("latest");
return block.timestamp;
Expand All @@ -44,26 +73,124 @@ async function blockNumber() {
return block.number;
}

async function snapshot () {
return ethers.provider.send('evm_snapshot', [])
}

async function restore (snapshotId: number) {
return ethers.provider.send('evm_revert', [snapshotId])
}

async function forceMine () {
return ethers.provider.send('evm_mine', [])
}


async function moveForwardPeriods(periods: number) {
const goToTime = deploymentConfig.MIN_VOTING_PERIOD_IN_SECONDS * periods;
await ethers.provider.send("evm_increaseTime", [goToTime]);
return true;
}

const deploymentConfig = {
GRACE_PERIOD_IN_SECONDS: 43200,
MIN_VOTING_PERIOD_IN_SECONDS: 172800,
MAX_VOTING_PERIOD_IN_SECONDS: 432000,
PROPOSAL_OFFERING: 0,
TOKEN_NAME: "wrapped ETH",
TOKEN_SYMBOL: "WETH",
};
const createPeriods = function(
deploymentConfig: any,
lootPaused: boolean,
sharesPaused: boolean)
{
let abiCoder = ethers.utils.defaultAbiCoder;

let periods = abiCoder.encode(
["uint32", "uint32", "uint32", "uint256", "bool", "bool"],
[
deploymentConfig.MIN_VOTING_PERIOD_IN_SECONDS,
deploymentConfig.MAX_VOTING_PERIOD_IN_SECONDS,
deploymentConfig.GRACE_PERIOD_IN_SECONDS,
deploymentConfig.PROPOSAL_OFFERING,
lootPaused,
sharesPaused,
]
);

return periods;
}

const encodeBaalInitFunctions = function(
baal: Baal,
periods: any,
wethAddress: string,
shamanAddress: string,
summonerAddress: string,
shares: number,
loot: number)
{

let initFunctions = [];

initFunctions.push(baal.interface.encodeFunctionData(
"setPeriods",
[periods]
));
initFunctions.push(baal.interface.encodeFunctionData(
"setGuildTokens",
[[wethAddress]]
));
initFunctions.push(baal.interface.encodeFunctionData(
"setShamans",
[
[shamanAddress],
true,
]
));
initFunctions.push(baal.interface.encodeFunctionData(
"mintShares", [
[summonerAddress],
[shares],
]
));

initFunctions.push(baal.interface.encodeFunctionData(
"mintLoot",
[
[summonerAddress],
[loot],
]
));

return initFunctions;
}

const completeInitializationParams = function (multisend: MultiSend, baal: Baal, encodedFunctionDataList: any) {
let abiCoder = ethers.utils.defaultAbiCoder;

const initalizationActions = encodeMultiAction(
multisend,
encodedFunctionDataList,
Array(encodedFunctionDataList.length).fill(baal.address),
Array(encodedFunctionDataList.length).fill(BigNumber.from(0)),
Array(encodedFunctionDataList.length).fill(0)
);

let encodedInitParams = abiCoder.encode(
["string", "string", "address", "bytes"],
[
deploymentConfig.TOKEN_NAME,
deploymentConfig.TOKEN_SYMBOL,
multisend.address,
initalizationActions,
]
);

return encodedInitParams;
}

describe("Baal contract", function () {
let baal: Baal;
let weth: TestErc20;
let shaman: RageQuitBank;
let multisend: MultiSend;
let encodedInitParams: any;
let badEncodedInitParams: any;
let snapshotId: number;

let applicant: SignerWithAddress;
let summoner: SignerWithAddress;
Expand All @@ -79,9 +206,12 @@ describe("Baal contract", function () {
const no = false;

beforeEach(async function () {
snapshotId = await snapshot();

const BaalContract = await ethers.getContractFactory("Baal");
const ShamanContract = await ethers.getContractFactory("RageQuitBank");
const MultisendContract = await ethers.getContractFactory("MultiSend");

[summoner, applicant] = await ethers.getSigners();

const ERC20 = await ethers.getContractFactory("TestERC20");
Expand All @@ -95,64 +225,23 @@ describe("Baal contract", function () {

const abiCoder = ethers.utils.defaultAbiCoder;

const periods = abiCoder.encode(
["uint32", "uint32", "uint32", "uint256", "bool", "bool"],
[
deploymentConfig.MIN_VOTING_PERIOD_IN_SECONDS,
deploymentConfig.MAX_VOTING_PERIOD_IN_SECONDS,
deploymentConfig.GRACE_PERIOD_IN_SECONDS,
deploymentConfig.PROPOSAL_OFFERING,
lootPaused,
sharesPaused,
]
);

const setPeriods = await baal.interface.encodeFunctionData("setPeriods", [
periods,
]);
const setGuildTokens = await baal.interface.encodeFunctionData(
"setGuildTokens",
[[weth.address]]
);
const setShaman = await baal.interface.encodeFunctionData("setShamans", [
[shaman.address],
true,
]);
const mintShares = await baal.interface.encodeFunctionData("mintShares", [
[summoner.address],
[shares],
]);
const mintLoot = await baal.interface.encodeFunctionData("mintLoot", [
[summoner.address],
[loot],
]);
// const delegateSummoners = await baal.interface.encodeFunctionData('delegateSummoners', [[summoner.address], [summoner.address]])
const periods = createPeriods(deploymentConfig, lootPaused, sharesPaused);

const initalizationActions = encodeMultiAction(
multisend,
[setPeriods, setGuildTokens, setShaman, mintShares, mintLoot],
[baal.address, baal.address, baal.address, baal.address, baal.address],
[
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
BigNumber.from(0),
],
[0, 0, 0, 0, 0]
);

const encodedInitParams = abiCoder.encode(
["string", "string", "address", "bytes"],
[
deploymentConfig.TOKEN_NAME,
deploymentConfig.TOKEN_SYMBOL,
multisend.address,
initalizationActions,
]
const encodedFunctionDataList = encodeBaalInitFunctions(
baal,
periods,
weth.address,
shaman.address,
summoner.address,
shares,
loot
);

await baal.setUp(encodedInitParams);
encodedInitParams = completeInitializationParams(multisend, baal, encodedFunctionDataList);

// const delegateSummoners = await baal.interface.encodeFunctionData('delegateSummoners', [[summoner.address], [summoner.address]])


await shaman.init(baal.address);

Expand All @@ -175,10 +264,16 @@ describe("Baal contract", function () {
};
});

afterEach(async function () {
await restore(snapshotId);
})

describe("constructor", function () {
it("verify deployment parameters", async function () {
const now = await blockTime();

await baal.setUp(encodedInitParams);

const decimals = await baal.decimals();
expect(decimals).to.equal(18);

Expand Down Expand Up @@ -225,6 +320,45 @@ describe("Baal contract", function () {
const totalLoot = await baal.totalLoot();
expect(totalLoot).to.equal(500);
});

it("require fail - second initialization should fail", async function (){
await baal.setUp(encodedInitParams);
expect(baal.setUp(encodedInitParams)).to.be.revertedWith(revertMessages.initializableAlreadyInitialized);
});

it("require fail - if min and max voting times are switched", async function (){
let periods = createPeriods(deploymentConfigVoteTimeError, lootPaused, sharesPaused);

let encodedFunctionDataList = encodeBaalInitFunctions(
baal,
periods,
weth.address,
shaman.address,
summoner.address,
shares,
loot
);

encodedInitParams = completeInitializationParams(multisend, baal, encodedFunctionDataList);
expect(baal.setUp(encodedInitParams)).to.be.revertedWith(revertMessages.initializableAlreadyInitialized);
});

it("require fail - grace period negative", async function (){
let periods = createPeriods(deploymentConfigGracePeriodError, lootPaused, sharesPaused);

let encodedFunctionDataList = encodeBaalInitFunctions(
baal,
periods,
weth.address,
shaman.address,
summoner.address,
shares,
loot
);

encodedInitParams = completeInitializationParams(multisend, baal, encodedFunctionDataList);
expect(baal.setUp(encodedInitParams)).to.be.revertedWith(revertMessages.initializableAlreadyInitialized);
});
});

// describe('memberAction', function () {
Expand All @@ -242,6 +376,9 @@ describe("Baal contract", function () {
// })

describe("submitProposal", function () {
beforeEach(async function () {
await baal.setUp(encodedInitParams);
});
it("happy case", async function () {
const countBefore = await baal.proposalCount();

Expand Down Expand Up @@ -281,6 +418,7 @@ describe("Baal contract", function () {

describe("submitVote", function () {
beforeEach(async function () {
await baal.setUp(encodedInitParams);
await baal.submitProposal(
proposal.votingPeriod,
proposal.data,
Expand Down Expand Up @@ -322,6 +460,10 @@ describe("Baal contract", function () {
});

describe("processProposal", function () {
beforeEach(async function () {
await baal.setUp(encodedInitParams);
});

it("happy case yes wins", async function () {
const beforeProcessed = await baal.proposals(1);
await baal.submitProposal(
Expand Down Expand Up @@ -413,6 +555,7 @@ describe("Baal contract", function () {

describe("ragequit", function () {
beforeEach(async function () {
await baal.setUp(encodedInitParams);
await baal.submitProposal(
proposal.votingPeriod,
proposal.data,
Expand Down Expand Up @@ -447,6 +590,10 @@ describe("Baal contract", function () {
});

describe("getCurrentVotes", function () {
beforeEach(async function () {
await baal.setUp(encodedInitParams);
});

it("happy case - account with votes", async function () {
const currentVotes = await baal.getCurrentVotes(summoner.address);
const nCheckpoints = await baal.numCheckpoints(summoner.address);
Expand All @@ -466,6 +613,7 @@ describe("Baal contract", function () {

describe("getPriorVotes", function () {
beforeEach(async function () {
await baal.setUp(encodedInitParams);
await baal.submitProposal(
proposal.votingPeriod,
proposal.data,
Expand Down