Skip to content

Commit 248ff6e

Browse files
Daniel1984JacobPlaster
authored andcommitted
new Invoice, TradingAvgPrice, FundingAvgRate models and test coverage
1 parent 0aa0e9d commit 248ff6e

7 files changed

Lines changed: 428 additions & 0 deletions

File tree

lib/funding_avg_rate.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const numberValidator = require('./validators/number')
4+
const Model = require('./model')
5+
6+
const fields = {
7+
rateAvg: 0,
8+
amount: 1
9+
}
10+
11+
/**
12+
* Funding Average Rate model
13+
*/
14+
class FundingAvgRate extends Model {
15+
/**
16+
* @param {object|Array} data - funding average rate data
17+
* @param {number} data.rateAvg - pulse User ID
18+
* @param {number} data.amount - creation timestamp
19+
*/
20+
constructor (data = {}) {
21+
super({ data, fields })
22+
}
23+
24+
/**
25+
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
26+
* @returns {object} pojo
27+
*/
28+
static unserialize (data) {
29+
return super.unserialize({ data, fields })
30+
}
31+
32+
/**
33+
* @returns {string} str
34+
*/
35+
toString () {
36+
const { rateAvg, amount } = this
37+
38+
return [
39+
amount,
40+
'@',
41+
rateAvg
42+
].join(' ')
43+
}
44+
45+
/**
46+
* Validates a given funding average rate instance
47+
*
48+
* @param {object[]|object|FundingAvgRate[]|FundingAvgRate|Array} data - instance to validate
49+
* @returns {string} error - null if instance is valid
50+
*/
51+
static validate (data) {
52+
return super.validate({
53+
data,
54+
fields,
55+
validators: {
56+
rateAvg: numberValidator,
57+
amount: numberValidator
58+
}
59+
})
60+
}
61+
}
62+
63+
module.exports = FundingAvgRate

lib/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module.exports = {
3232
ChangeLog: require('./change_log'),
3333
PublicPulseProfile: require('./public_pulse_profile'),
3434
PulseMessage: require('./pulse_message'),
35+
Invoice: require('./invoice'),
36+
TradingAvgPrice: require('./trading_avg_price'),
37+
FundingAvgRate: require('./funding_avg_rate'),
3538

3639
isCollection: require('./util/is_collection')
3740
}

lib/invoice.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
'use strict'
2+
3+
const stringValidator = require('./validators/string')
4+
const Model = require('./model')
5+
6+
const fields = {
7+
invoiceHash: 0,
8+
invoice: 1,
9+
// PLACEHOLDER
10+
// PLACEHOLDER
11+
amount: 4
12+
}
13+
14+
/**
15+
* Deposit Invoice model
16+
*/
17+
class Invoice extends Model {
18+
/**
19+
* @param {object|Array} data - deposit invoice
20+
* @param {string} data.invoiceHash - Hashed invoice
21+
* @param {string} data.invoice - Requested invoice
22+
* @param {string} data.amount - Amount of invoice
23+
*/
24+
constructor (data = {}) {
25+
super({ data, fields })
26+
}
27+
28+
/**
29+
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
30+
* @returns {object} pojo
31+
*/
32+
static unserialize (data) {
33+
return super.unserialize({ data, fields })
34+
}
35+
36+
/**
37+
* @returns {string} str
38+
*/
39+
toString () {
40+
const { invoiceHash, invoice, amount } = this
41+
42+
return [
43+
`amount: ${amount}`,
44+
`invoice: ${invoice}`,
45+
`invoiceHash: ${invoiceHash}`
46+
].join(' ')
47+
}
48+
49+
/**
50+
* Validates a given invoice instance
51+
*
52+
* @param {object[]|object|Invoice[]|Invoice|Array} data - instance to validate
53+
* @returns {string} error - null if instance is valid
54+
*/
55+
static validate (data) {
56+
return super.validate({
57+
data,
58+
fields,
59+
validators: {
60+
invoiceHash: stringValidator,
61+
invoice: stringValidator,
62+
amount: stringValidator
63+
}
64+
})
65+
}
66+
}
67+
68+
module.exports = Invoice

