Skip to content

Commit

Permalink
fix: use custom errors
Browse files Browse the repository at this point in the history
  • Loading branch information
amarinkovic committed Dec 30, 2024
1 parent d36081f commit 80502c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
21 changes: 11 additions & 10 deletions src/facets/NaymsTokenFacet.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import { AppStorage, LibAppStorage } from "../shared/AppStorage.sol";
import { Modifiers } from "../shared/Modifiers.sol";
import { LibHelpers } from "../libs/LibHelpers.sol";
import { LibERC20Token } from "../libs/LibERC20Token.sol";
import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {AppStorage, LibAppStorage} from "../shared/AppStorage.sol";
import {Modifiers} from "../shared/Modifiers.sol";
import {LibHelpers} from "../libs/LibHelpers.sol";
import {LibERC20Token} from "../libs/LibERC20Token.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";

/**
* @title Nayms token facet.
Expand All @@ -15,6 +15,9 @@ import { ECDSA } from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
contract NaymsTokenFacet is Modifiers {
using LibHelpers for *;

error ERC20PermitDeadlineExpired();
error ERC20PermitInvalidSignature();

/**
* @dev Get total supply of token.
* @return total supply.
Expand Down Expand Up @@ -105,13 +108,11 @@ contract NaymsTokenFacet is Modifiers {
uint8 _v,
bytes32 _r,
bytes32 _s
)
external
{
) external {
AppStorage storage s = LibAppStorage.diamondStorage();

if (block.timestamp > _deadline) {
revert("ERC20Permit: expired deadline");
revert ERC20PermitDeadlineExpired();
}

bytes32 structHash =
Expand All @@ -121,7 +122,7 @@ contract NaymsTokenFacet is Modifiers {

address signer = ECDSA.recover(digest, _v, _r, _s);
if (signer == address(0) || signer != _owner) {
revert("ERC20Permit: invalid signature");
revert ERC20PermitInvalidSignature();
}

LibERC20Token._approve(_owner, _spender, _value, true);
Expand Down
50 changes: 23 additions & 27 deletions test/NaymToken.t.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// SPDX-License-Identifier: GPLv3
pragma solidity ^0.8.20;

import { Test, console as c, Vm } from "forge-std/Test.sol";

import { IERC20Errors } from "openzeppelin/interfaces/draft-IERC6093.sol";
import { NaymsOwnershipFacet } from "src/facets/NaymsOwnershipFacet.sol";

import { IDiamondCut } from "lib/diamond-2-hardhat/contracts/interfaces/IDiamondCut.sol";
import { DiamondProxy } from "src/generated/DiamondProxy.sol";
import { IDiamondProxy } from "src/generated/IDiamondProxy.sol";
import { LibDiamondHelper } from "src/generated/LibDiamondHelper.sol";
import { LibGovernance } from "src/libs/LibGovernance.sol";
import { LibACL } from "src/libs/LibACL.sol";
import { LibHelpers } from "src/libs/LibHelpers.sol";
import { InitDiamond } from "src/init/InitDiamond.sol";
import { NaymsTokenFacet } from "src/facets/NaymsTokenFacet.sol";
import { Modifiers } from "src/shared/Modifiers.sol";
import { StdStyle } from "forge-std/StdStyle.sol";
import { LibConstants as LC } from "src/libs/LibConstants.sol";
import {Test, console as c, Vm} from "forge-std/Test.sol";

import {IERC20Errors} from "openzeppelin/interfaces/draft-IERC6093.sol";
import {NaymsOwnershipFacet} from "src/facets/NaymsOwnershipFacet.sol";

import {IDiamondCut} from "lib/diamond-2-hardhat/contracts/interfaces/IDiamondCut.sol";
import {DiamondProxy} from "src/generated/DiamondProxy.sol";
import {IDiamondProxy} from "src/generated/IDiamondProxy.sol";
import {LibDiamondHelper} from "src/generated/LibDiamondHelper.sol";
import {LibGovernance} from "src/libs/LibGovernance.sol";
import {LibACL} from "src/libs/LibACL.sol";
import {LibHelpers} from "src/libs/LibHelpers.sol";
import {InitDiamond} from "src/init/InitDiamond.sol";
import {NaymsTokenFacet} from "src/facets/NaymsTokenFacet.sol";
import {Modifiers} from "src/shared/Modifiers.sol";
import {StdStyle} from "forge-std/StdStyle.sol";
import {LibConstants as LC} from "src/libs/LibConstants.sol";

contract NaymTokenTest is Test {
using StdStyle for *;
Expand Down Expand Up @@ -45,11 +45,11 @@ contract NaymTokenTest is Test {
c.log("\n -- D01 Deployment Defaults\n");
c.log("block.chainid", block.chainid);

bool BOOL_FORK_TEST = vm.envOr({ name: "BOOL_FORK_TEST", defaultValue: false });
tAddress = vm.envOr({ name: "DIAMOND_ADDRESS", defaultValue: address(0) });
bool BOOL_FORK_TEST = vm.envOr({name: "BOOL_FORK_TEST", defaultValue: false});
tAddress = vm.envOr({name: "DIAMOND_ADDRESS", defaultValue: address(0)});

c.log("Are tests being run on a fork?".yellow().bold(), BOOL_FORK_TEST);
bool TESTS_FORK_UPGRADE_DIAMOND = vm.envOr({ name: "TESTS_FORK_UPGRADE_DIAMOND", defaultValue: true });
bool TESTS_FORK_UPGRADE_DIAMOND = vm.envOr({name: "TESTS_FORK_UPGRADE_DIAMOND", defaultValue: true});
c.log("Are we testing diamond upgrades on a fork?".yellow().bold(), TESTS_FORK_UPGRADE_DIAMOND);

if (BOOL_FORK_TEST) {
Expand All @@ -68,7 +68,7 @@ contract NaymTokenTest is Test {
vm.createSelectFork(getChain(block.chainid).rpcUrl, FORK_BLOCK);
}
// Get diamond address from env
tAddress = vm.envOr({ name: "DIAMOND_ADDRESS", defaultValue: address(0) });
tAddress = vm.envOr({name: "DIAMOND_ADDRESS", defaultValue: address(0)});

t = IDiamondProxy(tAddress);

Expand Down Expand Up @@ -290,15 +290,11 @@ contract NaymTokenTest is Test {
(uint8 v, bytes32 r, bytes32 s) = vm.sign(privateKey, hash);

// Should revert with expired deadline
vm.expectRevert("ERC20Permit: expired deadline");
vm.expectRevert(NaymsTokenFacet.ERC20PermitDeadlineExpired.selector);
t.permit(owner, spender, value, deadline, v, r, s);
}

function scheduleAndUpgradeDiamond(
IDiamondCut.FacetCut[] memory _cut,
address _init,
bytes memory _calldata
)
function scheduleAndUpgradeDiamond(IDiamondCut.FacetCut[] memory _cut, address _init, bytes memory _calldata)
internal
{
// 1. schedule upgrade
Expand Down

0 comments on commit 80502c8

Please sign in to comment.