Skip to content

Commit

Permalink
fix(TWABDelegator): check contract address
Browse files Browse the repository at this point in the history
  • Loading branch information
PierrickGT committed Mar 2, 2022
1 parent 73d9c5a commit 68bfa1e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions contracts/TWABDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ contract TWABDelegator is ERC20, LowLevelDelegator, PermitAndMulticall {
* @param _amount Amount of tickets to stake
*/
function stake(address _to, uint256 _amount) external {
_requireRecipientNotContractAddress(_to);
_requireAmountGtZero(_amount);

IERC20(ticket).safeTransferFrom(msg.sender, address(this), _amount);
Expand All @@ -203,6 +204,7 @@ contract TWABDelegator is ERC20, LowLevelDelegator, PermitAndMulticall {
*/
function unstake(address _to, uint256 _amount) external {
_requireRecipientNotZeroAddress(_to);
_requireRecipientNotContractAddress(_to);
_requireAmountGtZero(_amount);

_burn(msg.sender, _amount);
Expand Down Expand Up @@ -606,6 +608,14 @@ contract TWABDelegator is ERC20, LowLevelDelegator, PermitAndMulticall {
require(_amount > 0, "TWABDelegator/amount-gt-zero");
}

/**
* @notice Require to verify that `_to` is not this contract address.
* @param _to Address to check
*/
function _requireRecipientNotContractAddress(address _to) internal view {
require(_to != address(this), "TWABDelegator/to-not-this-addr");
}

/**
* @notice Require to verify that `_to` is not address zero.
* @param _to Address to check
Expand Down
12 changes: 12 additions & 0 deletions test/TWABDelegator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ describe('Test Set Name', () => {
expect(await twabDelegator.balanceOf(stranger.address)).to.eq(amount);
});

it('should fail to stake tickets if recipient is contract address', async () => {
await expect(twabDelegator.stake(twabDelegator.address, amount)).to.be.revertedWith(
'TWABDelegator/to-not-this-addr',
);
});

it('should fail to stake tickets if recipient is address zero', async () => {
await expect(twabDelegator.stake(AddressZero, amount)).to.be.revertedWith(
'ERC20: mint to the zero address',
Expand Down Expand Up @@ -189,6 +195,12 @@ describe('Test Set Name', () => {
);
});

it('should fail to unstake if recipient is the contract address', async () => {
await expect(twabDelegator.unstake(twabDelegator.address, amount)).to.be.revertedWith(
'TWABDelegator/to-not-this-addr',
);
});

it('should fail to unstake if amount is zero', async () => {
await twabDelegator.stake(owner.address, amount);

Expand Down

0 comments on commit 68bfa1e

Please sign in to comment.