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

add additional unit tests demonstrating delegation flows #138

Merged
merged 1 commit into from
Dec 20, 2023
Merged
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
100 changes: 100 additions & 0 deletions test/unit/AjnaToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,106 @@ contract AjnaTokenTest is Test {
assertEq(_token.getPastVotes(delegate2, 11112), 1_000 * 1e18);
}

function testMultipleSelfDelegation() external {
address delegator = address(2222);
address delegate1 = address(3333);

// set delegator balance to 1_000 AJNA tokens
deal(address(_token), delegator, 1_000 * 1e18);

// check voting power
assertEq(_token.getVotes(delegator), 0);
assertEq(_token.getVotes(delegate1), 0);

vm.roll(11111);
changePrank(delegator);
vm.expectEmit(true, true, false, true);
emit DelegateChanged(delegator, address(0), delegator);
vm.expectEmit(true, true, false, true);
emit DelegateVotesChanged(delegator, 0, 1_000 * 1e18);
_token.delegate(delegator);

// check accounts balances
assertEq(_token.balanceOf(delegator), 1_000 * 1e18);
assertEq(_token.balanceOf(delegate1), 0);
// check voting power
assertEq(_token.getVotes(delegator), 1_000 * 1e18);
assertEq(_token.getVotes(delegate1), 0);

assertEq(_token.delegates(delegator), delegator);

// additional tokens are transferred to delegator
vm.roll(11121);
changePrank(_tokenHolder);
uint256 addAmount = 1 * 1e18;
_token.approve(_tokenHolder, addAmount);
vm.expectEmit(true, true, false, true);
emit Transfer(_tokenHolder, delegator, addAmount);
vm.expectEmit(true, true, false, true);
emit DelegateVotesChanged(delegator, 1_000 * 1e18, 1_001 * 1e18);
_token.transferFrom(_tokenHolder, delegator, addAmount);

// check accounts balances
assertEq(_token.balanceOf(delegator), 1_001 * 1e18);
assertEq(_token.balanceOf(delegate1), 0);
// check voting power
assertEq(_token.getVotes(delegator), 1_001 * 1e18);
assertEq(_token.getVotes(delegate1), 0);
}

function testMultipleDelegationDifferentDelegatee() external {
address delegator = address(2222);
address delegate1 = address(3333);
address delegate2 = address(4444);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the role of delegate2 in this test? Nobody delegates to this actor, and we confirm their voting power is 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, same question as Ed


// set delegator balance to 1_000 AJNA tokens
deal(address(_token), delegator, 1_000 * 1e18);

// check voting power
assertEq(_token.getVotes(delegator), 0);
assertEq(_token.getVotes(delegate1), 0);
assertEq(_token.getVotes(delegate2), 0);

vm.roll(11111);
changePrank(delegator);
vm.expectEmit(true, true, false, true);
emit DelegateChanged(delegator, address(0), delegate1);
vm.expectEmit(true, true, false, true);
emit DelegateVotesChanged(delegate1, 0, 1_000 * 1e18);
_token.delegate(delegate1);

// check accounts balances
assertEq(_token.balanceOf(delegator), 1_000 * 1e18);
assertEq(_token.balanceOf(delegate1), 0);
assertEq(_token.balanceOf(delegate2), 0);
// check voting power
assertEq(_token.getVotes(delegator), 0);
assertEq(_token.getVotes(delegate1), 1_000 * 1e18);
assertEq(_token.getVotes(delegate2), 0);

assertEq(_token.delegates(delegator), delegate1);

// additional tokens are transferred to delegator
vm.roll(11121);
changePrank(_tokenHolder);
uint256 addAmount = 2_000 * 1e18;
_token.approve(_tokenHolder, addAmount);
vm.expectEmit(true, true, false, true);
emit Transfer(_tokenHolder, delegator, addAmount);
vm.expectEmit(true, true, false, true);
emit DelegateVotesChanged(delegate1, 1_000 * 1e18, 3_000 * 1e18);
_token.transferFrom(_tokenHolder, delegator, addAmount);

// check accounts balances
assertEq(_token.balanceOf(delegator), 3_000 * 1e18);
assertEq(_token.balanceOf(delegate1), 0);
assertEq(_token.balanceOf(delegate2), 0);
// check voting power
assertEq(_token.getVotes(delegator), 0);
assertEq(_token.getVotes(delegate1), 3_000 * 1e18);
assertEq(_token.getVotes(delegate2), 0);
}

function testCalculateVotingPower() external {
address delegator1 = address(1111);
address delegator2 = address(2222);
Expand Down
Loading