[breaking] move blockchain class to core

@types
neeboo 6 years ago
parent b2f659a7d0
commit 7f3473ce06
  1. 32
      examples/testNode.js
  2. 5
      packages/harmony-account/tsconfig.test.json
  3. 320
      packages/harmony-core/src/blockchain.ts
  4. 3
      packages/harmony-core/src/harmony.ts
  5. 3
      packages/harmony-core/src/types.ts
  6. 5
      packages/harmony-core/tsconfig.test.json
  7. 5
      packages/harmony-crypto/tsconfig.test.json
  8. 83
      packages/harmony-network/src/blockchain/blockchain.ts
  9. 8
      packages/harmony-network/src/index.ts
  10. 4
      packages/harmony-network/src/messenger/messenger.ts
  11. 2
      packages/harmony-network/src/providers/baseProvider.ts
  12. 2
      packages/harmony-network/src/providers/http.ts
  13. 320
      packages/harmony-network/src/rpcMethod/blockchain.ts
  14. 0
      packages/harmony-network/src/rpcMethod/net.ts
  15. 0
      packages/harmony-network/src/rpcMethod/rpc.ts
  16. 0
      packages/harmony-network/src/rpcMethod/rpcBuilder.ts
  17. 2
      packages/harmony-network/src/types.ts
  18. 5
      packages/harmony-network/tsconfig.test.json
  19. 5
      packages/harmony-transaction/tsconfig.test.json
  20. 8
      scripts/typings/schema.ts
  21. 9
      tsconfig.test.json

