Skip to content

Commit

Permalink
feat: Gas optimization from ChainSecurity audit (SC-680) (#41)
Browse files Browse the repository at this point in the history
* feat: optimize for gas

* fix: remove comment
  • Loading branch information
lucas-manuel authored Sep 30, 2024
1 parent 1b6384b commit 2e65f5e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
5 changes: 2 additions & 3 deletions src/PSM3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions test/unit/Deposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/Getters.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion test/unit/PreviewDeposit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 2e65f5e

Please sign in to comment.