Skip to content

Commit

Permalink
chore: Rename balances state variable of ERC20 to reflect its extra d…
Browse files Browse the repository at this point in the history
…ata stored in its bits
  • Loading branch information
peculiarity committed Jul 16, 2024
1 parent 6d7f242 commit 104aa87
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
20 changes: 10 additions & 10 deletions src/token/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract ERC20 is Auth, IERC20Metadata, IERC20Permit {
/// @inheritdoc IERC20
uint256 public totalSupply;

mapping(address => uint256) internal balances;
mapping(address => uint256) internal balancesWithMetadata;
/// @inheritdoc IERC20
mapping(address => mapping(address => uint256)) public allowance;
/// @inheritdoc IERC20Permit
Expand Down Expand Up @@ -49,11 +49,11 @@ contract ERC20 is Auth, IERC20Metadata, IERC20Permit {

/// @inheritdoc IERC20
function balanceOf(address user) public view virtual returns (uint256) {
return balances[user];
return balancesWithMetadata[user];
}

function _setBalance(address user, uint256 value) internal {
balances[user] = value;
balancesWithMetadata[user] = value;
}

/// @inheritdoc IERC20Permit
Expand All @@ -79,8 +79,8 @@ contract ERC20 is Auth, IERC20Metadata, IERC20Permit {
require(balance >= value, "ERC20/insufficient-balance");

unchecked {
balances[msg.sender] -= value;
balances[to] += value; // note: we don't need an overflow check here b/c sum of all balances == totalSupply
balancesWithMetadata[msg.sender] -= value;
balancesWithMetadata[to] += value; // note: we don't need an overflow check here b/c sum of all balancesWithMetadata == totalSupply
}

emit Transfer(msg.sender, to, value);
Expand Down Expand Up @@ -109,8 +109,8 @@ contract ERC20 is Auth, IERC20Metadata, IERC20Permit {
}

unchecked {
balances[from] -= value;
balances[to] += value; // note: we don't need an overflow check here b/c sum of all balances == totalSupply
balancesWithMetadata[from] -= value;
balancesWithMetadata[to] += value; // note: we don't need an overflow check here b/c sum of all balancesWithMetadata == totalSupply
}

emit Transfer(from, to, value);
Expand All @@ -131,9 +131,9 @@ contract ERC20 is Auth, IERC20Metadata, IERC20Permit {
function mint(address to, uint256 value) public virtual auth {
require(to != address(0) && to != address(this), "ERC20/invalid-address");
unchecked {
// We don't need an overflow check here b/c balances[to] <= totalSupply
// We don't need an overflow check here b/c balancesWithMetadata[to] <= totalSupply
// and there is an overflow check below
balances[to] += value;
balancesWithMetadata[to] += value;
}
totalSupply = totalSupply + value;

Expand All @@ -157,7 +157,7 @@ contract ERC20 is Auth, IERC20Metadata, IERC20Permit {

unchecked {
// We don't need overflow checks b/c require(balance >= value) and balance <= totalSupply
balances[from] -= value;
balancesWithMetadata[from] -= value;
totalSupply = totalSupply - value;
}

Expand Down
4 changes: 2 additions & 2 deletions src/token/Tranche.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ contract Tranche is ERC20, ITranche {
// --- ERC20 overrides ---
/// @inheritdoc IERC20
function balanceOf(address user) public view override(ERC20, IERC20) returns (uint256) {
return balances[user].getLSBits(128);
return balancesWithMetadata[user].getLSBits(128);
}

/// @inheritdoc ITranche
function hookDataOf(address user) public view returns (bytes16) {
return bytes16(uint128(balances[user].getMSBits(128)));
return bytes16(uint128(balancesWithMetadata[user].getMSBits(128)));
}

/// @inheritdoc ITranche
Expand Down
4 changes: 2 additions & 2 deletions test/mocks/MockERC20Wrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract MockERC20Wrapper is ERC20, Mock, IERC20Wrapper {
);

// Obviously unsafe, just for testing purposes
balances[account] += value;
balancesWithMetadata[account] += value;
totalSupply = totalSupply + value;
emit Transfer(address(0), account, value);

Expand All @@ -32,7 +32,7 @@ contract MockERC20Wrapper is ERC20, Mock, IERC20Wrapper {

function withdrawTo(address account, uint256 value) external returns (bool) {
if (method_fail["withdrawTo"]) return false;
balances[msg.sender] -= value;
balancesWithMetadata[msg.sender] -= value;
totalSupply = totalSupply - value;

IERC20Metadata(underlying).transfer(account, value);
Expand Down

0 comments on commit 104aa87

Please sign in to comment.