@ -20,9 +20,9 @@ server.listen(port, function(err, blockchain) {
// // console.log(acc.address);
acc.getBalance().then((c) => {
console.log(c);
});
// acc.getBalance().then((c) => {
// console.log(c);
// });
const txn = harmony.transactions.newTx({
nonce: 1,
to: sendTo,
@ -35,31 +35,7 @@ server.listen(port, function(err, blockchain) {
acc.signTransaction(txn, true).then((signed) => {
// console.log(signed.txPayload);
harmony.messenger
.send('hmy_sendTransaction', [signed.txPayload])
.then((res) => {
console.log(res);
txn.confirm(res).then((result) => {
harmony.blockchain
.getBlockByHash({
hash: result.receipt.blockHash,
returnObject: false,
})
.then((obj) => {
console.log(obj);
});
});
// harmony.blockchain.getBlockByHash({ hash: res }).then((obj) => {
// console.log(obj);
// });
});
// signed.sendTransaction().then((msg) => {
// const [txn, result] = msg;
// txn.confirm(result).then((result) => {
// console.log(`--------------`);
// console.log(result);
// });
// });
harmony.
});
// console.log(harmony.messenger.setRPCPrefix('eth_getPPP'));

@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.test.json",
"include": ["src", "test", "../../typings/**/*.d.ts"],
"references": []
}

@ -0,0 +1,320 @@
import { RPCMethod, Messenger } from '@harmony/network';
import {
assertObject,
AssertType,
HarmonyCore,
ChainType,
} from '@harmony/utils';
import { Transaction } from '@harmony/transaction';
class Blockchain extends HarmonyCore {
messenger: Messenger;
chainType: ChainType;
constructor(messenger: Messenger, chainType: ChainType = ChainType.Harmony) {
super(chainType);
this.messenger = messenger;
this.chainType = chainType;
}
setMessenger(messenger: Messenger) {
this.messenger = messenger;
}
/**
*
*/
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getBalance({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetBalance,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
returnObject: ['isBoolean', AssertType.optional],
})
async getBlockByHash({
blockHash,
returnObject = true,
}: {
blockHash: string;
returnObject: boolean;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockByHash,
[blockHash, returnObject],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
returnObject: ['isBoolean', AssertType.optional],
})
async getBlockByNumber({
blockNumber,
tag = 'latest',
returnObject = true,
}: {
blockNumber: string;
tag: string;
returnObject: boolean;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockByNumber,
[blockNumber || tag, returnObject],
this.chainPrefix,
);
return result;
}
@assertObject({
blockHash: ['isHash', AssertType.required],
})
async getBlockTransactionCountByHash({ blockHash }: { blockHash: string }) {
const result = await this.messenger.send(
RPCMethod.GetBlockTransactionCountByHash,
[blockHash],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockNumber: ['isHex', AssertType.required],
tag: ['isString', AssertType.optional],
})
async getBlockTransactionCountByNumber({
blockNumber,
tag = 'latest',
}: {
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockTransactionCountByNumber,
[blockNumber || tag],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
index: ['isHex', AssertType.required],
})
async getTransactionByBlockHashAndIndex({
blockHash,
index,
}: {
blockHash: string;
index: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetTransactionByBlockHashAndIndex,
[blockHash, index],
this.chainPrefix,
);
return result;
}
@assertObject({
blockNumber: ['isHex', AssertType.required],
index: ['isHex', AssertType.required],
tag: ['isString', AssertType.optional],
})
async getTransactionByBlockNumberAndIndex({
blockNumber,
index,
tag = 'latest',
}: {
blockNumber: string;
index: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetTransactionByBlockNumberAndIndex,
[blockNumber || tag, index],
this.chainPrefix,
);
return result;
}
@assertObject({
txnHash: ['isHash', AssertType.required],
})
async getTransactionByHash({ txnHash }: { txnHash: string }) {
const result = await this.messenger.send(
RPCMethod.GetTransactionByHash,
[txnHash],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
txnHash: ['isString', AssertType.required],
})
async getTransactionReceipt({ txnHash }: { txnHash: string }) {
const result = await this.messenger.send(
RPCMethod.GetTransactionReceipt,
[txnHash],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getCode({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetCode,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
/**
*
*/
async syncing() {
const result = await this.messenger.send(
RPCMethod.Syncing,
[],
this.chainPrefix,
);
if (result.responseType === 'raw') {
return result.result;
}
return result;
}
async net_peerCount() {
const result = await this.messenger.send(
RPCMethod.PeerCount,
[],
this.chainPrefix,
);
return result;
}
@assertObject({
address: ['isAddress', AssertType.required],
position: ['isHex', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getStorageAt({
address,
position,
blockNumber,
tag = 'latest',
}: {
address: string;
position: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetStorageAt,
[address, position, blockNumber || tag],
this.chainPrefix,
);
return result;
}
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getTransactionCount({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetTransactionCount,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
async sendTransaction(transaction: Transaction) {
if (!transaction.isSigned || !transaction) {
throw new Error('transaction is not signed or not exist');
}
const result = await this.messenger.send(
RPCMethod.SendTransaction,
[transaction.txPayload],
this.chainPrefix,
);
return result;
}
// async sendRawTransaction(transaction: Transaction) {
// if (!transaction.isSigned || !transaction) {
// throw new Error('transaction is not signed or not exist');
// }
// const [txn, result] = await transaction.sendTransaction();
// if (txn.isPending) {
// return result;
// }
// }
}
export { Blockchain };

@ -1,9 +1,10 @@
import * as crypto from '@harmony/crypto';
import * as utils from '@harmony/utils';
import { HttpProvider, Messenger, Blockchain } from '@harmony/network';
import { HttpProvider, Messenger } from '@harmony/network';
import { TransactionFactory, Transaction } from '@harmony/transaction';
import { Wallet, Account } from '@harmony/account';
import { Blockchain } from './blockchain';
class Harmony extends utils.HarmonyCore {
Modules = {

@ -1,6 +1,7 @@
import { HttpProvider, Messenger, Blockchain } from '@harmony/network';
import { HttpProvider, Messenger } from '@harmony/network';
import { TransactionFactory, Transaction } from '@harmony/transaction';
import { Wallet, Account } from '@harmony/account';
import { Blockchain } from './blockchain';
export interface HarmonyModule {
HttpProvider: HttpProvider;

@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.test.json",
"include": ["src", "test", "../../typings/**/*.d.ts"],
"references": []
}

@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.test.json",
"include": ["src", "test", "../../typings/**/*.d.ts"],
"references": []
}

@ -1,83 +0,0 @@
import { RPCMethod } from './rpc';
import { Messenger } from '../messenger/messenger';
import {
assertObject,
AssertType,
HarmonyCore,
ChainType,
} from '@harmony/utils';
class Blockchain extends HarmonyCore {
messenger: Messenger;
chainType: ChainType;
constructor(messenger: Messenger, chainType: ChainType = ChainType.Harmony) {
super(chainType);
this.messenger = messenger;
this.chainType = chainType;
}
setMessenger(messenger: Messenger) {
this.messenger = messenger;
}
/**
*
*/
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getBalance({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetBalance,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
@assertObject({
hash: ['isHash', AssertType.required],
returnObject: ['isBoolean', AssertType.optional],
})
async getBlockByHash({
hash,
returnObject = true,
}: {
hash: string;
returnObject: boolean;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockByHash,
[hash, returnObject],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
hash: ['isString', AssertType.required],
})
async getTransactionReceipt({ hash }: { hash: string }) {
const result = await this.messenger.send(
RPCMethod.GetTransactionReceipt,
[hash],
this.chainPrefix,
);
return result;
}
}
export { Blockchain };

@ -6,10 +6,10 @@ export * from './providers/http';
export * from './messenger/messenger';
export * from './messenger/responseMiddleware';
// rpc builder and blockchain method
export * from './blockchain/rpcBuilder';
export * from './blockchain/net';
export * from './blockchain/rpc';
export * from './blockchain/blockchain';
export * from './rpcMethod/rpcBuilder';
export * from './rpcMethod/net';
export * from './rpcMethod/rpc';
// utils
export * from './util';
// types

@ -1,9 +1,9 @@
import { HarmonyCore, ChainType, isString } from '@harmony/utils';
import { JsonRpc } from '../blockchain/rpcbuilder';
import { JsonRpc } from '../rpcMethod/rpcbuilder';
import { ResponseMiddleware } from './responseMiddleware';
import { HttpProvider } from '../providers/http';
import { getResultForData } from '../util';
import { RPCMethod } from '../blockchain/rpc';
import { RPCMethod } from '../rpcMethod/rpc';
const defaultConfig = {
Default: {

@ -1,5 +1,5 @@
import { ReqMiddleware, ResMiddleware, MiddlewareType } from '../types';
import { RPCMethod } from '../blockchain/rpc';
import { RPCMethod } from '../rpcMethod/rpc';
class BaseProvider {
middlewares = {

@ -5,7 +5,7 @@ import {
performRPC,
DEFAULT_TIMEOUT,
DEFAULT_HEADERS,
} from '../blockchain/net';
} from '../rpcMethod/net';
import { RPCRequestPayload } from '../types';

@ -0,0 +1,320 @@
import { RPCMethod } from './rpc';
import { Messenger } from '../messenger/messenger';
import {
assertObject,
AssertType,
HarmonyCore,
ChainType,
} from '@harmony/utils';
// import {Transaction} from '@harmony/transaction'
class Blockchain extends HarmonyCore {
messenger: Messenger;
chainType: ChainType;
constructor(messenger: Messenger, chainType: ChainType = ChainType.Harmony) {
super(chainType);
this.messenger = messenger;
this.chainType = chainType;
}
setMessenger(messenger: Messenger) {
this.messenger = messenger;
}
/**
*
*/
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getBalance({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetBalance,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
returnObject: ['isBoolean', AssertType.optional],
})
async getBlockByHash({
blockHash,
returnObject = true,
}: {
blockHash: string;
returnObject: boolean;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockByHash,
[blockHash, returnObject],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
returnObject: ['isBoolean', AssertType.optional],
})
async getBlockByNumber({
blockNumber,
tag = 'latest',
returnObject = true,
}: {
blockNumber: string;
tag: string;
returnObject: boolean;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockByNumber,
[blockNumber || tag, returnObject],
this.chainPrefix,
);
return result;
}
@assertObject({
blockHash: ['isHash', AssertType.required],
})
async getBlockTransactionCountByHash({ blockHash }: { blockHash: string }) {
const result = await this.messenger.send(
RPCMethod.GetBlockTransactionCountByHash,
[blockHash],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockNumber: ['isHex', AssertType.required],
tag: ['isString', AssertType.optional],
})
async getBlockTransactionCountByNumber({
blockNumber,
tag = 'latest',
}: {
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetBlockTransactionCountByNumber,
[blockNumber || tag],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
blockHash: ['isHash', AssertType.required],
index: ['isHex', AssertType.required],
})
async getTransactionByBlockHashAndIndex({
blockHash,
index,
}: {
blockHash: string;
index: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetTransactionByBlockHashAndIndex,
[blockHash, index],
this.chainPrefix,
);
return result;
}
@assertObject({
blockNumber: ['isHex', AssertType.required],
index: ['isHex', AssertType.required],
tag: ['isString', AssertType.optional],
})
async getTransactionByBlockNumberAndIndex({
blockNumber,
index,
tag = 'latest',
}: {
blockNumber: string;
index: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetTransactionByBlockNumberAndIndex,
[blockNumber || tag, index],
this.chainPrefix,
);
return result;
}
@assertObject({
txnHash: ['isHash', AssertType.required],
})
async getTransactionByHash({ txnHash }: { txnHash: string }) {
const result = await this.messenger.send(
RPCMethod.GetTransactionByHash,
[txnHash],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
txnHash: ['isString', AssertType.required],
})
async getTransactionReceipt({ txnHash }: { txnHash: string }) {
const result = await this.messenger.send(
RPCMethod.GetTransactionReceipt,
[txnHash],
this.chainPrefix,
);
return result;
}
/**
*
*/
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getCode({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetCode,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
/**
*
*/
async syncing() {
const result = await this.messenger.send(
RPCMethod.Syncing,
[],
this.chainPrefix,
);
if (result.responseType === 'raw') {
return result.result;
}
return result;
}
async net_peerCount() {
const result = await this.messenger.send(
RPCMethod.PeerCount,
[],
this.chainPrefix,
);
return result;
}
@assertObject({
address: ['isAddress', AssertType.required],
position: ['isHex', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getStorageAt({
address,
position,
blockNumber,
tag = 'latest',
}: {
address: string;
position: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetStorageAt,
[address, position, blockNumber || tag],
this.chainPrefix,
);
return result;
}
@assertObject({
address: ['isAddress', AssertType.required],
blockNumber: ['isHex', AssertType.optional],
tag: ['isString', AssertType.optional],
})
async getTransactionCount({
address,
blockNumber,
tag = 'latest',
}: {
address: string;
blockNumber: string;
tag: string;
}) {
const result = await this.messenger.send(
RPCMethod.GetTransactionCount,
[address, blockNumber || tag],
this.chainPrefix,
);
return result;
}
// async sendTransaction(transaction: Transaction) {
// if (!transaction.isSigned || !transaction) {
// throw new Error('transaction is not signed or not exist');
// }
// const result = await this.messenger.send(
// RPCMethod.SendTransaction,
// [transaction.txPayload],
// this.chainPrefix,
// );
// return result;
// }
// async sendRawTransaction(transaction: Transaction) {
// if (!transaction.isSigned || !transaction) {
// throw new Error('transaction is not signed or not exist');
// }
// const [txn, result] = await transaction.sendTransaction();
// if (txn.isPending) {
// return result;
// }
// }
}
export { Blockchain };

@ -1,4 +1,4 @@
import { RPCMethod, RPCErrorCode } from './blockchain/rpc';
import { RPCMethod, RPCErrorCode } from './rpcMethod/rpc';
export type ReqMiddleware = Map<string | RPCMethod | RegExp, any[]>;
export type ResMiddleware = Map<string | RPCMethod | RegExp, any[]>;

@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.test.json",
"include": ["src", "test", "../../typings/**/*.d.ts"],
"references": []
}

@ -0,0 +1,5 @@
{
"extends": "../../tsconfig.test.json",
"include": ["src", "test", "../../typings/**/*.d.ts"],
"references": []
}

@ -39,10 +39,10 @@ async function generateSchemas() {
const schema = schemas.generateSchema(prog, '*', settings);
fs.writeFileSync(
path.join(pkgPath, 'test', 'schema.json'),
JSON.stringify(schema, undefined, 2),
);
// fs.writeFileSync(
// path.join(pkgPath, 'test', 'schema.json'),
// JSON.stringify(schema, undefined, 2),
// );
});
}

@ -2,5 +2,12 @@
"extends": "./tsconfig.base.json",
"files": [],
"include": ["./typings/**/*.d.ts"],
"references": [{ "path": "packages/harmony-utils" }]
"references": [
{ "path": "packages/harmony-core" },
{ "path": "packages/harmony-account" },
{ "path": "packages/harmony-crypto" },
{ "path": "packages/harmony-utils" },
{ "path": "packages/harmony-network" },
{ "path": "packages/harmony-transaction" }
]
}

Loading…
Cancel
Save