Skip to content

Commit

Permalink
move all after checks to CWToken
Browse files Browse the repository at this point in the history
  • Loading branch information
ihoroleksiienko committed Jul 3, 2024
1 parent 80bdba3 commit ac8109d
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 93 deletions.
7 changes: 0 additions & 7 deletions contracts/BRLCTokenBridgeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,4 @@ contract BRLCTokenBridgeable is ERC20Base, ERC20Bridgeable, ERC20Freezable {
) internal virtual override(ERC20Base, ERC20Freezable) {
super._afterTokenTransfer(from, to, amount);
}

/**
* @inheritdoc ERC20Freezable
*/
function _balanceOf_ERC20Freezable(address account) internal view override returns (uint256) {
return balanceOf(account);
}
}
47 changes: 20 additions & 27 deletions contracts/base/CWToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ contract CWToken is
uint256 amount
) internal virtual override(ERC20Base, ERC20Mintable, ERC20Freezable, ERC20Restrictable, ERC20Hookable) {
super._afterTokenTransfer(from, to, amount);

uint256 balanceTotal = balanceOf(from);
uint256 balanceFrozen = balanceOfFrozen(from);
uint256 balancePreminted = balanceOfPremint(from);
uint256 balanceRestricted = balanceOfRestricted(from, bytes32(0));

if (balanceTotal < balanceFrozen + balancePreminted + balanceRestricted) {
uint256 balanceFreezable = (balanceTotal >= balancePreminted) ? balanceTotal - balancePreminted : 0;

if (balancePreminted != 0 && balanceTotal < balancePreminted) {
revert TransferExceededPremintedAmount();
} else if (balanceFrozen != 0 &&
msg.sig != this.transferFrozen.selector &&
balanceFreezable < balanceFrozen
) {
revert TransferExceededFrozenAmount();
} else if (balanceRestricted != 0) {
revert TransferExceededRestrictedAmount();
}
}
}

/**
Expand All @@ -135,31 +155,4 @@ contract CWToken is
) public view override(ERC20Base, ERC20Trustable) returns (uint256) {
return super.allowance(owner, spender);
}

/**
* @inheritdoc ERC20Mintable
*/
function _balanceOf_ERC20Mintable(address account) internal view virtual override returns (uint256) {
return balanceOf(account);
}

/**
* @inheritdoc ERC20Freezable
*/
function _balanceOf_ERC20Freezable(address account) internal view virtual override returns (uint256) {
return balanceOf(account) - balanceOfPremint(account);
}

/**
* @inheritdoc ERC20Restrictable
*/
function _balanceOf_ERC20Restrictable(address account) internal view virtual override returns (uint256) {
uint256 frozenBalance = balanceOfFrozen(account);
uint256 restBalance = balanceOf(account) - balanceOfPremint(account);
if (frozenBalance < restBalance) {
return restBalance - frozenBalance;
} else {
return 0;
}
}
}
13 changes: 0 additions & 13 deletions contracts/base/ERC20Freezable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,11 @@ abstract contract ERC20Freezable is ERC20Base, IERC20Freezable {
return balanceOfFrozen(account);
}

/**
* @notice Returns the transferable amount of tokens owned by account
*
* @param account The account to get the balance of
*/
function _balanceOf_ERC20Freezable(address account) internal view virtual returns (uint256);

/**
* @inheritdoc ERC20Base
*/
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._afterTokenTransfer(from, to, amount);
uint256 frozen = _frozenBalances[from];
if (frozen != 0 && msg.sig != this.transferFrozen.selector) {
if (_balanceOf_ERC20Freezable(from) < frozen) {
revert TransferExceededFrozenAmount();
}
}
}

/**
Expand Down
13 changes: 0 additions & 13 deletions contracts/base/ERC20Mintable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -420,12 +420,6 @@ abstract contract ERC20Mintable is ERC20Base, IERC20Mintable {
*/
function _afterTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._afterTokenTransfer(from, to, amount);
uint256 preminted = balanceOfPremint(from);
if (preminted != 0) {
if (_balanceOf_ERC20Mintable(from) < preminted) {
revert TransferExceededPremintedAmount();
}
}
}

function _getExtendedStorageSlot() internal pure returns (ExtendedStorageSlot storage r) {
Expand Down Expand Up @@ -563,11 +557,4 @@ abstract contract ERC20Mintable is ERC20Base, IERC20Mintable {
}
premintRecords.pop();
}

/**
* @notice Returns the transferable amount of tokens owned by account
*
* @param account The account to get the balance of
*/
function _balanceOf_ERC20Mintable(address account) internal view virtual returns (uint256);
}
11 changes: 0 additions & 11 deletions contracts/base/ERC20Restrictable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,10 @@ abstract contract ERC20Restrictable is ERC20Base, IERC20Restrictable {
}
}

if (_balanceOf_ERC20Restrictable(from) < restrictedBalance) {
revert TransferExceededRestrictedAmount();
}

_totalRestrictedBalances[from] = restrictedBalance;
}
}

/**
* @notice Returns the transferable amount of tokens owned by account
*
* @param account The account to get the balance of
*/
function _balanceOf_ERC20Restrictable(address account) internal view virtual returns (uint256);

/**
* @dev This empty reserved space is put in place to allow future versions
* to add new variables without shifting down storage in the inheritance chain
Expand Down
7 changes: 0 additions & 7 deletions contracts/mocks/base/ERC20FreezableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,4 @@ contract ERC20FreezableMock is ERC20Freezable {
_mint(account, amount);
return true;
}

/**
* @inheritdoc ERC20Freezable
*/
function _balanceOf_ERC20Freezable(address account) internal view virtual override returns (uint256) {
return balanceOf(account);
}
}
8 changes: 0 additions & 8 deletions contracts/mocks/base/ERC20MintableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ contract ERC20MintableMock is ERC20Mintable {
*/
function initialize(string memory name_, string memory symbol_) public initializer {
__ERC20Mintable_init(name_, symbol_);
_balanceOf_ERC20Mintable(address(0)); // To ensure 100% coverage
}

/**
Expand All @@ -53,11 +52,4 @@ contract ERC20MintableMock is ERC20Mintable {
function call_parent_initialize_unchained() public {
__ERC20Mintable_init_unchained();
}

/**
* @inheritdoc ERC20Mintable
*/
function _balanceOf_ERC20Mintable(address account) internal view virtual override returns (uint256) {
return balanceOf(account);
}
}
7 changes: 0 additions & 7 deletions contracts/mocks/base/ERC20RestrictableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,4 @@ contract ERC20RestrictableMock is ERC20Restrictable {
_mint(account, amount);
return true;
}

/**
* @inheritdoc ERC20Restrictable
*/
function _balanceOf_ERC20Restrictable(address account) internal view virtual override returns (uint256) {
return balanceOf(account);
}
}

0 comments on commit ac8109d

Please sign in to comment.