-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(intelligence): add intelligence websocket client
- Loading branch information
1 parent
1b27bc1
commit d6f3872
Showing
17 changed files
with
220 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './intel-client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Test } from '@nestjs/testing'; | ||
import { ConfigIntelServer, Depth, SupportedExchange, Symbol, Ticker, Transaction } from 'dripjs-types'; | ||
|
||
import { ApplicationModule, IntelChannel } from '../service'; | ||
import { IntelClient } from './intel-client'; | ||
|
||
// tslint:disable-next-line | ||
const config: ConfigIntelServer = require('config').container.intelService; | ||
|
||
describe('intel-client', () => { | ||
let app: INestApplication; | ||
let client: IntelClient; | ||
const exchange = SupportedExchange.Bitmex; | ||
const pair = 'XBTUSD'; | ||
|
||
beforeAll(async () => { | ||
const serverPort = config.port; | ||
const testingModule = await Test.createTestingModule({ | ||
imports: [ApplicationModule], | ||
}).compile(); | ||
app = testingModule.createNestApplication(); | ||
await app.listenAsync(serverPort); | ||
|
||
client = new IntelClient({ | ||
...config, | ||
ip: '127.0.0.1', | ||
}); | ||
client.connect(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await app.close(); | ||
client.disconnect(); | ||
}); | ||
|
||
it('getSymbols', async () => { | ||
const symbols = await client.getSymbols(exchange); | ||
expect(symbols.length).toBeGreaterThan(0); | ||
}); | ||
|
||
it('tick$', (done) => { | ||
client.ticker$(exchange, pair).subscribe((res) => { | ||
expect(res.channel).toEqual(IntelChannel.Ticker); | ||
expect(res.data).toBeDefined(); | ||
expect((<Ticker>res.data).ask).toBeGreaterThan(0); | ||
expect((<Ticker>res.data).bid).toBeGreaterThan(0); | ||
client.stopTicker(exchange, pair); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('depth$', (done) => { | ||
client.depth$(exchange, pair).subscribe((res) => { | ||
expect(res.channel).toEqual(IntelChannel.Depth); | ||
expect(res.data).toBeDefined(); | ||
expect((<Depth>res.data).asks.length).toBeGreaterThan(20); | ||
expect((<Depth>res.data).bids.length).toBeGreaterThan(20); | ||
client.stopDepth(exchange, pair); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('transaction$', (done) => { | ||
client.transaction$(exchange, pair).subscribe((res) => { | ||
expect(res.channel).toEqual(IntelChannel.Transaction); | ||
expect(res.data).toBeDefined(); | ||
expect((<Transaction>res.data).amount).toBeGreaterThan(0); | ||
expect((<Transaction>res.data).price).toBeGreaterThan(0); | ||
expect((<Transaction>res.data).time).toBeGreaterThan(0); | ||
client.stopTransaction(exchange, pair); | ||
done(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { SocketIORxjs } from 'dripjs-common'; | ||
import { SupportedExchange } from 'dripjs-types'; | ||
import { Observable } from 'rxjs'; | ||
import { filter } from 'rxjs/operators'; | ||
|
||
import { IntelChannel, IntelRealtimeResponse } from '../service'; | ||
|
||
export interface IntelClientOptions { | ||
ip: string; | ||
port: number; | ||
username: string; | ||
password: string; | ||
} | ||
|
||
export class IntelClient { | ||
private ioRxjs: SocketIORxjs; | ||
|
||
constructor(private readonly options: IntelClientOptions) {} | ||
|
||
connect(): void { | ||
this.ioRxjs = new SocketIORxjs(`http://${this.options.ip}:${this.options.port}`, { | ||
transportOptions: { | ||
polling: { | ||
extraHeaders: { | ||
username: this.options.username, | ||
password: this.options.password, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
disconnect(): void { | ||
this.ioRxjs.close(); | ||
} | ||
|
||
async getSymbols(exchange: SupportedExchange): Promise<Symbol[]> { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
this.ioRxjs.emit('symbols', exchange, (res) => { | ||
resolve(res); | ||
}); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
} | ||
|
||
ticker$(exchange: SupportedExchange, symbol: string): Observable<IntelRealtimeResponse> { | ||
const channel = IntelChannel.Ticker; | ||
|
||
return this.subscribe(channel, exchange, symbol); | ||
} | ||
|
||
stopTicker(exchange: SupportedExchange, symbol: string): void { | ||
const channel = IntelChannel.Ticker; | ||
this.unsubscribe(channel, exchange, symbol); | ||
} | ||
|
||
depth$(exchange: SupportedExchange, symbol: string): Observable<IntelRealtimeResponse> { | ||
const channel = IntelChannel.Depth; | ||
|
||
return this.subscribe(channel, exchange, symbol); | ||
} | ||
|
||
stopDepth(exchange: SupportedExchange, symbol: string): void { | ||
const channel = IntelChannel.Depth; | ||
this.unsubscribe(channel, exchange, symbol); | ||
} | ||
|
||
transaction$(exchange: SupportedExchange, symbol: string): Observable<IntelRealtimeResponse> { | ||
const channel = IntelChannel.Transaction; | ||
|
||
return this.subscribe(channel, exchange, symbol); | ||
} | ||
|
||
stopTransaction(exchange: SupportedExchange, symbol: string): void { | ||
const channel = IntelChannel.Transaction; | ||
this.unsubscribe(channel, exchange, symbol); | ||
} | ||
|
||
private subscribe(channel: IntelChannel, exchange: SupportedExchange, symbol: string): Observable<IntelRealtimeResponse> { | ||
this.ioRxjs.socket.on(channel, (res) => this.ioRxjs.next(res)); | ||
this.ioRxjs.emit('subscribe', exchange, symbol, channel); | ||
|
||
return this.ioRxjs.message$.pipe(filter((res: IntelRealtimeResponse) => res.channel === channel)); | ||
} | ||
|
||
private unsubscribe(channel: IntelChannel, exchange: SupportedExchange, symbol: string): void { | ||
this.ioRxjs.emit('unsubscribe', exchange, symbol, channel); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './core'; | ||
export * from './service'; | ||
export * from './client'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export * from './types'; | ||
export * from './helpers'; |
2 changes: 1 addition & 1 deletion
2
projects/intelligence/service/exceptions/intel-exception-filter.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './app.module'; | ||
export * from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters