-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathfunding_offer.js
More file actions
211 lines (193 loc) · 5.54 KB
/
funding_offer.js
File metadata and controls
211 lines (193 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
'use strict'
const _flatten = require('lodash/flatten')
const _isEmpty = require('lodash/isEmpty')
const _compact = require('lodash/compact')
const { prepareAmount, preparePrice } = require('bfx-api-node-util')
const numberValidator = require('./validators/number')
const dateValidator = require('./validators/date')
const amountValidator = require('./validators/amount')
const boolValidator = require('./validators/bool')
const stringValidator = require('./validators/string')
const symbolValidator = require('./validators/symbol')
const Model = require('./model')
const statuses = ['ACTIVE', 'EXECUTED', 'PARTIALLY FILLED', 'CANCELED']
const boolFields = ['notify', 'hidden', 'renew']
const fields = {
id: 0,
symbol: 1,
mtsCreate: 2,
mtsUpdate: 3,
amount: 4,
amountOrig: 5,
type: 6,
flags: 9,
status: 10,
rate: 14,
period: 15,
notify: 16,
hidden: 17,
renew: 19,
rateReal: 20
}
/**
* Funding Offer model
*/
class FundingOffer extends Model {
/**
* @param {object|Array} data - funding offer data
* @param {number} data.id - id
* @param {string} data.symbol - symbol
* @param {number} data.mtsCreate - creation timestamp
* @param {number} data.mtsUpdate - last update timestamp
* @param {string} data.amount - remaining amount
* @param {string} data.amountOrig - original amount
* @param {string} data.type - funding offer type
* @param {number} data.flags - flags
* @param {string} data.status - current status
* @param {number} data.rate - rate
* @param {number} data.rateReal - rate real
* @param {number} data.period - period for the offer
* @param {number|boolean} data.notify - notify flag
* @param {number|boolean} data.hidden - hidden flag
* @param {number|boolean} data.renew - renew flag
* @param {object} [apiInterface] - rest or websocket object capable of
* submitting funding offers
*/
constructor (data = {}, apiInterface) {
super({ data, fields, boolFields })
this._apiInterface = apiInterface
}
/**
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
* @returns {object} pojo
*/
static unserialize (data) {
return super.unserialize({ data, fields, boolFields })
}
/**
* Creates an order map that can be used in either the websocket `on`
* command or a rest request body
*
* @returns {object} on
*/
toNewOfferPacket () {
return {
type: this.type,
symbol: this.symbol,
amount: prepareAmount(+this.amount),
rate: prepareAmount(+this.rate),
period: this.period,
flags: this.flags
}
}
/**
* Submit the funding offer
*
* @param {RESTv2} apiInterface - optional rest instance
* @returns {Promise} p
*/
async submit (apiInterface = this._apiInterface) {
if (!apiInterface) {
throw new Error('no API interface provided')
}
return apiInterface.submitFundingOffer(this).then((offerArray) => {
Object.assign(this, FundingOffer.unserialize(offerArray))
return this
})
}
/**
* Cancel the funding offer
*
* @param {RESTv2} apiInterface - optional rest instance
* @returns {Promise} p
*/
async cancel (apiInterface = this._apiInterface) {
if (!apiInterface) {
throw new Error('no API interface provided')
}
return apiInterface.cancelFundingOffer(this.id).then((offerArray) => {
Object.assign(this, FundingOffer.unserialize(offerArray))
return this
})
}
/**
* Close the funding offer
*
* @param {RESTv2} apiInterface - optional rest instance
* @returns {Promise} p
*/
async close (apiInterface = this._apiInterface) {
if (!apiInterface) {
throw new Error('no API interface provided')
}
return apiInterface.closeFunding({
id: this.id,
type: this.type
}).then((offerArray) => {
Object.assign(this, FundingOffer.unserialize(offerArray))
return this
})
}
/**
* Returns a string representation of the position
*
* @returns {string} desc
*/
toString () {
const {
id, symbol, status, amount, amountOrig, rate, period, renew
} = this
return _compact(_flatten([
id && `(id: ${id})`,
'funding offer for',
symbol.substring(1),
!_isEmpty(status) && `(${status})`,
'for',
prepareAmount(amount),
(!!amountOrig && amountOrig !== amount) && `(${prepareAmount(amountOrig)})`,
'@',
preparePrice(rate),
`(period ${period})`,
`[renew ${renew ? 'Y' : 'N'}]`
])).join(' ')
}
/**
* Validates a given funding offer instance
*
* @param {object[]|object|FundingOffer[]|FundingOffer|Array} data - instance to validate
* @returns {string} error - null if instance is valid
*/
static validate (data) {
return super.validate({
data,
fields,
validators: {
mtsCreate: dateValidator,
mtsUpdate: dateValidator,
mtsOpening: dateValidator,
mtsLastPayout: dateValidator,
amountOrig: amountValidator,
amount: amountValidator,
flags: numberValidator,
rate: numberValidator,
period: numberValidator,
rateReal: numberValidator,
notify: boolValidator,
hidden: boolValidator,
renew: boolValidator,
noClose: boolValidator,
status: stringValidator,
symbol: symbolValidator,
type: stringValidator,
id: numberValidator
}
})
}
}
FundingOffer.status = {}
FundingOffer.type = {
LEND: 'lend',
LOAN: 'loan'
}
statuses.forEach(s => (FundingOffer.status[s.split(' ').join('_')] = s))
module.exports = FundingOffer