parent
b823f1a03c
commit
cff1cda279
File diff suppressed because it is too large
Load Diff
@ -1,8 +1,9 @@ |
||||
{ |
||||
"extends": "../../tsconfig.base.json", |
||||
"compilerOptions": { |
||||
"outDir": "lib", |
||||
"outDir": "dist", |
||||
"rootDir": "src" |
||||
}, |
||||
"include": ["src", "../../typings/**/*.d.ts"] |
||||
"include": ["src", "../../typings/**/*.d.ts"], |
||||
"references": [{ "path": "../harmony-utils" }] |
||||
} |
||||
|
@ -0,0 +1,148 @@ |
||||
import { JsonRpc } from './rpcbuilder'; |
||||
import { ResponseMiddleware } from './responseMiddleware'; |
||||
import { HttpProvider } from './http'; |
||||
import { getResultForData } from './util'; |
||||
import { RPCMethod } from './rpc'; |
||||
|
||||
const defaultConfig = { |
||||
Default: { |
||||
CHAIN_ID: 0, |
||||
Network_ID: 'Default', |
||||
nodeProviderUrl: 'http://localhost:4200', |
||||
}, |
||||
DevNet: { |
||||
CHAIN_ID: 333, |
||||
Network_ID: 'DevNet', |
||||
nodeProviderUrl: 'https://devnet.harmony.one', |
||||
}, |
||||
TestNet: { |
||||
CHAIN_ID: 2, |
||||
Network_ID: 'TestNet', |
||||
nodeProviderUrl: 'https://devnet.harmony.one', |
||||
}, |
||||
MainNet: { |
||||
CHAIN_ID: 1, |
||||
Network_ID: 'MainNet', |
||||
nodeProviderUrl: 'https://mainnet.harmony.one', |
||||
}, |
||||
}; |
||||
|
||||
/** |
||||
* @class Messenger |
||||
* @description Messenger instance |
||||
* @param {HttpProvider} provider HttpProvider |
||||
* @param {Object} config config object |
||||
* @return {Messenger} Messenger instance |
||||
*/ |
||||
class Messenger { |
||||
provider: HttpProvider; |
||||
config?: object; |
||||
// tslint:disable-next-line: variable-name
|
||||
Network_ID: string = 'Default'; |
||||
JsonRpc: JsonRpc; |
||||
|
||||
constructor(provider: HttpProvider, config?: object) { |
||||
/** |
||||
* @var {Provider} provider |
||||
* @memberof Messenger.prototype |
||||
* @description Provider instance |
||||
*/ |
||||
this.provider = provider; |
||||
|
||||
/** |
||||
* @var {Object} config |
||||
* @memberof Messenger.prototype |
||||
* @description Messenger config |
||||
*/ |
||||
this.config = config || defaultConfig; |
||||
/** |
||||
* @var {Number} Network_ID |
||||
* @memberof Messenger.prototype |
||||
* @description Network ID for current provider |
||||
*/ |
||||
|
||||
/** |
||||
* @var {JsonRpc} JsonRpc |
||||
* @memberof Messenger.prototype |
||||
* @description JsonRpc instance |
||||
*/ |
||||
this.JsonRpc = new JsonRpc(); |
||||
|
||||
// set Network ID
|
||||
this.setNetworkID(defaultConfig.Default.Network_ID); |
||||
} |
||||
|
||||
/** |
||||
* @function send |
||||
* @memberof Messenger.prototype |
||||
* @param {String} method - RPC method |
||||
* @param {Object} params - RPC method params |
||||
* @return {Object} RPC result |
||||
*/ |
||||
send = async (method: RPCMethod, params: any) => { |
||||
this.providerCheck(); |
||||
try { |
||||
const payload = this.JsonRpc.toPayload(method, params); |
||||
this.setResMiddleware((data: any) => new ResponseMiddleware(data)); |
||||
const result = await this.provider.send(payload); |
||||
return getResultForData(result); // getResultForData(result)
|
||||
} catch (e) { |
||||
throw new Error(e); |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* @function setProvider |
||||
* @memberof Messenger |
||||
* @description provider setter |
||||
* @param {Provider} provider - provider instance |
||||
*/ |
||||
setProvider(provider: HttpProvider) { |
||||
this.provider = provider; |
||||
} |
||||
|
||||
/** |
||||
* @function providerCheck |
||||
* @memberof Messenger |
||||
* @description provider checker |
||||
* @return {Error|null} provider validator |
||||
*/ |
||||
providerCheck() { |
||||
if (!this.provider) { |
||||
throw new Error('provider is not found'); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @function setReqMiddleware |
||||
* @description set request middleware |
||||
* @memberof Messenger |
||||
* @param {any} middleware - middle ware for req |
||||
* @param {String} method - method name |
||||
*/ |
||||
setReqMiddleware(middleware: any, method = '*') { |
||||
return this.provider.middlewares.request.use(middleware, method); |
||||
} |
||||
|
||||
/** |
||||
* @function setResMiddleware |
||||
* @description set response middleware |
||||
* @memberof Messenger |
||||
* @param {any} middleware - middle ware for req |
||||
* @param {String} method - method name |
||||
*/ |
||||
setResMiddleware(middleware: any, method = '*') { |
||||
return this.provider.middlewares.response.use(middleware, method); |
||||
} |
||||
|
||||
/** |
||||
* @function setNetworkID |
||||
* @description set network id |
||||
* @memberof Messenger |
||||
* @param {String} id network id string |
||||
*/ |
||||
setNetworkID(id: string) { |
||||
this.Network_ID = id; |
||||
} |
||||
} |
||||
export { Messenger }; |
@ -0,0 +1,34 @@ |
||||
import { RPCResponseBody } from './types'; |
||||
/** |
||||
* @class ResponseMiddleware |
||||
* @description Response middleware of RPC |
||||
* @param {Object} ResponseBody - response from rpc |
||||
* @return {ResponseMiddleware} response middleware instance |
||||
*/ |
||||
class ResponseMiddleware { |
||||
result: any; |
||||
error: any; |
||||
raw: any; |
||||
constructor(ResponseBody: RPCResponseBody<any, any>) { |
||||
this.result = ResponseBody.result; |
||||
this.error = ResponseBody.error; |
||||
this.raw = ResponseBody; |
||||
} |
||||
|
||||
get getResult() { |
||||
return typeof this.result === 'string' |
||||
? this.result |
||||
: { ...this.result, responseType: 'result' }; |
||||
} |
||||
|
||||
get getError() { |
||||
return typeof this.error === 'string' |
||||
? this.error |
||||
: { ...this.error, responseType: 'error' }; |
||||
} |
||||
|
||||
get getRaw() { |
||||
return { ...this.raw, responseType: 'raw' }; |
||||
} |
||||
} |
||||
export { ResponseMiddleware }; |
@ -0,0 +1,46 @@ |
||||
import { RPCRequestPayload } from './types'; |
||||
import { RPCMethod } from './rpc'; |
||||
/** |
||||
* @class JsonRpc |
||||
* @description json rpc instance |
||||
* @return {JsonRpc} Json RPC instance |
||||
*/ |
||||
class JsonRpc { |
||||
messageId: number; |
||||
constructor() { |
||||
/** |
||||
* @var {Number} messageId |
||||
* @memberof JsonRpc.prototype |
||||
* @description message id, default 0 |
||||
*/ |
||||
this.messageId = 0; |
||||
} |
||||
|
||||
/** |
||||
* @function toPayload |
||||
* @memberof JsonRpc.prototype |
||||
* @description convert method and params to payload object |
||||
* @param {String} method - RPC method |
||||
* @param {Array<object>} params - params that send to RPC |
||||
* @return {Object} payload object |
||||
*/ |
||||
toPayload = ( |
||||
method: RPCMethod, |
||||
params: string, |
||||
): RPCRequestPayload<object> => { |
||||
// FIXME: error to be done by shared/errors
|
||||
if (!method) throw new Error('jsonrpc method should be specified!'); |
||||
|
||||
// advance message ID
|
||||
this.messageId += 1; |
||||
|
||||
return { |
||||
jsonrpc: '2.0', |
||||
id: this.messageId, |
||||
method, |
||||
params: params !== undefined ? [params] : [], |
||||
}; |
||||
}; |
||||
} |
||||
|
||||
export { JsonRpc }; |
@ -0,0 +1,15 @@ |
||||
/** |
||||
* @function getResultForData |
||||
* @description get result for data by default |
||||
* @param {any} data - object get from provider |
||||
* @return {any} data result or data |
||||
*/ |
||||
export function getResultForData(data: any): any { |
||||
if (data.result) { |
||||
return data.getResult; |
||||
} |
||||
if (data.error) { |
||||
return data.getError; |
||||
} |
||||
return data.getRaw; |
||||
} |
@ -1 +1,2 @@ |
||||
export * from './validators'; |
||||
export * from './transformers'; |
||||
|
@ -0,0 +1,67 @@ |
||||
import BN from 'bn.js'; |
||||
import { isString, isNumber, isHex } from './validators'; |
||||
|
||||
export const enum Units { |
||||
wei = 'wei', |
||||
kwei = 'kwei', |
||||
Mwei = 'Mwei', |
||||
Gwei = 'Gwei', |
||||
szabo = 'szabo', |
||||
finney = 'finney', |
||||
ether = 'ether', |
||||
} |
||||
|
||||
export const numberToString = ( |
||||
obj: BN | number | string, |
||||
radix: number = 10, |
||||
): string => { |
||||
if (BN.isBN(obj)) { |
||||
return obj.toString(radix); |
||||
} else if (isNumber(obj)) { |
||||
return new BN(obj).toString(radix); |
||||
} else if (isString(obj) && isNumber(Number(obj))) { |
||||
return new BN(obj).toString(radix); |
||||
} else { |
||||
throw new Error(`cannot parse number:${obj} to string`); |
||||
} |
||||
}; |
||||
|
||||
export const add0xToString = (obj: string): string => { |
||||
if (isString(obj) && !obj.startsWith('-')) { |
||||
return '0x' + obj.replace('0x', ''); |
||||
} else if (isString(obj) && obj.startsWith('-')) { |
||||
return '-0x' + obj.replace('-', ''); |
||||
} else { |
||||
throw new Error(`${obj} is not String`); |
||||
} |
||||
}; |
||||
|
||||
export const strip0x = (obj: string): string => { |
||||
return obj.toLowerCase().replace('0x', ''); |
||||
}; |
||||
|
||||
export const numberToHex = (obj: any): string => { |
||||
try { |
||||
return add0xToString(numberToString(obj, 16)); |
||||
} catch (error) { |
||||
throw error; |
||||
} |
||||
}; |
||||
|
||||
export const hexToNumber = (hex: string): string => { |
||||
if (isHex(hex)) { |
||||
return new BN(strip0x(hex), 'hex').toString(); |
||||
} else { |
||||
throw new Error(`${hex} is not hex number`); |
||||
} |
||||
}; |
||||
|
||||
export const toWei = (obj: any): string => { |
||||
return ''; |
||||
}; |
||||
|
||||
export const fromWei = (obj: any): string => { |
||||
return ''; |
||||
}; |
||||
|
||||
export class Unit {} |
@ -0,0 +1,103 @@ |
||||
import * as path from 'path'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import * as rollup from 'rollup'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import alias from 'rollup-plugin-alias'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import builtins from 'rollup-plugin-node-builtins'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import commonjs from 'rollup-plugin-commonjs'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import license from 'rollup-plugin-license'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import json from 'rollup-plugin-json'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import globals from 'rollup-plugin-node-globals'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import resolve from 'rollup-plugin-node-resolve'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import typescript2 from 'rollup-plugin-typescript2'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import ts from 'typescript'; |
||||
|
||||
import { projects, preProcessFunc, preProcessProjects } from './projects'; |
||||
|
||||
async function bundles() { |
||||
await preProcessFunc(preProcessProjects); |
||||
|
||||
for (const pkg of projects) { |
||||
const base = { |
||||
input: path.join(pkg.src, 'index.ts'), |
||||
plugins: [ |
||||
alias({ |
||||
elliptic: path.resolve( |
||||
__dirname, |
||||
'../', |
||||
'includes/elliptic/elliptic.js', |
||||
), |
||||
}), |
||||
resolve({ |
||||
browser: true, |
||||
jsnext: true, |
||||
preferBuiltins: true, |
||||
}), |
||||
// babel(browserConfig),
|
||||
globals(), |
||||
builtins(), |
||||
commonjs(), |
||||
json(), |
||||
typescript2({ |
||||
typescript: ts, // ensure we're using the same typescript (3.x) for rollup as for regular builds etc
|
||||
tsconfig: path.join(pkg.path, 'tsconfig.json'), |
||||
tsconfigOverride: { |
||||
module: 'esnext', |
||||
stripInternal: true, |
||||
emitDeclarationOnly: false, |
||||
composite: false, |
||||
declaration: false, |
||||
declarationMap: false, |
||||
sourceMap: true, |
||||
}, |
||||
}), |
||||
license({ |
||||
banner: `Test Banner`, |
||||
}), |
||||
], |
||||
external: projects |
||||
.filter((p) => p.name !== pkg.name) |
||||
.map((p) => p.scopedName) |
||||
.concat(['cross-fetch']), |
||||
}; |
||||
|
||||
const pkgBundler = await rollup.rollup(base); |
||||
|
||||
await pkgBundler.write({ |
||||
file: pkg.esm, |
||||
name: pkg.globalName, |
||||
format: 'esm', |
||||
sourcemap: true, |
||||
}); |
||||
await pkgBundler.write({ |
||||
file: pkg.umd, |
||||
exports: 'named', |
||||
name: pkg.globalName, |
||||
globals: { |
||||
...projects.reduce((g, pkg) => { |
||||
g[pkg.scopedName] = pkg.globalName; |
||||
return g; |
||||
}, {}), |
||||
tslib: 'tslib', |
||||
}, |
||||
format: 'umd', |
||||
sourcemap: true, |
||||
}); |
||||
await pkgBundler.write({ |
||||
file: pkg.cjs, |
||||
name: pkg.globalName, |
||||
format: 'cjs', |
||||
sourcemap: true, |
||||
}); |
||||
} |
||||
} |
||||
|
||||
bundles(); |
@ -0,0 +1,91 @@ |
||||
import * as path from 'path'; |
||||
import * as fs from 'fs'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import webpack from 'webpack'; |
||||
// tslint:disable-next-line: no-implicit-dependencies
|
||||
import camelCase from 'camelcase'; |
||||
|
||||
export const rootPath = path.resolve(__dirname, '../'); |
||||
export const includesPath = path.join(rootPath, 'includes'); |
||||
export const packagesPath = path.join(rootPath, 'packages'); |
||||
|
||||
export const preProcessProjects = { |
||||
preprocess: [ |
||||
{ |
||||
name: 'elliptic', |
||||
path: path.resolve(__dirname, '../node_modules/elliptic'), |
||||
entry: 'lib/elliptic.js', |
||||
outDir: path.join(includesPath, 'elliptic'), |
||||
}, |
||||
], |
||||
}; |
||||
|
||||
export function preProcessFunc(project) { |
||||
const modules = project.preprocess.map((mod) => { |
||||
return new Promise((resolve, reject) => { |
||||
const compiler = webpack({ |
||||
entry: { |
||||
[mod.name]: path.join(mod.path, mod.entry), |
||||
}, |
||||
output: { |
||||
filename: '[name].js', |
||||
library: mod.name, |
||||
libraryTarget: 'commonjs2', |
||||
path: mod.outDir, |
||||
}, |
||||
mode: 'production', |
||||
optimization: { |
||||
minimize: false, |
||||
}, |
||||
}); |
||||
|
||||
compiler.run((err, stats) => { |
||||
if (err) { |
||||
reject(err); |
||||
} else { |
||||
// logPreProcess(
|
||||
// `Successfully preprocessed ${Object.keys(
|
||||
// stats.compilation.assets,
|
||||
// ).join(' ,')}`,
|
||||
// );
|
||||
resolve(stats); |
||||
} |
||||
}); |
||||
}); |
||||
}); |
||||
|
||||
return Promise.all(modules); |
||||
} |
||||
|
||||
export const projects = fs |
||||
.readdirSync(packagesPath) |
||||
.filter((p) => fs.lstatSync(path.join(packagesPath, p)).isDirectory()) |
||||
.map((p) => { |
||||
const pkgName = path.basename(p); |
||||
const pkgGlobalName = camelCase(pkgName); |
||||
const pkgPath = path.join(packagesPath, p); |
||||
const pkgSrc = path.join(pkgPath, 'src'); |
||||
const pkgScopedName = `@harmony/${p.replace('harmony-', '')}`; |
||||
const pkgDist = path.join(pkgPath, 'dist'); |
||||
|
||||
const pkgUmd = path.join(pkgDist, 'index.umd.js'); |
||||
const pkgEsm = path.join(pkgDist, 'index.esm.js'); |
||||
const pkgSystem = path.join(pkgDist, 'index.system.js'); |
||||
const pkgAmd = path.join(pkgDist, 'index.amd.js'); |
||||
const pkgCjs = path.join(pkgDist, 'index.cjs.js'); |
||||
const pkgIife = path.join(pkgDist, 'index.js'); |
||||
return { |
||||
name: pkgName, |
||||
globalName: pkgGlobalName, |
||||
scopedName: pkgScopedName, |
||||
path: pkgPath, |
||||
src: pkgSrc, |
||||
dist: pkgDist, |
||||
umd: pkgUmd, |
||||
esm: pkgEsm, |
||||
cjs: pkgCjs, |
||||
amd: pkgAmd, |
||||
iife: pkgIife, |
||||
system: pkgSystem, |
||||
}; |
||||
}); |
Loading…
Reference in new issue