Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@harmony-js/transaction

Package Overview
Dependencies
Maintainers
1
Versions
74
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@harmony-js/transaction - npm Package Compare versions

Comparing version 0.0.12 to 0.0.13

1

dist/factory.d.ts

@@ -10,2 +10,3 @@ import { Messenger } from '@harmony-js/network';

newTx(txParams?: TxParams | any): Transaction;
clone(transaction: Transaction): Transaction;
recover(txHash: string): Transaction;

@@ -12,0 +13,0 @@ }

@@ -19,2 +19,5 @@ "use strict";

};
TransactionFactory.prototype.clone = function (transaction) {
return new transaction_1.Transaction(transaction.txParams, this.messenger, "INITIALIZED" /* INTIALIZED */);
};
TransactionFactory.prototype.recover = function (txHash) {

@@ -21,0 +24,0 @@ var newTxn = new transaction_1.Transaction(undefined, this.messenger, "INITIALIZED" /* INTIALIZED */);

189

dist/index.cjs.js

@@ -189,2 +189,3 @@ /**

this.blockNumbers = [];
this.confirmations = 0;
this.confirmationCheck = 0;

@@ -399,3 +400,3 @@ this.messenger = messenger;

return __awaiter(this, void 0, void 0, function () {
var res;
var res, currentBlock;
return __generator(this, function (_a) {

@@ -410,24 +411,26 @@ switch (_a.label) {

res = _a.sent();
if (res.isResult() && res.result !== null) {
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
if (this.receipt) {
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/, true];
}
else {
this.txStatus = "PENDING" /* PENDING */;
return [2 /*return*/, false];
}
if (!(res.isResult() && res.result !== null)) return [3 /*break*/, 5];
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
this.confirmations += 1;
if (!this.receipt) return [3 /*break*/, 2];
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else {
return [2 /*return*/, false];
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/];
return [2 /*return*/, true];
case 2:
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 3:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
return [2 /*return*/, false];
case 4: return [3 /*break*/, 6];
case 5: return [2 /*return*/, false];
case 6: return [2 /*return*/];
}

@@ -441,17 +444,30 @@ });

return __awaiter(this, void 0, void 0, function () {
var attempt, err_1, currentBlock, result, error_1;
var oldBlock, checkBlock, attempt, newBlock, nextBlock, err_1, result, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.messenger.provider instanceof network.HttpProvider)) return [3 /*break*/, 9];
if (!(this.messenger.provider instanceof network.HttpProvider)) return [3 /*break*/, 13];
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 1:
oldBlock = _a.sent();
checkBlock = oldBlock;
attempt = 0;
_a.label = 1;
case 1:
if (!(attempt < maxAttempts)) return [3 /*break*/, 8];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
if (!(attempt < maxAttempts)) return [3 /*break*/, 12];
_a.label = 3;
case 3:
_a.trys.push([3, 8, , 9]);
return [4 /*yield*/, this.getBlockNumber()];
case 4:
newBlock = _a.sent();
nextBlock = '0x' +
new crypto.BN(checkBlock.substring(2), 'hex')
.add(new crypto.BN(attempt === 0 ? attempt : 1))
.toString('hex');
if (!new crypto.BN(newBlock.substring(2), 'hex').gte(new crypto.BN(nextBlock.substring(2), 'hex'))) return [3 /*break*/, 6];
checkBlock = newBlock;
return [4 /*yield*/, this.trackTx(txHash)];
case 3:
case 5:
if (_a.sent()) {

@@ -461,4 +477,8 @@ this.emitConfirm(this.txStatus);

}
return [3 /*break*/, 5];
case 4:
return [3 /*break*/, 7];
case 6:
attempt = attempt - 1 >= 0 ? attempt - 1 : 0;
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
err_1 = _a.sent();

@@ -468,33 +488,30 @@ this.txStatus = "REJECTED" /* REJECTED */;

