Skip to content
This repository has been archived by the owner on Feb 6, 2019. It is now read-only.

Commit

Permalink
Merge pull request #47 from OpenSTFoundation/deepesh/gh42/event_notif…
Browse files Browse the repository at this point in the history
…ications

Merging event notification changes with pricer
  • Loading branch information
Abhay Kumar Singh authored Feb 14, 2018
2 parents fbb7844 + 2f35ae0 commit d8eae6e
Show file tree
Hide file tree
Showing 16 changed files with 1,651 additions and 279 deletions.
216 changes: 216 additions & 0 deletions helpers/basic_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
"use strict";

/**
* Perform basic validations
*
* @module helpers/basic_helper
*/

const rootPrefix = '..'
, BigNumber = require('bignumber.js')
;


/**
* Basic helper methods constructor
*
* @constructor
*
*/
const BasicHelperKlass = function() {};

BasicHelperKlass.prototype = {

/**
* Check if address is valid or not
*
* @param {string} address - Address
*
* @return {boolean}
*/
isAddressValid: function (address) {
if (typeof address !== "string") {
return false;
}
return /^0x[0-9a-fA-F]{40}$/.test(address);
},

/**
* Check if uuid is valid or not
*
* @param {string} uuid - Branded Token UUID
*
* @return {boolean}
*/
isUuidValid: function (uuid) {
if (typeof uuid !== "string") {
return false;
}
return /^0x[0-9a-fA-F]{64}$/.test(uuid);
},

/**
* Check if transaction hash is valid or not
*
* @param {string} transactionHash - Transaction hash
*
* @return {boolean}
*/
isTxHashValid: function (transactionHash) {
if (typeof transactionHash !== "string") {
return false;
}
return /^0x[0-9a-fA-F]{64}$/.test(transactionHash);
},

/**
* Check if amount is valid wei number and not zero
*
* @param {number} amountInWei - amount in wei
*
* @return {boolean}
*/
isNonZeroWeiValid: function (amountInWei) {
const oneForMod = new BigNumber('1');

// Convert amount in BigNumber
var bigNumAmount = null;
if (amountInWei instanceof BigNumber) {
bigNumAmount = amountInWei;
} else {
var numAmount = Number(amountInWei);
if (!isNaN(numAmount)) {
bigNumAmount = new BigNumber(numAmount);
}
}

return (!bigNumAmount || bigNumAmount.lessThan(1) || bigNumAmount.isNaN() ||
!bigNumAmount.isFinite() || bigNumAmount.mod(oneForMod) != 0) ? false : true;
},

/**
* Check if tag is valid or not
*
* @param {string} tag - transaction tag
*
* @return {boolean}
*/
isTagValid: function (tag) {
if (typeof tag !== "string") {
return false;
}
return (/^[a-z0-9_\-.]{1,}$/i).test(tag);
},

/**
* get return type for transaction
*
* @param {string} returnType - return from geth transactions when following event is received
*
* @return {string}
*/
getReturnType: function (returnType) {
return ['uuid', 'txHash', 'txReceipt'].includes(returnType) ? returnType : 'txHash';
},

/**
* check if return type is uuid or not
*
* @param {string} returnType - return type
*
* @return {boolean}
*/
isReturnTypeUUID: function(returnType) {
return returnType === 'uuid';
},

/**
* check if return type is txHash or not
*
* @param {string} returnType - return type
*
* @return {boolean}
*/
isReturnTypeTxHash: function(returnType) {
return returnType === 'txHash';
},

/**
* check if return type is txReceipt or not
*
* @param {string} returnType - return type
*
* @return {boolean}
*/
isReturnTypeTxReceipt: function(returnType) {
return returnType === 'txReceipt';
},

/**
* Check if branded token name is valid or not
*
* @param {string} name - Branded token name
*
* @return {boolean}
*/
isBTNameValid: function (name) {
if (typeof name !== "string") {
return false;
}
return (/^[a-z0-9\s]{1,}$/i).test(name);
},

/**
* Check if branded token symbol is valid or not
*
* @param {string} symbol - Branded token symbol
*
* @return {boolean}
*/
isBTSymbolValid: function (symbol) {
if (typeof symbol !== "string") {
return false;
}
return (/^[a-z0-9]{1,}$/i).test(symbol);
},

/**
* Check if branded token conversion rate is valid or not
*
* @param {number} conversionRate - Branded token conversion rate
*
* @return {boolean}
*/
isBTConversionRateValid: function (conversionRate) {
if (isNaN(conversionRate) || (conversionRate % 1) != 0 || parseInt(conversionRate) < 1) {
return false;
}
return true;
},

/**
* Convert wei to proper string. Make sure it's a valid number
*
* @param {number} amountInWei - amount in wei to be formatted
*
* @return {string}
*/
formatWeiToString: function (amountInWei) {
const oThis = this;
return oThis.convertToBigNumber(amountInWei).toString(10);
},

/**
* Convert number to big number. Make sure it's a valid number
*
* @param {number} amountInWei - amount in wei to be formatted
*
* @return {BigNumber}
*/
convertToBigNumber: function (number) {
return (number instanceof BigNumber) ? number : new BigNumber(number);
}

};

module.exports = new BasicHelperKlass();
2 changes: 0 additions & 2 deletions lib/contract_interact/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,10 @@ const helper = {

var handleResponse = function (response) {
if (response) {
console.log("----> addressToNameMap" , addressToNameMap);
//clearInterval(txSetInterval);
const web3EventsDecoderResult = web3EventsDecoder.perform(response, addressToNameMap);
onResolve(web3EventsDecoderResult);
} else {
console.log('Waiting for ' + transactionHash + ' to be mined');
tryReceipt();
}
};
Expand Down
Loading

0 comments on commit d8eae6e

Please sign in to comment.