From 51892f1808aaa4ee5793081ff2dea3eef7c0ac5f Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Wed, 30 Aug 2023 09:59:15 -0700 Subject: [PATCH 1/3] Added address(0) protections for mint and transfer --- src/TwabController.sol | 9 +++++++++ test/TwabController.t.sol | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/TwabController.sol b/src/TwabController.sol index d53d41d..fa68b2c 100644 --- a/src/TwabController.sol +++ b/src/TwabController.sol @@ -18,6 +18,9 @@ error PeriodLengthTooShort(); /// @param periodOffset The period offset that was passed in error PeriodOffsetInFuture(uint48 periodOffset); +/// @notice Emitted when a user to tries to mint or transfer to the zero address +error TransferToZeroAddress(); + // The minimum period length uint48 constant MINIMUM_PERIOD_LENGTH = 1 hours; @@ -447,6 +450,9 @@ contract TwabController { * @param _amount The amount to mint */ function mint(address _to, uint112 _amount) external { + if (_to == address(0)) { + revert TransferToZeroAddress(); + } _transferBalance(msg.sender, address(0), _to, _amount); } @@ -471,6 +477,9 @@ contract TwabController { * @param _amount The amount to transfer */ function transfer(address _from, address _to, uint112 _amount) external { + if (_to == address(0)) { + revert TransferToZeroAddress(); + } _transferBalance(msg.sender, _from, _to, _amount); } diff --git a/test/TwabController.t.sol b/test/TwabController.t.sol index f0eeb6a..8acee6d 100644 --- a/test/TwabController.t.sol +++ b/test/TwabController.t.sol @@ -11,6 +11,7 @@ import { MINIMUM_PERIOD_LENGTH, PeriodLengthTooShort, SPONSORSHIP_ADDRESS, + TransferToZeroAddress, PeriodOffsetInFuture } from "../src/TwabController.sol"; import { @@ -735,6 +736,23 @@ contract TwabControllerTest is BaseTest { assertEq(twabController.delegateOf(mockVault, alice), bob); } + function testMint_toZero() public { + vm.startPrank(mockVault); + + uint96 _amount = 1000e18; + vm.expectRevert(abi.encodeWithSelector(TransferToZeroAddress.selector)); + twabController.mint(address(0), _amount); + } + + function testTransfer_toZero() public { + vm.startPrank(mockVault); + + uint96 _amount = 1000e18; + twabController.mint(alice, _amount); + vm.expectRevert(abi.encodeWithSelector(TransferToZeroAddress.selector)); + twabController.transfer(alice, address(0), _amount); + } + function testDelegate_toSelf() public { vm.startPrank(mockVault); From 8bb13a00814e3b8c0e74608629080f3e2af5fb93 Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Wed, 30 Aug 2023 10:10:13 -0700 Subject: [PATCH 2/3] Update src/TwabController.sol Co-authored-by: Pierrick Turelier --- src/TwabController.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TwabController.sol b/src/TwabController.sol index fa68b2c..6a3ab95 100644 --- a/src/TwabController.sol +++ b/src/TwabController.sol @@ -18,7 +18,7 @@ error PeriodLengthTooShort(); /// @param periodOffset The period offset that was passed in error PeriodOffsetInFuture(uint48 periodOffset); -/// @notice Emitted when a user to tries to mint or transfer to the zero address +/// @notice Emitted when a user tries to mint or transfer to the zero address error TransferToZeroAddress(); // The minimum period length From 49240b4fdf715f258104168da5321d675c6af003 Mon Sep 17 00:00:00 2001 From: Brendan Asselstine Date: Wed, 30 Aug 2023 10:14:29 -0700 Subject: [PATCH 3/3] Moved natspec --- src/TwabController.sol | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/TwabController.sol b/src/TwabController.sol index 6a3ab95..9ebf77c 100644 --- a/src/TwabController.sol +++ b/src/TwabController.sol @@ -24,6 +24,7 @@ error TransferToZeroAddress(); // The minimum period length uint48 constant MINIMUM_PERIOD_LENGTH = 1 hours; +// Allows users to revoke their chances to win by delegating to the sponsorship address. address constant SPONSORSHIP_ADDRESS = address(1); /** @@ -40,8 +41,6 @@ address constant SPONSORSHIP_ADDRESS = address(1); */ contract TwabController { using SafeCast for uint256; - - /// @notice Allows users to revoke their chances to win by delegating to the sponsorship address. /// @notice Sets the minimum period length for Observations. When a period elapses, a new Observation is recorded, otherwise the most recent Observation is updated. uint48 public immutable PERIOD_LENGTH;