Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit 6611d9c

Browse files
committed
Adding compatibility
1 parent 340d632 commit 6611d9c

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

src/eosjs-api.ts

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ import {
2323
import { JsonRpc } from './eosjs-jsonrpc';
2424
import {
2525
Abi,
26+
BlockTaposInfo,
2627
GetInfoResult,
2728
PushTransactionArgs,
2829
GetBlockHeaderStateResult,
30+
GetBlockInfoResult,
2931
GetBlockResult
3032
} from './eosjs-rpc-interfaces';
3133
import * as ser from './eosjs-serialize';
@@ -349,11 +351,7 @@ export class Api {
349351
{ sign, requiredKeys, authorization = [] }: QueryConfig
350352
): Promise<any> {
351353
const info = await this.rpc.get_info();
352-
const refBlock = {
353-
block_num: info.last_irreversible_block_num,
354-
id: info.last_irreversible_block_id,
355-
timestamp: info.last_irreversible_block_time,
356-
};
354+
const refBlock = await this.tryRefBlockFromGetInfo(info);
357355
const queryBuffer = new ser.SerialBuffer({ textEncoder: this.textEncoder, textDecoder: this.textDecoder });
358356
ser.serializeQuery(queryBuffer, query);
359357

@@ -443,18 +441,15 @@ export class Api {
443441
info = await this.rpc.get_info();
444442
}
445443
if (useLastIrreversible) {
446-
return { ...ser.transactionHeader({
447-
block_num: info.last_irreversible_block_num,
448-
id: info.last_irreversible_block_id,
449-
timestamp: info.last_irreversible_block_time,
450-
}, expireSeconds), ...transaction };
444+
const block = await this.tryRefBlockFromGetInfo(info);
445+
return { ...ser.transactionHeader(block, expireSeconds), ...transaction };
451446
}
452447

453448
const taposBlockNumber: number = info.head_block_num - blocksBehind;
454449

455450
const refBlock: GetBlockHeaderStateResult | GetBlockResult =
456451
taposBlockNumber <= info.last_irreversible_block_num
457-
? await this.rpc.get_block_info(taposBlockNumber)
452+
? await this.tryGetBlockInfo(taposBlockNumber)
458453
: await this.tryGetBlockHeaderState(taposBlockNumber);
459454

460455
return { ...ser.transactionHeader(refBlock, expireSeconds), ...transaction };
@@ -470,7 +465,36 @@ export class Api {
470465
try {
471466
return await this.rpc.get_block_header_state(taposBlockNumber);
472467
} catch (error) {
473-
return await this.rpc.get_block_info(taposBlockNumber);
468+
return await this.tryGetBlockInfo(taposBlockNumber);
469+
}
470+
}
471+
472+
private async tryGetBlockInfo(blockNumber: number): Promise<GetBlockInfoResult | GetBlockResult> {
473+
try {
474+
return await this.rpc.get_block_info(blockNumber);
475+
} catch (error) {
476+
return await this.rpc.get_block(blockNumber);
477+
}
478+
}
479+
480+
private async tryRefBlockFromGetInfo(info: GetInfoResult): Promise<BlockTaposInfo | GetBlockInfoResult | GetBlockResult> {
481+
if (
482+
info.hasOwnProperty('last_irreversible_block_id') &&
483+
info.hasOwnProperty('last_irreversible_block_num') &&
484+
info.hasOwnProperty('last_irreversible_block_time')
485+
) {
486+
return {
487+
block_num: info.last_irreversible_block_num,
488+
id: info.last_irreversible_block_id,
489+
timestamp: info.last_irreversible_block_time,
490+
};
491+
} else {
492+
const block = await this.tryGetBlockInfo(info.last_irreversible_block_num);
493+
return {
494+
block_num: block.block_num,
495+
id: block.id,
496+
timestamp: block.timestamp,
497+
};
474498
}
475499
}
476500

0 commit comments

Comments
 (0)