Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
670 additions
and
251 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
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
23 changes: 23 additions & 0 deletions
23
src/app/core/rpc/new-tx-notifier/new-tx-notifier.service.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { HttpClient, HttpClientModule } from '@angular/common/http'; | ||
|
||
import { RpcModule } from '../rpc.module'; | ||
import { NewTxNotifierService } from './new-tx-notifier.service'; | ||
import { CoreModule } from 'app/core/core.module'; | ||
|
||
describe('NewTxNotifierService', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
HttpClientModule, | ||
CoreModule.forRoot(), | ||
// RpcModule.forRoot() | ||
], | ||
providers: [NewTxNotifierService] | ||
}); | ||
}); | ||
|
||
it('should be created', inject([NewTxNotifierService], (service: NewTxNotifierService) => { | ||
expect(service).toBeTruthy(); | ||
})); | ||
}); |
82 changes: 82 additions & 0 deletions
82
src/app/core/rpc/new-tx-notifier/new-tx-notifier.service.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Injectable, OnDestroy } from '@angular/core'; | ||
import { Log } from 'ng2-logger'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
import { NotificationService } from 'app/core/notification/notification.service'; | ||
import { RpcService } from 'app/core/rpc/rpc.service'; | ||
|
||
@Injectable() | ||
export class NewTxNotifierService implements OnDestroy { | ||
|
||
log: any = Log.create('new-tx-notifier.service id:' + Math.floor((Math.random() * 1000) + 1)); | ||
private destroyed: boolean = false; | ||
|
||
private lastTxId: string = undefined; | ||
|
||
|
||
constructor( | ||
private _rpc: RpcService, | ||
private _notification: NotificationService | ||
) { | ||
|
||
this.log.d('tx notifier service running!'); | ||
// TODO: when state moves into its own service, fix it here | ||
// TODO: throttle (block sync).. | ||
this._rpc.state.observe('blocks'). | ||
throttle(val => Observable.interval(30000/*ms*/)). | ||
takeWhile(() => !this.destroyed).subscribe( | ||
(blocks: number) => { | ||
this.newTransaction(); | ||
} | ||
); | ||
} | ||
|
||
// TODO: trigger by ZMQ in the future | ||
newTransaction(): void { | ||
this.log.d('newTransaction()'); | ||
|
||
const options = { | ||
'count': 10 | ||
}; | ||
this._rpc.call('filtertransactions', [options]) | ||
.subscribe( | ||
(txs: Array<any>) => { | ||
|
||
// if no transactions: stop | ||
if (txs.length === 0) { | ||
return; | ||
} | ||
|
||
// not initialized yet | ||
if (this.lastTxId === undefined) { | ||
this.lastTxId = txs[0].txid; | ||
} else { | ||
txs.some((tx) => { | ||
// we hit our last transaction, abort notifications | ||
if (this.lastTxId === tx.txid) { | ||
return true; | ||
} | ||
|
||
this.notifyNewTransaction(tx); | ||
}) | ||
|
||
// update tip | ||
this.lastTxId = txs[0].txid; | ||
} | ||
}); | ||
} | ||
|
||
private notifyNewTransaction(tx: any) { | ||
this.log.d('notify new tx: ' + tx); | ||
if (tx.category === 'receive') { | ||
this._notification.sendNotification('Incoming transaction', tx.amount + ' PART received'); | ||
} else if (tx.category === 'stake') { | ||
this._notification.sendNotification('New stake reward', tx.amount + ' PART received'); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.destroyed = true; | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.