Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrencev committed Sep 6, 2021
1 parent 86a7375 commit 3beaa51
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 102 deletions.
6 changes: 2 additions & 4 deletions contracts/governance/Timelock.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*Submitted for verification at Etherscan.io on 2021-09-01
*/
*/

// Sources flattened with hardhat v2.6.1 https://hardhat.org

Expand Down Expand Up @@ -213,7 +213,6 @@ library SafeMath {
}
}


// File contracts/Timelock.sol

pragma solidity ^0.5.16;
Expand Down Expand Up @@ -367,7 +366,6 @@ contract Timelock {
}
}


// File contracts/Mock.sol

pragma solidity ^0.5.16;
Expand All @@ -381,4 +379,4 @@ contract Mock {
message = _message;
emit Received(_message, msg.value);
}
}
}
2 changes: 1 addition & 1 deletion contracts/test/Receiver.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pragma solidity ^0.8.2;

contract Receiver {
function receiveETH(string calldata) public payable {}
function receiveEth(string calldata) public payable {}

function receiveNoEth(string calldata) public {}
}
190 changes: 93 additions & 97 deletions test/governance/DopeDAO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,139 +10,135 @@ import { DopeWarsLoot, DopeDAOTest, Receiver, Timelock } from "../../typechain";
const { deployContract } = hre.waffle;

describe("DopeDAO", function () {
before(async function () {
this.signers = {} as Signers;
before(async function () {
this.signers = {} as Signers;

const signers: SignerWithAddress[] = await hre.ethers.getSigners();
this.signers.admin = signers[0];
this.signers.alice = signers[1];
this.signers.bob = signers[2];
});

describe("setup", function () {

beforeEach(async function () {
const lootArtifact: Artifact = await hre.artifacts.readArtifact("DopeWarsLoot");
this.loot = <DopeWarsLoot>await deployContract(this.signers.admin, lootArtifact, []);

const timelockArtifact: Artifact = await hre.artifacts.readArtifact("Timelock");
this.timelock = <Timelock>await deployContract(this.signers.admin, timelockArtifact, [this.signers.admin.address, 60]);

const daoArtifact: Artifact = await hre.artifacts.readArtifact("DopeDAOTest");
this.dao = <DopeDAOTest>await deployContract(this.signers.admin, daoArtifact, [this.loot.address, this.timelock.address]);

const receiverArtifact: Artifact = await hre.artifacts.readArtifact("Receiver");
this.receiver = <Receiver>await deployContract(this.signers.admin, receiverArtifact, []);
const signers: SignerWithAddress[] = await hre.ethers.getSigners();
this.signers.admin = signers[0];
this.signers.alice = signers[1];
this.signers.bob = signers[2];
});

await Promise.all([...Array(5).keys()].map(async (i) => this.loot.claim(i + 1)))
describe("lifecycle", function () {
beforeEach(async function () {
const lootArtifact: Artifact = await hre.artifacts.readArtifact("DopeWarsLoot");
this.loot = <DopeWarsLoot>await deployContract(this.signers.admin, lootArtifact, []);

const timelockArtifact: Artifact = await hre.artifacts.readArtifact("Timelock");
this.timelock = <Timelock>await deployContract(this.signers.admin, timelockArtifact, [this.signers.admin.address, 10]);

});
const daoArtifact: Artifact = await hre.artifacts.readArtifact("DopeDAOTest");
this.dao = <DopeDAOTest>await deployContract(this.signers.admin, daoArtifact, [this.loot.address, this.timelock.address]);

it("propose and execute a proposal with no eth", async function () {
const eta = Date.now() + 10;
const sig = "setPendingAdmin(address)"
const data = new ethers.utils.AbiCoder().encode(["address"], [this.dao.address]);
const receiverArtifact: Artifact = await hre.artifacts.readArtifact("Receiver");
this.receiver = <Receiver>await deployContract(this.signers.admin, receiverArtifact, []);

await expect(this.timelock.queueTransaction(this.timelock.address, 0, sig, data, eta)).to.not.be.reverted;
await Promise.all([...Array(5).keys()].map(async (i) => this.loot.claim(i + 1)))
});

await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [eta],
});
it("propose and execute a proposal with no eth", async function () {
let now = await hre.waffle.provider.getBlock('latest').then(block => block.timestamp)
const eta = now + 11;
const sig = "setPendingAdmin(address)"
const data = new ethers.utils.AbiCoder().encode(["address"], [this.dao.address]);

await expect(this.timelock.executeTransaction(this.timelock.address, 0, sig, data, eta)).to.not.be.reverted;
await this.dao.__acceptAdmin()
await this.timelock.queueTransaction(this.timelock.address, 0, sig, data, eta);

// console.log((await ethers.provider.getBalance(this.timelock.address)).toString())
await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [eta],
});

const calldata = new ethers.utils.AbiCoder().encode(["string"], ["gang"]);
await this.timelock.executeTransaction(this.timelock.address, 0, sig, data, eta);
await this.dao.__acceptAdmin()

const txn = await this.dao["propose(address[],uint256[],string[],bytes[],string)"](
[this.receiver.address], [0], ["receiveNoEth(string)"], [calldata], "Send no ETH"
)
const calldata = new ethers.utils.AbiCoder().encode(["string"], ["gang"]);

const receipt = await txn.wait()
const proposalId = receipt.events![0].args!.proposalId
const txn = await this.dao["propose(address[],uint256[],string[],bytes[],string)"](
[this.receiver.address], [0], ["receiveNoEth(string)"], [calldata], "Send no ETH"
)

// check proposal id exists
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("0")
const receipt = await txn.wait()
const proposalId = receipt.events![0].args!.proposalId

await hre.network.provider.send("evm_mine");
// check proposal id exists
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("0")

await expect(this.dao.castVote(proposalId, 1)).to.not.be.reverted;
await hre.network.provider.send("evm_mine");

// check we have voted
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("5")
await this.dao.castVote(proposalId, 1);

await expect(this.dao["queue(uint256)"](proposalId)).to.not.be.reverted;
// check we have voted
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("5")

await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [Date.now() + 1],
});
await this.dao["queue(uint256)"](proposalId);

await expect(this.dao["execute(uint256)"](proposalId)).to.not.be.reverted;
now = await hre.waffle.provider.getBlock('latest').then(block => block.timestamp)
await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [now + 11],
});

// check it executed
expect((await this.dao.proposals(proposalId)).executed).to.eql(true);
})
await this.dao["execute(uint256)"](proposalId)