throw err_1;
case 5:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 7];
return [4 /*yield*/, sleep(interval * attempt)];
case 6:
case 9:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 11];
// await sleep(interval * attempt);
return [4 /*yield*/, sleep(interval)];
case 10:
// await sleep(interval * attempt);
_a.sent();
_a.label = 7;
case 7:
_a.label = 11;
case 11:
attempt += 1;
return [3 /*break*/, 1];
case 8:
return [3 /*break*/, 2];
case 12:
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
throw new Error("The transaction is still not confirmed after " + maxAttempts + " attempts.");
case 9:
_a.trys.push([9, 15, , 16]);
case 13:
_a.trys.push([13, 18, , 19]);
return [4 /*yield*/, this.trackTx(txHash)];
case 10:
if (!_a.sent()) return [3 /*break*/, 12];
return [4 /*yield*/, this.getBlockNumber()];
case 11:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
case 14:
if (!_a.sent()) return [3 /*break*/, 15];
this.emitConfirm(this.txStatus);
return [2 /*return*/, this];
case 12: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts, interval)];
case 13:
case 15: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts)];
case 16:
result = _a.sent();
return [2 /*return*/, result];
case 14: return [3 /*break*/, 16];
case 15:
case 17: return [3 /*break*/, 19];
case 18:
error_1 = _a.sent();

@@ -505,3 +522,3 @@ this.txStatus = "REJECTED" /* REJECTED */;

interval + " mil seconds.");
case 16: return [2 /*return*/];
case 19: return [2 /*return*/];
}

@@ -511,6 +528,5 @@ });

};
Transaction.prototype.socketConfirm = function (txHash, maxAttempts, interval) {
Transaction.prototype.socketConfirm = function (txHash, maxAttempts) {
var _this = this;
if (maxAttempts === void 0) { maxAttempts = 20; }
if (interval === void 0) { interval = 1000; }
return new Promise(function (resolve, reject) {

@@ -520,30 +536,25 @@ var newHeads = Promise.resolve(new network.SubscriptionMethod(['newHeads'], _this.messenger));

p.onData(function (data) { return __awaiter(_this, void 0, void 0, function () {
var currentBlock;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getBlockNumber()];
case 0:
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 5];
return [4 /*yield*/, this.trackTx(txHash)];
case 1:
currentBlock = _a.sent();
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 6];
return [4 /*yield*/, this.trackTx(txHash)];
case 2:
if (!_a.sent()) return [3 /*break*/, 4];
if (!_a.sent()) return [3 /*break*/, 3];
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 3:
case 2:
_a.sent();
resolve(this);
return [3 /*break*/, 6];
case 4:
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
if (!(this.confirmationCheck === maxAttempts * interval)) return [3 /*break*/, 6];
return [3 /*break*/, 5];
case 3:
if (!(this.confirmationCheck === maxAttempts)) return [3 /*break*/, 5];
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 5:
case 4:
_a.sent();
resolve(this);
_a.label = 6;
case 6: return [2 /*return*/];
_a.label = 5;
case 5: return [2 /*return*/];
}

@@ -583,6 +594,8 @@ });

return __awaiter(this, void 0, void 0, function () {
var currentBlock;
var currentBlock, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 1:

@@ -594,2 +607,6 @@ currentBlock = _a.sent();

return [2 /*return*/, currentBlock.result];
case 2:
error_2 = _a.sent();
throw error_2;
case 3: return [2 /*return*/];
}

@@ -599,2 +616,27 @@ });

};
Transaction.prototype.getBlockByNumber = function (blockNumber) {
return __awaiter(this, void 0, void 0, function () {
var block, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_getBlockByNumber" /* GetBlockByNumber */, [
blockNumber,
true,
])];
case 1:
block = _a.sent();
if (block.isError()) {
throw block.message;
}
return [2 /*return*/, block.result];
case 2:
error_3 = _a.sent();
throw error_3;
case 3: return [2 /*return*/];
}
});
});
};
return Transaction;

@@ -617,2 +659,5 @@ }());

};
TransactionFactory.prototype.clone = function (transaction) {
return new Transaction(transaction.txParams, this.messenger, "INITIALIZED" /* INTIALIZED */);
};
TransactionFactory.prototype.recover = function (txHash) {

@@ -619,0 +664,0 @@ var newTxn = new Transaction(undefined, this.messenger, "INITIALIZED" /* INTIALIZED */);

@@ -186,2 +186,3 @@ /**

this.blockNumbers = [];
this.confirmations = 0;
this.confirmationCheck = 0;

@@ -396,3 +397,3 @@ this.messenger = messenger;

return __awaiter(this, void 0, void 0, function () {
var res;
var res, currentBlock;
return __generator(this, function (_a) {

@@ -407,24 +408,26 @@ switch (_a.label) {

res = _a.sent();
if (res.isResult() && res.result !== null) {
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
if (this.receipt) {
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/, true];
}
else {
this.txStatus = "PENDING" /* PENDING */;
return [2 /*return*/, false];
}
if (!(res.isResult() && res.result !== null)) return [3 /*break*/, 5];
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
this.confirmations += 1;
if (!this.receipt) return [3 /*break*/, 2];
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else {
return [2 /*return*/, false];
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/];
return [2 /*return*/, true];
case 2:
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 3:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
return [2 /*return*/, false];
case 4: return [3 /*break*/, 6];
case 5: return [2 /*return*/, false];
case 6: return [2 /*return*/];
}

