Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"scripts": {
"testCommand": "jest --detect-open-handles",
"testCommand": "jest",
"prepublishOnly": "npm run build",
"build": "tsc -p ./tsconfig-cjs.json",
"test": "node-state -e -n -m typescript -o ./test/_state.ts -r"
Expand Down Expand Up @@ -46,7 +46,7 @@
"@typescript-eslint/eslint-plugin": "^8.56.0",
"@typescript-eslint/parser": "^8.56.0",
"@waves/node-state": "^0.2.0-snapshot.1",
"@waves/waves-transactions": "4.4.0-snapshot.2",
"@waves/waves-transactions": "4.4.0-snapshot.3",
"eslint": "^9.35.0",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-import": "^2.32.0",
Expand All @@ -60,6 +60,7 @@
},
"overrides": {
"test-exclude": "^8.0.0",
"glob": "^13.0.6"
"glob": "^13.0.6",
"flatted": "^3.4.2"
}
}
105 changes: 105 additions & 0 deletions src/api-node/finality/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import request from '../../tools/request';
import {TLong} from '../../interface';
import {fetchActivationStatus} from '../activation';
import {fetchHeight, IBlockHeader} from '../blocks';


/**
* GET /blocks/headers/finalized
* Last finalized block header
* @param base
* @param options
*/
export function fetchFinalized(base: string, options: RequestInit = Object.create(null)): Promise<IBlockHeader> {
return request({
base,
url: `/blocks/headers/finalized`,
options
});
}

/**
* GET last finalized block height
* @param base
* @param options
*/
export function fetchFinalizedHeight(base: string, options: RequestInit = Object.create(null)): Promise<{ height: number }> {
return request({
base,
url: `/blocks/height/finalized`,
options
})
}

/**
* GET finalized block height at
* @param base
* @param height
* @param options
*/
export function fetchFinalizedHeightAt(base: string, height: number, options: RequestInit = Object.create(null)): Promise<{ height: number }> {
return request({
base,
url: `/blocks/finalized/at/${height}`,
options
})
}

/**
* GET /generators/at/{height}
* Committed generators list at height
* @param base
* @param height
* @param options
*/
export function fetchCommittedGeneratorsAt(base: string, height: number, options: RequestInit = Object.create(null)): Promise<Array<ICommittedGenerator>> {
return request({
base,
url: `/generators/at/${height}`,
options
});
}

/**
* Get committed generator index for provided address.
* Returns index from 0, or -1 when address is missing in the list.
* @param base
* @param height
* @param address
* @param options
*/
export function fetchCommittedGeneratorIndex(base: string, height: number, address: string, options: RequestInit = Object.create(null)): Promise<number> {
return fetchCommittedGeneratorsAt(base, height, options).then((list) => {
const index = list.findIndex((item) => item.address === address);
return index >= 0 ? index : -1;
});
}

export function fetchFinalityInfo(base: string, options: RequestInit = Object.create(null)): Promise<IFinalityInfo> {
return request({
base,
url: '/blockchain/finality',
options
})
}

export interface IGenerationPeriod {
start: number;
end: number;
}

export interface IFinalityInfo {
height: number;
finalizedHeight: number;
currentGenerationPeriod?: IGenerationPeriod;
currentGenerators: ICommittedGenerator[];
nextGenerationPeriod?: IGenerationPeriod;
nextGenerators: ICommittedGenerator[];
}

export interface ICommittedGenerator {
address: string;
balance: TLong;
transactionId: string;
conflictHeight?: number;
}
116 changes: 0 additions & 116 deletions src/api-node/finalization/index.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as utilsModule from './api-node/utils';
import * as debugModule from './api-node/debug';
import * as aliasModule from './api-node/alias';
import * as activationModule from './api-node/activation';
import * as finalizationModule from './api-node/finalization';
import * as finalityModule from './api-node/finality';
import * as nodeModule from './api-node/node';
import * as assetsModule from './api-node/assets';
import * as ethModule from './api-node/eth';
Expand Down Expand Up @@ -49,7 +49,7 @@ export function create(base: string) {
const debug: TWrapRecord<typeof debugModule> = wrapRecord(base, debugModule);
const alias: TWrapRecord<typeof aliasModule> = wrapRecord(base, aliasModule);
const activation: TWrapRecord<typeof activationModule> = wrapRecord(base, activationModule);
const finalization: TWrapRecord<typeof finalizationModule> = wrapRecord(base, finalizationModule);
const finality: TWrapRecord<typeof finalityModule> = wrapRecord(base, finalityModule);
const node: TWrapRecord<typeof nodeModule> = wrapRecord(base, nodeModule);
const assets: TWrapRecord<typeof assetsModule> = wrapRecord(base, assetsModule);
const eth: TWrapRecord<typeof ethModule> = wrapRecord(base, ethModule);
Expand Down Expand Up @@ -92,7 +92,7 @@ export function create(base: string) {
debug,
alias,
activation,
finalization,
finality,
node,
assets,
eth
Expand Down
2 changes: 1 addition & 1 deletion src/nodeInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface INodeRequestOptions {

const DEFAULT_NODE_REQUEST_OPTIONS: Required<INodeRequestOptions> = {
timeout: 120000,
apiBase: 'https://nodes.wavesplatform.com',
apiBase: 'https://nodes.wavesnodes.com',
};

export const currentHeight = async (apiBase: string): Promise<number> =>
Expand Down
25 changes: 25 additions & 0 deletions test/api-node/finality.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {MASTER_ACCOUNT, NODE_URL} from '../_state';
import {create} from '../../src';

import {commitToGeneration} from '@waves/waves-transactions';

const api = create(NODE_URL);

it('Finality info', async () => {
const finalityInfo = await api.finality.fetchFinalityInfo()

const tx = await api.transactions.broadcast(commitToGeneration({
chainId: 82,
generationPeriodStart: finalityInfo.nextGenerationPeriod!.start
}, MASTER_ACCOUNT.SEED))

await api.tools.transactions.wait(tx, {confirmations: 1})

const newFinality = await api.finality.fetchFinalityInfo()
expect(newFinality.nextGenerators).toContainEqual({
commitTxnId: tx.id,
balance: 0,
address: MASTER_ACCOUNT.ADDRESS
})

}, 10000)