[chore] update Transaction.confirm

@types
neeboo 6 years ago
parent ff247a2def
commit bf2912dd4f
  1. 61
      examples/testGanache.js
  2. 1
      packages/harmony-contract/src/methods/method.ts
  3. 3
      packages/harmony-network/src/index.ts
  4. 1
      packages/harmony-network/src/providers/ws.ts
  5. 53
      packages/harmony-network/src/subscriptions/Subscription.ts
  6. 95
      packages/harmony-transaction/src/transaction.ts

@ -1,6 +1,9 @@
const { Harmony } = require('../packages/harmony-core/dist');
const { ChainID, ChainType } = require('../packages/harmony-utils/dist');
const { SubscribeBlockTracker } = require('../packages/harmony-network/dist');
const {
SubscribeBlockTracker,
SubscriptionMethod,
} = require('../packages/harmony-network/dist');
const ganache = require('ganache-cli');
@ -207,28 +210,58 @@ async function main() {
setTimeout(async () => {
const s2 = await harmony.wallet.signTransaction(txn, undefined, password);
await s2.sendTransaction();
const [sentTxn, TranID] = await s2.sendTransaction();
await sentTxn.confirm(TranID, 20, 1000);
console.log({
blockNumbers: sentTxn.blockNumbers,
txStatus: sentTxn.txStatus,
});
}, 5000);
setTimeout(async () => {
const s3 = await harmony.wallet.signTransaction(txn, undefined, password);
await s3.sendTransaction();
const [sentTxn, TranID] = await s3.sendTransaction();
await sentTxn.confirm(TranID, 20, 1000);
console.log({
blockNumbers: sentTxn.blockNumbers,
txStatus: sentTxn.txStatus,
});
}, 10000);
setTimeout(async () => {
const s3 = await harmony.wallet.signTransaction(txn, undefined, password);
await s3.sendTransaction();
const [sentTxn, TranID] = await s3.sendTransaction();
await sentTxn.confirm(TranID, 20, 1000);
console.log({
blockNumbers: sentTxn.blockNumbers,
txStatus: sentTxn.txStatus,
});
}, 15000);
setTimeout(async () => {
const s4 = await harmony.wallet.signTransaction(txn, undefined, password);
await s4.sendTransaction();
const [sentTxn, TranID] = await s4.sendTransaction();
await sentTxn.confirm(TranID, 20, 1000);
console.log({
blockNumbers: sentTxn.blockNumbers,
txStatus: sentTxn.txStatus,
});
}, 20000);
setTimeout(async () => {
const s5 = await harmony.wallet.signTransaction(txn, undefined, password);
await s5.sendTransaction();
const [sentTxn, TranID] = await s5.sendTransaction();
await sentTxn.confirm(TranID, 20, 1000);
console.log({
blockNumbers: sentTxn.blockNumbers,
txStatus: sentTxn.txStatus,
});
}, 25000);
setTimeout(async () => {
const s6 = await harmony.wallet.signTransaction(txn, undefined, password);
await s6.sendTransaction();
await sentTxn.confirm(TranID, 20, 1000);
console.log({
blockNumbers: sentTxn.blockNumbers,
txStatus: sentTxn.txStatus,
});
}, 30000);
}
@ -238,9 +271,23 @@ server.listen(port, function(err, blockchain) {
// const txn = await harmony.blockchain.getTransactionByHash({
// txnHash: res.params.result,
// });
// console.log(txn);
// console.log({ res, txn });
// });
// });
// const newPending = new SubscriptionMethod(
// ['newPendingTransactions'],
// harmony.messenger,
// );
// newPending.onData(async (res) => {
// const txn = await harmony.blockchain.getTransactionByHash({
// txnHash: res.params.result,
// });
// console.log({ res, txn });
// });
// const newHeads = new SubscriptionMethod(['newHeads'], harmony.messenger);
// newHeads.onData((res) => {
// console.log(res.params.result.number);
// });
main();
});