@@ -438,17 +441,30 @@ });

return __awaiter(this, void 0, void 0, function () {
var attempt, err_1, currentBlock, result, error_1;
var oldBlock, checkBlock, attempt, newBlock, nextBlock, err_1, result, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.messenger.provider instanceof HttpProvider)) return [3 /*break*/, 9];
if (!(this.messenger.provider instanceof HttpProvider)) return [3 /*break*/, 13];
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 1:
oldBlock = _a.sent();
checkBlock = oldBlock;
attempt = 0;
_a.label = 1;
case 1:
if (!(attempt < maxAttempts)) return [3 /*break*/, 8];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
if (!(attempt < maxAttempts)) return [3 /*break*/, 12];
_a.label = 3;
case 3:
_a.trys.push([3, 8, , 9]);
return [4 /*yield*/, this.getBlockNumber()];
case 4:
newBlock = _a.sent();
nextBlock = '0x' +
new BN(checkBlock.substring(2), 'hex')
.add(new BN(attempt === 0 ? attempt : 1))
.toString('hex');
if (!new BN(newBlock.substring(2), 'hex').gte(new BN(nextBlock.substring(2), 'hex'))) return [3 /*break*/, 6];
checkBlock = newBlock;
return [4 /*yield*/, this.trackTx(txHash)];
case 3:
case 5:
if (_a.sent()) {

@@ -458,4 +474,8 @@ this.emitConfirm(this.txStatus);

}
return [3 /*break*/, 5];
case 4:
return [3 /*break*/, 7];
case 6:
attempt = attempt - 1 >= 0 ? attempt - 1 : 0;
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
err_1 = _a.sent();

@@ -465,33 +485,30 @@ this.txStatus = "REJECTED" /* REJECTED */;

throw err_1;
case 5:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 7];
return [4 /*yield*/, sleep(interval * attempt)];
case 6:
case 9:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 11];
// await sleep(interval * attempt);
return [4 /*yield*/, sleep(interval)];
case 10:
// await sleep(interval * attempt);
_a.sent();
_a.label = 7;
case 7:
_a.label = 11;
case 11:
attempt += 1;
return [3 /*break*/, 1];
case 8:
return [3 /*break*/, 2];
case 12:
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
throw new Error("The transaction is still not confirmed after " + maxAttempts + " attempts.");
case 9:
_a.trys.push([9, 15, , 16]);
case 13:
_a.trys.push([13, 18, , 19]);
return [4 /*yield*/, this.trackTx(txHash)];
case 10:
if (!_a.sent()) return [3 /*break*/, 12];
return [4 /*yield*/, this.getBlockNumber()];
case 11:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
case 14:
if (!_a.sent()) return [3 /*break*/, 15];
this.emitConfirm(this.txStatus);
return [2 /*return*/, this];
case 12: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts, interval)];
case 13:
case 15: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts)];
case 16:
result = _a.sent();
return [2 /*return*/, result];
case 14: return [3 /*break*/, 16];
case 15:
case 17: return [3 /*break*/, 19];
case 18:
error_1 = _a.sent();

@@ -502,3 +519,3 @@ this.txStatus = "REJECTED" /* REJECTED */;

interval + " mil seconds.");
case 16: return [2 /*return*/];
case 19: return [2 /*return*/];
}

