Added ERC20WithCommonStorage

pull/7/head
Neeraj Kashyap 2021-12-17 08:30:40 -08:00
rodzic f3e9baac91
commit fe3a1e8634
3 zmienionych plików z 432 dodań i 9 usunięć

Wyświetl plik

@ -1,2 +1,16 @@
# dao
Moonstream DAO
## Development environment
### VSCode setup
If you are using the Solidity extension in VSCode, merge the following snippet into your settings.json:
```json
{
"solidity.remappings": [
"@openzeppelin-contracts/=<path to your home directory>/.brownie/packages/OpenZeppelin/openzeppelin-contracts@4.3.2"
]
}
```

Wyświetl plik

@ -0,0 +1,409 @@
// SPDX-License-Identifier: Apache-2.0
/**
* Authors: Moonstream Engineering (engineering@moonstream.to)
* GitHub: https://github.com/bugout-dev/dao
*
* This is an implementation of an ERC20 contract adapted to use a common storage structure suited to
* DELEGATECALL proxies.
*
* For example, it can be used as a facet on an EIP2535 Diamond proxy.
*
* Our implementation is adapted from the OpenZeppelin implementation at
* https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/token/ERC20
* The major difference is that, instead of defining its own state variables, it uses state inherited
* from LibERC20.sol.
*/
pragma solidity ^0.8.0;
import "@openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import "@openzeppelin-contracts/contracts/utils/Context.sol";
import "./LibERC20.sol";
contract ERC20WithCommonStorage is Context, IERC20, IERC20Metadata {
/**
* @dev Sets the values for {name} and {symbol}.
*
* The default value of {decimals} is 18. To select a different value for
* {decimals} you should overload it.
*
* All two of these values are immutable: they can only be set once during
* construction.
*/
constructor(string memory name_, string memory symbol_) {
LibERC20.ERC20Storage storage es = LibERC20.erc20Storage();
es.name = name_;
es.symbol = symbol_;
}
/**
* @dev Returns the name of the token.
*/
function name() public view virtual override returns (string memory) {
return LibERC20.erc20Storage().name;
}
/**
* @dev Returns the symbol of the token, usually a shorter version of the
* name.
*/
function symbol() public view virtual override returns (string memory) {
return LibERC20.erc20Storage().symbol;
}
/**
* @dev Returns the number of decimals used to get its user representation.
* For example, if `decimals` equals `2`, a balance of `505` tokens should
* be displayed to a user as `5.05` (`505 / 10 ** 2`).
*
* Tokens usually opt for a value of 18, imitating the relationship between
* Ether and Wei. This is the value {ERC20} uses, unless this function is
* overridden;
*
* NOTE: This information is only used for _display_ purposes: it in
* no way affects any of the arithmetic of the contract, including
* {IERC20-balanceOf} and {IERC20-transfer}.
*/
function decimals() public view virtual override returns (uint8) {
return 18;
}
/**
* @dev See {IERC20-totalSupply}.
*/
function totalSupply() public view virtual override returns (uint256) {
return LibERC20.erc20Storage().totalSupply;
}
/**
* @dev See {IERC20-balanceOf}.
*/
function balanceOf(address account)
public
view
virtual
override
returns (uint256)
{
return LibERC20.erc20Storage().balances[account];
}
/**
* @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)
public
virtual
override
returns (bool)
{
_transfer(_msgSender(), recipient, amount);
return true;
}
/**
* @dev See {IERC20-allowance}.
*/
function allowance(address owner, address spender)
public
view
virtual
override
returns (uint256)
{
return LibERC20.erc20Storage().allowances[owner][spender];
}
/**
* @dev See {IERC20-approve}.
*
* Requirements:
*
* - `spender` cannot be the zero address.
*/
function approve(address spender, uint256 amount)
public
virtual
override
returns (bool)
{
_approve(_msgSender(), 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
) public virtual override returns (bool) {
_transfer(sender, recipient, amount);
uint256 currentAllowance = LibERC20.erc20Storage().allowances[sender][
_msgSender()
];
require(
currentAllowance >= amount,
"ERC20WithCommonStorage: transfer amount exceeds allowance"
);
unchecked {
_approve(sender, _msgSender(), currentAllowance - 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)
public
virtual
returns (bool)
{
_approve(
_msgSender(),
spender,
LibERC20.erc20Storage().allowances[_msgSender()][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)
public
virtual
returns (bool)
{
uint256 currentAllowance = LibERC20.erc20Storage().allowances[
_msgSender()
][spender];
require(
currentAllowance >= subtractedValue,
"ERC20WithCommonStorage: decreased allowance below zero"
);
unchecked {
_approve(_msgSender(), spender, currentAllowance - subtractedValue);
}
return true;
}
/**
* @dev Moves `amount` of tokens from `sender` to `recipient`.
*
* This 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 {
require(
sender != address(0),
"ERC20WithCommonStorage: transfer from the zero address"
);
require(
recipient != address(0),
"ERC20WithCommonStorage: transfer to the zero address"
);
_beforeTokenTransfer(sender, recipient, amount);
LibERC20.ERC20Storage storage es = LibERC20.erc20Storage();
uint256 senderBalance = es.balances[sender];
require(
senderBalance >= amount,
"ERC20WithCommonStorage: transfer amount exceeds balance"
);
unchecked {
es.balances[sender] = senderBalance - amount;
}
es.balances[recipient] += amount;
emit Transfer(sender, recipient, amount);
_afterTokenTransfer(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:
*
* - `account` cannot be the zero address.
*/
function _mint(address account, uint256 amount) internal virtual {
require(
account != address(0),
"ERC20WithCommonStorage: mint to the zero address"
);
_beforeTokenTransfer(address(0), account, amount);
LibERC20.ERC20Storage storage es = LibERC20.erc20Storage();
es.totalSupply += amount;
es.balances[account] += amount;
emit Transfer(address(0), account, amount);
_afterTokenTransfer(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 {
require(
account != address(0),
"ERC20WithCommonStorage: burn from the zero address"
);
_beforeTokenTransfer(account, address(0), amount);
LibERC20.ERC20Storage storage es = LibERC20.erc20Storage();
uint256 accountBalance = es.balances[account];
require(
accountBalance >= amount,
"ERC20WithCommonStorage: burn amount exceeds balance"
);
unchecked {
es.balances[account] = accountBalance - amount;
}
es.totalSupply -= amount;
emit Transfer(account, address(0), amount);
_afterTokenTransfer(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 {
require(
owner != address(0),
"ERC20WithCommonStorage: approve from the zero address"
);
require(
spender != address(0),
"ERC20WithCommonStorage: approve to the zero address"
);
LibERC20.erc20Storage().allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
/**
* @dev Hook that is called before any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* will be transferred to `to`.
* - when `from` is zero, `amount` tokens will be minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens will be burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
/**
* @dev Hook that is called after any transfer of tokens. This includes
* minting and burning.
*
* Calling conditions:
*
* - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens
* has been transferred to `to`.
* - when `from` is zero, `amount` tokens have been minted for `to`.
* - when `to` is zero, `amount` of ``from``'s tokens have been burned.
* - `from` and `to` are never both zero.
*
* To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
*/
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}
}

Wyświetl plik

@ -1,17 +1,17 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
pragma solidity ^0.8.0;
library LibDiamond {
library LibERC20 {
bytes32 constant ERC20_STORAGE_POSITION =
keccak256("moonstreamdao.eth.storage.erc20");
struct ERC20Storage {
address controller;
uint256 totalSupply;
string name;
string symbol;
mapping(address => uint256) balances;
mapping(address => mapping(address => uint256)) allowances;
address controller;
uint256 totalSupply;
}
function erc20Storage() internal pure returns (ERC20Storage storage es) {
@ -26,19 +26,19 @@ library LibDiamond {
address indexed newController
);
function setController(address _newController) internal {
function setController(address newController) internal {
ERC20Storage storage es = erc20Storage();
address previousController = es.controller;
es.controller = _newController;
emit ControlTransferred(previousController, _newController);
es.controller = newController;
emit ControlTransferred(previousController, newController);
}
function getController()
internal
view
returns (address contractController_)
returns (address contractController)
{
contractController_ = erc20Storage().controller;
contractController = erc20Storage().controller;
}
function enforceIsController() internal view {