dgf-prototype/ethereum/test/DAO.js

329 lines
15 KiB
JavaScript
Raw Normal View History

2024-03-05 21:21:27 -06:00
const {
2024-03-10 11:55:59 -05:00
time,
2024-03-05 21:21:27 -06:00
loadFixture,
} = require('@nomicfoundation/hardhat-toolbox/network-helpers');
const { expect } = require('chai');
const { ethers } = require('hardhat');
2024-03-06 16:57:09 -06:00
describe('DAO', () => {
2024-03-10 22:29:51 -05:00
async function deploy() {
2024-03-05 21:21:27 -06:00
const [account1, account2] = await ethers.getSigners();
const DAO = await ethers.getContractFactory('DAO');
const dao = await DAO.deploy();
return { dao, account1, account2 };
}
2024-03-10 11:55:59 -05:00
it('Should deploy', async () => {
2024-03-10 22:29:51 -05:00
const { dao } = await loadFixture(deploy);
2024-03-10 11:55:59 -05:00
expect(dao).to.exist;
2024-03-11 13:39:56 -05:00
expect(await dao.totalSupply()).to.equal(0);
2024-03-10 11:55:59 -05:00
});
2024-03-12 14:10:06 -05:00
describe('Post', () => {
it('should be able to add a post', async () => {
const { dao, account1 } = await loadFixture(deploy);
2024-03-19 22:22:36 -05:00
const contentId = 'some-id';
await expect(dao.addPost(account1, contentId)).to.emit(dao, 'PostAdded').withArgs(0);
2024-03-12 14:10:06 -05:00
const post = await dao.posts(0);
expect(post.author).to.equal(account1);
expect(post.sender).to.equal(account1);
2024-03-19 22:22:36 -05:00
expect(post.contentId).to.equal(contentId);
2024-03-12 14:10:06 -05:00
});
it('should be able to add a post on behalf of another account', async () => {
const { dao, account1, account2 } = await loadFixture(deploy);
2024-03-19 22:22:36 -05:00
const contentId = 'some-id';
await dao.addPost(account2, contentId);
2024-03-12 14:10:06 -05:00
const post = await dao.posts(0);
expect(post.author).to.equal(account2);
expect(post.sender).to.equal(account1);
2024-03-19 22:22:36 -05:00
expect(post.contentId).to.equal(contentId);
2024-03-12 14:10:06 -05:00
});
});
2024-03-05 21:21:27 -06:00
describe('Validation Pool', () => {
2024-03-10 11:55:59 -05:00
let dao;
let account1;
let account2;
2024-03-10 11:55:59 -05:00
const POOL_DURATION = 3600; // 1 hour
2024-03-11 13:39:56 -05:00
const POOL_FEE = 100;
2024-03-26 18:58:00 -05:00
const emptyCallbackData = ethers.AbiCoder.defaultAbiCoder().encode([], []);
const initiateValidationPool = ({
postIndex, duration,
2024-03-28 15:06:14 -05:00
quorum, winRatio, bindingPercent,
2024-03-26 18:58:00 -05:00
redistributeLosingStakes, callbackOnValidate,
callbackData, fee,
} = {}) => dao.initiateValidationPool(
postIndex ?? 0,
duration ?? POOL_DURATION,
2024-03-28 15:06:14 -05:00
quorum ?? [1, 3],
winRatio ?? [1, 2],
2024-03-26 18:58:00 -05:00
bindingPercent ?? 100,
redistributeLosingStakes ?? true,
callbackOnValidate ?? false,
callbackData ?? emptyCallbackData,
{ value: fee ?? POOL_FEE },
);
2024-03-10 11:55:59 -05:00
beforeEach(async () => {
({ dao, account1, account2 } = await loadFixture(deploy));
2024-03-19 22:22:36 -05:00
await dao.addPost(account1, 'content-id');
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ fee: POOL_FEE });
2024-03-10 11:55:59 -05:00
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(0);
expect(await dao.validationPoolCount()).to.equal(1);
2024-03-10 12:57:30 -05:00
expect(await dao.memberCount()).to.equal(0);
expect(await dao.balanceOf(account1)).to.equal(0);
2024-03-11 13:39:56 -05:00
expect(await dao.totalSupply()).to.equal(POOL_FEE);
2024-03-10 11:55:59 -05:00
});
2024-03-11 13:39:56 -05:00
describe('Initiate', () => {
it('should not be able to initiate a validation pool without a fee', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ fee: 0 });
2024-03-11 13:39:56 -05:00
await expect(init()).to.be.revertedWith('Fee is required to initiate validation pool');
});
2024-03-10 11:55:59 -05:00
2024-03-26 15:20:54 -05:00
it('should not be able to initiate a validation pool with a quorum below the minimum', async () => {
2024-03-28 15:06:14 -05:00
const init = () => initiateValidationPool({ quorum: [1, 11] });
2024-03-26 15:20:54 -05:00
await expect(init()).to.be.revertedWith('Quorum is below minimum');
});
it('should not be able to initiate a validation pool with a quorum greater than 1', async () => {
2024-03-28 15:06:14 -05:00
const init = () => initiateValidationPool({ quorum: [11, 10] });
2024-03-26 15:20:54 -05:00
await expect(init()).to.be.revertedWith('Quorum is greater than one');
});
2024-03-13 12:02:08 -05:00
it('should not be able to initiate a validation pool with duration below minimum', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ duration: 0 });
2024-03-13 12:02:08 -05:00
await expect(init()).to.be.revertedWith('Duration is too short');
});
it('should not be able to initiate a validation pool with duration above maximum', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ duration: 40000000000000 });
2024-03-13 12:02:08 -05:00
await expect(init()).to.be.revertedWith('Duration is too long');
});
it('should not be able to initiate a validation pool with bindingPercent above 100', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ bindingPercent: 101 });
await expect(init()).to.be.revertedWith('Binding percent must be <= 100');
});
2024-03-11 13:39:56 -05:00
it('should be able to initiate a second validation pool', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool();
2024-03-11 13:39:56 -05:00
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(1);
expect(await dao.validationPoolCount()).to.equal(2);
});
2024-03-10 11:55:59 -05:00
2024-03-11 13:39:56 -05:00
it('Should be able to fetch pool instance', async () => {
const pool = await dao.validationPools(0);
expect(pool).to.exist;
2024-03-29 18:08:30 -05:00
expect(pool.params.duration).to.equal(POOL_DURATION);
2024-03-12 17:53:04 -05:00
expect(pool.postIndex).to.equal(0);
expect(pool.resolved).to.be.false;
expect(pool.sender).to.equal(account1);
2024-03-11 13:39:56 -05:00
});
2024-03-10 11:55:59 -05:00
});
describe('Stake', async () => {
beforeEach(async () => {
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(0);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(0);
2024-03-26 18:58:00 -05:00
await initiateValidationPool();
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
});
it('should be able to stake before validation pool has elapsed', async () => {
await dao.stakeOnValidationPool(1, 10, true);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(1)).to.emit(dao, 'ValidationPoolResolved').withArgs(1, true, true);
expect(await dao.balanceOf(account1)).to.equal(200);
expect(await dao.balanceOf(dao.target)).to.equal(0);
});
it('should not be able to stake after validation pool has elapsed', async () => {
time.increase(POOL_DURATION + 1);
await expect(dao.stakeOnValidationPool(1, 10, true)).to.be.revertedWith('Pool end time has passed');
});
it('should be able to stake against a validation pool', async () => {
await dao.stakeOnValidationPool(1, 10, false);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(1)).to.emit(dao, 'ValidationPoolResolved').withArgs(1, false, true);
expect(await dao.balanceOf(account1)).to.equal(200);
expect(await dao.balanceOf(dao.target)).to.equal(0);
const pool = await dao.validationPools(1);
expect(pool.outcome).to.be.false;
});
});
2024-03-11 13:39:56 -05:00
describe('Evaluate outcome', () => {
it('should not be able to evaluate outcome before duration has elapsed if not all rep has been staked', async () => {
time.increase(POOL_DURATION + 1);
await expect(dao.evaluateOutcome(0));
await initiateValidationPool({ fee: 100 });
await expect(dao.evaluateOutcome(1)).to.be.revertedWith('Pool end time has not yet arrived');
});
it('should not be able to evaluate outcome before duration has elapsed unless all rep has been staked', async () => {
time.increase(POOL_DURATION + 1);
await expect(dao.evaluateOutcome(0));
await initiateValidationPool({ fee: 100 });
await dao.stakeOnValidationPool(1, 100, true);
await expect(dao.evaluateOutcome(1)).to.emit(dao, 'ValidationPoolResolved').withArgs(1, true, true);
2024-03-11 13:39:56 -05:00
});
2024-03-10 11:55:59 -05:00
2024-03-11 13:39:56 -05:00
it('should be able to evaluate outcome after duration has elapsed', async () => {
expect(await dao.balanceOf(dao.target)).to.equal(100);
2024-03-11 13:39:56 -05:00
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(0)).to.emit(dao, 'ValidationPoolResolved').withArgs(0, true, true);
2024-03-11 13:39:56 -05:00
expect(await dao.memberCount()).to.equal(1);
expect(await dao.balanceOf(account1)).to.equal(100);
2024-03-12 17:53:04 -05:00
const pool = await dao.validationPools(0);
expect(pool.resolved).to.be.true;
expect(pool.outcome).to.be.true;
2024-03-11 13:39:56 -05:00
});
2024-03-10 11:55:59 -05:00
2024-03-11 13:39:56 -05:00
it('should not be able to evaluate outcome more than once', async () => {
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(0)).to.emit(dao, 'ValidationPoolResolved').withArgs(0, true, true);
2024-03-11 13:39:56 -05:00
await expect(dao.evaluateOutcome(0)).to.be.revertedWith('Pool is already resolved');
});
it('should be able to evaluate outcome of second validation pool', async () => {
2024-03-18 14:03:53 -05:00
const init = () => dao.initiateValidationPool(
0,
POOL_DURATION,
2024-03-28 15:06:14 -05:00
[1, 3],
[1, 2],
100,
true,
2024-03-18 14:03:53 -05:00
false,
2024-03-26 18:58:00 -05:00
emptyCallbackData,
2024-03-18 14:03:53 -05:00
{ value: POOL_FEE },
);
2024-03-11 13:39:56 -05:00
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(1);
expect(await dao.validationPoolCount()).to.equal(2);
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(0)).to.emit(dao, 'ValidationPoolResolved').withArgs(0, true, true);
expect(await dao.balanceOf(account1)).to.equal(100);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(1)).to.emit(dao, 'ValidationPoolResolved').withArgs(1, true, true);
2024-03-11 13:39:56 -05:00
expect(await dao.balanceOf(account1)).to.equal(200);
});
2024-03-26 15:20:54 -05:00
it('should not be able to evaluate outcome if quorum is not met', async () => {
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(0)).to.emit(dao, 'ValidationPoolResolved').withArgs(0, true, true);
2024-03-26 15:20:54 -05:00
2024-03-28 15:06:14 -05:00
const init = () => initiateValidationPool({ quorum: [1, 1] });
2024-03-26 15:20:54 -05:00
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(1);
expect(await dao.validationPoolCount()).to.equal(2);
time.increase(POOL_DURATION + 1);
2024-03-27 14:03:57 -05:00
await expect(dao.evaluateOutcome(1)).to.emit(dao, 'ValidationPoolResolved').withArgs(1, false, false);
2024-03-26 15:20:54 -05:00
});
2024-03-10 11:55:59 -05:00
describe('Validation pool options', () => {
beforeEach(async () => {
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(0);
await dao.addPost(account2, 'content-id');
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ postIndex: 1 });
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(1);
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(1);
});
2024-03-11 13:39:56 -05:00
it('Binding validation pool should redistribute stakes', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool();
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(2);
await dao.connect(account1).stakeOnValidationPool(2, 10, true);
await dao.connect(account2).stakeOnValidationPool(2, 10, false);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(2);
expect(await dao.balanceOf(account1)).to.equal(210);
expect(await dao.balanceOf(account2)).to.equal(90);
expect(await dao.balanceOf(dao.target)).to.equal(0);
});
2024-03-11 13:39:56 -05:00
it('Non binding validation pool should not redistribute stakes', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ bindingPercent: 0 });
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(2);
await dao.connect(account1).stakeOnValidationPool(2, 10, true);
await dao.connect(account2).stakeOnValidationPool(2, 10, false);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(2);
expect(await dao.balanceOf(account1)).to.equal(200);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(0);
});
2024-03-11 13:39:56 -05:00
it('Partially binding validation pool should redistribute some stakes', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({ bindingPercent: 50 });
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(2);
await dao.connect(account1).stakeOnValidationPool(2, 10, true);
await dao.connect(account2).stakeOnValidationPool(2, 10, false);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(2);
expect(await dao.balanceOf(account1)).to.equal(205);
expect(await dao.balanceOf(account2)).to.equal(95);
expect(await dao.balanceOf(dao.target)).to.equal(0);
expect(await dao.totalSupply()).to.equal(300);
});
it('If redistributeLosingStakes is false, validation pool should burn binding portion of losing stakes', async () => {
2024-03-26 18:58:00 -05:00
const init = () => initiateValidationPool({
bindingPercent: 50,
redistributeLosingStakes: false,
});
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(2);
await dao.connect(account1).stakeOnValidationPool(2, 10, true);
await dao.connect(account2).stakeOnValidationPool(2, 10, false);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(2);
expect(await dao.balanceOf(account1)).to.equal(200);
expect(await dao.balanceOf(account2)).to.equal(95);
expect(await dao.balanceOf(dao.target)).to.equal(0);
expect(await dao.totalSupply()).to.equal(295);
});
2024-03-29 18:08:30 -05:00
it('If redistributeLosingStakes is false and bindingPercent is 0, accounts should recover initial balances', async () => {
const init = () => initiateValidationPool({
bindingPercent: 0,
redistributeLosingStakes: false,
});
await expect(init()).to.emit(dao, 'ValidationPoolInitiated').withArgs(2);
await dao.connect(account1).stakeOnValidationPool(2, 10, true);
await dao.connect(account2).stakeOnValidationPool(2, 10, false);
expect(await dao.balanceOf(account1)).to.equal(100);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(100);
2024-03-29 18:08:30 -05:00
time.increase(POOL_DURATION + 1);
await dao.evaluateOutcome(2);
expect(await dao.balanceOf(account1)).to.equal(200);
expect(await dao.balanceOf(account2)).to.equal(100);
expect(await dao.balanceOf(dao.target)).to.equal(0);
expect(await dao.totalSupply()).to.equal(300);
});
2024-03-11 13:39:56 -05:00
});
2024-03-05 21:21:27 -06:00
});
});
});