@@ -508,6 +525,5 @@ });

};
Transaction.prototype.socketConfirm = function (txHash, maxAttempts, interval) {
Transaction.prototype.socketConfirm = function (txHash, maxAttempts) {
var _this = this;
if (maxAttempts === void 0) { maxAttempts = 20; }
if (interval === void 0) { interval = 1000; }
return new Promise(function (resolve, reject) {

@@ -517,30 +533,25 @@ var newHeads = Promise.resolve(new SubscriptionMethod(['newHeads'], _this.messenger));

p.onData(function (data) { return __awaiter(_this, void 0, void 0, function () {
var currentBlock;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getBlockNumber()];
case 0:
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 5];
return [4 /*yield*/, this.trackTx(txHash)];
case 1:
currentBlock = _a.sent();
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 6];
return [4 /*yield*/, this.trackTx(txHash)];
case 2:
if (!_a.sent()) return [3 /*break*/, 4];
if (!_a.sent()) return [3 /*break*/, 3];
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 3:
case 2:
_a.sent();
resolve(this);
return [3 /*break*/, 6];
case 4:
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
if (!(this.confirmationCheck === maxAttempts * interval)) return [3 /*break*/, 6];
return [3 /*break*/, 5];
case 3:
if (!(this.confirmationCheck === maxAttempts)) return [3 /*break*/, 5];
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 5:
case 4:
_a.sent();
resolve(this);
_a.label = 6;
case 6: return [2 /*return*/];
_a.label = 5;
case 5: return [2 /*return*/];
}

@@ -580,6 +591,8 @@ });

return __awaiter(this, void 0, void 0, function () {
var currentBlock;
var currentBlock, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 1:

@@ -591,2 +604,6 @@ currentBlock = _a.sent();

return [2 /*return*/, currentBlock.result];
case 2:
error_2 = _a.sent();
throw error_2;
case 3: return [2 /*return*/];
}

@@ -596,2 +613,27 @@ });

};
Transaction.prototype.getBlockByNumber = function (blockNumber) {
return __awaiter(this, void 0, void 0, function () {
var block, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_getBlockByNumber" /* GetBlockByNumber */, [
blockNumber,
true,
])];
case 1:
block = _a.sent();
if (block.isError()) {
throw block.message;
}
return [2 /*return*/, block.result];
case 2:
error_3 = _a.sent();
throw error_3;
case 3: return [2 /*return*/];
}
});
});
};
return Transaction;

@@ -614,2 +656,5 @@ }());

};
TransactionFactory.prototype.clone = function (transaction) {
return new Transaction(transaction.txParams, this.messenger, "INITIALIZED" /* INTIALIZED */);
};
TransactionFactory.prototype.recover = function (txHash) {

@@ -616,0 +661,0 @@ var newTxn = new Transaction(undefined, this.messenger, "INITIALIZED" /* INTIALIZED */);

@@ -187,2 +187,3 @@ /**

this.blockNumbers = [];
this.confirmations = 0;
this.confirmationCheck = 0;

@@ -397,3 +398,3 @@ this.messenger = messenger;

return __awaiter(this, void 0, void 0, function () {
var res;
var res, currentBlock;
return __generator(this, function (_a) {

@@ -408,24 +409,26 @@ switch (_a.label) {

res = _a.sent();
if (res.isResult() && res.result !== null) {
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
if (this.receipt) {
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/, true];
}
else {
this.txStatus = "PENDING" /* PENDING */;
return [2 /*return*/, false];
}
if (!(res.isResult() && res.result !== null)) return [3 /*break*/, 5];
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
this.confirmations += 1;
if (!this.receipt) return [3 /*break*/, 2];
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else {
return [2 /*return*/, false];
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/];
return [2 /*return*/, true];
case 2:
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 3:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
return [2 /*return*/, false];
case 4: return [3 /*break*/, 6];
case 5: return [2 /*return*/, false];
case 6: return [2 /*return*/];
}

@@ -439,17 +442,30 @@ });

return __awaiter(this, void 0, void 0, function () {
var attempt, err_1, currentBlock, result, error_1;
var oldBlock, checkBlock, attempt, newBlock, nextBlock, err_1, result, error_1;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.messenger.provider instanceof network.HttpProvider)) return [3 /*break*/, 9];
if (!(this.messenger.provider instanceof network.HttpProvider)) return [3 /*break*/, 13];
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 1:
oldBlock = _a.sent();
checkBlock = oldBlock;
attempt = 0;
_a.label = 1;
case 1:
if (!(attempt < maxAttempts)) return [3 /*break*/, 8];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
if (!(attempt < maxAttempts)) return [3 /*break*/, 12];
_a.label = 3;
case 3:
_a.trys.push([3, 8, , 9]);
return [4 /*yield*/, this.getBlockNumber()];
case 4:
newBlock = _a.sent();
nextBlock = '0x' +
new crypto.BN(checkBlock.substring(2), 'hex')
.add(new crypto.BN(attempt === 0 ? attempt : 1))
.toString('hex');
if (!new crypto.BN(newBlock.substring(2), 'hex').gte(new crypto.BN(nextBlock.substring(2), 'hex'))) return [3 /*break*/, 6];
checkBlock = newBlock;
return [4 /*yield*/, this.trackTx(txHash)];
case 3:
case 5:
if (_a.sent()) {

@@ -459,4 +475,8 @@ this.emitConfirm(this.txStatus);

}
return [3 /*break*/, 5];
case 4:
return [3 /*break*/, 7];
case 6:
attempt = attempt - 1 >= 0 ? attempt - 1 : 0;
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
err_1 = _a.sent();