@ -167,6 +167,7 @@ export class ContractMethod {
protected async confirm(id: string) {
try {
const result = await this.transaction.confirm(id);
if (result.receipt && result.receipt.status === '0x1') {
if (this.methodKey === 'contractConstructor') {
this.contract.setStatus(ContractStatus.DEPLOYED);

@ -18,6 +18,9 @@ export * from './rpcMethod/rpc';
export * from './tracker/baseTracker';
export * from './tracker/pollingTracker';
export * from './tracker/subscribeTracker';
// subscriptinos
export * from './subscriptions/Subscription';
// utils
export * from './util';
// types

@ -175,7 +175,6 @@ class WSProvider extends BaseSocket {
if (msg && msg.data) {
let result;
let event;
try {
result = isObject(msg.data) ? msg.data : JSON.parse(msg.data);

@ -0,0 +1,53 @@
import { Messenger } from '../messenger/messenger';
import { RPCMethod } from '../rpcMethod/rpc';
import { WSProvider } from '../providers/ws';
import { RPCRequestPayload } from '../types';
export class SubscriptionMethod extends WSProvider {
params: any[];
messenger: Messenger;
subscriptionId: any = null;
constructor(params: any[], messenger: Messenger) {
super(messenger.provider.url);
this.params = params;
this.messenger = messenger;
this.start();
}
constructPayload(method: string, payload: any): RPCRequestPayload<any> {
let rpcMethod = method;
rpcMethod = this.messenger.setRPCPrefix(method, this.messenger.chainPrefix);
return this.jsonRpc.toPayload(rpcMethod, payload);
}
async start() {
const subscribePayload = this.constructPayload(
RPCMethod.Subscribe,
this.params,
);
try {
const id = await super.subscribe(subscribePayload);
this.subscriptionId = id;
this.on(id, (result: any) => {
this.emitter.emit('data', result);
});
this.once('error', (error) => {
this.removeEventListener(id);
this.emitter.emit('error', error);
this.removeEventListener('*');
});
} catch (error) {
this.emitter.emit('error', error);
this.removeEventListener('*');
}
return this;
}
unsubscribe() {
const unsubscribePayload = this.constructPayload(RPCMethod.UnSubscribe, [
this.subscriptionId,
]);
return super.unsubscribe(unsubscribePayload);
}
}

@ -13,7 +13,9 @@ import {
RPCMethod,
Emitter,
HttpProvider,
SubscribeReturns,
// WSProvider,
// SubscribeReturns,
SubscriptionMethod,
} from '@harmony-js/network';
import { TxParams, TxStatus, TransasctionReceipt } from './types';
import {
@ -251,6 +253,7 @@ class Transaction {
this.id = res.result;
this.emitTransactionHash(this.id);
this.setTxStatus(TxStatus.PENDING);
// await this.confirm(this.id, 20, 1000);
return [this, res.result];
} else if (res.isError()) {
this.emitConfirm(`transaction failed:${res.message}`);
@ -321,68 +324,70 @@ class Transaction {
`The transaction is still not confirmed after ${maxAttempts} attempts.`,
);
} else {
await this.socketConfirm(txHash, maxAttempts, interval);
return this;
try {
if (await this.trackTx(txHash)) {
const currentBlock = await this.getBlockNumber();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
this.emitConfirm(this.txStatus);
return this;
} else {
const result = await this.socketConfirm(
txHash,
maxAttempts,
interval,
);
return result;
}
} catch (error) {
this.txStatus = TxStatus.REJECTED;
this.emitConfirm(this.txStatus);
throw new Error(
`The transaction is still not confirmed after ${maxAttempts *
interval} mil seconds.`,
);
}
}
}
async socketConfirm(
socketConfirm(
txHash: string,
maxAttempts: number = 20,
interval: number = 1000,
) {
try {
const [newHeads, subscriptionId] = await this.messenger.subscribe(
RPCMethod.Subscribe,
['newHeads'],
SubscribeReturns.all,
): Promise<Transaction> {
return new Promise((resolve, reject) => {
const newHeads = Promise.resolve(
new SubscriptionMethod(['newHeads'], this.messenger),
);
newHeads
.onData(async (data: any) => {
const currentBlock = await this.messenger.send(
RPCMethod.BlockNumber,
[],
);
if (currentBlock.isError()) {
throw currentBlock.message;
}
newHeads.then((p) => {
p.onData(async (data: any) => {
const currentBlock = await this.getBlockNumber();
if (!this.blockNumbers.includes(data.number)) {
const tracker = await this.trackTx(txHash);
if (tracker) {
if (await this.trackTx(txHash)) {
this.emitConfirm(this.txStatus);
await this.messenger.unsubscribe(RPCMethod.UnSubscribe, [
subscriptionId,
]);
await p.unsubscribe();
resolve(this);
} else {
this.blockNumbers.push(currentBlock.result);
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
if (this.confirmationCheck === maxAttempts * interval) {
this.txStatus = TxStatus.REJECTED;
this.emitConfirm(this.txStatus);
await this.messenger.unsubscribe(RPCMethod.UnSubscribe, [
subscriptionId,
]);
await p.unsubscribe();
resolve(this);
}
}
}
})
.onError(async (error: any) => {
}).onError(async (error: any) => {
this.txStatus = TxStatus.REJECTED;
this.emitConfirm(this.txStatus);
this.emitError(error);
await this.messenger.unsubscribe(RPCMethod.UnSubscribe, [
subscriptionId,
]);
throw new Error(error);
await p.unsubscribe();
reject(error);
});
} catch (error) {
throw error;
}
});
});
}
emitTransactionHash(transactionHash: string) {
@ -397,5 +402,13 @@ class Transaction {
emitConfirm(data: any) {
this.emitter.emit(TransactionEvents.confirmation, data);
}
async getBlockNumber() {
const currentBlock = await this.messenger.send(RPCMethod.BlockNumber, []);
if (currentBlock.isError()) {
throw currentBlock.message;
}
return currentBlock.result;
}
}
export { Transaction };

Loading…
Cancel
Save