// Copyright (C) 2020 Energi Core // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see . pragma solidity = 0.5.16; import { IEnergiswapPairsERC20 } from "../energiswapPairsManager/IEnergiswapPairsERC20.sol"; import { IERC20Storage } from "../energiswapPair/IERC20Storage.sol"; import { IEnergiswapPairsERC20Storage } from "../energiswapPairsManager/IEnergiswapPairsERC20Storage.sol"; import { IGovernedContract } from '../interfaces/IGovernedContract.sol'; import { IEnergiswapPairsManager } from '../energiswapPairsManager/IEnergiswapPairsManager.sol'; import { IEnergiswapFactory } from '../energiswapFactory/IEnergiswapFactory.sol'; import { IEnergiswapPairsERC20Upgrade } from './interfaces/IEnergiswapPairsERC20Upgrade.sol'; import { IStorageBase } from "../interfaces/IStorageBase.sol"; import { SafeMath } from "../libraries/SafeMath.sol"; contract EnergiswapPairsERC20Upgrade is IEnergiswapPairsERC20Upgrade { using SafeMath for uint; address public _storage; address public manager; bool public initialized = false; string public constant name = 'Energiswap'; string public constant symbol = 'ESWAP'; uint8 public constant decimals = 18; bytes32 private DOMAIN_SEPARATOR; // keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"); bytes32 private constant PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9; constructor(address __storage) public { _storage = __storage; uint chainId = 49797; // Energi Testnet chainId, update to 39797 for Energi Mainnet deployment DOMAIN_SEPARATOR = keccak256( abi.encode( keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'), keccak256(bytes(name)), keccak256(bytes('1')), chainId ) ); } modifier requireManager { require(msg.sender == manager, "EnergiswapPairsERC20: FORBIDDEN, not pairsManager"); _; } // Initialize contract. This function can only be called once function initialize(address _manager) external { require(initialized == false, 'EnergiswapPairsERC20: already initialized'); manager = _manager; initialized = true; } // This function is called by EnergiswapPairsManager in order to upgrade to new EnergiswapPairsManager and // EnergiswapPairsERC20 implementations function destroy(address _newManager, address _factory) external requireManager { // Updates EnergiswapPairsERC20Storage contract with new EnergiswapPairsManager implementation address manager = _newManager; // Gets new EnergiswapPairsERC20 implementation address from new EnergiswapPairsManager implementation address _newImpl = IEnergiswapPairsManager(_newManager).pairsERC20(); // Updates EnergiswapFactory contract with new EnergiswapPairsERC20 implementation address IEnergiswapFactory(_factory).setPairsERC20(_newImpl); // Updates EnergiswapPairsERC20Storage contract with new EnergiswapPairsERC20 implementation address as owner IStorageBase(_storage).setOwner(_newImpl); // Updates all deployed pairs' ERC20Storage contracts with new EnergiswapPairsERC20 implementation address for (uint i = 0; i < IEnergiswapPairsERC20Storage(_storage).allPairsLength(); i++) { IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(IEnergiswapPairsERC20Storage(_storage).allPairs(i)) ).setPairsERC20(_newImpl); } // Self-destruct selfdestruct(address(uint160(address(_newImpl)))); } // This function (placeholder) would be called on the new implementation if necessary for the upgrade // solium-disable-next-line no-empty-blocks function migrate(address _oldImpl) external requireManager {} // This function is called by EnergiswapPairsManager in order to register a new pair's proxy address and // ERC20Storage address function registerPair(address _pairProxy, address _erc20Storage) external requireManager { IEnergiswapPairsERC20Storage(_storage).setERC20Storage(_pairProxy, _erc20Storage); IEnergiswapPairsERC20Storage(_storage).pushPair(_pairProxy); } // ERC20 functions: // These functions are called by EnergiswapPairsManager and forward calls to the corresponding pair's ERC20Storage // contract function _mint(address pair, address to, uint value) external requireManager { // totalSupply = totalSupply.add(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setTotalSupply( IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getTotalSupply().add(value) ); // balanceOf[to] = balanceOf[to].add(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setBalance( to, IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getBalance(to).add(value) ); // emit Transfer(address(0), to, value); IEnergiswapPairsManager(manager).emitTransfer(pair, address(0), to, value); } function _burn(address pair, address from, uint value) external requireManager { // balanceOf[from] = balanceOf[from].sub(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setBalance( from, IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getBalance(from).sub(value) ); // totalSupply = totalSupply.sub(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setTotalSupply( IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getTotalSupply().sub(value) ); // emit Transfer(from, address(0), value); IEnergiswapPairsManager(manager).emitTransfer(pair, from, address(0), value); } function _approve(address pair, address owner, address spender, uint value) private { // allowance[owner][spender] = value; IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).setAllowance(owner, spender, value); // emit Approval(owner, spender, value); IEnergiswapPairsManager(manager).emitApproval(pair, owner, spender, value); } function _transfer(address pair, address from, address to, uint value) private { // balanceOf[from] = balanceOf[from].sub(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setBalance( from, IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getBalance(from).sub(value) ); // balanceOf[to] = balanceOf[to].add(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setBalance( to, IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getBalance(to).add(value) ); // emit Transfer(from, to, value); IEnergiswapPairsManager(manager).emitTransfer(pair, from, to, value); } function approve(address pair, address owner, address spender, uint value) external requireManager returns (bool) { _approve(pair, owner, spender, value); return true; } function transfer(address pair, address from, address to, uint value) external requireManager returns (bool) { _transfer(pair, from, to, value); return true; } function transferFrom(address pair, address spender, address from, address to, uint value) external requireManager returns (bool) { // if (allowance[from][msg.sender] != uint(-1)) { if(IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getAllowance(from, spender) != uint(-1)) { // allowance[from][msg.sender] = allowance[from][msg.sender].sub(value); IERC20Storage( IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair) ).setAllowance( from, spender, IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getAllowance(from, spender).sub(value) ); } _transfer(pair, from, to, value); return true; } function permit(address pair, address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external requireManager { require(deadline >= block.timestamp, 'EnergiswapPairsERC20: EXPIRED'); uint nonce = IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getNonce(owner); bytes32 digest = keccak256( abi.encodePacked( '\x19\x01', DOMAIN_SEPARATOR, keccak256(abi.encode(PERMIT_TYPEHASH, owner, spender, value, nonce++, deadline)) ) ); address recoveredAddress = ecrecover(digest, v, r, s); require(recoveredAddress != address(0) && recoveredAddress == owner, 'EnergiswapPairsERC20: INVALID_SIGNATURE'); _approve(pair, owner, spender, value); } function nonce(address pair, address owner) external view returns(uint _nonce) { _nonce = IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getNonce(owner); } // Expose ERC20 getter functions function totalSupply(address pair) external view returns (uint _totalSupply) { _totalSupply = IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getTotalSupply(); } function balanceOf(address pair, address account) external view returns (uint _balance) { _balance = IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getBalance(account); } function allowance(address pair, address owner, address spender) external view returns (uint _allowance) { _allowance = IERC20Storage(IEnergiswapPairsERC20Storage(_storage).getERC20Storage(pair)).getAllowance(owner, spender); } }