|
| 1 | +import request from '../../tools/request'; |
| 2 | +import {TLong} from '../../interface'; |
| 3 | +import {fetchActivationStatus} from '../activation'; |
| 4 | +import {fetchHeight, IBlockHeader} from '../blocks'; |
| 5 | + |
| 6 | + |
| 7 | +/** |
| 8 | + * GET /blocks/headers/finalized |
| 9 | + * Last finalized block header |
| 10 | + * @param base |
| 11 | + * @param options |
| 12 | + */ |
| 13 | +export function fetchFinalized(base: string, options: RequestInit = Object.create(null)): Promise<IBlockHeader> { |
| 14 | + return request({ |
| 15 | + base, |
| 16 | + url: `/blocks/headers/finalized`, |
| 17 | + options |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * GET last finalized block height |
| 23 | + * @param base |
| 24 | + * @param options |
| 25 | + */ |
| 26 | +export function fetchFinalizedHeight(base: string, options: RequestInit = Object.create(null)): Promise<{ height: number }> { |
| 27 | + return request({ |
| 28 | + base, |
| 29 | + url: `/blocks/height/finalized`, |
| 30 | + options |
| 31 | + }) |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * GET finalized block height at |
| 36 | + * @param base |
| 37 | + * @param height |
| 38 | + * @param options |
| 39 | + */ |
| 40 | +export function fetchFinalizedHeightAt(base: string, height: number, options: RequestInit = Object.create(null)): Promise<{ height: number }> { |
| 41 | + return request({ |
| 42 | + base, |
| 43 | + url: `/blocks/finalized/at/${height}`, |
| 44 | + options |
| 45 | + }) |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * GET /generators/at/{height} |
| 50 | + * Committed generators list at height |
| 51 | + * @param base |
| 52 | + * @param height |
| 53 | + * @param options |
| 54 | + */ |
| 55 | +export function fetchCommittedGeneratorsAt(base: string, height: number, options: RequestInit = Object.create(null)): Promise<Array<ICommittedGenerator>> { |
| 56 | + return request({ |
| 57 | + base, |
| 58 | + url: `/generators/at/${height}`, |
| 59 | + options |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Get committed generator index for provided address. |
| 65 | + * Returns index from 0, or -1 when address is missing in the list. |
| 66 | + * @param base |
| 67 | + * @param height |
| 68 | + * @param address |
| 69 | + * @param options |
| 70 | + */ |
| 71 | +export function fetchCommittedGeneratorIndex(base: string, height: number, address: string, options: RequestInit = Object.create(null)): Promise<number> { |
| 72 | + return fetchCommittedGeneratorsAt(base, height, options).then((list) => { |
| 73 | + const index = list.findIndex((item) => item.address === address); |
| 74 | + return index >= 0 ? index : -1; |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +export function fetchFinalityInfo(base: string, options: RequestInit = Object.create(null)): Promise<IFinalityInfo> { |
| 79 | + return request({ |
| 80 | + base, |
| 81 | + url: '/blockchain/finality', |
| 82 | + options |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +export interface IGenerationPeriod { |
| 87 | + start: number; |
| 88 | + end: number; |
| 89 | +} |
| 90 | + |
| 91 | +export interface IFinalityInfo { |
| 92 | + height: number; |
| 93 | + finalizedHeight: number; |
| 94 | + currentGenerationPeriod?: IGenerationPeriod; |
| 95 | + currentGenerators: ICommittedGenerator[]; |
| 96 | + nextGenerationPeriod?: IGenerationPeriod; |
| 97 | + nextGenerators: INextCommittedGenerator[]; |
| 98 | +} |
| 99 | + |
| 100 | +export interface ICommittedGenerator { |
| 101 | + address: string; |
| 102 | + balance: TLong; |
| 103 | + transactionId: string; |
| 104 | + conflictHeight?: number; |
| 105 | +} |
| 106 | + |
| 107 | +export interface INextCommittedGenerator { |
| 108 | + address: string; |
| 109 | + transactionId: string; |
| 110 | +} |
0 commit comments