Refresh reward stats when a new block is mined

This commit is contained in:
nymkappa 2022-03-22 18:07:26 +09:00
parent 2644f2fb07
commit 8cc005cb2c
No known key found for this signature in database
GPG Key ID: E155910B16E8BD04

View File

@ -1,7 +1,8 @@
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core'; import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { map } from 'rxjs/operators'; import { map, skip, switchMap } from 'rxjs/operators';
import { ApiService } from 'src/app/services/api.service'; import { ApiService } from 'src/app/services/api.service';
import { StateService } from 'src/app/services/state.service';
@Component({ @Component({
selector: 'app-reward-stats', selector: 'app-reward-stats',
@ -12,17 +13,24 @@ import { ApiService } from 'src/app/services/api.service';
export class RewardStatsComponent implements OnInit { export class RewardStatsComponent implements OnInit {
public $rewardStats: Observable<any>; public $rewardStats: Observable<any>;
constructor(private apiService: ApiService) { } constructor(private apiService: ApiService, private stateService: StateService) { }
ngOnInit(): void { ngOnInit(): void {
this.$rewardStats = this.apiService.getRewardStats$() this.$rewardStats = this.stateService.blocks$
.pipe( .pipe(
map((res) => { // (we always receives some blocks at start so only trigger for the last one)
return { skip(this.stateService.env.MEMPOOL_BLOCKS_AMOUNT - 1),
totalReward: res.totalReward, switchMap(() => {
rewardPerTx: res.totalReward / res.totalTx, return this.apiService.getRewardStats$()
feePerTx: res.totalFee / res.totalTx, .pipe(
}; map((stats) => {
return {
totalReward: stats.totalReward,
rewardPerTx: stats.totalReward / stats.totalTx,
feePerTx: stats.totalFee / stats.totalTx,
};
})
);
}) })
); );
} }