contract GlobalSharingEconomyCoin is Pausable, ERC20 {
  using SafeMath for uint256;
  event BatchTransfer(address indexed owner, bool value);

  string public name;
  string public symbol;
  uint8 public decimals;

  mapping(address => uint256) balances;
  mapping (address => mapping (address => uint256)) internal allowed;
  mapping (address => bool) allowedBatchTransfers;

  constructor() public {
    name = "GlobalSharingEconomyCoin";
    symbol = "GSE";
    decimals = 8;
    totalSupply = 10000000000 * 10 ** uint256(decimals);
    balances[msg.sender] = totalSupply;
    allowedBatchTransfers[msg.sender] = true;
  }

  function transfer(address _to, uint256 _value) whenNotPaused public returns (bool) {
    require(_to != address(0));
    require(_value <= balances[msg.sender]);

    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    emit Transfer(msg.sender, _to, _value);
    return true;
  }

  function setBatchTransfer(address _address, bool _value) public onlyOwner returns (bool) {
    allowedBatchTransfers[_address] = _value;
    emit BatchTransfer(_address, _value);
    return true;
  }

  function getBatchTransfer(address _address) public onlyOwner view returns (bool) {
    return allowedBatchTransfers[_address];
  }

  /**
   * 只允许项目方空投，如果项目方禁止批量转币，也同时禁用空投
   */
  function airdrop(address[] _funds, uint256 _amount) public whenNotPaused whenPrivateFundEnabled returns (bool) {
    require(allowedBatchTransfers[msg.sender]);
    uint256 fundslen = _funds.length;
    // 根据gaslimit的限制，超过300个地址的循环基本就无法成功执行了
    require(fundslen > 0 && fundslen < 300);
    
    uint256 totalAmount = 0;
    for (uint i = 0; i < fundslen; ++i){
      balances[_funds[i]] = balances[_funds[i]].add(_amount);
      totalAmount = totalAmount.add(_amount);
      emit Transfer(msg.sender, _funds[i], _amount);
    }

    // 如果执行失败，则会回滚整个交易
    require(balances[msg.sender] >= totalAmount);
    balances[msg.sender] = balances[msg.sender].sub(totalAmount);
    return true;
  }

  /**
   * 允许交易所和项目放方批量转币
   * _funds: 批量转币地址
   * _amounts: 每个地址的转币数量，长度必须跟_funds的长度相同
   */
  function batchTransfer(address[] _funds, uint256[] _amounts) public whenNotPaused whenPrivateFundEnabled returns (bool) {
    require(allowedBatchTransfers[msg.sender]);
    uint256 fundslen = _funds.length;
    uint256 amountslen = _amounts.length;
    require(fundslen == amountslen && fundslen > 0 && fundslen < 300);

    uint256 totalAmount = 0;
    for (uint i = 0; i < amountslen; ++i){
      totalAmount = totalAmount.add(_amounts[i]);
    }

    require(balances[msg.sender] >= totalAmount);
    for (uint j = 0; j < amountslen; ++j) {
      balances[_funds[j]] = balances[_funds[j]].add(_amounts[j]);
      emit Transfer(msg.sender, _funds[j], _amounts[j]);
    }
    balances[msg.sender] = balances[msg.sender].sub(totalAmount);
    return true;
  }

  function balanceOf(address _owner) public view returns (uint256 balance) {
    return balances[_owner];
  }

  function transferFrom(address _from, address _to, uint256 _value) public whenNotPaused returns (bool) {
    require(_to != address(0));
    require(_value <= balances[_from]);
    require(_value <= allowed[_from][msg.sender]);

    balances[_from] = balances[_from].sub(_value);
    balances[_to] = balances[_to].add(_value);
    allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
    emit Transfer(_from, _to, _value);
    return true;
  }

  function approve(address _spender, uint256 _value) public whenNotPaused returns (bool) {
    allowed[msg.sender][_spender] = _value;
    emit Approval(msg.sender, _spender, _value);
    return true;
  }

  function allowance(address _owner, address _spender) public view returns (uint256) {
    return allowed[_owner][_spender];
  }
}
function batchTransfer(address[] _funds, uint256[] _amounts) public whenNotPaused whenPrivateFundEnabled returns (bool) {
  require(allowedBatchTransfers[msg.sender]);
  uint256 fundslen = _funds.length;
  uint256 amountslen = _amounts.length;
  require(fundslen == amountslen && fundslen > 0 && fundslen < 300);

  uint256 totalAmount = 0;
  for (uint i = 0; i < amountslen; ++i){
    totalAmount = totalAmount.add(_amounts[i]);
  }

  require(balances[msg.sender] >= totalAmount);
  for (uint j = 0; j < amountslen; ++j) {
    balances[_funds[j]] = balances[_funds[j]].add(_amounts[j]);
    emit Transfer(msg.sender, _funds[j], _amounts[j]);
  }
  balances[msg.sender] = balances[msg.sender].sub(totalAmount);
  return true;
}

/**
 * 允许交易所和项目放方批量转币
 * _funds: 批量转币地址
 * _amounts: 每个地址的转币数量，长度必须跟_funds的长度相同
 */