@@ -466,33 +486,30 @@ this.txStatus = "REJECTED" /* REJECTED */;

throw err_1;
case 5:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 7];
return [4 /*yield*/, sleep(interval * attempt)];
case 6:
case 9:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 11];
// await sleep(interval * attempt);
return [4 /*yield*/, sleep(interval)];
case 10:
// await sleep(interval * attempt);
_a.sent();
_a.label = 7;
case 7:
_a.label = 11;
case 11:
attempt += 1;
return [3 /*break*/, 1];
case 8:
return [3 /*break*/, 2];
case 12:
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
throw new Error("The transaction is still not confirmed after " + maxAttempts + " attempts.");
case 9:
_a.trys.push([9, 15, , 16]);
case 13:
_a.trys.push([13, 18, , 19]);
return [4 /*yield*/, this.trackTx(txHash)];
case 10:
if (!_a.sent()) return [3 /*break*/, 12];
return [4 /*yield*/, this.getBlockNumber()];
case 11:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
case 14:
if (!_a.sent()) return [3 /*break*/, 15];
this.emitConfirm(this.txStatus);
return [2 /*return*/, this];
case 12: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts, interval)];
case 13:
case 15: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts)];
case 16:
result = _a.sent();
return [2 /*return*/, result];
case 14: return [3 /*break*/, 16];
case 15:
case 17: return [3 /*break*/, 19];
case 18:
error_1 = _a.sent();

@@ -503,3 +520,3 @@ this.txStatus = "REJECTED" /* REJECTED */;

interval + " mil seconds.");
case 16: return [2 /*return*/];
case 19: return [2 /*return*/];
}

@@ -509,6 +526,5 @@ });

};
Transaction.prototype.socketConfirm = function (txHash, maxAttempts, interval) {
Transaction.prototype.socketConfirm = function (txHash, maxAttempts) {
var _this = this;
if (maxAttempts === void 0) { maxAttempts = 20; }
if (interval === void 0) { interval = 1000; }
return new Promise(function (resolve, reject) {

@@ -518,30 +534,25 @@ var newHeads = Promise.resolve(new network.SubscriptionMethod(['newHeads'], _this.messenger));

p.onData(function (data) { return __awaiter(_this, void 0, void 0, function () {
var currentBlock;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getBlockNumber()];
case 0:
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 5];
return [4 /*yield*/, this.trackTx(txHash)];
case 1:
currentBlock = _a.sent();
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 6];
return [4 /*yield*/, this.trackTx(txHash)];
case 2:
if (!_a.sent()) return [3 /*break*/, 4];
if (!_a.sent()) return [3 /*break*/, 3];
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 3:
case 2:
_a.sent();
resolve(this);
return [3 /*break*/, 6];
case 4:
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
if (!(this.confirmationCheck === maxAttempts * interval)) return [3 /*break*/, 6];
return [3 /*break*/, 5];
case 3:
if (!(this.confirmationCheck === maxAttempts)) return [3 /*break*/, 5];
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 5:
case 4:
_a.sent();
resolve(this);
_a.label = 6;
case 6: return [2 /*return*/];
_a.label = 5;
case 5: return [2 /*return*/];
}

@@ -581,6 +592,8 @@ });

return __awaiter(this, void 0, void 0, function () {
var currentBlock;
var currentBlock, error_2;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 1:

@@ -592,2 +605,6 @@ currentBlock = _a.sent();

return [2 /*return*/, currentBlock.result];
case 2:
error_2 = _a.sent();
throw error_2;
case 3: return [2 /*return*/];
}

@@ -597,2 +614,27 @@ });

};
Transaction.prototype.getBlockByNumber = function (blockNumber) {
return __awaiter(this, void 0, void 0, function () {
var block, error_3;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_getBlockByNumber" /* GetBlockByNumber */, [
blockNumber,
true,
])];
case 1:
block = _a.sent();
if (block.isError()) {
throw block.message;
}
return [2 /*return*/, block.result];
case 2:
error_3 = _a.sent();
throw error_3;
case 3: return [2 /*return*/];
}
});
});
};
return Transaction;

