[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
*.tsbuildinfo
.vscode/
devTestOnly/
# package.json

@ -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;
}
}
}

@ -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');
}

@ -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

@ -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) {

@ -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();
}
}

@ -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();
}
}

@ -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<any> {
constructPayload(
method: string,
param: any,
options?: any,
): RPCRequestPayload<any> {
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) {

@ -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;

@ -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<Transaction> {
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)) {

Loading…
Cancel
Save