# @version ^0.2.8
# https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md
'''
@title FLEX Coin ERC-20 Token
@author Sitt Guruvanich
@notice Implementation of FLEX Coin used by CoinFLEX
@dev Follows the ERC-20 token standard as defined at
     https://eips.ethereum.org/EIPS/eip-20
'''
from vyper.interfaces import ERC20
implements: ERC20

### Events ###
event Transfer: # ERC-20
  _from: indexed(address)
  _to: indexed(address)
  _value: uint256

event Approval: # ERC-20
  _owner: indexed(address)
  _spender: indexed(address)
  _value: uint256

event OwnershipTransferred: # Ownable
  _prev_owner: indexed(address)
  _new_owner:  indexed(address)

### Member Variables ###
name: public(String[64])
symbol: public(String[32])
balanceOf: public(HashMap[address, uint256])
allowance: public(HashMap[address, HashMap[address, uint256]])
totalSupply: public(uint256)
owner: public(address) # Implements Ownable

@external
def __init__(_name: String[64], _symbol: String[32]):
  self.name = _name
  self.symbol = _symbol
  self.owner = msg.sender
  log Transfer(ZERO_ADDRESS, msg.sender, 0)

@view
@external
def decimals() -> uint256:
  '''
  @notice Get the number of decimals for this token
  @dev Implemented as a view method to reduce gas costs
  @return uint256 decimal places
  '''
  return 18

@external
def transfer(_to : address, _value : uint256) -> bool:
  '''
  @dev Transfer token for a specified address
  @param _to The address to transfer to.
  @param _value The amount to be transferred.
  '''
  # NOTE: vyper does not allow underflows
  #       so the following subtraction would revert on insufficient balance
  self.balanceOf[msg.sender] -= _value
  self.balanceOf[_to] += _value
  log Transfer(msg.sender, _to, _value)
  return True

@external
def transferFrom(_from : address, _to : address, _value : uint256) -> bool:
  '''
  @dev Transfer tokens from one address to another.
  @param _from address The address which you want to send tokens from
  @param _to address The address which you want to transfer to
  @param _value uint256 the amount of tokens to be transferred
  '''
  self.balanceOf[_from] -= _value
  self.balanceOf[_to] += _value
  _allowance: uint256 = self.allowance[_from][msg.sender]
  if _allowance != MAX_UINT256:
    self.allowance[_from][msg.sender] = _allowance - _value
  log Transfer(_from, _to, _value)
  return True

@external
def approve(_spender : address, _value : uint256) -> bool:
  '''
  @notice Approve the passed address to transfer the specified amount of
          tokens on behalf of msg.sender
  @dev Beware that changing an allowance via this method brings the risk
       that someone may use both the old and new allowance by unfortunate
       transaction ordering. This may be mitigated with the use of
       {increaseAllowance} and {decreaseAllowance}.
       https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
  @param _spender The address which will transfer the funds
  @param _value The amount of tokens that may be transferred
  @return bool success
  '''
  self.allowance[msg.sender][_spender] = _value
  log Approval(msg.sender, _spender, _value)
  return True

@external
def increaseAllowance(_spender: address, _added_value: uint256) -> bool:
  '''
  @notice Increase the allowance granted to `_spender` by the caller
  @dev This is alternative to {approve} that can be used as a mitigation for
       the potential race condition
  @param _spender The address which will transfer the funds
  @param _added_value The amount of to increase the allowance
  @return bool success
  '''
  allowance: uint256 = self.allowance[msg.sender][_spender] + _added_value
  self.allowance[msg.sender][_spender] = allowance
  log Approval(msg.sender, _spender, allowance)
  return True

@external
def decreaseAllowance(_spender: address, _subtracted_value: uint256) -> bool:
  '''
  @notice Decrease the allowance granted to `_spender` by the caller
  @dev This is alternative to {approve} that can be used as a mitigation for
       the potential race condition
  @param _spender The address which will transfer the funds
  @param _subtracted_value The amount of to decrease the allowance
  @return bool success
  '''
  allowance: uint256 = self.allowance[msg.sender][_spender] - _subtracted_value
  self.allowance[msg.sender][_spender] = allowance
  log Approval(msg.sender, _spender, allowance)
  return True

@external
def mint(_to: address, _value: uint256) -> bool:
  '''
  @notice Mint an amount of the token and assigns it to an account.
       This encapsulates the modification of balances such that the
       proper events are emitted.
  @dev only contract owner has access to this action
  @param _to The account that will receive the created tokens.
  @param _value The amount that will be created.
  '''
  assert msg.sender == self.owner, 'Only contract owner is permitted to this action.' # dev: only owner
  self.totalSupply += _value
  self.balanceOf[_to] += _value
  log Transfer(ZERO_ADDRESS, _to, _value)
  return True

@external
def burnFrom(_to: address, _value: uint256) -> bool:
  '''
  @notice Burn an amount of the token from a given account.
  @dev only contract owner has access to this action
  @param _to The account whose tokens will be burned.
  @param _value The amount that will be burned.
  '''
  assert msg.sender == self.owner, 'Only contract owner is permitted to this action.' # dev: only owner
  self.totalSupply -= _value
  self.balanceOf[_to] -= _value
  log Transfer(_to, ZERO_ADDRESS, _value)
  return True

@external
def transferOwnership(_addr: address) -> bool:
  '''
  @notice Transfer ownership for this contract; Only one owner address allowed
  @dev only contract owner has access to this action
  @param _addr  the address to transfer this contract's ownership to
  '''
  assert msg.sender == self.owner, 'Only contract owner is permitted to this action.' # dev: only owner
  assert _addr != ZERO_ADDRESS, 'Cannot transfer contract ownership to zero address.' # dev: cannot transfer ownership to zero address
  self.owner = _addr # dev: set new owner
  log OwnershipTransferred(msg.sender, _addr)
  return True