Skip to content

Instantly share code, notes, and snippets.

@Olanetsoft
Created January 11, 2024 22:13
Show Gist options
  • Save Olanetsoft/764eb5da0594565ff588775493183b8f to your computer and use it in GitHub Desktop.
Save Olanetsoft/764eb5da0594565ff588775493183b8f to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.22+commit.4fc1097e.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IERC20 } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IERC20.sol';
/**
* @dev Implementation of the {IERC20} interface.
*
* This implementation is agnostic to the way tokens are created. This means
* that a supply mechanism has to be added in a derived contract using {_mint}.
* For a generic mechanism see {ERC20PresetMinterPauser}.
*
* TIP: For a detailed writeup see our guide
* https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How
* to implement supply mechanisms].
*
* We have followed general OpenZeppelin guidelines: functions revert instead
* of returning `false` on failure. This behavior is nonetheless conventional
* and does not conflict with the expectations of ERC20 applications.
*
* Additionally, an {Approval} event is emitted on calls to {transferFrom}.
* This allows applications to reconstruct the allowance for all accounts just
* by listening to said events. Other implementations of the EIP may not emit
* these events, as it isn't required by the specification.
*
* Finally, the non-standard {decreaseAllowance} and {increaseAllowance}
* functions have been added to mitigate the well-known issues around setting
* allowances. See {IERC20-approve}.
*/
contract ERC20 is IERC20 {
mapping(address => uint256) public override balanceOf;
mapping(address => mapping(address => uint256)) public override allowance;
uint256 public override totalSupply;
uint256 internal constant UINT256_MAX = 2 ** 256 - 1;
/**
* @dev See {IERC20-transfer}.
*
* Requirements:
*
* - `recipient` cannot be the zero address.
* - the caller must have a balance of at least `amount`.
*/
function transfer(address recipient, uint256 amount) external virtual override returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}
/**
* @dev See {IERC20-approve}.
*
* NOTE: If `amount` is the maximum `uint256`, the allowance is not updated on
* `transferFrom`. This is semantically equivalent to an infinite approval.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount) external virtual override returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
/**
* @dev See {IERC20-transferFrom}.
*
* Emits an {Approval} event indicating the updated allowance. This is not
* required by the EIP. See the note at the beginning of {ERC20}.
*
* Requirements:
*
* - `sender` and `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
* - the caller must have allowance for ``sender``'s tokens of at least
* `amount`.
*/
function transferFrom(address sender, address recipient, uint256 amount) external virtual override returns (bool) {
uint256 _allowance = allowance[sender][msg.sender];
if (_allowance != UINT256_MAX) {
_approve(sender, msg.sender, _allowance - amount);
}
_transfer(sender, recipient, amount);
return true;
}
/**
* @dev Atomically increases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function increaseAllowance(address spender, uint256 addedValue) external virtual returns (bool) {
_approve(msg.sender, spender, allowance[msg.sender][spender] + addedValue);
return true;
}
/**
* @dev Atomically decreases the allowance granted to `spender` by the caller.
*
* This is an alternative to {approve} that can be used as a mitigation for
* problems described in {IERC20-approve}.
*
* Emits an {Approval} event indicating the updated allowance.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `spender` must have allowance for the caller of at least
* `subtractedValue`.
*/
function decreaseAllowance(address spender, uint256 subtractedValue) external virtual returns (bool) {
_approve(msg.sender, spender, allowance[msg.sender][spender] - subtractedValue);
return true;
}
/**
* @dev Moves tokens `amount` from `sender` to `recipient`.
*
* This is internal function is equivalent to {transfer}, and can be used to
* e.g. implement automatic token fees, slashing mechanisms, etc.
*
* Emits a {Transfer} event.
*
* Requirements:
*
* - `sender` cannot be the zero address.
* - `recipient` cannot be the zero address.
* - `sender` must have a balance of at least `amount`.
*/
function _transfer(address sender, address recipient, uint256 amount) internal virtual {
if (sender == address(0) || recipient == address(0)) revert InvalidAccount();
balanceOf[sender] -= amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
}
/** @dev Creates `amount` tokens and assigns them to `account`, increasing
* the total supply.
*
* Emits a {Transfer} event with `from` set to the zero address.
*
* Requirements:
*
* - `to` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
if (account == address(0)) revert InvalidAccount();
totalSupply += amount;
balanceOf[account] += amount;
emit Transfer(address(0), account, amount);
}
/**
* @dev Destroys `amount` tokens from `account`, reducing the
* total supply.
*
* Emits a {Transfer} event with `to` set to the zero address.
*
* Requirements:
*
* - `account` cannot be the zero address.
* - `account` must have at least `amount` tokens.
*/
function _burn(address account, uint256 amount) internal virtual {
if (account == address(0)) revert InvalidAccount();
balanceOf[account] -= amount;
totalSupply -= amount;
emit Transfer(account, address(0), amount);
}
/**
* @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.
*
* This internal function is equivalent to `approve`, and can be used to
* e.g. set automatic allowances for certain subsystems, etc.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `owner` cannot be the zero address.
* - `spender` cannot be the zero address.
*/
function _approve(address owner, address spender, uint256 amount) internal virtual {
if (owner == address(0) || spender == address(0)) revert InvalidAccount();
allowance[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title IERC20MintableBurnable Interface
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20MintableBurnable {
/**
* @notice Function to mint new tokens.
* @dev Can only be called by the minter address.
* @param to The address that will receive the minted tokens.
* @param amount The amount of tokens to mint.
*/
function mint(address to, uint256 amount) external;
/**
* @notice Function to burn tokens.
* @dev Can only be called by the minter address.
* @param from The address that will have its tokens burnt.
* @param amount The amount of tokens to burn.
*/
function burn(address from, uint256 amount) external;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IRolesBase } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IRolesBase.sol';
/**
* @title IMinter Interface
* @notice An interface for a contract module which provides a basic access control mechanism, where
* there is an account (a minter) that can be granted exclusive access to specific functions.
*/
interface IMinter is IRolesBase {
/**
* @notice Change the minter of the contract.
* @dev Can only be called by the current minter.
* @param minter_ The address of the new minter.
*/
function transferMintership(address minter_) external;
/**
* @notice Proposed a change of the minter of the contract.
* @dev Can only be called by the current minter.
* @param minter_ The address of the new minter.
*/
function proposeMintership(address minter_) external;
/**
* @notice Accept a change of the minter of the contract.
* @dev Can only be called by the proposed minter.
* @param fromMinter The previous minter.
*/
function acceptMintership(address fromMinter) external;
/**
* @notice Query if an address is a minter
* @param addr the address to query for
* @return bool Boolean value representing whether or not the address is a minter.
*/
function isMinter(address addr) external view returns (bool);
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { IMinter } from './IMinter.sol';
import { RolesBase } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/RolesBase.sol';
import { RolesConstants } from './RolesConstants.sol';
/**
* @title Minter Contract
* @notice A contract module which provides a basic access control mechanism, where
* there is an account (a minter) that can be granted exclusive access to
* specific functions.
* @dev This module is used through inheritance.
*/
contract Minter is IMinter, RolesBase, RolesConstants {
/**
* @notice Internal function that stores the new minter address in the correct storage slot.
* @param minter_ The address of the new minter.
*/
function _addMinter(address minter_) internal {
_addRole(minter_, uint8(Roles.MINTER));
}
/**
* @notice Changes the minter of the contract.
* @dev Can only be called by the current minter.
* @param minter_ The address of the new minter.
*/
function transferMintership(address minter_) external onlyRole(uint8(Roles.MINTER)) {
_transferRole(msg.sender, minter_, uint8(Roles.MINTER));
}
/**
* @notice Proposes a change of the minter of the contract.
* @dev Can only be called by the current minter.
* @param minter_ The address of the new minter.
*/
function proposeMintership(address minter_) external onlyRole(uint8(Roles.MINTER)) {
_proposeRole(msg.sender, minter_, uint8(Roles.MINTER));
}
/**
* @notice Accept a change of the minter of the contract.
* @dev Can only be called by the proposed minter.
* @param fromMinter The previous minter.
*/
function acceptMintership(address fromMinter) external {
_acceptRole(fromMinter, msg.sender, uint8(Roles.MINTER));
}
/**
* @notice Query if an address is a minter
* @param addr the address to query for
* @return bool Boolean value representing whether or not the address is a minter.
*/
function isMinter(address addr) external view returns (bool) {
return hasRole(addr, uint8(Roles.MINTER));
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
/**
* @title RolesConstants
* @notice This contract contains enum values representing different contract roles.
*/
contract RolesConstants {
enum Roles {
MINTER,
OPERATOR,
FLOW_LIMITER
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import { ERC20 } from './ERC20.sol';
import { Minter } from './Minter.sol';
import { IERC20MintableBurnable } from './IERC20MintableBurnable.sol';
contract TestMintableBurnableERC20 is ERC20, Minter, IERC20MintableBurnable {
string public name;
string public symbol;
uint8 public decimals;
constructor(string memory name_, string memory symbol_, uint8 decimals_) {
name = name_;
symbol = symbol_;
decimals = decimals_;
_addMinter(msg.sender);
_mint(msg.sender, 100 * 10 ** decimals_);
}
function mint(address account, uint256 amount) external onlyRole(uint8(Roles.MINTER)) {
_mint(account, amount);
}
function burn(address account, uint256 amount) external onlyRole(uint8(Roles.MINTER)) {
_burn(account, amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment