hierarchy rebuild and APIs revamped

pull/21/head
Wen Zhang 5 years ago committed by Ganesha Upadhyaya
parent fddea85f1f
commit 2d637dede9
  1. 22
      packages/harmony-account/README.md
  2. 11
      packages/harmony-account/guide.ts
  3. 6
      packages/harmony-account/src/hdnode.ts
  4. 1
      packages/harmony-account/src/index.ts
  5. 1
      packages/harmony-account/src/types.ts
  6. 1
      packages/harmony-account/src/utils.ts
  7. 354
      packages/harmony-core/src/blockchain.ts
  8. 12
      packages/harmony-core/src/harmonyExtension.ts
  9. 1
      packages/harmony-core/src/index.ts
  10. 14
      packages/harmony-core/src/truffleProvider.ts
  11. 1
      packages/harmony-core/src/types.ts
  12. 1
      packages/harmony-core/src/util.ts

@ -1,15 +1,15 @@
1. [About This Package](#about-this-package)
2. [Usage of Account](#usage-of-account)
1. [Dependencies](#dependencies)
2. [Examples](#examples)
1. [Create a random account](#create-a-random-account)
2. [Import an existing privateKey to create Account](#import-an-existing-privatekey-to-create-account)
3. [Encrypt/Export keyStore file, Decrypt/Import keyStore file](#encryptexport-keystore-file-decryptimport-keystore-file)
4. [Address format getter](#address-format-getter)
5. [Sign a transaction](#sign-a-transaction)
3. [Usage of Wallet](#usage-of-wallet)
1. [Dependencies](#dependencies-1)
- [About This Package](#about-this-package)
- [Usage of Account](#usage-of-account)
- [Dependencies](#dependencies)
- [Examples](#examples)
- [Create a random account](#create-a-random-account)
- [Import an existing privateKey to create Account](#import-an-existing-privatekey-to-create-account)
- [Encrypt/Export keyStore file, Decrypt/Import keyStore file](#encryptexport-keystore-file-decryptimport-keystore-file)
- [Address format getter](#address-format-getter)
- [Sign a transaction](#sign-a-transaction)
- [Usage of Wallet](#usage-of-wallet)
- [Dependencies](#dependencies-1)
# About This Package

@ -1,10 +1,5 @@
/**
* @packageDocumentation
* @module harmony-account
*
*/
/**
# Contents
1. [About This Package](#about-this-package)
2. [Usage of Account](#usage-of-account)
1. [Dependencies](#dependencies)
@ -261,5 +256,7 @@ The `Wallet` class is class that stores all `Account` instance, you can do CRUD
```
*
* @packageDocumentation
* @module harmony-account
*/
export class INSTRUCTION {}
export interface README {}

@ -19,10 +19,16 @@ import { Messenger, HttpProvider, WSProvider } from '@harmony-js/network';
import { Transaction, TxStatus, TransasctionReceipt } from '@harmony-js/transaction';
import { Account } from './account';
/**
* @hidden
*/
export interface WalletsInterfaces {
[key: string]: Account;
}
/**
* @hidden
*/
export interface Web3TxPrams {
id?: string;
from?: string;

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-account
* @ignore
*/
export * from './account';

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-account
* @hidden
*/
/**

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-account
* @hidden
*/
import { HttpProvider, Messenger } from '@harmony-js/network';

@ -1,4 +1,19 @@
/**
* ## Initialize the Harmony instance
* Before using harmony-core package, you should initialize the Harmony instance
* ```javascript
* // initialize the Harmony instance
* const hmy = new Harmony(
* // rpc url
* 'https://api.s0.b.hmny.io/',
* {
* // chainType set to Harmony
* chainType: ChainType.Harmony,
* // chainType set to HmyLocal
* chainId: ChainID.HmyLocal,
* },
* );
* ```
* @packageDocumentation
* @module harmony-core
*/
@ -27,6 +42,9 @@ import { Transaction } from '@harmony-js/transaction';
import { StakingTransaction } from '@harmony-js/staking';
class Blockchain {
/**
* @hidden
*/
messenger: Messenger;
/**
@ -72,7 +90,7 @@ class Blockchain {
* ```
*
* @example
* ```
* ```javascript
* hmy.blockchain.getBalance({
* address: 'one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7',
* blockNumber: 'latest'
@ -118,7 +136,7 @@ class Blockchain {
* ```
*
* @example
* ```
* ```javascript
* hmy.blockchain.getBlockNumber().then((value) => {
* console.log(value.result);
* });
@ -143,7 +161,7 @@ class Blockchain {
* @returns `Promise` - The block object
*
* @example
* ```
* ```javascript
* hmy.blockchain.getBlockByHash({
* blockHash: '0x9cd821b576efdff61280e8857ef218fb2cff8db0cf0fb27dfceef7237042b79e',
* }).then((value) => {
@ -183,7 +201,7 @@ class Blockchain {
* @returns `Promise` - The block object
*
* @example
* ```
* ```javascript
* hmy.blockchain.getBlockByNumber({
* blockNumber: '0x89',
* }).then((value) => {
@ -222,7 +240,7 @@ class Blockchain {
* @returns `Promise` - The number of transactions in the given block.
*
* @example
* ```
* ```javascript
* hmy.blockchain.getBlockTransactionCountByHash({
* blockHash: '0x4142514a238157e7fe57b9d54abedb33943507fa15b3799954c273a12705ced1'
* }).then((value) => {
@ -258,7 +276,7 @@ class Blockchain {
* @returns `Promise` - The number of transactions in the given block.
*
* @example
* ```
* ```javascript
* hmy.blockchain.getBlockTransactionCountByNumber({
* blockNumber: '0x2403C'
* }).then((value) => {
@ -295,7 +313,7 @@ class Blockchain {
* @returns `Promise` - A transaction object
*
* @example
* ```
* ```javascript
* hmy.blockchain.getTransactionByBlockHashAndIndex({
* blockHash: '0x4142514a238157e7fe57b9d54abedb33943507fa15b3799954c273a12705ced1',
* index: '0x0'
@ -336,7 +354,7 @@ class Blockchain {
* @returns `Promise` - A transaction object
*
* @example
* ```
* ```javascript
* hmy.blockchain.getTransactionByBlockNumberAndIndex({
* blockNumber: '0x2403C',
* index: '0x0'
@ -376,7 +394,7 @@ class Blockchain {
* @returns `Promise` - A transaction object
*
* @example
* ```
* ```javascript
* hmy.blockchain.getTransactionByHash({
* txnHash: '0x146a0cf7e8da45b44194207c4e7785564527059483b765f9a04424554443b224'
* }).then((value) => {
@ -412,7 +430,7 @@ class Blockchain {
* @returns `Promise` - A transaction receipt object, or `null` when no receipt was found
*
* @example
* ```
* ```javascript
* hmy.blockchain.getTransactionReceipt({
* txnHash: '0x146a0cf7e8da45b44194207c4e7785564527059483b765f9a04424554443b224'
* }).then((value) => {
@ -448,7 +466,7 @@ class Blockchain {
* @returns `Promise` - A transaction receipt object, or `null` when no receipt was found
*
* @example
* ```
* ```javascript
* // This transaction sends from shard 0 to shard 1
* hmy.blockchain.getCxReceiptByHash({
* txnHash: '0x7fae9252fbda68d718e610bc10cf2b5c6a9cafb42d4a6b9d6e392c77d587b9ea',
@ -481,7 +499,7 @@ class Blockchain {
* @return `Promise` - The data at given `address`
*
* @example
* ```
* ```javascript
* hmy.blockchain.getCode({
* address: '0x08AE1abFE01aEA60a47663bCe0794eCCD5763c19',
* blockNumber: 'latest'
@ -520,7 +538,7 @@ class Blockchain {
* @returns `Promise` - number of peer count
*
* @example
* ```
* ```javascript
* hmy.blockchain.net_peerCount().then((value) => {
* console.log(value.result);
* });
@ -539,7 +557,7 @@ class Blockchain {
* @returns `Promise` - the current version.
*
* @example
* ```
* ```javascript
* hmy.blockchain.net_version().then((value) => {
* console.log(value.result);
* });
@ -558,7 +576,7 @@ class Blockchain {
* @returns `Promise` - the current protocol version.
*
* @example
* ```
* ```javascript
* hmy.blockchain.getProtocolVersion().then((value) => {
* console.log(value.result);
* });
@ -574,6 +592,24 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Get the storage at a specific position of an address
*
* @param address The address to get the storage from
* @param position The index position of the storage
* @param blockNumber by default it's `latest`.
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
*
* @example
* ```javascript
* hmy.blockchain.getStorageAt({
* address: 'one1d0kw95t6kkljmkk9vu0zv25jraut8ngv5vrs5g',
* position: '0x0'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
address: ['isValidAddress', AssertType.required],
position: ['isHex', AssertType.required],
@ -600,6 +636,23 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Get the numbers of transactions sent from this address.
*
* @param address The address to get the numbers of transactions from
* @param blockNumber by default it's `latest`
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @return `Promise` - The number of transactions sent from the given address.
*
* @example
* ```javascript
* hmy.blockchain.getTransactionCount({
* address: "one1d0kw95t6kkljmkk9vu0zv25jraut8ngv5vrs5g"
* }).then((value) => {
* console.log(value.result);
* });
* ```
*/
@assertObject({
address: ['isValidAddress', AssertType.required],
blockNumber: ['isBlockNumber', AssertType.optional],
@ -623,6 +676,18 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Get the sharding structure of current network
*
* @return `Promise` - The sharding structure of current network.
*
* @example
* ```javascript
* hmy.blockchain.getShardingStructure().then((value) => {
* console.log(value);
* });
* ```
*/
async getShardingStructure() {
const result = await this.messenger.send(
RPCMethod.GetShardingStructure,
@ -632,6 +697,43 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Sends a signed transaction to the network.
*
* @param transaction `Object` - The transaction object to send:
* @return The **callbalck** will return the 32 bytes transaction hash
*
* @example
* ```javascript
* // add privateKey to wallet
* const privateKey = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e';
* hmy.wallet.addByPrivateKey(privateKey);
*
* async function transfer() {
* const txn = hmy.transactions.newTx({
* // token send to
* to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2',
* // amount to send
* value: '10000',
* // gas limit, you can use string
* gasLimit: '210000',
* // send token from shardID
* shardID: 0,
* // send token to toShardID
* toShardID: 0,
* // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
* gasPrice: new hmy.utils.Unit('100').asGwei().toWei(),
* });
*
* // sign the transaction use wallet;
* const signedTxn = await hmy.wallet.signTransaction(txn);
* const txnHash = await hmy.blockchain.sendTransaction(signedTxn);
* console.log(txnHash.result);
* }
*
* transfer();
* ```
*/
async sendTransaction(transaction: Transaction) {
if (!transaction.isSigned() || !transaction) {
throw new Error('transaction is not signed or not exist');
@ -647,6 +749,45 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Sends a raw transaction to the network.
*
* @param transaction `Object` - The transaction object to send:
* @return The **callbalck** will return the 32 bytes transaction hash
*
* @example
* ```javascript
* // add privateKey to wallet
* const privateKey = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e';
* hmy.wallet.addByPrivateKey(privateKey);
*
* async function transfer() {
* const txn = hmy.transactions.newTx({
* // token send to
* to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2',
* // amount to send
* value: '10000',
* // gas limit, you can use string
* gasLimit: '210000',
* // send token from shardID
* shardID: 0,
* // send token to toShardID
* toShardID: 0,
* // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
* gasPrice: new hmy.utils.Unit('100').asGwei().toWei(),
* });
*
* // sign the transaction use wallet;
* const signedTxn = await hmy.wallet.signTransaction(txn);
* recovered = signedTxn.recover(signedTxn.rawTransaction);
*
* const txnHash = await hmy.blockchain.sendRawTransaction(recovered);
* console.log(txnHash);
* }
*
* transfer();
* ```
*/
async sendRawTransaction(transaction: Transaction) {
if (!transaction.isSigned() || !transaction) {
throw new Error('transaction is not signed or not exist');
@ -657,6 +798,43 @@ class Blockchain {
}
}
/**
* send a transaction and check whether it exists
*
* @param transaction `Object` - The transaction object to send:
* @return The **callbalck** will return the 32 bytes transaction hash
*
* @example
* ```javascript
* // add privateKey to wallet
* const privateKey = '45e497bd45a9049bcb649016594489ac67b9f052a6cdf5cb74ee2427a60bf25e';
* hmy.wallet.addByPrivateKey(privateKey);
*
* async function transfer() {
* const txn = hmy.transactions.newTx({
* // token send to
* to: 'one166axnkjmghkf3df7xfvd0hn4dft8kemrza4cd2',
* // amount to send
* value: '10000',
* // gas limit, you can use string
* gasLimit: '210000',
* // send token from shardID
* shardID: 0,
* // send token to toShardID
* toShardID: 0,
* // gas Price, you can use Unit class, and use Gwei, then remember to use toWei(), which will be transformed to BN
* gasPrice: new hmy.utils.Unit('100').asGwei().toWei(),
* });
*
* // sign the transaction use wallet;
* const signedTxn = await hmy.wallet.signTransaction(txn);
* const txnHash = await hmy.blockchain.createObservedTransaction(signedTxn);
* console.log(txnHash);
* }
*
* transfer();
* ```
*/
createObservedTransaction(transaction: Transaction) {
try {
transaction.sendTransaction().then((response: any) => {
@ -672,6 +850,17 @@ class Blockchain {
}
}
/**
* send raw staking transaction
*
* @param staking
* @ignore
*
* @warning
* ```
* At present, this function is not implement yet, will Coming soon!!!
* ```
*/
async sendRawStakingTransaction(staking: StakingTransaction) {
if (!staking.isSigned() || !staking) {
throw new Error('staking transaction is not signed or not exist');
@ -681,6 +870,18 @@ class Blockchain {
return result;
}
}
/**
* send raw staking transaction and check whether it exists
*
* @param staking
* @ignore
*
* @warning
* ```
* At present, this function is not implement yet, will Coming soon!!!
* ```
*/
createObservedStakingTransaction(staking: StakingTransaction) {
try {
staking.sendTransaction().then((response: any) => {
@ -696,6 +897,29 @@ class Blockchain {
}
}
/**
* Executes a message call or transaction and returns the amount of the gas used.
*
* @param to the address will send to
* @param data the data will send to that address
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @return `promise` - the used gas for the simulated call/transaction.
*
* @warning
* ```
* At present, this function hmy_estimateGas is not implement yet, will Coming soon!!!
* ```
*
* @example
* ```javascript
* hmy.blockchain.estimateGas({
* to: 'one1d0kw95t6kkljmkk9vu0zv25jraut8ngv5vrs5g',
* data: '0xc6888fa10000000000000000000000000000000000000000000000000000000000000003'
* }).then((value) => {
* console.log(value);
* });
* ```
*/
@assertObject({
to: ['isValidAddress', AssertType.optional],
data: ['isHex', AssertType.optional],
@ -719,6 +943,19 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Returns the current gas price oracle. The gas price is determined by the last few blocks median gas price.
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
* @return `promise` - Number string of the current gas price in wei.
*
* @example
* ```javascript
* hmy.blockchain.gasPrice().then((value) => {
* console.log(value);
* });
* ```
*/
async gasPrice(shardID: number = this.messenger.currentShard) {
const result = await this.messenger.send(
RPCMethod.GasPrice,
@ -747,6 +984,28 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Return new pending Transactions
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
*
* @example
* ```javascript
* const hmy = new Harmony(
* // rpc url
* 'ws://api.s0.b.hmny.io/',
* {
* // chainType set to Harmony
* chainType: ChainType.Harmony,
* // chainType set to HmyLocal
* chainId: ChainID.HmyLocal,
* },
* );
*
* const tmp = hmy.blockchain.newPendingTransactions();
* console.log(tmp)
* ```
*/
newPendingTransactions(shardID: number = this.messenger.currentShard) {
if (this.messenger.provider instanceof WSProvider) {
return new NewPendingTransactions(this.messenger, shardID);
@ -755,6 +1014,27 @@ class Blockchain {
}
}
/**
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
*
* @example
* ```javascript
* const hmy = new Harmony(
* // rpc url
* 'ws://api.s0.b.hmny.io/',
* {
* // chainType set to Harmony
* chainType: ChainType.Harmony,
* // chainType set to HmyLocal
* chainId: ChainID.HmyLocal,
* },
* );
*
* const tmp = hmy.blockchain.newBlockHeaders();
* console.log(tmp)
* ```
*/
newBlockHeaders(shardID: number = this.messenger.currentShard) {
if (this.messenger.provider instanceof WSProvider) {
return new NewHeaders(this.messenger, shardID);
@ -763,6 +1043,27 @@ class Blockchain {
}
}
/**
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
*
* @example
* ```javascript
* const hmy = new Harmony(
* // rpc url
* 'ws://api.s0.b.hmny.io/',
* {
* // chainType set to Harmony
* chainType: ChainType.Harmony,
* // chainType set to HmyLocal
* chainId: ChainID.HmyLocal,
* },
* );
*
* const tmp = hmy.blockchain.syncing();
* console.log(tmp)
* ```
*/
syncing(shardID: number = this.messenger.currentShard) {
if (this.messenger.provider instanceof WSProvider) {
return new Syncing(this.messenger, shardID);
@ -771,6 +1072,29 @@ class Blockchain {
}
}
/**
*
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
*
* @example
* ```javascript
* const hmy = new Harmony(
* // rpc url
* 'ws://api.s0.b.hmny.io/',
* {
* // chainType set to Harmony
* chainType: ChainType.Harmony,
* // chainType set to HmyLocal
* chainId: ChainID.HmyLocal,
* },
* );
*
* const tmp = hmy.blockchain.logs({
* from: '0x12'
* });
* console.log(tmp)
* ```
*/
logs(options: any, shardID: number = this.messenger.currentShard) {
if (this.messenger.provider instanceof WSProvider) {
return new LogSub(options, this.messenger, shardID);

@ -20,20 +20,32 @@ import { Blockchain } from './blockchain';
import { ContractFactory } from '@harmony-js/contract';
import { HarmonyConfig } from './util';
/**
* @hidden
*/
export enum ExtensionType {
MathWallet = 'MathWallet',
}
/**
* @hidden
*/
export interface ExtensionAccount {
address: 'string';
name: 'string';
}
/**
* @hidden
*/
export interface ExtensionNetwork {
chain_url: string;
net_version: number;
}
/**
* @hidden
*/
export interface ExtensionInterface {
signTransaction: (
transaction: Transaction,

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-core
* @hidden
*/
export * from './harmony';

@ -13,6 +13,9 @@ import {
import { ChainID, ChainType, Unit } from '@harmony-js/utils';
import { HDNode } from '@harmony-js/account';
/**
* @hidden
*/
export interface ArgsResolver {
newArgs: any;
id: number;
@ -21,16 +24,27 @@ export interface ArgsResolver {
callback: (error: any, res?: any) => void;
}
/**
* @hidden
*/
export interface HDOptions {
menmonic?: string;
index: number;
addressCount: number;
}
/**
* @hidden
*/
export interface ChainOptions {
shardID: number;
chainType: ChainType;
chainId: ChainID;
}
/**
* @hidden
*/
export interface TransactionOptions {
gasLimit: string;
gasPrice: string;

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-core
* @hidden
*/
import { HttpProvider, Messenger } from '@harmony-js/network';

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-core
* @hidden
*/
import { ChainType, ChainID, defaultConfig } from '@harmony-js/utils';

Loading…
Cancel
Save