-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
151 lines (122 loc) · 3.96 KB
/
index.js
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
const EventEmitter = require('events')
const request = require('request-promise')
const speakeasy = require('speakeasy')
const ICase = require('./interfaces/ICase')
const ICaseSite = require('./interfaces/ICaseSite')
const IEthereum = require('./interfaces/IEthereum')
const IItem = require('./interfaces/IItem')
const ITrade = require('./interfaces/ITrade')
const IUser = require('./interfaces/IUser')
const OfferState = require('./interfaces/ITrade/states')
class ExpressTrade extends EventEmitter {
constructor(options) {
super()
// Check for config
if (typeof options !== 'object') {
throw new Error('Options not specified!')
}
if (options.apiKey === undefined) {
throw new Error('apiKey not specified!')
}
if (options.twoFactorSecret === undefined) {
throw new Error('twoFactorSecret not specified!')
}
// Initialize internal settings
this.options = {
apiUrl: 'https://api-trade.wax.io/',
pollInterval: null,
...options
}
this.request = request.defaults({
baseUrl: this.options.apiUrl,
auth: { user: this.options.apiKey },
json: true
})
// Initialize interfaces
this.ICase = new ICase({ request: this.request })
this.ICaseSite = new ICaseSite({ request: this.request })
this.IEthereum = new IEthereum({ request: this.request })
this.IItem = new IItem({ request: this.request })
this.ITrade = new ITrade({ request: this.request, generateToken: () => this.generateToken() })
this.IUser = new IUser({ request: this.request })
// Polling
this.pollData = {}
if (this.options.pollInterval !== null) {
if (this.options.pollInterval < 1000) {
throw new Error('pollInterval minimal value is 1000. Please change it!')
}
this.startPolling(this.options.pollInterval)
}
}
startPolling(interval) {
this.pollInterval = setInterval(() => this.poll(), interval)
}
stopPolling() {
clearInterval(this.pollInterval)
this.pollInterval = undefined
}
async poll() {
let offers
try {
const offersResponse = await this.ITrade.GetOffers({})
offers = offersResponse.offers
} catch (err) {
console.error(`Cannot poll offers - ${err.message}`)
return
}
const pollData = {}
// First-run time poll
if (Object.keys(this.pollData).length === 0) {
for (const offer of offers) {
pollData[offer.id] = offer
}
this.pollData = pollData
return
}
for (const offer of offers) {
const newState = offer.state
pollData[offer.id] = offer
if (this.pollData[offer.id] === undefined) {
const event = offer.sent_by_you ? 'OfferSent' : 'OfferReceived'
this.emit(event, offer)
} else if (newState !== this.pollData[offer.id].state) {
this.emit('OfferChanged', offer)
switch (newState) {
case OfferState.STATE_ACCEPTED:
this.emit('OfferAccepted', offer)
break
case OfferState.STATE_EXPIRED:
this.emit('OfferExpired', offer)
break
case OfferState.STATE_CANCELLED:
this.emit('OfferCancelled', offer)
break
case OfferState.STATE_DECLINED:
this.emit('OfferDeclined', offer)
break
case OfferState.STATE_INVALID_ITEMS:
this.emit('OfferNoLongerValid', offer)
break
case OfferState.STATE_PENDING_CASE_OPEN:
this.emit('OfferCasePending', offer)
break
case OfferState.STATE_EXPIRED_CASE_OPEN:
this.emit('OfferCaseExpired', offer)
break
case OfferState.STATE_FAILED_CASE_OPEN:
this.emit('OfferCaseFailed', offer)
break
}
}
}
// Save new pollData
this.pollData = pollData
}
generateToken() {
return speakeasy.totp({
secret: this.options.twoFactorSecret,
encoding: 'base32'
})
}
}
module.exports = ExpressTrade