1 | import BigNumber from 'bignumber.js'
|
2 | import { AccountInfo } from '../types/accounts'
|
3 | import { BackendInstance, BackendServices } from '../types/backend'
|
4 |
|
5 | import { create as createLogger } from '../common/log'
|
6 | const log = createLogger('one-to-one-backend')
|
7 |
|
8 | export interface OneToOneOptions {
|
9 | spread: number,
|
10 | ratesApiUrl: string,
|
11 | getInfo: (accountId: string) => AccountInfo,
|
12 | getAssetCode: (accountId: string) => string
|
13 | }
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | export default class OneToOneBackend implements BackendInstance {
|
19 | protected spread: number
|
20 | protected getInfo: (accountId: string) => AccountInfo | undefined
|
21 |
|
22 | |
23 |
|
24 |
|
25 |
|
26 |
|
27 | constructor (opts: OneToOneOptions, api: BackendServices) {
|
28 | this.spread = opts.spread || 0
|
29 | this.getInfo = api.getInfo
|
30 | }
|
31 |
|
32 | |
33 |
|
34 |
|
35 | async connect () {
|
36 |
|
37 | }
|
38 |
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | async getRate (sourceAccount: string, destinationAccount: string) {
|
49 | const sourceInfo = this.getInfo(sourceAccount)
|
50 | const destinationInfo = this.getInfo(destinationAccount)
|
51 |
|
52 | if (!sourceInfo) {
|
53 | log.error('unable to fetch account info for source account. accountId=%s', sourceAccount)
|
54 | throw new Error('unable to fetch account info for source account. accountId=' + sourceAccount)
|
55 | }
|
56 | if (!destinationInfo) {
|
57 | log.error('unable to fetch account info for destination account. accountId=%s', destinationAccount)
|
58 | throw new Error('unable to fetch account info for destination account. accountId=' + destinationAccount)
|
59 | }
|
60 |
|
61 | const scaleDiff = destinationInfo.assetScale - sourceInfo.assetScale
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | const rate = new BigNumber(1).minus(this.spread).shiftedBy(scaleDiff).toPrecision(15)
|
69 |
|
70 | return Number(rate)
|
71 | }
|
72 |
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 | submitPayment () {
|
79 | return Promise.resolve()
|
80 | }
|
81 | }
|