generated from AngleProtocol/boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: base of vedelegation invariant tests #12
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa1b389
feat: base of vedelegation invariant tests
0xtekgrinder 8cda491
refactor: improve the invariant tests
0xtekgrinder 4b81e53
refactor: doesn't use anymore the lockAmounts
0xtekgrinder c3f7ef1
wip: delegator invariant failing
0xtekgrinder 81ed380
tests: use TimestampStore for invariant tests
0xtekgrinder 61e3f12
tests: add Param handler
0xtekgrinder 18ecf53
tests: add extendLock amount and duration for delegation
0xtekgrinder 8dda1d8
chore: remove useless params in foundry.toml
0xtekgrinder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
|
||
pragma solidity ^0.8.19; | ||
|
||
import { IERC20 } from "oz/token/ERC20/IERC20.sol"; | ||
import { IERC20Metadata } from "oz/token/ERC20/extensions/IERC20Metadata.sol"; | ||
import "oz/utils/Strings.sol"; | ||
import { Delegator } from "./actors/Delegator.t.sol"; | ||
import { Fixture, AngleGovernor } from "../Fixture.t.sol"; | ||
|
||
//solhint-disable | ||
import { console } from "forge-std/console.sol"; | ||
|
||
contract DelegationInvariants is Fixture { | ||
uint256 internal constant _NUM_DELEGATORS = 10; | ||
|
||
Delegator internal _delegatorHandler; | ||
|
||
function setUp() public virtual override { | ||
super.setUp(); | ||
|
||
_delegatorHandler = new Delegator(_NUM_DELEGATORS, ANGLE, address(veANGLE), address(token)); | ||
|
||
// Label newly created addresses | ||
for (uint256 i; i < _NUM_DELEGATORS; i++) | ||
vm.label(_delegatorHandler.actors(i), string.concat("Delegator ", Strings.toString(i))); | ||
|
||
targetContract(address(_delegatorHandler)); | ||
|
||
{ | ||
bytes4[] memory selectors = new bytes4[](6); | ||
selectors[0] = Delegator.delegate.selector; | ||
selectors[1] = Delegator.createLock.selector; | ||
selectors[2] = Delegator.extandLockTime.selector; | ||
selectors[3] = Delegator.extandLockAmount.selector; | ||
selectors[4] = Delegator.wrap.selector; | ||
selectors[5] = Delegator.withdraw.selector; | ||
targetSelector(FuzzSelector({ addr: address(_delegatorHandler), selectors: selectors })); | ||
} | ||
} | ||
|
||
function invariant_wow() public { | ||
for (uint256 i; i < _NUM_DELEGATORS; i++) { | ||
address actor = _delegatorHandler.actors(i); | ||
uint256 votes = token.getVotes(actor); | ||
|
||
assertEq(token.delegates(actor), _delegatorHandler.delegations(actor)); | ||
if (_delegatorHandler.delegations(actor) != address(0)) { | ||
assertEq(votes, 0, "Delegator should not have votes"); | ||
} else { | ||
Delegator.Lock memory lock = _delegatorHandler.locks(actor); | ||
if (lock.end > block.timestamp) { | ||
assertEq(votes, lock.amount, "Delegator should have votes"); | ||
} else { | ||
assertEq(votes, 0, "Delegator should not have votes"); | ||
} | ||
} | ||
} | ||
for (uint256 i; i < _delegatorHandler.delegateesLength(); i++) { | ||
address delegatee = _delegatorHandler.delegatees(i); | ||
uint256 votes = token.getVotes(delegatee); | ||
|
||
uint256 amount = 0; | ||
address[] memory delegators = _delegatorHandler.reverseDelegationsView(delegatee); | ||
for (uint256 j; j < delegators.length; j++) { | ||
address delegator = delegators[j]; | ||
Delegator.Lock memory lock = _delegatorHandler.locks(delegator); | ||
if (lock.end > block.timestamp) amount += lock.amount; | ||
} | ||
assertEq(votes, _delegatorHandler.delegationAmounts(delegatee), "Delegatee should have votes"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
pragma solidity ^0.8.19; | ||
|
||
import "./BaseActor.t.sol"; | ||
import { IERC5805 } from "oz/interfaces/IERC5805.sol"; | ||
import { MockANGLE } from "../../external/MockANGLE.sol"; | ||
import "contracts/interfaces/IveANGLE.sol"; | ||
import "contracts/utils/Errors.sol"; | ||
import { console } from "forge-std/console.sol"; | ||
|
||
contract Delegator is BaseActor { | ||
IveANGLE public veToken; | ||
IERC5805 public veDelegation; | ||
|
||
struct Lock { | ||
uint256 amount; | ||
uint256 end; | ||
} | ||
|
||
mapping(address => uint256) public delegationAmounts; | ||
mapping(address => address) public delegations; | ||
mapping(address => address[]) public reverseDelegations; | ||
mapping(address => Lock) public _locks; | ||
address[] public delegatees; | ||
|
||
constructor( | ||
uint256 _nbrActor, | ||
IERC20 _agToken, | ||
address _veToken, | ||
address _veDelegation | ||
) BaseActor(_nbrActor, "Delegator", _agToken) { | ||
veToken = IveANGLE(_veToken); | ||
veDelegation = IERC5805(_veDelegation); | ||
} | ||
|
||
function locks(address locker) public view returns (Lock memory) { | ||
return _locks[locker]; | ||
} | ||
|
||
function reverseDelegationsView(address locker) public view returns (address[] memory) { | ||
return reverseDelegations[locker]; | ||
} | ||
|
||
function delegateesLength() public view returns (uint256) { | ||
return delegatees.length; | ||
} | ||
|
||
function delegate(uint256 acordIndex, address toDelegate) public useActor(acordIndex) { | ||
if (toDelegate == address(0)) return; | ||
|
||
uint256 balance = veToken.balanceOf(_currentActor); | ||
address currentDelegatee = delegations[_currentActor]; | ||
|
||
if (balance == 0) { | ||
return; | ||
} | ||
|
||
veDelegation.delegate(toDelegate); | ||
|
||
// Update delegations | ||
delegations[_currentActor] = toDelegate; | ||
for (uint256 i; i < delegatees.length; i++) { | ||
if (delegatees[i] == toDelegate) { | ||
return; | ||
} | ||
} | ||
delegatees.push(toDelegate); | ||
reverseDelegations[toDelegate].push(_currentActor); | ||
0xtekgrinder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (uint256 i; i < reverseDelegations[currentDelegatee].length; i++) { | ||
if (reverseDelegations[currentDelegatee][i] == _currentActor) { | ||
reverseDelegations[currentDelegatee][i] = reverseDelegations[currentDelegatee][ | ||
reverseDelegations[currentDelegatee].length - 1 | ||
]; | ||
reverseDelegations[currentDelegatee].pop(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
function createLock(uint256 acordIndex, uint256 amount, uint256 duration) public useActor(acordIndex) { | ||
0xtekgrinder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (veToken.locked__end(_currentActor) != 0) { | ||
return; | ||
} | ||
duration = bound(duration, 1 weeks, 365 days * 4); | ||
amount = bound(amount, 100e18, 1_500e18); | ||
|
||
MockANGLE(address(agToken)).mint(_currentActor, amount); | ||
agToken.approve(address(veToken), amount); | ||
|
||
veToken.create_lock(amount, block.timestamp + duration); | ||
|
||
_locks[_currentActor] = Lock({ | ||
amount: veToken.balanceOf(_currentActor), | ||
end: veToken.locked__end(_currentActor) | ||
}); | ||
} | ||
|
||
function withdraw() public { | ||
if (veToken.locked__end(_currentActor) != 0 && veToken.locked__end(_currentActor) < block.timestamp) { | ||
veToken.withdraw(); | ||
|
||
_locks[_currentActor] = Lock({ amount: 0, end: 0 }); | ||
} | ||
} | ||
|
||
function extandLockTime(uint256 acordIndex, uint256 duration) public useActor(acordIndex) { | ||
0xtekgrinder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uint256 end = veToken.locked__end(_currentActor); | ||
if (end == 0 || end + 1 weeks > block.timestamp + 365 days * 4) { | ||
return; | ||
} | ||
|
||
duration = bound(duration, end + 1 weeks, block.timestamp + 365 days * 4); | ||
veToken.increase_unlock_time(duration); | ||
_locks[_currentActor].end = veToken.locked__end(_currentActor); | ||
} | ||
|
||
function extandLockAmount(uint256 acordIndex, uint256 amount) public useActor(acordIndex) { | ||
if (veToken.balanceOf(_currentActor) == 0) { | ||
return; | ||
} | ||
amount = bound(amount, 100e18, 1_500e18); | ||
|
||
MockANGLE(address(agToken)).mint(_currentActor, amount); | ||
agToken.approve(address(veToken), amount); | ||
veToken.increase_amount(amount); | ||
|
||
_locks[_currentActor].amount = veToken.balanceOf(_currentActor); | ||
} | ||
|
||
function wrap(uint256 timestamp) public { | ||
0xtekgrinder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
timestamp = bound(timestamp, block.timestamp, 365 days * 5); | ||
vm.warp(timestamp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this one you can also compare to what voting power there is in the veANGLE
Also totalSupply == sum all delegate (also the self delegation)