Skip to content

Commit

Permalink
Merge pull request #19 from GenerationSoftware/gen-574-c4-mr-39-new-u…
Browse files Browse the repository at this point in the history
…ndelegation-logic-is-broken-it-wont-work-if

Cleaned up sponsorship comments
  • Loading branch information
asselstine authored Aug 30, 2023
2 parents 3985dbf + 3309233 commit 7c62ba6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/TwabController.sol
Original file line number Diff line number Diff line change
Expand Up @@ -636,15 +636,15 @@ contract TwabController {

/**
* @notice Sets a delegate for a user which forwards the delegateBalance tied to the user's
* balance to the delegate's delegateBalance.
* balance to the delegate's delegateBalance. "Sponsoring" means the funds aren't delegated
* to anyone; this can be done by passing address(0) or the SPONSORSHIP_ADDRESS as the delegate.
* @param _vault The vault for which the delegate is being set
* @param _from the address to delegate from
* @param _to the address to delegate to
*/
function _delegate(address _vault, address _from, address _to) internal {
address _currentDelegate = _delegateOf(_vault, _from);
// Treat address(0) as un-delegating. They could also pass the sponsorship address, but this lets
// them do so without knowing the sponsorship special value address.
// address(0) is interpreted as sponsoring, so they don't need to know the sponsorship address.
address to = _to == address(0) ? SPONSORSHIP_ADDRESS : _to;
if (to == _currentDelegate) {
revert SameDelegateAlreadySet(to);
Expand Down
34 changes: 34 additions & 0 deletions test/TwabController.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,40 @@ contract TwabControllerTest is BaseTest {
vm.stopPrank();
}

function testDelegateOf_default() public {
assertEq(twabController.delegateOf(mockVault, alice), alice);
}

function testDelegateOf_sponsorship() public {
vm.startPrank(mockVault);
twabController.sponsor(alice);
assertEq(twabController.delegateOf(mockVault, alice), SPONSORSHIP_ADDRESS);
}

function testDelegateOf_addressZero() public {
vm.startPrank(alice);
twabController.delegate(mockVault, address(0));
assertEq(twabController.delegateOf(mockVault, alice), SPONSORSHIP_ADDRESS);
}

function testDelegateOf_address() public {
address bob = makeAddr("bob");
vm.startPrank(alice);
twabController.delegate(mockVault, bob);
assertEq(twabController.delegateOf(mockVault, alice), bob);
}

function testDelegate_toSelf() public {
vm.startPrank(mockVault);

uint96 _amount = 1000e18;
twabController.mint(alice, _amount);

assertEq(twabController.delegateOf(mockVault, alice), alice);
assertEq(twabController.delegateBalanceOf(mockVault, alice), _amount);
assertEq(twabController.balanceOf(mockVault, alice), _amount);
}

function testDelegate_interpretBurnAddressAsSponsorship() external {
uint96 _amount = 1000e18;
twabController.mint(alice, _amount);
Expand Down

0 comments on commit 7c62ba6

Please sign in to comment.