Skip to content

Commit

Permalink
rm common errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sangier committed Aug 7, 2024
1 parent ba85ba5 commit 7502e67
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 24 deletions.
16 changes: 0 additions & 16 deletions src/errors/IIErrors.sol

This file was deleted.

12 changes: 12 additions & 0 deletions src/errors/IISdkCoinErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ pragma solidity >=0.8.25;
interface IISdkCoinErrors {
/// @param decimals client type
error UnsupportedTokenDecimals(uint8 decimals);

/// @param _address address
error ZeroAddress(address _address);

/// @param amount Amount of tokens being transferred
error ZeroAmountUint256(uint256 amount);

/// @param amount Amount of tokens being transferred
error ZeroAmountUint64(uint64 amount);

/// @notice Thrown when a requested operation or action is not supported by the contract
error Unsupported();
}
9 changes: 4 additions & 5 deletions src/utils/SdkCoin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity >=0.8.25;
// https://docs.openzeppelin.com/contracts/4.x/api/token/erc20#IERC20Metadata
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import { IIErrors } from "../errors/IIErrors.sol";
import { IISdkCoinErrors } from "../errors/IISdkCoinErrors.sol";

library SdkCoin {
Expand All @@ -23,7 +22,7 @@ library SdkCoin {
function _getERC20TokenDecimals(address tokenAddress) internal view returns (uint8) {
// Input validation
if (tokenAddress == address(0x0)) {
revert IIErrors.ZeroAddress(tokenAddress);
revert IISdkCoinErrors.ZeroAddress(tokenAddress);
}
// If the tokens extends the IERC20 it should be using IERC20Metadata which supports the decimals() call
// Why this? --> https://detectors.auditbase.com/decimals-erc20-standard-solidity
Expand Down Expand Up @@ -62,7 +61,7 @@ library SdkCoin {
}
// Amount input validation
if (amount == 0) {
revert IIErrors.ZeroAmountUint256(amount);
revert IISdkCoinErrors.ZeroAmountUint256(amount);
}
// Ensure the amount respects the token's decimals
// Handle the case where the input amount exceeds the token's precision
Expand All @@ -79,7 +78,7 @@ library SdkCoin {
remainder = 0;
} else {
// revert as this is unreachable
revert IIErrors.Unsupported();
revert IISdkCoinErrors.Unsupported();
}
return (SafeCast.toUint64(temp_convertedAmount), remainder);
}
Expand All @@ -103,7 +102,7 @@ library SdkCoin {
}
// Amount input validation
if (amount == 0) {
revert IIErrors.ZeroAmountUint64(amount);
revert IISdkCoinErrors.ZeroAmountUint64(amount);
}
uint256 factor;
uint256 convertedAmount;
Expand Down
5 changes: 2 additions & 3 deletions test/sdkCoinTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ pragma solidity >=0.8.25 <0.9.0;
import { Test } from "forge-std/Test.sol";
import { SdkCoin } from "../src/utils/SdkCoin.sol";
import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import { IIErrors } from "../src/errors/IIErrors.sol";
import { IISdkCoinErrors } from "../src/errors/IISdkCoinErrors.sol";

// Discuss - Do we want to move mock contract to a mock folder?
Expand Down Expand Up @@ -344,14 +343,14 @@ contract SdkCoinTest is Test {
function testConvertERC20toSdkCoinAmount_ZeroAddress() public {
uint256 evmAmount = 1_000_000; // 1 Cosmos coin

vm.expectRevert(abi.encodeWithSelector(IIErrors.ZeroAddress.selector, address(0)));
vm.expectRevert(abi.encodeWithSelector(IISdkCoinErrors.ZeroAddress.selector, address(0)));
SdkCoin._convertERC20AmountToSdkCoin(address(0), evmAmount);
}

function testConvertSdkCoinAmountToERC20_ZeroAddress() public {
uint64 cosmosAmount = 1_000_000; // 1 Cosmos coin

vm.expectRevert(abi.encodeWithSelector(IIErrors.ZeroAddress.selector, address(0)));
vm.expectRevert(abi.encodeWithSelector(IISdkCoinErrors.ZeroAddress.selector, address(0)));
SdkCoin._convertSdkCoinAmountToERC20(address(0), cosmosAmount);
}

Expand Down

0 comments on commit 7502e67

Please sign in to comment.