@@ -615,2 +657,5 @@ }());

};
TransactionFactory.prototype.clone = function (transaction) {
return new Transaction(transaction.txParams, this.messenger, "INITIALIZED" /* INTIALIZED */);
};
TransactionFactory.prototype.recover = function (txHash) {

@@ -617,0 +662,0 @@ var newTxn = new Transaction(undefined, this.messenger, "INITIALIZED" /* INTIALIZED */);

@@ -9,2 +9,3 @@ import { Signature } from '@harmony-js/crypto';

blockNumbers: string[];
confirmations: number;
confirmationCheck: number;

@@ -51,3 +52,3 @@ receipt?: TransasctionReceipt;

confirm(txHash: string, maxAttempts?: number, interval?: number): Promise<Transaction>;
socketConfirm(txHash: string, maxAttempts?: number, interval?: number): Promise<Transaction>;
socketConfirm(txHash: string, maxAttempts?: number): Promise<Transaction>;
emitTransactionHash(transactionHash: string): void;

@@ -58,4 +59,5 @@ emitReceipt(receipt: any): void;

getBlockNumber(): Promise<any>;
getBlockByNumber(blockNumber: string): Promise<any>;
}
export { Transaction };
//# sourceMappingURL=transaction.d.ts.map

@@ -14,2 +14,3 @@ "use strict";

this.blockNumbers = [];
this.confirmations = 0;
this.confirmationCheck = 0;

@@ -224,3 +225,3 @@ this.messenger = messenger;

return tslib_1.__awaiter(this, void 0, void 0, function () {
var res;
var res, currentBlock;
return tslib_1.__generator(this, function (_a) {

@@ -235,24 +236,26 @@ switch (_a.label) {

res = _a.sent();
if (res.isResult() && res.result !== null) {
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
if (this.receipt) {
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/, true];
}
else {
this.txStatus = "PENDING" /* PENDING */;
return [2 /*return*/, false];
}
if (!(res.isResult() && res.result !== null)) return [3 /*break*/, 5];
this.receipt = res.result;
this.emitReceipt(this.receipt);
this.id = res.result.transactionHash;
this.confirmations += 1;
if (!this.receipt) return [3 /*break*/, 2];
if (this.receipt.status && this.receipt.status === '0x1') {
this.txStatus = "CONFIRMED" /* CONFIRMED */;
}
else {
return [2 /*return*/, false];
else if (this.receipt.status && this.receipt.status === '0x0') {
this.txStatus = "REJECTED" /* REJECTED */;
}
return [2 /*return*/];
return [2 /*return*/, true];
case 2:
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 3:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
return [2 /*return*/, false];
case 4: return [3 /*break*/, 6];
case 5: return [2 /*return*/, false];
case 6: return [2 /*return*/];
}

@@ -266,17 +269,30 @@ });

return tslib_1.__awaiter(this, void 0, void 0, function () {
var attempt, err_1, currentBlock, result, error_1;
var oldBlock, checkBlock, attempt, newBlock, nextBlock, err_1, result, error_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(this.messenger.provider instanceof network_1.HttpProvider)) return [3 /*break*/, 9];
if (!(this.messenger.provider instanceof network_1.HttpProvider)) return [3 /*break*/, 13];
this.txStatus = "PENDING" /* PENDING */;
return [4 /*yield*/, this.getBlockNumber()];
case 1:
oldBlock = _a.sent();
checkBlock = oldBlock;
attempt = 0;
_a.label = 1;
case 1:
if (!(attempt < maxAttempts)) return [3 /*break*/, 8];
_a.label = 2;
case 2:
_a.trys.push([2, 4, , 5]);
if (!(attempt < maxAttempts)) return [3 /*break*/, 12];
_a.label = 3;
case 3:
_a.trys.push([3, 8, , 9]);
return [4 /*yield*/, this.getBlockNumber()];
case 4:
newBlock = _a.sent();
nextBlock = '0x' +
new crypto_1.BN(checkBlock.substring(2), 'hex')
.add(new crypto_1.BN(attempt === 0 ? attempt : 1))
.toString('hex');
if (!new crypto_1.BN(newBlock.substring(2), 'hex').gte(new crypto_1.BN(nextBlock.substring(2), 'hex'))) return [3 /*break*/, 6];
checkBlock = newBlock;
return [4 /*yield*/, this.trackTx(txHash)];
case 3:
case 5:
if (_a.sent()) {

@@ -286,4 +302,8 @@ this.emitConfirm(this.txStatus);

}
return [3 /*break*/, 5];
case 4:
return [3 /*break*/, 7];
case 6:
attempt = attempt - 1 >= 0 ? attempt - 1 : 0;
_a.label = 7;
case 7: return [3 /*break*/, 9];
case 8:
err_1 = _a.sent();

@@ -293,33 +313,30 @@ this.txStatus = "REJECTED" /* REJECTED */;

throw err_1;
case 5:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 7];
return [4 /*yield*/, utils_2.sleep(interval * attempt)];
case 6:
case 9:
if (!(attempt + 1 < maxAttempts)) return [3 /*break*/, 11];
// await sleep(interval * attempt);
return [4 /*yield*/, utils_2.sleep(interval)];
case 10:
// await sleep(interval * attempt);
_a.sent();
_a.label = 7;
case 7:
_a.label = 11;
case 11:
attempt += 1;
return [3 /*break*/, 1];
case 8:
return [3 /*break*/, 2];
case 12:
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
throw new Error("The transaction is still not confirmed after " + maxAttempts + " attempts.");
case 9:
_a.trys.push([9, 15, , 16]);
case 13:
_a.trys.push([13, 18, , 19]);
return [4 /*yield*/, this.trackTx(txHash)];
case 10:
if (!_a.sent()) return [3 /*break*/, 12];
return [4 /*yield*/, this.getBlockNumber()];
case 11:
currentBlock = _a.sent();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
case 14:
if (!_a.sent()) return [3 /*break*/, 15];
this.emitConfirm(this.txStatus);
return [2 /*return*/, this];
case 12: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts, interval)];
case 13:
case 15: return [4 /*yield*/, this.socketConfirm(txHash, maxAttempts)];
case 16:
result = _a.sent();
return [2 /*return*/, result];
case 14: return [3 /*break*/, 16];
case 15:
case 17: return [3 /*break*/, 19];
case 18:
error_1 = _a.sent();

@@ -330,3 +347,3 @@ this.txStatus = "REJECTED" /* REJECTED */;

interval + " mil seconds.");
case 16: return [2 /*return*/];
case 19: return [2 /*return*/];
}

@@ -336,6 +353,5 @@ });

};
Transaction.prototype.socketConfirm = function (txHash, maxAttempts, interval) {
Transaction.prototype.socketConfirm = function (txHash, maxAttempts) {
var _this = this;
if (maxAttempts === void 0) { maxAttempts = 20; }
if (interval === void 0) { interval = 1000; }
return new Promise(function (resolve, reject) {

@@ -345,30 +361,25 @@ var newHeads = Promise.resolve(new network_1.SubscriptionMethod(['newHeads'], _this.messenger));

p.onData(function (data) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var currentBlock;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getBlockNumber()];
case 0:
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 5];
return [4 /*yield*/, this.trackTx(txHash)];
case 1:
currentBlock = _a.sent();
if (!!this.blockNumbers.includes(data.number)) return [3 /*break*/, 6];
return [4 /*yield*/, this.trackTx(txHash)];
case 2:
if (!_a.sent()) return [3 /*break*/, 4];
if (!_a.sent()) return [3 /*break*/, 3];
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 3:
case 2:
_a.sent();
resolve(this);
return [3 /*break*/, 6];
case 4:
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
if (!(this.confirmationCheck === maxAttempts * interval)) return [3 /*break*/, 6];
return [3 /*break*/, 5];
case 3:
if (!(this.confirmationCheck === maxAttempts)) return [3 /*break*/, 5];
this.txStatus = "REJECTED" /* REJECTED */;
this.emitConfirm(this.txStatus);
return [4 /*yield*/, p.unsubscribe()];
case 5:
case 4:
_a.sent();
resolve(this);
_a.label = 6;
case 6: return [2 /*return*/];
_a.label = 5;
case 5: return [2 /*return*/];
}

@@ -408,6 +419,8 @@ });

return tslib_1.__awaiter(this, void 0, void 0, function () {
var currentBlock;
var currentBlock, error_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_blockNumber" /* BlockNumber */, [])];
case 1:

@@ -419,2 +432,6 @@ currentBlock = _a.sent();

return [2 /*return*/, currentBlock.result];
case 2:
error_2 = _a.sent();
throw error_2;
case 3: return [2 /*return*/];
}

@@ -424,2 +441,27 @@ });

};
Transaction.prototype.getBlockByNumber = function (blockNumber) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var block, error_3;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.messenger.send("hmy_getBlockByNumber" /* GetBlockByNumber */, [
blockNumber,
true,
])];
case 1:
block = _a.sent();
if (block.isError()) {
throw block.message;
}
return [2 /*return*/, block.result];
case 2:
error_3 = _a.sent();
throw error_3;
case 3: return [2 /*return*/];
}
});
});
};
return Transaction;

