                             strongSelf.delegate?.exchangeDidUpdatePrices(strongSelf)
                         }
                     }
-                    
+
                 default:
                     break
                 }
             }
-            
+
             socket.connect()
             self.socket = socket
         } else {

import Foundation
import Starscream
import SwiftyJSON

class BitfinexExchange: Exchange {
    
    private struct Constants {
        static let WebSocketURL = URL(string: "wss://api.bitfinex.com/ws/2")!
        static let ProductListAPIPath = "https://api.bitfinex.com/v1/symbols"
        static let TickerAPIPathFormat = "https://api.bitfinex.com/v2/tickers?symbols=%@"
    }
    
    init(delegate: ExchangeDelegate? = nil) {
        super.init(site: .bitfinex, delegate: delegate)
    }
    
    override func load() {
        super.load(from: Constants.ProductListAPIPath) {
            $0.json.arrayValue.compactMap { result in
                let pairString = result.stringValue
                guard pairString.count == 6 else {
                    return nil
                }
                
                return CurrencyPair(
                    baseCurrency: String(pairString.prefix(3)),
                    quoteCurrency: String(pairString.suffix(3)),
                    customCode: "t\(pairString.uppercased())"
                )
            }
        }
    }
    
    override internal func fetch() {
        if isUpdatingInRealTime {
            let socket = WebSocket(request: URLRequest(url: Constants.WebSocketURL))
            socket.callbackQueue = socketResponseQueue
            
            var channelIds =  [String: Int]()
            selectedCurrencyPairs.forEach({ channelIds[$0.customCode] = 0 })
            socket.onEvent = { [weak self] event in

                            "channel": "ticker",
                            "symbol": productId
                        ])

                        if let string = json.rawString() {
                            socket.write(string: string)
                        }
                    })

                case .text(let text):
                    if let strongSelf = self {
                        let json = JSON(parseJSON: text)
