There is an opportunity to optimize gas consumption by caching the i_tokenContract state variable in a stack variable. By caching the state variable in a local variable, we can reduce the number of read operations from storage, resulting in potential gas savings.
function resolveDispute(uint256 buyerAward) external onlyArbiter nonReentrant inState(State.Disputed) {
uint256 tokenBalance = i_tokenContract.balanceOf(address(this));
uint256 totalFee = buyerAward + i_arbiterFee;
if (totalFee > tokenBalance) {
revert Escrow__TotalFeeExceedsBalance(tokenBalance, totalFee);
}
s_state = State.Resolved;
emit Resolved(i_buyer, i_seller);
if (buyerAward > 0) {
i_tokenContract.safeTransfer(i_buyer, buyerAward);
}
if (i_arbiterFee > 0) {
i_tokenContract.safeTransfer(i_arbiter, i_arbiterFee);
}
tokenBalance = i_tokenContract.balanceOf(address(this));
if (tokenBalance > 0) {
i_tokenContract.safeTransfer(i_seller, tokenBalance);
}
}
@@ -1,6 +1,5 @@
function resolveDispute(uint256 buyerAward) external onlyArbiter nonReentrant inState(State.Disputed) {
- IERC20 tokenContract = i_tokenContract; // Cache state variable in a stack variable
- uint256 tokenBalance = tokenContract.balanceOf(address(this));
+ uint256 tokenBalance = i_tokenContract.balanceOf(address(this));
uint256 totalFee = buyerAward + i_arbiterFee; // Reverts on overflow
if (totalFee > tokenBalance) {
revert Escrow__TotalFeeExceedsBalance(tokenBalance, totalFee);
@@ -10,13 +9,13 @@
emit Resolved(i_buyer, i_seller);
if (buyerAward > 0) {
- tokenContract.safeTransfer(i_buyer, buyerAward);
+ i_tokenContract.safeTransfer(i_buyer, buyerAward);
}
if (i_arbiterFee > 0) {
- tokenContract.safeTransfer(i_arbiter, i_arbiterFee);
+ i_tokenContract.safeTransfer(i_arbiter, i_arbiterFee);
}
- tokenBalance = tokenContract.balanceOf(address(this));
+ tokenBalance = i_tokenContract.balanceOf(address(this));
if (tokenBalance > 0) {
- tokenContract.safeTransfer(i_seller, tokenBalance);
+ i_tokenContract.safeTransfer(i_seller, tokenBalance);
}
}