@@ -426,0 +468,0 @@ }());

{
"name": "@harmony-js/transaction",
"version": "0.0.12",
"version": "0.0.13",
"description": "transaction package for harmony",

@@ -20,8 +20,8 @@ "main": "dist/index.js",

"dependencies": {
"@harmony-js/core": "0.0.12",
"@harmony-js/crypto": "0.0.12",
"@harmony-js/network": "0.0.12",
"@harmony-js/utils": "0.0.12"
"@harmony-js/core": "0.0.13",
"@harmony-js/crypto": "0.0.13",
"@harmony-js/network": "0.0.13",
"@harmony-js/utils": "0.0.13"
},
"gitHead": "872d5803a69dc3b90136142180001148b2e34f4d"
"gitHead": "82b12ed970053a0600c59061711d840c80f53529"
}

@@ -23,2 +23,11 @@ import { getContractAddress } from '@harmony-js/crypto';

}
clone(transaction: Transaction): Transaction {
return new Transaction(
transaction.txParams,
this.messenger,
TxStatus.INTIALIZED,
);
}
recover(txHash: string): Transaction {

@@ -25,0 +34,0 @@ const newTxn = new Transaction(

@@ -34,2 +34,3 @@ import {

blockNumbers: string[] = [];
confirmations: number = 0;
confirmationCheck: number = 0;

@@ -283,2 +284,3 @@ receipt?: TransasctionReceipt;

this.id = res.result.transactionHash;
this.confirmations += 1;

@@ -294,2 +296,5 @@ if (this.receipt) {

this.txStatus = TxStatus.PENDING;
const currentBlock = await this.getBlockNumber();
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
return false;

@@ -309,7 +314,27 @@ }

this.txStatus = TxStatus.PENDING;
const oldBlock = await this.getBlockNumber();
let checkBlock = oldBlock;
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
try {
if (await this.trackTx(txHash)) {
this.emitConfirm(this.txStatus);
return this;
const newBlock = await this.getBlockNumber();
const nextBlock =
'0x' +
new BN(checkBlock.substring(2), 'hex')
.add(new BN(attempt === 0 ? attempt : 1))
.toString('hex');
if (
new BN(newBlock.substring(2), 'hex').gte(
new BN(nextBlock.substring(2), 'hex'),
)
) {
checkBlock = newBlock;
if (await this.trackTx(txHash)) {
this.emitConfirm(this.txStatus);
return this;
}
} else {
attempt = attempt - 1 >= 0 ? attempt - 1 : 0;
}

@@ -321,4 +346,6 @@ } catch (err) {

}
if (attempt + 1 < maxAttempts) {
await sleep(interval * attempt);
// await sleep(interval * attempt);
await sleep(interval);
}

@@ -334,13 +361,6 @@ }

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,
);
const result = await this.socketConfirm(txHash, maxAttempts);
return result;

@@ -362,3 +382,2 @@ }

maxAttempts: number = 20,
interval: number = 1000,
): Promise<Transaction> {

@@ -371,3 +390,2 @@ return new Promise((resolve, reject) => {

p.onData(async (data: any) => {
const currentBlock = await this.getBlockNumber();
if (!this.blockNumbers.includes(data.number)) {

@@ -379,6 +397,3 @@ if (await this.trackTx(txHash)) {

} else {
this.blockNumbers.push(currentBlock);
this.confirmationCheck += 1;
if (this.confirmationCheck === maxAttempts * interval) {
if (this.confirmationCheck === maxAttempts) {
this.txStatus = TxStatus.REJECTED;

@@ -416,9 +431,27 @@ this.emitConfirm(this.txStatus);

async getBlockNumber() {
const currentBlock = await this.messenger.send(RPCMethod.BlockNumber, []);
if (currentBlock.isError()) {
throw currentBlock.message;
try {
const currentBlock = await this.messenger.send(RPCMethod.BlockNumber, []);
if (currentBlock.isError()) {
throw currentBlock.message;
}
return currentBlock.result;
} catch (error) {
throw error;
}
return currentBlock.result;
}
async getBlockByNumber(blockNumber: string) {
try {
const block = await this.messenger.send(RPCMethod.GetBlockByNumber, [
blockNumber,
true,
]);
if (block.isError()) {
throw block.message;
}
return block.result;
} catch (error) {
throw error;
}
}
}
export { Transaction };

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc