contract ERC20Bridgeable is ERC20, BridgerRole {

    uint256 public bridgeBalance;
    mapping (address => bool) public intermediateAddresses;
    mapping (address => bool) public bridgeAddresses;

    /**
     * @dev Adds bridge address witch is used as a bridge minter
     */
    function addBridgeAddress(address _bridge) public onlyBridger {
        require(_bridge != address(0), "Bridge address can't be 0x0");
        require(!isBridger(_bridge), "Bridge can't be bridger");
        require(balanceOf(_bridge) == 0, "Bridge balance should be 0");
  
        bridgeAddresses[_bridge] = true;
    }
  
    modifier onlyBridge() {
        require(bridgeAddresses[msg.sender], "Only bridge allowed to do this");
        _;
    }

    /**
     * @dev Adds intermediate address witch is used as a bridge receiver and burner
     */
    function addIntermediateAddress(address _intermediate) public onlyBridge {
        require(_intermediate != address(0), "Address can't be 0x0");
        require(balanceOf(_intermediate) == 0, "Intermediate balance should be 0");
  
        intermediateAddresses[_intermediate] = true;
    }
  
    function _transfer(address from, address to, uint256 value) internal {
        require(!bridgeAddresses[to], "Transfer to a bridge isn't allowed");
  
        if (bridgeAddresses[from]) {
          require(!intermediateAddresses[to], "Bridge can't transfer to intermediate");
          require(value <= bridgeBalance, "Value should be <= bridge balance");
  
          bridgeBalance = bridgeBalance.sub(value);
          _mint(from, value);
          super._transfer(from, to, value);
          return;
        }
  
        if (intermediateAddresses[to]) {
          bridgeBalance = bridgeBalance.add(value);
          super._transfer(from, to, value);
          _burn(to, value);
          return;
        }
  
        super._transfer(from, to, value);
    }

    /**
     * @dev Returns the total cap on the token's total supply + bridge balance.
     */
    function totalCap() public view returns (uint256) {
        return totalSupply() + bridgeBalance;
    }

}
/**
 * @dev Extension of {ERC20} that allows bridges to swap token
 * between main network and sidechains
 */
function totalCap() public view returns (uint256) {
    return totalSupply() + bridgeBalance;
}

/**
 * @dev Returns the total cap on the token's total supply + bridge balance.
 */