1 2
import Config from './config'
2 2
import Accounts from './accounts'
3
import reduct = require('reduct')
4
import {
5
  BackendConstructor,
6
  BackendInstance,
7
  SubmitPaymentParams
8
} from '../types/backend'
9

10 2
import { loadModuleOfType } from '../lib/utils'
11

12 2
const DEFAULT_BACKEND = 'one-to-one'
13

14 2
export default class RateBackend implements BackendInstance {
15
  protected backend: BackendInstance
16
  private accounts: Accounts
17

18
  constructor (deps: reduct.Injector) {
19 2
    const config = deps(Config)
20 2
    this.accounts = deps(Accounts)
21

22 2
    const Backend: BackendConstructor = loadModuleOfType('backend', config.backend || DEFAULT_BACKEND)
23 2
    this.backend = new Backend(Object.assign({
24
      spread: config.spread
25
    }, config.backendConfig), {
26 2
      getInfo: (account: string) => this.accounts.getInfo(account),
27
      accounts: this.accounts
28
    })
29
  }
30

31
  connect () {
32 2
    return this.backend.connect()
33
  }
34

35
  getRate (sourceAccount: string, destinationAccount: string) {
36 2
    return this.backend.getRate(sourceAccount, destinationAccount)
37
  }
38

39
  submitPayment (params: SubmitPaymentParams) {
40 2
    return this.backend.submitPayment(params)
41
  }
42

43
  submitPacket (params: SubmitPaymentParams) {
44 2
    if (this.backend.submitPacket) {
45 0
      return this.backend.submitPacket(params)
46
    }
47 2
    return Promise.resolve()
48
  }
49

50
  async getStatus () {
51 2
    const rates = {}
52 2
    const accountIds = this.accounts.getAccountIds()
53 2
    for (const srcAccount of accountIds) {
54 2
      const accountRates = rates[srcAccount] = {}
55 2
      for (const dstAccount of accountIds) {
56 2
        if (srcAccount === dstAccount) continue
57 2
        accountRates[dstAccount] = await this.backend.getRate(srcAccount, dstAccount)
58
      }
59
    }
60 2
    return rates
61
  }
62
}

Read our documentation on viewing source code .

Loading