From 2e65f5e449dcda4d9ed164e4ac15e3f90f7b295d Mon Sep 17 00:00:00 2001 From: Lucas Manuel Date: Mon, 30 Sep 2024 16:27:20 -0400 Subject: [PATCH] feat: Gas optimization from ChainSecurity audit (SC-680) (#41) * feat: optimize for gas * fix: remove comment --- src/PSM3.sol | 5 ++--- test/unit/Deposit.t.sol | 5 +++-- test/unit/Getters.t.sol | 2 +- test/unit/PreviewDeposit.t.sol | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/PSM3.sol b/src/PSM3.sol index 69d24bc..f26a80d 100644 --- a/src/PSM3.sol +++ b/src/PSM3.sol @@ -190,9 +190,8 @@ contract PSM3 is IPSM3, Ownable { function previewDeposit(address asset, uint256 assetsToDeposit) public view override returns (uint256) { - require(_isValidAsset(asset), "PSM3/invalid-asset"); - // Convert amount to 1e18 precision denominated in value of USD then convert to shares. + // NOTE: Don't need to check valid asset here since `_getAssetValue` will revert if invalid return convertToShares(_getAssetValue(asset, assetsToDeposit, false)); // Round down } @@ -297,7 +296,7 @@ contract PSM3 is IPSM3, Ownable { if (asset == address(usdc)) return _getUsdcValue(amount); else if (asset == address(usds)) return _getUsdsValue(amount); else if (asset == address(susds)) return _getSUsdsValue(amount, roundUp); - else revert("PSM3/invalid-asset"); + else revert("PSM3/invalid-asset-for-value"); } function _getUsdcValue(uint256 amount) internal view returns (uint256) { diff --git a/test/unit/Deposit.t.sol b/test/unit/Deposit.t.sol index 95d82e7..12face7 100644 --- a/test/unit/Deposit.t.sol +++ b/test/unit/Deposit.t.sol @@ -19,8 +19,9 @@ contract PSMDepositTests is PSMTestBase { psm.deposit(address(usdc), user1, 0); } - function test_deposit_notUsdcOrUsds() public { - vm.expectRevert("PSM3/invalid-asset"); + function test_deposit_invalidAsset() public { + // NOTE: This reverts in _getAssetValue + vm.expectRevert("PSM3/invalid-asset-for-value"); psm.deposit(makeAddr("new-asset"), user1, 100e6); } diff --git a/test/unit/Getters.t.sol b/test/unit/Getters.t.sol index 668a2aa..3b5208e 100644 --- a/test/unit/Getters.t.sol +++ b/test/unit/Getters.t.sol @@ -208,7 +208,7 @@ contract PSMHarnessTests is PSMTestBase { } function test_getAssetValue_zeroAddress() public { - vm.expectRevert("PSM3/invalid-asset"); + vm.expectRevert("PSM3/invalid-asset-for-value"); psmHarness.getAssetValue(address(0), 1, false); } diff --git a/test/unit/PreviewDeposit.t.sol b/test/unit/PreviewDeposit.t.sol index 5aa403a..1b3ee60 100644 --- a/test/unit/PreviewDeposit.t.sol +++ b/test/unit/PreviewDeposit.t.sol @@ -8,7 +8,7 @@ import { MockRateProvider, PSMTestBase } from "test/PSMTestBase.sol"; contract PSMPreviewDeposit_FailureTests is PSMTestBase { function test_previewDeposit_invalidAsset() public { - vm.expectRevert("PSM3/invalid-asset"); + vm.expectRevert("PSM3/invalid-asset-for-value"); psm.previewDeposit(makeAddr("other-token"), 1); }