it("propose and execute a proposal with eth", async function () {
const eta = Date.now() + 10;
const sig = "setPendingAdmin(address)"
const data = new ethers.utils.AbiCoder().encode(["address"], [this.dao.address]);
// check it executed
expect((await this.dao.proposals(proposalId)).executed).to.eql(true);
})

const value = ethers.utils.parseEther("0.1")
// send eth to the timelock
await this.signers.alice.sendTransaction({
to: this.timelock.address,
value
})
it("propose and execute a proposal with eth", async function () {
let now = await hre.waffle.provider.getBlock('latest').then(block => block.timestamp)
const eta = now + 12;
const sig = "setPendingAdmin(address)"
const data = new ethers.utils.AbiCoder().encode(["address"], [this.dao.address]);

await expect(this.timelock.queueTransaction(this.timelock.address, 0, sig, data, eta)).to.not.be.reverted;
const value = ethers.utils.parseEther("0.1")
// send eth to the timelock
await this.signers.alice.sendTransaction({
to: this.timelock.address,
value
})

await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [eta],
});
await this.timelock.queueTransaction(this.timelock.address, 0, sig, data, eta)

await expect(this.timelock.executeTransaction(this.timelock.address, 0, sig, data, eta)).to.not.be.reverted;
await this.dao.__acceptAdmin()
await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [eta],
});

// console.log((await ethers.provider.getBalance(this.timelock.address)).toString())
await this.timelock.executeTransaction(this.timelock.address, 0, sig, data, eta)
await this.dao.__acceptAdmin()

const calldata = new ethers.utils.AbiCoder().encode(["string"], ["gang"]);
const calldata = new ethers.utils.AbiCoder().encode(["string"], ["gang"]);

const txn = await this.dao["propose(address[],uint256[],string[],bytes[],string)"](
[this.receiver.address], [value], ["receiveEth(string)"], [calldata], "Send ETH"
)
const txn = await this.dao["propose(address[],uint256[],string[],bytes[],string)"](
[this.receiver.address], [value], ["receiveEth(string)"], [calldata], "Send ETH"
)

const receipt = await txn.wait()
const proposalId = receipt.events![0].args!.proposalId
const receipt = await txn.wait()
const proposalId = receipt.events![0].args!.proposalId

// check proposal id exists
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("0")
// check proposal id exists
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("0")

await hre.network.provider.send("evm_mine");
await hre.network.provider.send("evm_mine");

await expect(this.dao.castVote(proposalId, 1)).to.not.be.reverted;
await this.dao.castVote(proposalId, 1);

// check we have voted
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("5")
// check we have voted
expect((await this.dao.proposals(proposalId)).forVotes.toString()).to.eql("5")

await expect(this.dao["queue(uint256)"](proposalId)).to.not.be.reverted;
await this.dao["queue(uint256)"](proposalId);

await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [Date.now() + 1],
});
now = await hre.waffle.provider.getBlock('latest').then(block => block.timestamp)
await hre.network.provider.request({
method: "evm_setNextBlockTimestamp",
params: [now + 11],
});

await expect(this.dao["execute(uint256)"](proposalId)).to.not.be.reverted;
await this.dao["execute(uint256)"](proposalId)

// check it executed
expect((await this.dao.proposals(proposalId)).executed).to.eql(true);
// check it executed
expect((await this.dao.proposals(proposalId)).executed).to.eql(true);
})
})

})
})

0 comments on commit 3beaa51

Please sign in to comment.