Artifact Content
Not logged in

Artifact 7648831862b44daccf31291095dba2b06cc19471:


import Config from './config'
import Accounts from './accounts'
import reduct = require('reduct')
import {
  BackendConstructor,
  BackendInstance,
  SubmitPaymentParams
} from '../types/backend'

import { loadModuleOfType } from '../lib/utils'

const DEFAULT_BACKEND = 'one-to-one'

export default class RateBackend implements BackendInstance {
  protected backend: BackendInstance

  constructor (deps: reduct.Injector) {
    const config = deps(Config)
    const accounts = deps(Accounts)

    const Backend: BackendConstructor = loadModuleOfType('backend', config.backend || DEFAULT_BACKEND)
    this.backend = new Backend(Object.assign({
      spread: config.spread
    }, config.backendConfig), {
      getInfo: (account: string) => accounts.getInfo(account)
    })
  }

  connect () {
    return this.backend.connect()
  }

  getRate (sourceAccount: string, destinationAccount: string) {
    return this.backend.getRate(sourceAccount, destinationAccount)
  }

  submitPayment (params: SubmitPaymentParams) {
    return this.backend.submitPayment(params)
  }
}