Finished revamped the package: harmony-core

pull/21/head
Wen Zhang 5 years ago committed by Ganesha Upadhyaya
parent 2d637dede9
commit 1d37b68d79
  1. 1
      package.json
  2. 2
      packages/harmony-account/guide.ts
  3. 1
      packages/harmony-contract/src/abi/abiCoder.ts
  4. 1
      packages/harmony-contract/src/abi/api.ts
  5. 1
      packages/harmony-contract/src/abi/index.ts
  6. 1
      packages/harmony-contract/src/abi/utils.ts
  7. 17
      packages/harmony-core/src/blockchain.ts
  8. 121
      packages/harmony-core/src/harmony.ts
  9. 86
      packages/harmony-core/src/harmonyExtension.ts
  10. 13
      packages/harmony-core/src/truffleProvider.ts

@ -127,6 +127,7 @@
"tslint-config-prettier": "^1.15.0",
"typedoc": "^0.17.0-3",
"typedoc-plugin-external-module-name": "^3.0.0",
"typedoc-plugin-no-inherit": "^1.1.10",
"typescript": "^3.8.3",
"typescript-json-schema": "^0.36.0",
"webpack": "^4.20.2",

@ -259,4 +259,6 @@ The `Wallet` class is class that stores all `Account` instance, you can do CRUD
* @packageDocumentation
* @module harmony-account
*/
/**@ignore */
export interface README {}

@ -1,4 +1,5 @@
/**
*
* @packageDocumentation
* @module harmony-contract
*/

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-contract
* @hidden
*/
import { AbiCoder as ABICoder, ParamType, toUtf8Bytes } from './abiCoder';

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-contract
* @hidden
*/
import { AbiCoderClass } from './api';

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-contract
* @hidden
*/
import { isObject, isArray } from '@harmony-js/utils';

