This repository has been archived by the owner on Jul 18, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added getStatus for cardano-explorer data provider.
- Loading branch information
1 parent
5901300
commit 0f9e2b7
Showing
9 changed files
with
114 additions
and
10 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
77 changes: 73 additions & 4 deletions
77
src/entities/raw-data-providers/cardano-explorer-api/cardano-explorer-api.js
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 |
---|---|---|
@@ -1,25 +1,94 @@ | ||
// @flow | ||
|
||
import { helpers } from 'inversify-vanillajs-helpers' | ||
import type { Logger } from 'bunyan' | ||
|
||
import type { RawDataProvider, RawDataParser, NetworkConfig } from '../../../interfaces' | ||
import type { | ||
RawDataProvider, | ||
NetworkConfig, | ||
DBConnection, | ||
} from '../../../interfaces' | ||
import type { NodeStatusType } from '../../../interfaces/raw-data-provider' | ||
import SERVICE_IDENTIFIER from '../../../constants/identifiers' | ||
import { GENESIS_PARENT } from '../../../blockchain/shelley/block' | ||
|
||
import { | ||
GET_BEST_BLOCK_NUM, | ||
} from './db-queries' | ||
|
||
class CardanoExplorerApi implements RawDataProvider { | ||
parser: RawDataParser | ||
logger: Logger | ||
|
||
conn: DBConnection | ||
|
||
networkConfig: NetworkConfig | ||
|
||
constructor( | ||
networkConfig: NetworkConfig, | ||
logger: Logger, | ||
conn: DBConnection, | ||
) { | ||
this.logger = logger | ||
this.networkConfig = networkConfig | ||
this.conn = conn | ||
} | ||
|
||
getStatus() { | ||
console.log('Get status called.') | ||
async getGenesis(hash: string): Promise<Object> { | ||
this.logger.debug(`getGenesis: ${hash}`) | ||
// const resp = await this.getJson(`/genesis/${hash}`) | ||
// const { data } = resp | ||
// return data | ||
// not supported right now in jormungandr, so we're hardcoding this for now | ||
// as something empty to not cause any issues. | ||
return { | ||
protocolConsts: { | ||
protocolMagic: null, | ||
}, | ||
nonAvvmBalances: [], | ||
avvmDistr: [], | ||
} | ||
} | ||
|
||
async getStatus(): Promise<NodeStatusType> { | ||
const resp = await this.conn.query(GET_BEST_BLOCK_NUM) | ||
this.logger.debug('[cardano-explorer-api].getStatus', resp) | ||
if (resp.rows.length === 1) { | ||
const { | ||
epoch, slotNo, height, hash, | ||
} = resp.rows[0] | ||
const tipStatus = { | ||
height, | ||
hash, | ||
slot: [epoch, slotNo], | ||
} | ||
return { | ||
packedEpochs: epoch, | ||
tip: { | ||
local: tipStatus, | ||
remote: tipStatus, | ||
}, | ||
} | ||
} | ||
|
||
const emptyTip = { | ||
height: 0, | ||
slot: [0, 0], | ||
hash: GENESIS_PARENT, | ||
} | ||
return { | ||
packedEpochs: 0, | ||
tip: { | ||
local: emptyTip, | ||
remote: emptyTip, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
helpers.annotate(CardanoExplorerApi, [ | ||
SERVICE_IDENTIFIER.NETWORK_CONFIG, | ||
SERVICE_IDENTIFIER.LOGGER, | ||
{ type: SERVICE_IDENTIFIER.DB_CONNECTION, named: 'cardanoExplorerDbConnection' }, | ||
]) | ||
|
||
export default CardanoExplorerApi |
16 changes: 16 additions & 0 deletions
16
src/entities/raw-data-providers/cardano-explorer-api/db-queries.js
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,16 @@ | ||
// @flow | ||
|
||
import squel from 'squel' | ||
|
||
export const sql = squel.useFlavour('postgres') | ||
|
||
export const GET_BEST_BLOCK_NUM = sql.select() | ||
.from('"Block"') | ||
.field('id', 'hash') | ||
.field('number', 'height') | ||
.field('"epochNo"', 'epoch') | ||
.field('"slotNo"') | ||
.where('number IS NOT NULL') | ||
.order('number', false) | ||
.limit(1) | ||
.toString() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
// @flow | ||
|
||
export type NodeTipStatusType = { | ||
height: number, | ||
slot: [number, number], | ||
hash: string, | ||
} | ||
|
||
export type NodeStatusType = { | ||
packedEpochs: number, | ||
tip: { | ||
local: NodeTipStatusType, | ||
remote: NodeTipStatusType | ||
} | ||
} | ||
|
||
export interface RawDataProvider { | ||
postSignedTx(txPayload: string): Promise<any>; | ||
getBlock(id: string): Promise<string>; | ||
getEpoch(id: number): Promise<string>; | ||
getGenesis(hash: string): Promise<Object>; | ||
getStatus(): Promise<any>; | ||
getStatus(): Promise<NodeStatusType>; | ||
} |
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