Skip to content

Commit

Permalink
perf: improve gas efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChin committed Nov 19, 2024
1 parent 95d25bc commit d6e1dd4
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/contracts/Grateful.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
//////////////////////////////////////////////////////////////*/

/// @inheritdoc IGrateful
IPool public aavePool;
IPool public immutable aavePool;

Check warning on line 44 in src/contracts/Grateful.sol

View workflow job for this annotation

GitHub Actions / Lint Commit Messages

Immutable variables name should be capitalized SNAKE_CASE

/// @inheritdoc IGrateful
mapping(address => bool) public tokensWhitelisted;
Expand Down Expand Up @@ -84,8 +84,12 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
modifier onlyWhenTokensWhitelisted(
address[] memory _tokens
) {
for (uint256 i = 0; i < _tokens.length; i++) {
uint256 tokensLength = _tokens.length;
for (uint256 i = 0; i < tokensLength;) {
_ensureTokenWhitelisted(_tokens[i]);
unchecked {
i++;
}
}
_;
}
Expand All @@ -103,10 +107,14 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
constructor(address[] memory _tokens, IPool _aavePool, uint256 _initialFee) Ownable(msg.sender) {
aavePool = _aavePool;
fee = _initialFee;
for (uint256 i = 0; i < _tokens.length; i++) {
uint256 tokensLength = _tokens.length;
for (uint256 i = 0; i < tokensLength;) {
tokensWhitelisted[_tokens[i]] = true;
IERC20 token = IERC20(_tokens[i]);
token.forceApprove(address(_aavePool), type(uint256).max);
unchecked {
i++;
}
}
}

Expand Down Expand Up @@ -292,8 +300,11 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
address[] memory _tokens
) external onlyWhenTokensWhitelisted(_tokens) {
uint256 tokensLength = _tokens.length;
for (uint256 i = 0; i < tokensLength; i++) {
for (uint256 i = 0; i < tokensLength;) {
_withdraw(_tokens[i], 0, true);
unchecked {
i++;
}
}
}

Expand All @@ -306,8 +317,11 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
if (tokensLength != _assets.length) {
revert Grateful_MismatchedArrays();
}
for (uint256 i = 0; i < tokensLength; i++) {
for (uint256 i = 0; i < tokensLength;) {
_withdraw(_tokens[i], _assets[i], false);
unchecked {
i++;
}
}
}

Expand Down

0 comments on commit d6e1dd4

Please sign in to comment.