revamped functions in harmony-core/src/blockchain.ts

pull/21/head
Wen Zhang 5 years ago committed by Ganesha Upadhyaya
parent 47672bcd68
commit fddea85f1f
  1. 231
      packages/harmony-core/src/blockchain.ts

@ -29,12 +29,24 @@ import { StakingTransaction } from '@harmony-js/staking';
class Blockchain {
messenger: Messenger;
/**
* @hidden
*/
constructor(messenger: Messenger) {
this.messenger = messenger;
}
/**
* @hidden
*/
setMessenger(messenger: Messenger) {
this.messenger = messenger;
}
/**
*
* @hidden
*/
getRpcResult(result: any) {
if (result instanceof ResponseMiddleware) {
return result.getRaw;
@ -54,7 +66,7 @@ class Blockchain {
*
* @hint
* ```
* the third param `shardID` binding with the endpoint
* the third param `shardID` is binding with the endpoint
* shard 0: localhost:9500
* shard 1: localhost:9501
* ```
@ -92,6 +104,26 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Returns the current block number.
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @return `Promise` - The number of the most recent block.
*
* @hint
* ```
* the third param `shardID` is binding with the endpoint
* shard 0: localhost:9500
* shard 1: localhost:9501
* ```
*
* @example
* ```
* hmy.blockchain.getBlockNumber().then((value) => {
* console.log(value.result);
* });
* ```
*/
async getBlockNumber(shardID: number = this.messenger.currentShard) {
const result = await this.messenger.send(
RPCMethod.BlockNumber,
@ -101,8 +133,23 @@ class Blockchain {
);
return this.getRpcResult(result);
}
/**
* Returns a block matching the block Hash.
*
* @param blockHash the block hash
* @param returnObject By default it is `true`, Features in development, IGNORE it!
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - The block object
*
* @example
* ```
* hmy.blockchain.getBlockByHash({
* blockHash: '0x9cd821b576efdff61280e8857ef218fb2cff8db0cf0fb27dfceef7237042b79e',
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
@ -128,7 +175,21 @@ class Blockchain {
}
/**
* Returns a block matching the block Number.
*
* @param blockNumber the block number
* @param returnObject By default it is `true`, Features in development, IGNORE it!
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - The block object
*
* @example
* ```
* hmy.blockchain.getBlockByNumber({
* blockNumber: '0x89',
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
blockNumber: ['isBlockNumber', AssertType.optional],
@ -153,6 +214,22 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Returns the number of transaction in a given block.
*
* @param blockHash the block number Hash
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - The number of transactions in the given block.
*
* @example
* ```
* hmy.blockchain.getBlockTransactionCountByHash({
* blockHash: '0x4142514a238157e7fe57b9d54abedb33943507fa15b3799954c273a12705ced1'
* }).then((value) => {
* console.log(value):
* });
* ```
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
shardID: ['isNumber', AssertType.optional],
@ -173,6 +250,22 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Returns the number of transaction in a given block.
*
* @param blockNumber the block number Hash
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - The number of transactions in the given block.
*
* @example
* ```
* hmy.blockchain.getBlockTransactionCountByNumber({
* blockNumber: '0x2403C'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
blockNumber: ['isBlockNumber', AssertType.required],
shardID: ['isNumber', AssertType.optional],
@ -194,7 +287,22 @@ class Blockchain {
}
/**
* Returns a transaction based on a block hash and the transactions index position.
*
* @param blockHash the block number Hash
* @param index The transactions index position. **Hex Number**
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - A transaction object
*
* @example
* ```
* hmy.blockchain.getTransactionByBlockHashAndIndex({
* blockHash: '0x4142514a238157e7fe57b9d54abedb33943507fa15b3799954c273a12705ced1',
* index: '0x0'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
@ -219,6 +327,24 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Returns a transaction based on a block number and the transactions index position.
*
* @param blockNumber the block number
* @param index The transactions index position. **Hex Number**
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - A transaction object
*
* @example
* ```
* hmy.blockchain.getTransactionByBlockNumberAndIndex({
* blockNumber: '0x2403C',
* index: '0x0'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
blockNumber: ['isBlockNumber', AssertType.optional],
index: ['isHex', AssertType.required],
@ -242,6 +368,22 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Returns a transaction matching the given transaction hash.
*
* @param txnHash The transaction hash
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - A transaction object
*
* @example
* ```
* hmy.blockchain.getTransactionByHash({
* txnHash: '0x146a0cf7e8da45b44194207c4e7785564527059483b765f9a04424554443b224'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
txnHash: ['isHash', AssertType.required],
shardID: ['isNumber', AssertType.optional],
@ -263,7 +405,20 @@ class Blockchain {
}
/**
* Returns the receipt of a transaction by transaction hash.
*
* @param txnHash The transaction hash
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - A transaction receipt object, or `null` when no receipt was found
*
* @example
* ```
* hmy.blockchain.getTransactionReceipt({
* txnHash: '0x146a0cf7e8da45b44194207c4e7785564527059483b765f9a04424554443b224'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
txnHash: ['isString', AssertType.required],
@ -285,6 +440,24 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Get transaction recepit from cross shard transaction
*
* @param txnHash The transaction hash
* @param shardID the shard id of receiver's address
* @returns `Promise` - A transaction receipt object, or `null` when no receipt was found
*
* @example
* ```
* // This transaction sends from shard 0 to shard 1
* hmy.blockchain.getCxReceiptByHash({
* txnHash: '0x7fae9252fbda68d718e610bc10cf2b5c6a9cafb42d4a6b9d6e392c77d587b9ea',
* shardID: 1,
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
txnHash: ['isString', AssertType.required],
shardID: ['isNumber', AssertType.required],
@ -298,8 +471,24 @@ class Blockchain {
);
return this.getRpcResult(result);
}
/**
* Get the code at a specific address.
*
* @param address The address to get the code from (eg:smart contract)
* @param blockNumber (OPTIONAL) If you pass this parameter it will not use the default block
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @return `Promise` - The data at given `address`
*
* @example
* ```
* hmy.blockchain.getCode({
* address: '0x08AE1abFE01aEA60a47663bCe0794eCCD5763c19',
* blockNumber: 'latest'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
address: ['isValidAddress', AssertType.required],
@ -324,17 +513,57 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Get the number of peers connected to.
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - number of peer count
*
* @example
* ```
* hmy.blockchain.net_peerCount().then((value) => {
* console.log(value.result);
* });
* ```
*/
async net_peerCount(shardID: number = this.messenger.currentShard) {
const result = await this.messenger.send(RPCMethod.PeerCount, [], 'net', shardID);
return this.getRpcResult(result);
}
/**
* Get the version of net.
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - the current version.
*
* @example
* ```
* hmy.blockchain.net_version().then((value) => {
* console.log(value.result);
* });
* ```
*/
async net_version(shardID: number = this.messenger.currentShard) {
const result = await this.messenger.send(RPCMethod.NetVersion, [], 'net', shardID);
return this.getRpcResult(result);
}
/**
* Get the protocal version.
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @returns `Promise` - the current protocol version.
*
* @example
* ```
* hmy.blockchain.getProtocolVersion().then((value) => {
* console.log(value.result);
* });
* ```
*/
async getProtocolVersion(shardID: number = this.messenger.currentShard) {
const result = await this.messenger.send(
RPCMethod.ProtocolVersion,

Loading…
Cancel
Save