@ -966,6 +966,23 @@ class Blockchain {
return this.getRpcResult(result);
}
/**
* Executes a message call transaction,
* which is directly executed in the VM of the node, but never mined into the blockchain.
*
* @param payload some data you want put into these fucntions
* @param blockNumber by default it's `latest`
* @param shardID `shardID` is binding with the endpoint, IGNORE it!
*
* @example
* ```javascript
* hmy.blockchain.call({
* to: "0x08AE1abFE01aEA60a47663bCe0794eCCD5763c19",
* }).then((value) => {
* console.log(value);
* });
* ```
*/
async call({
payload,
blockNumber = DefaultBlockParams.latest,

@ -15,6 +15,7 @@ import { Blockchain } from './blockchain';
import { HarmonyConfig } from './util';
export class Harmony extends utils.HarmonyCore {
/**@ignore*/
Modules = {
HttpProvider,
WSProvider,
@ -28,17 +29,57 @@ export class Harmony extends utils.HarmonyCore {
Account,
Contract,
};
/**@ignore*/
messenger: Messenger;
/**@ignore*/
transactions: TransactionFactory;
/**@ignore*/
stakings: StakingFactory;
/**@ignore*/
wallet: Wallet;
/**@ignore*/
blockchain: Blockchain;
/**@ignore*/
contracts: ContractFactory;
/**@ignore*/
crypto: any;
/**@ignore*/
utils: any;
/**@ignore*/
defaultShardID?: number;
/**@ignore*/
private provider: HttpProvider | WSProvider;
/**
* Create a harmony instance
*
* @param url The end-points of the hmy blockchain
* @param config set up `ChainID` and `ChainType`, typically we can use the default values
*
* @example
* ```
* // import or require Harmony class
* const { Harmony } = require('@harmony-js/core');
*
* // import or require settings
* const { ChainID, ChainType } = require('@harmony-js/utils');
*
* // Initialize the Harmony instance
* const hmy = new Harmony(
* // rpc url:
* // local: http://localhost:9500
* // testnet: https://api.s0.b.hmny.io/
* // mainnet: https://api.s0.t.hmny.io/
* 'http://localhost:9500',
* {
* // chainType set to Harmony
* chainType: ChainType.Harmony,
* // chainType set to HmyLocal
* chainId: ChainID.HmyLocal,
* },
* );
* ```
*/
constructor(
url: string,
config: HarmonyConfig = {
@ -64,28 +105,106 @@ export class Harmony extends utils.HarmonyCore {
this.setShardID(this.defaultShardID);
}
}
/**
* Will change the provider for its module.
*
* @param provider a valid provider, you can replace it with your own working node
*
* @example
* ```javascript
* const tmp = hmy.setProvider('http://localhost:9500');
* ```
*/
public setProvider(provider: string | HttpProvider | WSProvider): void {
this.provider = new Provider(provider).provider;
this.messenger.setProvider(this.provider);
this.setMessenger(this.messenger);
}
/**
* set the chainID
*
* @hint
* ```
* Default = 0,
* EthMainnet = 1,
Morden = 2,
Ropsten = 3,
Rinkeby = 4,
RootstockMainnet = 30,
RootstockTestnet = 31,
Kovan = 42,
EtcMainnet = 61,
EtcTestnet = 62,
Geth = 1337,
Ganache = 0,
HmyMainnet = 1,
HmyTestnet = 2,
HmyLocal = 2,
HmyPangaea = 3
* ```
* @param chainId
*
* @example
* ```
* hmy.setChainId(2);
* ```
*/
public setChainId(chainId: utils.ChainID) {
this.chainId = chainId;
this.messenger.setChainId(this.chainId);
this.setMessenger(this.messenger);
}
/**
* Change the Shard ID
*
* @example
* ```
* hmy.setShardID(2);
* ```
*/
public setShardID(shardID: number) {
this.defaultShardID = shardID;
this.messenger.setDefaultShardID(this.defaultShardID);
this.setMessenger(this.messenger);
}
/**
* set the chainType
*
* @param chainType `hmy` or `eth`
*
* @example
* ```
* // set chainType to hmy
* hmy.setChainType('hmy');
* // set chainType to eth
* hmy.setChainType('eth');
* ```
*/
public setChainType(chainType: utils.ChainType) {
this.chainType = chainType;
this.messenger.setChainType(this.chainType);
this.setMessenger(this.messenger);
}
/**
* Set the sharding Structure
*
* @param shardingStructures The array of information of sharding structures
*
* @example
* ```javascript
* hmy.shardingStructures([
* {"current":true,"http":"http://127.0.0.1:9500",
* "shardID":0,"ws":"ws://127.0.0.1:9800"},
* {"current":false,"http":"http://127.0.0.1:9501",
* "shardID":1,"ws":"ws://127.0.0.1:9801"}
* ]);
* ```
*/
public shardingStructures(shardingStructures: ShardingItem[]) {
for (const shard of shardingStructures) {
const shardID =
@ -99,6 +218,8 @@ export class Harmony extends utils.HarmonyCore {
}
this.setMessenger(this.messenger);
}
/**@ignore*/
private setMessenger(messenger: Messenger) {
this.blockchain.setMessenger(messenger);
this.wallet.setMessenger(messenger);

@ -20,32 +20,24 @@ import { Blockchain } from './blockchain';
import { ContractFactory } from '@harmony-js/contract';
import { HarmonyConfig } from './util';
/**
* @hidden
*/
/** @hidden */
export enum ExtensionType {
MathWallet = 'MathWallet',
}
/**
* @hidden
*/
/** @hidden */
export interface ExtensionAccount {
address: 'string';
name: 'string';
}
/**
* @hidden
*/
/** @hidden */
export interface ExtensionNetwork {
chain_url: string;
net_version: number;
}
/**
* @hidden
*/
/** @hidden */
export interface ExtensionInterface {
signTransaction: (
transaction: Transaction,
@ -61,17 +53,41 @@ export interface ExtensionInterface {
}
export class HarmonyExtension {
/**@ignore*/
extensionType: ExtensionType | null;
/**@ignore*/
wallet: ExtensionInterface;
/**@ignore*/
provider: HttpProvider | WSProvider;
/**@ignore*/
messenger: Messenger;
/**@ignore*/
blockchain: Blockchain;
/**@ignore*/
transactions: TransactionFactory;
/**@ignore*/
contracts: ContractFactory;
/**@ignore*/
crypto: any;
/**@ignore*/
utils: any;
/**@ignore*/
defaultShardID?: number;
/**
* Create an blockchain instance support wallet injection
*
* @param wallet could be MathWallet instance
* @param config (optional), using default `Chain_Id` and `Chain_Type`
*
* @example
* ```javascript
* // Using Mathwallet instance
* export const initEx = async() => {
* hmyEx = await new HarmonyExtension(window.harmony);
* }
* ```
*/
constructor(
wallet: ExtensionInterface,
config: HarmonyConfig = {
@ -98,12 +114,31 @@ export class HarmonyExtension {
this.crypto = crypto;
this.utils = utils;
}
/**
* Will change the provider for its module.
*
* @param provider a valid provider, you can replace it with your own working node
*
* @example
* ```javascript
* const tmp = hmyEx.setProvider('http://localhost:9500');
* ```
*/
public setProvider(provider: string | HttpProvider | WSProvider): void {
this.provider = new Provider(provider).provider;
this.messenger.setProvider(this.provider);
this.setMessenger(this.messenger);
}
/**
* Change the Shard ID
*
* @example
* ```
* hmyEx.setShardID(2);
* ```
*/
public setShardID(shardID: number) {
this.defaultShardID = shardID;
this.messenger.setDefaultShardID(this.defaultShardID);
@ -156,12 +191,37 @@ export class HarmonyExtension {
}
return;
}
/**
* Get the wallet account
*
* @example
* ```javascript
* const account = hmyEx.login();
* console.log(account);
* ```
*/
public async login() {
const account = await this.wallet.getAccount();
// Use address
return account;
}
/**
* Set the sharding Structure
*
* @param shardingStructures The array of information of sharding structures
*
* @example
* ```javascript
* hmyEx.shardingStructures([
* {"current":true,"http":"http://127.0.0.1:9500",
* "shardID":0,"ws":"ws://127.0.0.1:9800"},
* {"current":false,"http":"http://127.0.0.1:9501",
* "shardID":1,"ws":"ws://127.0.0.1:9801"}
* ]);
* ```
*/
public shardingStructures(shardingStructures: ShardingItem[]) {
for (const shard of shardingStructures) {
const shardID =
@ -175,6 +235,8 @@ export class HarmonyExtension {
}
this.setMessenger(this.messenger);
}
/**@ignore*/
private setMessenger(messenger: Messenger) {
this.blockchain.setMessenger(messenger);
this.wallet.messenger = messenger;

@ -1,6 +1,7 @@
/**
* @packageDocumentation
* @module harmony-core
* @hidden
*/
import {
@ -13,9 +14,6 @@ import {
import { ChainID, ChainType, Unit } from '@harmony-js/utils';
import { HDNode } from '@harmony-js/account';
/**
* @hidden
*/
export interface ArgsResolver {
newArgs: any;
id: number;
@ -24,27 +22,18 @@ 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;

Loading…
Cancel
Save