[WIP] added branch @types

@types
neeboo 6 years ago
parent a36b9d862e
commit 70484f6c27
  1. 1
      .gitignore
  2. 65
      packages/harmony-contract/src/events/event.ts
  3. 51
      packages/harmony-core/src/blockchain.ts
  4. 4
      packages/harmony-network/src/index.ts
  5. 38
      packages/harmony-network/src/subscriptions/LogSub.ts
  6. 5
      packages/harmony-network/src/subscriptions/NewHeadersSub.ts
  7. 8
      packages/harmony-network/src/subscriptions/NewPendingTransactionsSub.ts
  8. 29
      packages/harmony-network/src/subscriptions/Subscription.ts
  9. 7
      packages/harmony-network/src/subscriptions/SyncingSub.ts
  10. 6
      packages/harmony-transaction/src/transaction.ts

1
.gitignore vendored

@ -4,6 +4,7 @@
*.lock *.lock
*.tsbuildinfo *.tsbuildinfo
.vscode/ .vscode/
devTestOnly/
# package.json # package.json

@ -1,13 +1,12 @@
import { AbiItemModel } from '../models/types'; import { AbiItemModel } from '../models/types';
import { Messenger, RPCMethod, WSProvider } from '@harmony-js/network'; import { LogSub } from '@harmony-js/network';
import { Contract } from '../contract'; import { Contract } from '../contract';
import { decode as eventLogDecoder } from '../utils/decoder'; import { decode as eventLogDecoder } from '../utils/decoder';
import { inputLogFormatter, outputLogFormatter } from '../utils/formatter'; import { inputLogFormatter, outputLogFormatter } from '../utils/formatter';
export class EventMethod { export class EventMethod extends LogSub {
params: any; params: any;
methodKey: string; methodKey: string;
contract: Contract; contract: Contract;
messenger?: Messenger;
abiItem: AbiItemModel; abiItem: AbiItemModel;
constructor( constructor(
methodKey: string, methodKey: string,
@ -15,57 +14,18 @@ export class EventMethod {
abiItem: AbiItemModel, abiItem: AbiItemModel,
contract: Contract, contract: Contract,
) { ) {
super(inputLogFormatter(params), contract.wallet.messenger);
this.methodKey = methodKey; this.methodKey = methodKey;
this.contract = contract; this.contract = contract;
this.messenger = contract.wallet.messenger;
this.params = params; this.params = params;
this.abiItem = abiItem; this.abiItem = abiItem;
super.subscribe();
} }
send() {}
call() {} // call() {}
estimateGas() {} // estimateGas() {}
encodeABI() {} // encodeABI() {}
subscribe(options: any) {
if (
options &&
typeof options.filter !== 'undefined' &&
typeof options.topics !== 'undefined'
) {
throw new Error(
'Invalid subscription options: Only filter or topics are allowed and not both',
);
}
if (this.emitter && this.messenger) {
const messenger = this.messenger;
const emitter = this.emitter;
const inputOptions = inputLogFormatter(options);
// 1. getLog
// 2. subscribe pastlog
// 3. emit data
messenger
.send(RPCMethod.GetPastLogs, [inputOptions])
.then((logs: any) => {
logs.forEach((log: any) => {
const formattedLog = this.onNewSubscriptionItem(log);
emitter.emit('data', formattedLog);
});
messenger.subscribe('logs', [inputOptions] || []);
})
.catch((error) => {
emitter.emit('error', error);
});
}
return this.contract;
// return this.eventSubscriptionFactory
// .createEventLogSubscription(
// this.eventLogDecoder,
// this.contract,
// this.eventOptionsMapper.map(abiItemModel, this.contract, options),
// abiItemModel,
// )
// .subscribe(callback);
// this.messenger.subscribe()
}
onNewSubscriptionItem(subscriptionItem: any) { onNewSubscriptionItem(subscriptionItem: any) {
// const log = outputLogFormatter(subscriptionItem); // const log = outputLogFormatter(subscriptionItem);
const log = eventLogDecoder( const log = eventLogDecoder(
@ -80,11 +40,4 @@ export class EventMethod {
return log; return log;
} }
get emitter() {
if (this.messenger && this.messenger.provider instanceof WSProvider) {
return this.messenger.provider.emitter;
} else {
return undefined;
}
}
} }

@ -3,8 +3,10 @@ import {
Messenger, Messenger,
ResponseMiddleware, ResponseMiddleware,
WSProvider, WSProvider,
// SubscribeReturns, NewPendingTransactions,
SubscriptionMethod, NewHeaders,
LogSub,
Syncing,
} from '@harmony-js/network'; } from '@harmony-js/network';
import { import {
@ -217,19 +219,6 @@ class Blockchain extends HarmonyCore {
return this.getRpcResult(result); return this.getRpcResult(result);
} }
/**
*
*/
async syncing() {
const result = await this.messenger.send(
RPCMethod.Syncing,
[],
this.chainPrefix,
);
return this.getRpcResult(result);
}
async net_peerCount() { async net_peerCount() {
const result = await this.messenger.send(RPCMethod.PeerCount, [], 'net'); const result = await this.messenger.send(RPCMethod.PeerCount, [], 'net');
@ -322,13 +311,7 @@ class Blockchain extends HarmonyCore {
newPendingTransactions() { newPendingTransactions() {
if (this.messenger.provider instanceof WSProvider) { if (this.messenger.provider instanceof WSProvider) {
// return this.messenger.subscribe( return new NewPendingTransactions(this.messenger);
// RPCMethod.Subscribe,
// ['newPendingTransactions'],
// SubscribeReturns.method,
// this.chainPrefix,
// );
return new SubscriptionMethod(['newPendingTransactions'], this.messenger);
} else { } else {
throw new Error('HttpProvider does not support this feature'); throw new Error('HttpProvider does not support this feature');
} }
@ -336,13 +319,23 @@ class Blockchain extends HarmonyCore {
newBlockHeaders() { newBlockHeaders() {
if (this.messenger.provider instanceof WSProvider) { if (this.messenger.provider instanceof WSProvider) {
// return this.messenger.subscribe( return new NewHeaders(this.messenger);
// RPCMethod.Subscribe, } else {
// ['newHeads'], throw new Error('HttpProvider does not support this feature');
// SubscribeReturns.method, }
// this.chainPrefix, }
// );
return new SubscriptionMethod(['newHeads'], this.messenger); syncing() {
if (this.messenger.provider instanceof WSProvider) {
return new Syncing(this.messenger);
} else {
throw new Error('HttpProvider does not support this feature');
}
}
logs(options: any) {
if (this.messenger.provider instanceof WSProvider) {
return new LogSub(options, this.messenger);
} else { } else {
throw new Error('HttpProvider does not support this feature'); throw new Error('HttpProvider does not support this feature');
} }

@ -21,6 +21,10 @@ export * from './tracker/subscribeTracker';
// subscriptinos // subscriptinos
export * from './subscriptions/Subscription'; export * from './subscriptions/Subscription';
export * from './subscriptions/LogSub';
export * from './subscriptions/NewHeadersSub';
export * from './subscriptions/NewPendingTransactionsSub';
export * from './subscriptions/SyncingSub';
// utils // utils
export * from './util'; export * from './util';
// types // types

@ -1,9 +1,43 @@
import { Messenger } from '../messenger/messenger'; import { Messenger } from '../messenger/messenger';
import { SubscriptionMethod } from './Subscription'; import { SubscriptionMethod } from './Subscription';
import { RPCMethod } from '../rpcMethod/rpc';
export class LogSub extends SubscriptionMethod { export class LogSub extends SubscriptionMethod {
constructor(params: any[] = ['logs'], messenger: Messenger) { constructor(options: any, messenger: Messenger) {
super(params, messenger); super('logs', options, messenger);
this.subscribe();
}
async subscribe() {
// if (
// (this.options.fromBlock && this.options.fromBlock !== 'latest') ||
// this.options.fromBlock === 0
// ) {
try {
const getPastLogs = await this.messenger.send(RPCMethod.GetPastLogs, [
...this.options,
]);
if (getPastLogs.isError()) {
this.emitter.emit('error', getPastLogs.message);
}
const logs = getPastLogs.result;
logs.forEach((log: any) => {
const formattedLog = this.onNewSubscriptionItem(log);
this.emitter.emit('data', formattedLog);
});
delete this.options.fromBlock;
super.start();
return this;
} catch (error) {
this.emitter.emit('error', error);
}
// }
// return this;
} }
onNewSubscriptionItem(subscriptionItem: any) { onNewSubscriptionItem(subscriptionItem: any) {

@ -2,7 +2,8 @@ import { Messenger } from '../messenger/messenger';
import { SubscriptionMethod } from './Subscription'; import { SubscriptionMethod } from './Subscription';
export class NewHeaders extends SubscriptionMethod { export class NewHeaders extends SubscriptionMethod {
constructor(params: any[] = ['newHeads'], messenger: Messenger) { constructor(messenger: Messenger) {
super(params, messenger); super('newHeads', undefined, messenger);
this.start();
} }
} }

@ -2,10 +2,8 @@ import { Messenger } from '../messenger/messenger';
import { SubscriptionMethod } from './Subscription'; import { SubscriptionMethod } from './Subscription';
export class NewPendingTransactions extends SubscriptionMethod { export class NewPendingTransactions extends SubscriptionMethod {
constructor( constructor(messenger: Messenger) {
params: any[] = ['newPendingTransactions'], super('newPendingTransactions', undefined, messenger);
messenger: Messenger, this.start();
) {
super(params, messenger);
} }
} }

@ -4,19 +4,29 @@ import { WSProvider } from '../providers/ws';
import { RPCRequestPayload } from '../types'; import { RPCRequestPayload } from '../types';
export class SubscriptionMethod extends WSProvider { export class SubscriptionMethod extends WSProvider {
params: any[]; param: any;
options: any;
messenger: Messenger; messenger: Messenger;
subscriptionId: any = null; subscriptionId: any = null;
constructor(params: any[], messenger: Messenger) { constructor(param: any, options: any, messenger: Messenger) {
super(messenger.provider.url); super(messenger.provider.url);
this.params = params; this.param = param;
this.options = options;
this.messenger = messenger; this.messenger = messenger;
this.start();
} }
constructPayload(method: string, payload: any): RPCRequestPayload<any> { constructPayload(
method: string,
param: any,
options?: any,
): RPCRequestPayload<any> {
let rpcMethod = method; let rpcMethod = method;
const payload: any = [];
payload.push(param);
if (options) {
payload.push(options);
}
rpcMethod = this.messenger.setRPCPrefix(method, this.messenger.chainPrefix); rpcMethod = this.messenger.setRPCPrefix(method, this.messenger.chainPrefix);
return this.jsonRpc.toPayload(rpcMethod, payload); return this.jsonRpc.toPayload(rpcMethod, payload);
} }
@ -24,8 +34,8 @@ export class SubscriptionMethod extends WSProvider {
async start() { async start() {
const subscribePayload = this.constructPayload( const subscribePayload = this.constructPayload(
RPCMethod.Subscribe, RPCMethod.Subscribe,
this.param,
this.params, this.options,
); );
try { try {
const id = await super.subscribe(subscribePayload); const id = await super.subscribe(subscribePayload);
@ -46,9 +56,10 @@ export class SubscriptionMethod extends WSProvider {
return this; return this;
} }
unsubscribe() { unsubscribe() {
const unsubscribePayload = this.constructPayload(RPCMethod.UnSubscribe, [ const unsubscribePayload = this.constructPayload(
RPCMethod.UnSubscribe,
this.subscriptionId, this.subscriptionId,
]); );
return super.unsubscribe(unsubscribePayload); return super.unsubscribe(unsubscribePayload);
} }
onNewSubscriptionItem(subscriptionItem: any) { onNewSubscriptionItem(subscriptionItem: any) {

@ -3,13 +3,14 @@ import { SubscriptionMethod } from './Subscription';
export class Syncing extends SubscriptionMethod { export class Syncing extends SubscriptionMethod {
isSyncing: boolean | null; isSyncing: boolean | null;
constructor(params: any[] = ['syncing'], messenger: Messenger) { constructor(messenger: Messenger) {
super(params, messenger); super('syncing', undefined, messenger);
this.isSyncing = null; this.isSyncing = null;
this.start();
} }
onNewSubscriptionItem(subscriptionItem: any) { onNewSubscriptionItem(subscriptionItem: any) {
const isSyncing = subscriptionItem.result.syncing; const isSyncing = subscriptionItem.params.result.syncing;
if (this.isSyncing === null) { if (this.isSyncing === null) {
this.isSyncing = isSyncing; this.isSyncing = isSyncing;

@ -15,7 +15,7 @@ import {
HttpProvider, HttpProvider,
// WSProvider, // WSProvider,
// SubscribeReturns, // SubscribeReturns,
SubscriptionMethod, NewHeaders,
} from '@harmony-js/network'; } from '@harmony-js/network';
import { TxParams, TxStatus, TransasctionReceipt } from './types'; import { TxParams, TxStatus, TransasctionReceipt } from './types';
import { import {
@ -375,9 +375,7 @@ class Transaction {
maxAttempts: number = 20, maxAttempts: number = 20,
): Promise<Transaction> { ): Promise<Transaction> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const newHeads = Promise.resolve( const newHeads = Promise.resolve(new NewHeaders(this.messenger));
new SubscriptionMethod(['newHeads'], this.messenger),
);
newHeads.then((p) => { newHeads.then((p) => {
p.onData(async (data: any) => { p.onData(async (data: any) => {
if (!this.blockNumbers.includes(data.number)) { if (!this.blockNumbers.includes(data.number)) {

Loading…
Cancel
Save