0

I'm a newbie solidity

I have a question about the security smart contract.

I want to use the modifier whenNotPaused of Pausable. If the smart contract is paused, then the functions transfer, approve, and burn will not be active. And my code is

function _transfer(address sender,address recipient,uint256 amount) 
  internal 
  virtual 
  override 
  whenNotPaused 
{
    super._transfer(sender, recipient, amount);
}
function _approve(address owner,address spender,uint256 amount) 
 internal
 virtual 
 override
 whenNotPaused 
{
    super._approve(owner, spender, amount);
}
function _burn(address account, uint256 amount)
 internal
 virtual
 override
 whenNotPaused
{
    super._burn(account, amount);
}

I don't know if I should override the public function or internal function.

Is that ok? Can anyone help me? I use solidity version 0.8.7

2
  • Could you link the base contracts that you are working with? (E.g. from OpenZepplin)
    – Richard
    Feb 25 at 9:51
  • I'm working with ERC20Burnable and Pausable. it's all part of openzeppelin Feb 25 at 10:05

1 Answer 1

1

As long the internal functions are called inside all the public method then it is okay. And it could be better to add the modifier one time to the internal function than adding it 4 times to public functions.

The easiest one is the following from the openzeppelin Wizard (https://docs.openzeppelin.com/contracts/4.x/wizard)

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20, ERC20Burnable, Pausable, Ownable {
    constructor() ERC20("MyToken", "MTK") {}

    function pause() public onlyOwner {
        _pause();
    }

    function unpause() public onlyOwner {
        _unpause();
    }

    function _beforeTokenTransfer(address from, address to, uint256 amount)
        internal
        whenNotPaused
        override
    {
        super._beforeTokenTransfer(from, to, amount);
    }
}
2
  • Did you mean the best way is adding modifier in both functions public and internal? Feb 26 at 2:13
  • No if all 5 functions call 1 internal function , then it is better to override only the internal function rather than adding 5 modifiers.
    – Majd TL
    Feb 26 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.