diff --git a/.gitignore b/.gitignore index b64a527..5a2d900 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.lock *.tsbuildinfo .vscode/ +devTestOnly/ # package.json diff --git a/packages/harmony-contract/src/events/event.ts b/packages/harmony-contract/src/events/event.ts index 9462c21..19552a5 100644 --- a/packages/harmony-contract/src/events/event.ts +++ b/packages/harmony-contract/src/events/event.ts @@ -1,13 +1,12 @@ import { AbiItemModel } from '../models/types'; -import { Messenger, RPCMethod, WSProvider } from '@harmony-js/network'; +import { LogSub } from '@harmony-js/network'; import { Contract } from '../contract'; import { decode as eventLogDecoder } from '../utils/decoder'; import { inputLogFormatter, outputLogFormatter } from '../utils/formatter'; -export class EventMethod { +export class EventMethod extends LogSub { params: any; methodKey: string; contract: Contract; - messenger?: Messenger; abiItem: AbiItemModel; constructor( methodKey: string, @@ -15,57 +14,18 @@ export class EventMethod { abiItem: AbiItemModel, contract: Contract, ) { + super(inputLogFormatter(params), contract.wallet.messenger); this.methodKey = methodKey; this.contract = contract; - this.messenger = contract.wallet.messenger; this.params = params; this.abiItem = abiItem; + super.subscribe(); } - send() {} - call() {} - estimateGas() {} - 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() - } + + // call() {} + // estimateGas() {} + // encodeABI() {} + onNewSubscriptionItem(subscriptionItem: any) { // const log = outputLogFormatter(subscriptionItem); const log = eventLogDecoder( @@ -80,11 +40,4 @@ export class EventMethod { return log; } - get emitter() { - if (this.messenger && this.messenger.provider instanceof WSProvider) { - return this.messenger.provider.emitter; - } else { - return undefined; - } - } } diff --git a/packages/harmony-core/src/blockchain.ts b/packages/harmony-core/src/blockchain.ts index 7c93a85..7455d55 100644 --- a/packages/harmony-core/src/blockchain.ts +++ b/packages/harmony-core/src/blockchain.ts @@ -3,8 +3,10 @@ import { Messenger, ResponseMiddleware, WSProvider, - // SubscribeReturns, - SubscriptionMethod, + NewPendingTransactions, + NewHeaders, + LogSub, + Syncing, } from '@harmony-js/network'; import { @@ -217,19 +219,6 @@ class Blockchain extends HarmonyCore { return this.getRpcResult(result); } - /** - * - */ - async syncing() { - const result = await this.messenger.send( - RPCMethod.Syncing, - [], - this.chainPrefix, - ); - - return this.getRpcResult(result); - } - async net_peerCount() { const result = await this.messenger.send(RPCMethod.PeerCount, [], 'net'); @@ -322,13 +311,7 @@ class Blockchain extends HarmonyCore { newPendingTransactions() { if (this.messenger.provider instanceof WSProvider) { - // return this.messenger.subscribe( - // RPCMethod.Subscribe, - // ['newPendingTransactions'], - // SubscribeReturns.method, - // this.chainPrefix, - // ); - return new SubscriptionMethod(['newPendingTransactions'], this.messenger); + return new NewPendingTransactions(this.messenger); } else { throw new Error('HttpProvider does not support this feature'); } @@ -336,13 +319,23 @@ class Blockchain extends HarmonyCore { newBlockHeaders() { if (this.messenger.provider instanceof WSProvider) { - // return this.messenger.subscribe( - // RPCMethod.Subscribe, - // ['newHeads'], - // SubscribeReturns.method, - // this.chainPrefix, - // ); - return new SubscriptionMethod(['newHeads'], this.messenger); + return new NewHeaders(this.messenger); + } else { + throw new Error('HttpProvider does not support this feature'); + } + } + + 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 { throw new Error('HttpProvider does not support this feature'); } diff --git a/packages/harmony-network/src/index.ts b/packages/harmony-network/src/index.ts index 653c72f..b1af99d 100644 --- a/packages/harmony-network/src/index.ts +++ b/packages/harmony-network/src/index.ts @@ -21,6 +21,10 @@ export * from './tracker/subscribeTracker'; // subscriptinos export * from './subscriptions/Subscription'; +export * from './subscriptions/LogSub'; +export * from './subscriptions/NewHeadersSub'; +export * from './subscriptions/NewPendingTransactionsSub'; +export * from './subscriptions/SyncingSub'; // utils export * from './util'; // types diff --git a/packages/harmony-network/src/subscriptions/LogSub.ts b/packages/harmony-network/src/subscriptions/LogSub.ts index 823982b..5300c27 100644 --- a/packages/harmony-network/src/subscriptions/LogSub.ts +++ b/packages/harmony-network/src/subscriptions/LogSub.ts @@ -1,9 +1,43 @@ import { Messenger } from '../messenger/messenger'; import { SubscriptionMethod } from './Subscription'; +import { RPCMethod } from '../rpcMethod/rpc'; export class LogSub extends SubscriptionMethod { - constructor(params: any[] = ['logs'], messenger: Messenger) { - super(params, messenger); + constructor(options: any, messenger: 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) { diff --git a/packages/harmony-network/src/subscriptions/NewHeadersSub.ts b/packages/harmony-network/src/subscriptions/NewHeadersSub.ts index 805c156..70ba7d8 100644 --- a/packages/harmony-network/src/subscriptions/NewHeadersSub.ts +++ b/packages/harmony-network/src/subscriptions/NewHeadersSub.ts @@ -2,7 +2,8 @@ import { Messenger } from '../messenger/messenger'; import { SubscriptionMethod } from './Subscription'; export class NewHeaders extends SubscriptionMethod { - constructor(params: any[] = ['newHeads'], messenger: Messenger) { - super(params, messenger); + constructor(messenger: Messenger) { + super('newHeads', undefined, messenger); + this.start(); } } diff --git a/packages/harmony-network/src/subscriptions/NewPendingTransactionsSub.ts b/packages/harmony-network/src/subscriptions/NewPendingTransactionsSub.ts index 58b2116..f77dae6 100644 --- a/packages/harmony-network/src/subscriptions/NewPendingTransactionsSub.ts +++ b/packages/harmony-network/src/subscriptions/NewPendingTransactionsSub.ts @@ -2,10 +2,8 @@ import { Messenger } from '../messenger/messenger'; import { SubscriptionMethod } from './Subscription'; export class NewPendingTransactions extends SubscriptionMethod { - constructor( - params: any[] = ['newPendingTransactions'], - messenger: Messenger, - ) { - super(params, messenger); + constructor(messenger: Messenger) { + super('newPendingTransactions', undefined, messenger); + this.start(); } } diff --git a/packages/harmony-network/src/subscriptions/Subscription.ts b/packages/harmony-network/src/subscriptions/Subscription.ts index 2a7036e..bf9abba 100644 --- a/packages/harmony-network/src/subscriptions/Subscription.ts +++ b/packages/harmony-network/src/subscriptions/Subscription.ts @@ -4,19 +4,29 @@ import { WSProvider } from '../providers/ws'; import { RPCRequestPayload } from '../types'; export class SubscriptionMethod extends WSProvider { - params: any[]; + param: any; + options: any; messenger: Messenger; subscriptionId: any = null; - constructor(params: any[], messenger: Messenger) { + constructor(param: any, options: any, messenger: Messenger) { super(messenger.provider.url); - this.params = params; + this.param = param; + this.options = options; this.messenger = messenger; - this.start(); } - constructPayload(method: string, payload: any): RPCRequestPayload { + constructPayload( + method: string, + param: any, + options?: any, + ): RPCRequestPayload { let rpcMethod = method; + const payload: any = []; + payload.push(param); + if (options) { + payload.push(options); + } rpcMethod = this.messenger.setRPCPrefix(method, this.messenger.chainPrefix); return this.jsonRpc.toPayload(rpcMethod, payload); } @@ -24,8 +34,8 @@ export class SubscriptionMethod extends WSProvider { async start() { const subscribePayload = this.constructPayload( RPCMethod.Subscribe, - - this.params, + this.param, + this.options, ); try { const id = await super.subscribe(subscribePayload); @@ -46,9 +56,10 @@ export class SubscriptionMethod extends WSProvider { return this; } unsubscribe() { - const unsubscribePayload = this.constructPayload(RPCMethod.UnSubscribe, [ + const unsubscribePayload = this.constructPayload( + RPCMethod.UnSubscribe, this.subscriptionId, - ]); + ); return super.unsubscribe(unsubscribePayload); } onNewSubscriptionItem(subscriptionItem: any) { diff --git a/packages/harmony-network/src/subscriptions/SyncingSub.ts b/packages/harmony-network/src/subscriptions/SyncingSub.ts index bb670d3..3df0b66 100644 --- a/packages/harmony-network/src/subscriptions/SyncingSub.ts +++ b/packages/harmony-network/src/subscriptions/SyncingSub.ts @@ -3,13 +3,14 @@ import { SubscriptionMethod } from './Subscription'; export class Syncing extends SubscriptionMethod { isSyncing: boolean | null; - constructor(params: any[] = ['syncing'], messenger: Messenger) { - super(params, messenger); + constructor(messenger: Messenger) { + super('syncing', undefined, messenger); this.isSyncing = null; + this.start(); } onNewSubscriptionItem(subscriptionItem: any) { - const isSyncing = subscriptionItem.result.syncing; + const isSyncing = subscriptionItem.params.result.syncing; if (this.isSyncing === null) { this.isSyncing = isSyncing; diff --git a/packages/harmony-transaction/src/transaction.ts b/packages/harmony-transaction/src/transaction.ts index ca519f6..28451c2 100644 --- a/packages/harmony-transaction/src/transaction.ts +++ b/packages/harmony-transaction/src/transaction.ts @@ -15,7 +15,7 @@ import { HttpProvider, // WSProvider, // SubscribeReturns, - SubscriptionMethod, + NewHeaders, } from '@harmony-js/network'; import { TxParams, TxStatus, TransasctionReceipt } from './types'; import { @@ -375,9 +375,7 @@ class Transaction { maxAttempts: number = 20, ): Promise { return new Promise((resolve, reject) => { - const newHeads = Promise.resolve( - new SubscriptionMethod(['newHeads'], this.messenger), - ); + const newHeads = Promise.resolve(new NewHeaders(this.messenger)); newHeads.then((p) => { p.onData(async (data: any) => { if (!this.blockNumbers.includes(data.number)) {