lib/trading_avg_price.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
const numberValidator = require('./validators/number')
4+
const Model = require('./model')
5+
6+
const fields = {
7+
priceAvg: 0,
8+
amount: 1
9+
}
10+
11+
/**
12+
* Trading Average Price model
13+
*/
14+
class TradingAvgPrice extends Model {
15+
/**
16+
* @param {object|Array} data - trading average price data
17+
* @param {number} data.priceAvg - pulse User ID
18+
* @param {number} data.amount - creation timestamp
19+
*/
20+
constructor (data = {}) {
21+
super({ data, fields })
22+
}
23+
24+
/**
25+
* @param {object[]|object|Array[]|Array} data - data to convert to POJO
26+
* @returns {object} pojo
27+
*/
28+
static unserialize (data) {
29+
return super.unserialize({ data, fields })
30+
}
31+
32+
/**
33+
* @returns {string} str
34+
*/
35+
toString () {
36+
const { priceAvg, amount } = this
37+
38+
return [
39+
amount,
40+
'@',
41+
priceAvg
42+
].join(' ')
43+
}
44+
45+
/**
46+
* Validates a given trading average price instance
47+
*
48+
* @param {object[]|object|TradingAvgPrice[]|TradingAvgPrice|Array} data - instance to validate
49+
* @returns {string} error - null if instance is valid
50+
*/
51+
static validate (data) {
52+
return super.validate({
53+
data,
54+
fields,
55+
validators: {
56+
priceAvg: numberValidator,
57+
amount: numberValidator
58+
}
59+
})
60+
}
61+
}
62+
63+
module.exports = TradingAvgPrice
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const assert = require('assert')
5+
const _includes = require('lodash/includes')
6+
const { FundingAvgRate } = require('../../../lib')
7+
const testModel = require('../../helpers/test_model')
8+
const testModelValidation = require('../../helpers/test_model_validation')
9+
10+
describe('Funding Average Rate model', () => {
11+
testModel({
12+
model: FundingAvgRate,
13+
orderedFields: [
14+
'rateAvg',
15+
'amount'
16+
]
17+
})
18+
19+
testModelValidation({
20+
model: FundingAvgRate,
21+
validData: {
22+
rateAvg: new Array(...(new Array(5))).map(() => Math.random()),
23+
amount: new Array(...(new Array(5))).map(() => Math.random())
24+
}
25+
})
26+
27+
it('initializes correctly', () => {
28+
const tap = new FundingAvgRate([
29+
0.0002303,
30+
100
31+
])
32+
33+
assert.strictEqual(tap.rateAvg, 0.0002303)
34+
assert.strictEqual(tap.amount, 100)
35+
})
36+
37+
it('serializes correctly', () => {
38+
const tap = new FundingAvgRate([
39+
0.0002303,
40+
100
41+
])
42+
43+
assert.deepStrictEqual(tap.serialize(), [
44+
0.0002303,
45+
100
46+
])
47+
})
48+
49+
it('unserializes correctly', () => {
50+
const obj = FundingAvgRate.unserialize([
51+
0.0002303,
52+
100
53+
])
54+
55+
assert.strictEqual(obj.rateAvg, 0.0002303)
56+
assert.strictEqual(obj.amount, 100)
57+
})
58+
59+
describe('toString', () => {
60+
it('includes pertinent information', () => {
61+
const tap = new FundingAvgRate({
62+
rateAvg: 0.0002303,
63+
amount: 100
64+
})
65+
66+
const str = tap.toString()
67+
assert.ok(_includes(str, '100 @ 0.0002303'), 'missing mandatory fields')
68+
})
69+
})
70+
})

test/lib/models/invoice.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* eslint-env mocha */
2+
'use strict'
3+
4+
const assert = require('assert')
5+
const _compact = require('lodash/compact')
6+
const _includes = require('lodash/includes')
7+
const { Invoice } = require('../../../lib')
8+
const testModel = require('../../helpers/test_model')
9+
// const testModelValidation = require('../../helpers/test_model_validation')
10+
11+
describe('Invoice model', () => {
12+
testModel({
13+
model: Invoice,
14+
orderedFields: [
15+
'invoiceHash',
16+
'invoice',
17+
null,
18+
null,
19+
'amount'
20+
]
21+
})
22+
23+
// testModelValidation({
24+
// model: Invoice,
25+
// validData: {
26+
// invoiceHash: ['foo', 'bar', 'baz', 'qux'],
27+
// invoice: ['foo1', 'bar2', 'baz3', 'qux4'],
28+
// amount: ['0.001', '0.002', '0.003', '0.004']
29+
// }
30+
// })
31+
32+
it('initializes correctly', () => {
33+
const invc = new Invoice([
34+
'invoiceHash',
35+
'invoice',
36+
null,
37+
null,
38+
'amount'
39+
])
40+
41+
assert.strictEqual(invc.invoiceHash, 'invoiceHash')
42+
assert.strictEqual(invc.invoice, 'invoice')
43+
assert.strictEqual(invc.amount, 'amount')
44+
})
45+
46+
it('serializes correctly', () => {
47+
const invc = new Invoice([
48+
'invoiceHash',
49+
'invoice',
50+
null,
51+
null,
52+
'amount'
53+
])
54+
55+
const arr = _compact(invc.serialize())
56+
assert.deepStrictEqual(arr, [
57+
'invoiceHash',
58+
'invoice',
59+
'amount'
60+
])
61+
})
62+
63+
it('unserializes correctly', () => {
64+
const obj = Invoice.unserialize([
65+
'invoiceHash',
66+
'invoice',
67+
null,
68+
null,
69+
'amount'
70+
])
71+
72+
assert.strictEqual(obj.invoiceHash, 'invoiceHash')
73+
assert.strictEqual(obj.invoice, 'invoice')
74+
assert.strictEqual(obj.amount, 'amount')
75+
})
76+
77+
describe('toString', () => {
78+
it('includes pertinent information', () => {
79+
const invc = new Invoice({
80+
invoiceHash: 'invoiceHash',
81+
invoice: 'invoice',
82+
amount: '0.0001'
83+
})
84+
85+
const str = invc.toString()
86+
assert.ok(_includes(str, 'invoiceHash: invoiceHash'), 'invoiceHash missing')
87+
assert.ok(_includes(str, 'invoice: invoice'), 'invoice missing')
88+
assert.ok(_includes(str, 'amount: 0.0001'), 'amount missing')
89+
})
90+
})
91+
})

0 commit comments

Comments
 (0)