From c7e4791c427850fa0759c1fe354d917c2f1e4524 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Tue, 13 Feb 2018 19:40:26 +0530 Subject: [PATCH 01/10] Event notification initial commit --- helpers/basic_helper.js | 216 ++++++++++++++++++ lib/contract_interact/helper.js | 4 +- lib/contract_interact/pricer.js | 171 +++++++++++--- lib/web3/providers/rpc.js | 3 + mocha_test/services/pricer/constants.js | 3 +- mocha_test/services/pricer/event_listner.js | 59 +++++ mocha_test/services/pricer/pricer_utils.js | 63 ++++- .../services/pricer/set_accepted_margin.js | 60 +++-- package.json | 2 + 9 files changed, 523 insertions(+), 58 deletions(-) create mode 100644 helpers/basic_helper.js create mode 100644 mocha_test/services/pricer/event_listner.js diff --git a/helpers/basic_helper.js b/helpers/basic_helper.js new file mode 100644 index 0000000..1c8b54b --- /dev/null +++ b/helpers/basic_helper.js @@ -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(); \ No newline at end of file diff --git a/lib/contract_interact/helper.js b/lib/contract_interact/helper.js index 3acd652..573b5d4 100644 --- a/lib/contract_interact/helper.js +++ b/lib/contract_interact/helper.js @@ -223,12 +223,12 @@ const helper = { var handleResponse = function (response) { if (response) { - console.log("----> addressToNameMap" , addressToNameMap); + //console.log("----> addressToNameMap" , addressToNameMap); //clearInterval(txSetInterval); const web3EventsDecoderResult = web3EventsDecoder.perform(response, addressToNameMap); onResolve(web3EventsDecoderResult); } else { - console.log('Waiting for ' + transactionHash + ' to be mined'); + //console.log('Waiting for ' + transactionHash + ' to be mined'); tryReceipt(); } }; diff --git a/lib/contract_interact/pricer.js b/lib/contract_interact/pricer.js index a026eeb..261cbd5 100644 --- a/lib/contract_interact/pricer.js +++ b/lib/contract_interact/pricer.js @@ -7,17 +7,21 @@ * @module lib/contract_interact/pricer * */ -const rootPrefix = '../..'; -const helper = require(rootPrefix + '/lib/contract_interact/helper'); -const coreAddresses = require(rootPrefix + '/config/core_addresses'); -const web3RpcProvider = require(rootPrefix + '/lib/web3/providers/rpc'); -const coreConstants = require(rootPrefix + '/config/core_constants'); -const contractName = 'pricer'; -const contractAbi = coreAddresses.getAbiForContract(contractName); -const GAS_LIMIT = coreConstants.OST_GAS_LIMIT; -const currContract = new web3RpcProvider.eth.Contract(contractAbi); -//const logger = require(rootPrefix + '/helpers/custom_console_logger'); -const responseHelper = require(rootPrefix + '/lib/formatter/response'); +const rootPrefix = '../..' + , helper = require(rootPrefix + '/lib/contract_interact/helper') + , coreAddresses = require(rootPrefix + '/config/core_addresses') + , web3RpcProvider = require(rootPrefix + '/lib/web3/providers/rpc') + , coreConstants = require(rootPrefix + '/config/core_constants') + , contractName = 'pricer' + , contractAbi = coreAddresses.getAbiForContract(contractName) + , GAS_LIMIT = coreConstants.OST_GAS_LIMIT + , currContract = new web3RpcProvider.eth.Contract(contractAbi) + , responseHelper = require(rootPrefix + '/lib/formatter/response') + , openSTNotification = require('@openstfoundation/openst-notification') + , uuid = require('uuid') + , basicHelper = require(rootPrefix + '/helpers/basic_helper'); + +//const logger = require(rootPrefix + '/helpers/custom_console_logger') /** @@ -153,10 +157,10 @@ Pricer.prototype = { return responseHelper.error('l_ci_p_spo_2', 'gas is mandatory'); } if (!helper.isAddressValid(address)) { - return responseHelper.error('l_ci_p_spo_3', 'address is invalid'); + return responseHelper.error('l_ci_p_spo_3', 'address is invalid'); } if (!helper.isAddressValid(senderAddress)) { - return responseHelper.error('l_ci_p_spo_4', 'address is invalid'); + return responseHelper.error('l_ci_p_spo_4', 'address is invalid'); } currContract.options.address = this.contractAddress; @@ -227,33 +231,138 @@ Pricer.prototype = { * @return {Promise} * */ - setAcceptedMargin: function (senderAddress, senderPassphrase, currency, acceptedMargin, gasPrice) { + setAcceptedMargin: function (senderAddress, senderPassphrase, currency, acceptedMargin, gasPrice, chainId, options) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_sam_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_sam_1', 'currency is mandatory')); } - if (gasPrice === undefined || gasPrice === '' || gasPrice == 0 || gasPrice === null) { - return responseHelper.error('l_ci_p_sam_2', 'gas price is mandatory'); + if (gasPrice === undefined || gasPrice === '' || gasPrice == 0 || gasPrice === null) { + return Promise.resolve(responseHelper.error('l_ci_p_sam_2', 'gas price is mandatory')); } if (acceptedMargin<0) { - return responseHelper.error('l_ci_p_sam_3', 'accepted margin cannot be negetive'); + return Promise.resolve(responseHelper.error('l_ci_p_sam_3', 'accepted margin cannot be negetive')); } if (!helper.isAddressValid(senderAddress)) { - return responseHelper.error('l_ci_p_sam_4', 'address is invalid'); + return Promise.resolve(responseHelper.error('l_ci_p_sam_4', 'address is invalid')); + } + + /*eslint-disable */ + options = options || {}; + + const oThis = this + , txUUID = uuid.v4() + , tag = options.tag + , returnType = basicHelper.getReturnType(options.returnType) + , notificationData = { + topics: ['payment.pricer.setAcceptedMargin'], + message: { + kind: '', // populate later: with every stage + payload: { + contract_name: contractName, + contract_address: oThis.contractAddress, + method: 'setAcceptedMargin', + params: {args: [], txParams: {}}, // populate later: when Tx params created + transaction_hash: '', // populate later: when Tx submitted + chain_id: web3RpcProvider.chainId, + chain_kind: web3RpcProvider.chainKind, + uuid: txUUID, + tag: tag, + error_data: {} // populate later: when error received + } + } + } + + const asyncSetMargin = function(){ + /*eslint-enable */ + const transactionObject = currContract.methods.setAcceptedMargin( + web3RpcProvider.utils.asciiToHex(currency), + acceptedMargin); + + const encodedABI = transactionObject.encodeABI(); + + const txParams = { + from: senderAddress, + to: oThis.contractAddress, + data: encodedABI, + gasPrice: gasPrice, + gas: GAS_LIMIT + }; + + // set params in notification data + notificationData.message.payload.params.txParams = txParams; + + return new Promise(function (onResolve, onReject) { + // Unlock account + web3RpcProvider.eth.personal.unlockAccount( + senderAddress, + senderPassphrase) + .then(function() { + web3RpcProvider.eth.sendTransaction(txParams) + .on('transactionHash', function(transactionHash) { + // set transaction hash in notification data + // eslint-disable-next-line camelcase + notificationData.message.payload.transaction_hash = transactionHash; + // Publish event + notificationData.message.kind = 'transaction_initiated'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxHash(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: transactionHash, + transaction_receipt: {} + })); + } + }) + .on('receipt', function(receipt) { + // Publish event + notificationData.message.kind = 'transaction_mined'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxReceipt(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: receipt.transactionHash, + transaction_receipt: receipt + })); + } + }); + }) + .catch(function(reason) { + // set error data in notification data + notificationData.message.payload.error_data = reason; + + // Publish event + notificationData.message.kind = 'error'; + openSTNotification.publishEvent.perform(notificationData); + + return Promise.resolve(responseHelper.error('l_ci_p_sam_5', 'Set accepted margin failed')); + }); + + }); + }; + + // Perform set margin transaction as requested + if (basicHelper.isReturnTypeUUID(returnType)) { + asyncSetMargin(); + return Promise.resolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: "", + transaction_receipt: {} + })); + } + else { + return asyncSetMargin(); } - const transactionObject = currContract.methods.setAcceptedMargin(web3RpcProvider.utils.asciiToHex(currency), acceptedMargin); - const encodedABI = transactionObject.encodeABI(); - return helper.sendTxAsyncFromAddr( - web3RpcProvider, - this.contractAddress, - encodedABI, - senderAddress, - senderPassphrase, - { gasPrice: gasPrice, gas: GAS_LIMIT } - ).then(function(transactionHash) { - return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); - }); }, diff --git a/lib/web3/providers/rpc.js b/lib/web3/providers/rpc.js index 995e25b..b1bb6bc 100644 --- a/lib/web3/providers/rpc.js +++ b/lib/web3/providers/rpc.js @@ -6,4 +6,7 @@ const Web3 = require('web3') , provider = new Web3.providers.HttpProvider(coreConstants.OST_PRICER_GETH_RPC_PROVIDER) , web3RpcProvider = new Web3(provider); +web3RpcProvider.chainId = coreConstants.OST_PRICER_CHAIN_ID; +web3RpcProvider.chainKind = ''; + module.exports = web3RpcProvider; diff --git a/mocha_test/services/pricer/constants.js b/mocha_test/services/pricer/constants.js index 1d79401..99e6742 100644 --- a/mocha_test/services/pricer/constants.js +++ b/mocha_test/services/pricer/constants.js @@ -21,6 +21,7 @@ const constants = { priceOracles: JSON.parse(process.env.OST_PO_PRICE_ORACLES), TC5Address: process.env.TEST_COIN1_C5_ADDRESS, TC2Address: process.env.TEST_COIN2_C2_ADDRESS, - TC3Address: process.env.TEST_COIN3_C3_ADDRESS + TC3Address: process.env.TEST_COIN3_C3_ADDRESS, + chainId: process.env.OST_PRICER_CHAIN_ID }; module.exports = constants; diff --git a/mocha_test/services/pricer/event_listner.js b/mocha_test/services/pricer/event_listner.js new file mode 100644 index 0000000..74ac19c --- /dev/null +++ b/mocha_test/services/pricer/event_listner.js @@ -0,0 +1,59 @@ +const openSTNotification = require('@openstfoundation/openst-notification'); + +var notificationRef = null; +var allEvents = {}; + + +module.exports.verifyIfEventFired = function(uuid, kind) { + const key = `${uuid}_${kind}`; + console.log("verifyIfEventFired"); + console.log(key); + console.log(allEvents[key]); + return allEvents[key] !== undefined && allEvents[key] !== "undefined" && allEvents[key] !== null; +}; + +module.exports.startObserving = function() { + if (notificationRef === null) { + openSTNotification.subscribeEvent.local( + ["payment.pricer.setAcceptedMargin"], + function(msgContent) { + const messageData = JSON.parse(msgContent); + const key = `${messageData.message.payload.uuid}_${messageData.message.kind}`; + allEvents[key] = messageData.message; + }); + notificationRef = openSTNotification; + } +}; + +/* + +{ + "topics": [ + "payment.pricer.setAcceptedMargin" + ], + "message": { + "kind": "transaction_mined", + "payload": { + "contract_name": "pricer", + "contract_address": "0xF7FB23e884Aeb83E1606E36b8327666df158aEb9", + "method": "setAcceptedMargin", + "params": { + "args": [], + "txParams": { + "from": "0x1bc1b631e56e8806f8335fa00298eef0d3ae50d1", + "to": "0xf7fb23e884aeb83e1606e36b8327666df158aeb9", + "data": "0xbd90bc3055534400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032", + "gasPrice": "0xba43b7400", + "gas": "0x8adae0" + } + }, + "transaction_hash": "0x7e752ae628e84d776ffcc78fbb25f3cf610b7c82f20a76c03e28d4da16eceab2", + "chain_id": "2000", + "chain_kind": "", + "uuid": "89787ab2-b969-4851-9fd2-4f8cbca69d89", + "error_data": {} + } + } +} + +*/ diff --git a/mocha_test/services/pricer/pricer_utils.js b/mocha_test/services/pricer/pricer_utils.js index fa7216c..0bbeebf 100644 --- a/mocha_test/services/pricer/pricer_utils.js +++ b/mocha_test/services/pricer/pricer_utils.js @@ -1,14 +1,67 @@ -const chai = require('chai') - , assert = chai.assert; +const chai = require('chai') + , assert = chai.assert + ,rootPrefix = '../../..' + , eventListner = require('./event_listner') + , helper = require(rootPrefix + '/lib/contract_interact/helper') + ; -module.exports.verifyReceipt = async (pricer, transactionHash) => { +eventListner.startObserving(); - const receipt = await pricer.getTxReceipt(transactionHash); +module.exports.verifyIfMined = async (pricer, transactionHash) => { + + const receipt = await pricer.getTxReceipt(transactionHash); assert.equal(receipt.isSuccess(), true); - assert.equal(transactionHash, receipt.data.transactionReceipt.formattedTransactionReceipt.transactionHash); assert.exists(receipt.data.transactionReceipt); + assert.equal(transactionHash, receipt.data.transactionReceipt.formattedTransactionReceipt.transactionHash); + +}; + +module.exports.verifyTransactionUUID = function (response) { + // verify if the response is success + assert.equal(response.isSuccess(), true, "response success check"); + + // verify if the uuid, transaction has and transaction receipt is available + assert.exists(response.data.transaction_uuid, "check if transaction uuid exists"); + assert.equal(response.data.transaction_hash, "", "check if transaction hash does not exists"); + assert.equal(response.data.transaction_receipt, "", "check if transaction receipt does not exists"); + + // verify if the events were fired + //assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_initiated"), false, "Event verification for transaction_initiated"); + //assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_mined"), false, "Event verification for transaction_mined"); + +}; + +module.exports.verifyTransactionHash = function (response) { + // verify if the response is success + assert.equal(response.isSuccess(), true, "response success check"); + + // verify if the uuid, transaction has and transaction receipt is available + assert.exists(response.data.transaction_uuid, "check if transaction uuid exists"); + assert.exists(response.data.transaction_hash, "check if transaction hash exists"); + assert.equal(response.data.transaction_receipt, "", "check if transaction receipt does not exists"); + + // verify if the events were fired + assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_initiated"), true, "Event verification for transaction_initiated"); + //assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_mined"), true, "Event verification for transaction_mined"); + +}; + +module.exports.verifyTransactionReceipt = function (response) { + console.log("verifyTransactionReceipt"); + console.log(response); + // verify if the response is success + assert.equal(response.isSuccess(), true, "response success check"); + + // verify if the uuid, transaction has and transaction receipt is available + assert.exists(response.data.transaction_uuid, "check if transaction uuid exists"); + assert.exists(response.data.transaction_hash, "check if transaction hash exists"); + assert.exists(response.data.transaction_receipt, "check if transaction receipt exists"); + + // verify if the events were fired + assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_initiated"), true, "Event verification for transaction_initiated"); + assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_mined"), true, "Event verification for transaction_mined"); }; diff --git a/mocha_test/services/pricer/set_accepted_margin.js b/mocha_test/services/pricer/set_accepted_margin.js index bf353e9..8920822 100644 --- a/mocha_test/services/pricer/set_accepted_margin.js +++ b/mocha_test/services/pricer/set_accepted_margin.js @@ -22,21 +22,23 @@ describe('Set accepted margins', function() { assert.notEqual(constants.deployer, constants.account1); assert.notEqual(constants.ops, constants.account1); - const response = await pricerOstUsd.setAcceptedMargin( + /* const response = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 30, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: 'txReceipt'}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + pricerUtils.verifyTransactionReceipt(response); + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if value is changed const amResult = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult.isSuccess(), true); assert.equal(30, amResult.data.acceptedMargins); + */ }); @@ -47,11 +49,14 @@ describe('Set accepted margins', function() { constants.opsPassphrase, constants.currencyBlank, 3, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: 'txReceipt'}); assert.equal(response.isFailure(), true); }); + it('should fail when gas amount is 0', async function() { const response = await pricerOstUsd.setAcceptedMargin( @@ -59,7 +64,9 @@ describe('Set accepted margins', function() { constants.opsPassphrase, constants.currencyUSD, 3, - 0); + 0, + constants.chainId, + {returnType: 'txReceipt'}); assert.equal(response.isFailure(), true); }); @@ -71,7 +78,9 @@ describe('Set accepted margins', function() { constants.opsPassphrase, constants.currencyUSD, 3, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: 'txReceipt'}); assert.equal(response.isFailure(), true); }); @@ -84,48 +93,61 @@ describe('Set accepted margins', function() { constants.opsPassphrase, constants.currencyUSD, -30, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: 'txReceipt'}); assert.equal(response.isFailure(), true); }); + it('should fail when sender is not ops', async function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set the accepted margin to 3 const response1 = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 3, - 0xBA43B7400); - assert.equal(response1.isSuccess(), true); - assert.exists(response1.data.transactionHash); + 0xBA43B7400, + constants.chainId, + {returnType: 'txReceipt'}); - await pricerUtils.verifyReceipt(pricerOstUsd, response1.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response1); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response1.data.transaction_hash); + + // verify if the accepted margin is set to 3 const amResult1 = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult1.isSuccess(), true); assert.equal(3, amResult1.data.acceptedMargins); + // set the accepted margin to 8 const response2 = await pricerOstUsd.setAcceptedMargin( constants.deployer, constants.deployerPassphrase, constants.currencyUSD, 8, 0xBA43B7400); - assert.equal(response2.isSuccess(), true); - assert.exists(response2.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response2.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response2); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response2.data.transaction_hash); + // verify if the accepted margin is still set to 3 const amResult2 = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult2.isSuccess(), true); assert.equal(3, amResult2.data.acceptedMargins); }); - +/* it('should pass when margin is 0', async function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); @@ -167,7 +189,7 @@ describe('Set accepted margins', function() { assert.equal(50, amResult.data.acceptedMargins); }); - +*/ }); diff --git a/package.json b/package.json index 10dee28..5bbb226 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,8 @@ "version": "0.9.2", "description": "", "dependencies": { + "@openstfoundation/openst-notification": "1.0.0-beta.1", + "@ostdotcom/ost-price-oracle": "^1.0.0-beta.3", "bignumber": "^1.1.0", "bignumber.js": "^4.1.0", "moment": "^2.19.2", From bb8ff261c793889db96f886ffaca794dacd4c5c7 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 00:02:25 +0530 Subject: [PATCH 02/10] Updated mocha tests for setAcceptedMargin, unsetPriceOracle --- lib/contract_interact/pricer.js | 264 +++++++++++++++++- mocha_test/scripts/deploy_price_oracle.sh | 2 +- .../services/pricer/accepted_margins.js | 44 ++- mocha_test/services/pricer/constants.js | 5 +- mocha_test/services/pricer/event_listner.js | 47 +--- mocha_test/services/pricer/pricer_utils.js | 31 +- .../services/pricer/set_accepted_margin.js | 138 +++++++-- .../services/pricer/set_price_oracle.js | 32 ++- .../services/pricer/unset_price_oracle.js | 138 +++++++-- 9 files changed, 575 insertions(+), 126 deletions(-) diff --git a/lib/contract_interact/pricer.js b/lib/contract_interact/pricer.js index 261cbd5..b32a7a8 100644 --- a/lib/contract_interact/pricer.js +++ b/lib/contract_interact/pricer.js @@ -148,7 +148,7 @@ Pricer.prototype = { * @return {Promise} * */ - setPriceOracle: function (senderAddress, senderPassphrase, currency, address, gasPrice) { + setPriceOracle: function (senderAddress, senderPassphrase, currency, address, gasPrice, chainId, options) { if (currency === undefined || currency === '' || currency === null) { return responseHelper.error('l_ci_p_spo_1', 'currency is mandatory'); @@ -163,7 +163,130 @@ Pricer.prototype = { return responseHelper.error('l_ci_p_spo_4', 'address is invalid'); } - currContract.options.address = this.contractAddress; + /*eslint-disable */ + options = options || {}; + + const oThis = this + , txUUID = uuid.v4() + , tag = options.tag + , returnType = basicHelper.getReturnType(options.returnType) + , notificationData = { + topics: ['payment.pricer.setPriceOracle'], + message: { + kind: '', // populate later: with every stage + payload: { + contract_name: contractName, + contract_address: oThis.contractAddress, + method: 'setPriceOracle', + params: {args: [], txParams: {}}, // populate later: when Tx params created + transaction_hash: '', // populate later: when Tx submitted + chain_id: web3RpcProvider.chainId, + chain_kind: web3RpcProvider.chainKind, + uuid: txUUID, + tag: tag, + error_data: {} // populate later: when error received + } + } + } + /*eslint-enable */ + + const asyncSetPriceOracle = function() { + + currContract.options.address = oThis.contractAddress; + currContract.setProvider( web3RpcProvider.currentProvider ); + + const transactionObject = currContract.methods.setPriceOracle( + web3RpcProvider.utils.asciiToHex(currency), + address); + + const encodedABI = transactionObject.encodeABI(); + + //TODO: calculate the gas limit + const txParams = { + from: senderAddress, + to: oThis.contractAddress, + data: encodedABI, + gasPrice: gasPrice, + gas: GAS_LIMIT + }; + + // set params in notification data + notificationData.message.payload.params.txParams = txParams; + + return new Promise(function (onResolve, onReject) { + // Unlock account + web3RpcProvider.eth.personal.unlockAccount( + senderAddress, + senderPassphrase) + .then(function() { + web3RpcProvider.eth.sendTransaction(txParams) + .on('transactionHash', function(transactionHash) { + // set transaction hash in notification data + // eslint-disable-next-line camelcase + notificationData.message.payload.transaction_hash = transactionHash; + // Publish event + notificationData.message.kind = 'transaction_initiated'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxHash(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: transactionHash, + transaction_receipt: {} + })); + } + }) + .on('receipt', function(receipt) { + // Publish event + notificationData.message.kind = 'transaction_mined'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxReceipt(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: receipt.transactionHash, + transaction_receipt: receipt + })); + } + }); + }) + .catch(function(reason) { + // set error data in notification data + notificationData.message.payload.error_data = reason; + + // Publish event + notificationData.message.kind = 'error'; + openSTNotification.publishEvent.perform(notificationData); + + return Promise.resolve(responseHelper.error('l_ci_p_spo_5', 'Set price oracles failed')); + }); + + }); + + }; + + // Perform set price oracle transaction as requested + if (basicHelper.isReturnTypeUUID(returnType)) { + asyncSetPriceOracle(); + return Promise.resolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: "", + transaction_receipt: {} + })); + } + else { + return asyncSetPriceOracle(); + } + + /* currContract.options.address = this.contractAddress; currContract.setProvider( web3RpcProvider.currentProvider ); const transactionObject = currContract.methods.setPriceOracle(web3RpcProvider.utils.asciiToHex(currency), address); const encodedABI = transactionObject.encodeABI(); @@ -177,6 +300,8 @@ Pricer.prototype = { ).then(function(transactionHash) { return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); }); + + */ }, @@ -191,7 +316,7 @@ Pricer.prototype = { * @return {Promise} * */ - unsetPriceOracle: function (senderAddress, senderPassphrase, currency, gasPrice) { + unsetPriceOracle: function (senderAddress, senderPassphrase, currency, gasPrice, chainId, options) { if (currency === undefined || currency === '' || currency === null) { return responseHelper.error('l_ci_p_uspo_1', 'currency is mandatory'); } @@ -202,6 +327,128 @@ Pricer.prototype = { return responseHelper.error('l_ci_p_spo_3', 'address is invalid'); } + /*eslint-disable */ + options = options || {}; + + const oThis = this + , txUUID = uuid.v4() + , tag = options.tag + , returnType = basicHelper.getReturnType(options.returnType) + , notificationData = { + topics: ['payment.pricer.unsetPriceOracle'], + message: { + kind: '', // populate later: with every stage + payload: { + contract_name: contractName, + contract_address: oThis.contractAddress, + method: 'unsetPriceOracle', + params: {args: [], txParams: {}}, // populate later: when Tx params created + transaction_hash: '', // populate later: when Tx submitted + chain_id: web3RpcProvider.chainId, + chain_kind: web3RpcProvider.chainKind, + uuid: txUUID, + tag: tag, + error_data: {} // populate later: when error received + } + } + } + /*eslint-enable */ + + const asyncUnsetPriceOracle = function() { + + currContract.options.address = oThis.contractAddress; + currContract.setProvider( web3RpcProvider.currentProvider ); + + const transactionObject = currContract.methods.unsetPriceOracle(web3RpcProvider.utils.asciiToHex(currency)); + const encodedABI = transactionObject.encodeABI(); + + //TODO: calculate the gas limit + const txParams = { + from: senderAddress, + to: oThis.contractAddress, + data: encodedABI, + gasPrice: gasPrice, + gas: GAS_LIMIT + }; + + // set params in notification data + notificationData.message.payload.params.txParams = txParams; + + return new Promise(function (onResolve, onReject) { + // Unlock account + web3RpcProvider.eth.personal.unlockAccount( + senderAddress, + senderPassphrase) + .then(function() { + web3RpcProvider.eth.sendTransaction(txParams) + .on('transactionHash', function(transactionHash) { + // set transaction hash in notification data + // eslint-disable-next-line camelcase + notificationData.message.payload.transaction_hash = transactionHash; + // Publish event + notificationData.message.kind = 'transaction_initiated'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxHash(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: transactionHash, + transaction_receipt: {} + })); + } + }) + .on('receipt', function(receipt) { + // Publish event + notificationData.message.kind = 'transaction_mined'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxReceipt(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: receipt.transactionHash, + transaction_receipt: receipt + })); + } + }); + }) + .catch(function(reason) { + // set error data in notification data + notificationData.message.payload.error_data = reason; + + // Publish event + notificationData.message.kind = 'error'; + openSTNotification.publishEvent.perform(notificationData); + + return Promise.resolve(responseHelper.error('l_ci_p_spo_4', 'unset price oracles failed')); + }); + + }); + + }; + + + // Perform set price oracle transaction as requested + if (basicHelper.isReturnTypeUUID(returnType)) { + asyncUnsetPriceOracle(); + return Promise.resolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: "", + transaction_receipt: {} + })); + } + else { + return asyncUnsetPriceOracle(); + } + + /* currContract.options.address = this.contractAddress; currContract.setProvider( web3RpcProvider.currentProvider ); const transactionObject = currContract.methods.unsetPriceOracle(web3RpcProvider.utils.asciiToHex(currency)); @@ -216,6 +463,7 @@ Pricer.prototype = { ).then(function(transactionHash) { return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); }); + */ }, @@ -271,15 +519,19 @@ Pricer.prototype = { } } } - - const asyncSetMargin = function(){ - /*eslint-enable */ + /*eslint-enable */ + const asyncSetMargin = function() { + + currContract.options.address = oThis.contractAddress; + currContract.setProvider( web3RpcProvider.currentProvider ); + const transactionObject = currContract.methods.setAcceptedMargin( web3RpcProvider.utils.asciiToHex(currency), acceptedMargin); const encodedABI = transactionObject.encodeABI(); + //TODO: calculate the gas limit const txParams = { from: senderAddress, to: oThis.contractAddress, diff --git a/mocha_test/scripts/deploy_price_oracle.sh b/mocha_test/scripts/deploy_price_oracle.sh index 661c321..bc7b13c 100644 --- a/mocha_test/scripts/deploy_price_oracle.sh +++ b/mocha_test/scripts/deploy_price_oracle.sh @@ -1,7 +1,7 @@ #!/bin/bash echo "\n********* Preparing price oracle deployment *************" -npm install @ostdotcom/ost-price-oracle +npm install @ostdotcom/ost-price-oracle@1.0.0-beta.3 cd ../../node_modules/@ostdotcom/ost-price-oracle/ # git clone git@github.com:OpenSTFoundation/ost-price-oracle.git diff --git a/mocha_test/services/pricer/accepted_margins.js b/mocha_test/services/pricer/accepted_margins.js index d93e28c..d04490f 100644 --- a/mocha_test/services/pricer/accepted_margins.js +++ b/mocha_test/services/pricer/accepted_margins.js @@ -47,49 +47,65 @@ describe('Get accepted margins', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set the accepted margin to 50 const response1 = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 50, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response1.isSuccess(), true); - assert.exists(response1.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response1.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response1); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response1.data.transaction_hash); + + // verify if the accepted margin is set to 50 const amResult1 = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult1.isSuccess(), true); assert.equal(50, amResult1.data.acceptedMargins); - + // set the accepted margin to 300 const response2 = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 300, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response2); - assert.equal(response2.isSuccess(), true); - assert.exists(response2.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response2.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response2.data.transaction_hash); + // verify if the accepted margin is set to 300 const amResult2 = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult2.isSuccess(), true); assert.equal(300, amResult2.data.acceptedMargins); - + // set the accepted margin to 100 const response3 = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyEUR, 100, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response3); - assert.equal(response3.isSuccess(), true); - assert.exists(response3.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response3.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response3.data.transaction_hash); + // verify if the accepted margin is set to 100 const amResult3 = await pricerOstUsd.acceptedMargins(constants.currencyEUR); assert.equal(amResult3.isSuccess(), true); assert.equal(100, amResult3.data.acceptedMargins); diff --git a/mocha_test/services/pricer/constants.js b/mocha_test/services/pricer/constants.js index 99e6742..c165866 100644 --- a/mocha_test/services/pricer/constants.js +++ b/mocha_test/services/pricer/constants.js @@ -22,6 +22,9 @@ const constants = { TC5Address: process.env.TEST_COIN1_C5_ADDRESS, TC2Address: process.env.TEST_COIN2_C2_ADDRESS, TC3Address: process.env.TEST_COIN3_C3_ADDRESS, - chainId: process.env.OST_PRICER_CHAIN_ID + chainId: process.env.OST_PRICER_CHAIN_ID, + returnTypeUUID: "uuid", + returnTypeHash: "txHash", + returnTypeReceipt: "txReceipt" }; module.exports = constants; diff --git a/mocha_test/services/pricer/event_listner.js b/mocha_test/services/pricer/event_listner.js index 74ac19c..b1f1cf1 100644 --- a/mocha_test/services/pricer/event_listner.js +++ b/mocha_test/services/pricer/event_listner.js @@ -5,55 +5,24 @@ var allEvents = {}; module.exports.verifyIfEventFired = function(uuid, kind) { - const key = `${uuid}_${kind}`; - console.log("verifyIfEventFired"); - console.log(key); - console.log(allEvents[key]); + const key = `${uuid}_${kind}`; return allEvents[key] !== undefined && allEvents[key] !== "undefined" && allEvents[key] !== null; }; module.exports.startObserving = function() { if (notificationRef === null) { openSTNotification.subscribeEvent.local( - ["payment.pricer.setAcceptedMargin"], - function(msgContent) { + [ + "payment.pricer.setAcceptedMargin", + "payment.pricer.setPriceOracle", + "payment.pricer.unsetPriceOracle" + ], + function(msgContent) { const messageData = JSON.parse(msgContent); const key = `${messageData.message.payload.uuid}_${messageData.message.kind}`; allEvents[key] = messageData.message; + console.log("Event received: "+ key); }); notificationRef = openSTNotification; } }; - -/* - -{ - "topics": [ - "payment.pricer.setAcceptedMargin" - ], - "message": { - "kind": "transaction_mined", - "payload": { - "contract_name": "pricer", - "contract_address": "0xF7FB23e884Aeb83E1606E36b8327666df158aEb9", - "method": "setAcceptedMargin", - "params": { - "args": [], - "txParams": { - "from": "0x1bc1b631e56e8806f8335fa00298eef0d3ae50d1", - "to": "0xf7fb23e884aeb83e1606e36b8327666df158aeb9", - "data": "0xbd90bc3055534400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032", - "gasPrice": "0xba43b7400", - "gas": "0x8adae0" - } - }, - "transaction_hash": "0x7e752ae628e84d776ffcc78fbb25f3cf610b7c82f20a76c03e28d4da16eceab2", - "chain_id": "2000", - "chain_kind": "", - "uuid": "89787ab2-b969-4851-9fd2-4f8cbca69d89", - "error_data": {} - } - } -} - -*/ diff --git a/mocha_test/services/pricer/pricer_utils.js b/mocha_test/services/pricer/pricer_utils.js index 0bbeebf..5c71280 100644 --- a/mocha_test/services/pricer/pricer_utils.js +++ b/mocha_test/services/pricer/pricer_utils.js @@ -1,10 +1,11 @@ - -const chai = require('chai') +/*eslint-disable */ +const chai = require('chai') , assert = chai.assert ,rootPrefix = '../../..' , eventListner = require('./event_listner') , helper = require(rootPrefix + '/lib/contract_interact/helper') ; +/*eslint-enable */ eventListner.startObserving(); @@ -19,44 +20,40 @@ module.exports.verifyIfMined = async (pricer, transactionHash) => { }; module.exports.verifyTransactionUUID = function (response) { + // verify if the response is success assert.equal(response.isSuccess(), true, "response success check"); // verify if the uuid, transaction has and transaction receipt is available - assert.exists(response.data.transaction_uuid, "check if transaction uuid exists"); - assert.equal(response.data.transaction_hash, "", "check if transaction hash does not exists"); - assert.equal(response.data.transaction_receipt, "", "check if transaction receipt does not exists"); - - // verify if the events were fired - //assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_initiated"), false, "Event verification for transaction_initiated"); - //assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_mined"), false, "Event verification for transaction_mined"); + assert.isNotEmpty(response.data.transaction_uuid, "check if transaction uuid exists"); + assert.isEmpty(response.data.transaction_hash, "check if transaction hash does not exists"); + assert.isEmpty(response.data.transaction_receipt, "check if transaction receipt does not exists"); }; module.exports.verifyTransactionHash = function (response) { + // verify if the response is success assert.equal(response.isSuccess(), true, "response success check"); // verify if the uuid, transaction has and transaction receipt is available - assert.exists(response.data.transaction_uuid, "check if transaction uuid exists"); - assert.exists(response.data.transaction_hash, "check if transaction hash exists"); - assert.equal(response.data.transaction_receipt, "", "check if transaction receipt does not exists"); + assert.isNotEmpty(response.data.transaction_uuid, "check if transaction uuid exists"); + assert.isNotEmpty(response.data.transaction_hash, "check if transaction hash exists"); + assert.isEmpty(response.data.transaction_receipt, "check if transaction receipt does not exists"); // verify if the events were fired assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_initiated"), true, "Event verification for transaction_initiated"); - //assert.equal(eventListner.verifyIfEventFired(response.data.transaction_uuid, "transaction_mined"), true, "Event verification for transaction_mined"); }; module.exports.verifyTransactionReceipt = function (response) { - console.log("verifyTransactionReceipt"); - console.log(response); + // verify if the response is success assert.equal(response.isSuccess(), true, "response success check"); // verify if the uuid, transaction has and transaction receipt is available - assert.exists(response.data.transaction_uuid, "check if transaction uuid exists"); - assert.exists(response.data.transaction_hash, "check if transaction hash exists"); + assert.isNotEmpty(response.data.transaction_uuid, "check if transaction uuid exists"); + assert.isNotEmpty(response.data.transaction_hash, "check if transaction hash exists"); assert.exists(response.data.transaction_receipt, "check if transaction receipt exists"); // verify if the events were fired diff --git a/mocha_test/services/pricer/set_accepted_margin.js b/mocha_test/services/pricer/set_accepted_margin.js index 8920822..b2b3c42 100644 --- a/mocha_test/services/pricer/set_accepted_margin.js +++ b/mocha_test/services/pricer/set_accepted_margin.js @@ -1,5 +1,6 @@ /* global describe, it */ +/*eslint-disable */ const chai = require('chai') , assert = chai.assert; @@ -9,9 +10,12 @@ const rootPrefix = "../../.." , pricerUtils = require('./pricer_utils') , pricerOstUsd = new pricer(constants.pricerOstUsdAddress) ; +/*eslint-enable */ describe('Set accepted margins', function() { + it('should pass the initial address checks', async function() { + // eslint-disable-next-line no-invalid-this this.timeout(100000); @@ -22,28 +26,96 @@ describe('Set accepted margins', function() { assert.notEqual(constants.deployer, constants.account1); assert.notEqual(constants.ops, constants.account1); - /* const response = await pricerOstUsd.setAcceptedMargin( + // set the accepted margin to 30 + const response = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 30, 0xBA43B7400, constants.chainId, - {returnType: 'txReceipt'}); + {returnType: constants.returnTypeReceipt}); + // verify if the transaction receipt is valid pricerUtils.verifyTransactionReceipt(response); + + // verify if the transaction has was actually mined await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); // verify if value is changed const amResult = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult.isSuccess(), true); assert.equal(30, amResult.data.acceptedMargins); - */ }); + it('should pass for interaction layer test when return type is uuid', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // set the accepted margin to 50 + const response = await pricerOstUsd.setAcceptedMargin( + constants.ops, + constants.opsPassphrase, + constants.currencyUSD, + 50, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeUUID}); + + // verify if the transaction receipt is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionUUID(response); + + }); + + it('should pass for interaction layer test when return type is txHash', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // set the accepted margin to 150 + const response = await pricerOstUsd.setAcceptedMargin( + constants.ops, + constants.opsPassphrase, + constants.currencyUSD, + 150, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeHash}); + + // verify if the transaction hash is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionHash(response); + + }); + + it('should pass for interaction layer test when return type is txReceipt', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // set the accepted margin to 70 + const response = await pricerOstUsd.setAcceptedMargin( + constants.ops, + constants.opsPassphrase, + constants.currencyUSD, + 70, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid. + // We will not check here if the value is really set as its just interaction layer testing. + pricerUtils.verifyTransactionReceipt(response); + + }); + + it('should fail when currency is blank', async function() { + // set the accepted margin to 3 const response = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, @@ -51,7 +123,9 @@ describe('Set accepted margins', function() { 3, 0xBA43B7400, constants.chainId, - {returnType: 'txReceipt'}); + {returnType: constants.returnTypeReceipt}); + + // verify if the response is failure assert.equal(response.isFailure(), true); }); @@ -59,6 +133,7 @@ describe('Set accepted margins', function() { it('should fail when gas amount is 0', async function() { + // set the accepted margin to 3 const response = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, @@ -66,13 +141,16 @@ describe('Set accepted margins', function() { 3, 0, constants.chainId, - {returnType: 'txReceipt'}); + {returnType: constants.returnTypeReceipt}); + + // verify if the response is failure assert.equal(response.isFailure(), true); }); it('should fail when sender address is 0', async function() { + // set the accepted margin to 3 const response = await pricerOstUsd.setAcceptedMargin( 0, constants.opsPassphrase, @@ -80,7 +158,9 @@ describe('Set accepted margins', function() { 3, 0xBA43B7400, constants.chainId, - {returnType: 'txReceipt'}); + {returnType: constants.returnTypeReceipt}); + + // verify if the response is failure assert.equal(response.isFailure(), true); }); @@ -88,6 +168,7 @@ describe('Set accepted margins', function() { it('should fail when accepted margin is negetive', async function() { + // set the accepted margin to -30 const response = await pricerOstUsd.setAcceptedMargin( 0, constants.opsPassphrase, @@ -95,7 +176,9 @@ describe('Set accepted margins', function() { -30, 0xBA43B7400, constants.chainId, - {returnType: 'txReceipt'}); + {returnType: constants.returnTypeReceipt}); + + // verify if the response is failure assert.equal(response.isFailure(), true); }); @@ -113,7 +196,7 @@ describe('Set accepted margins', function() { 3, 0xBA43B7400, constants.chainId, - {returnType: 'txReceipt'}); + {returnType: constants.returnTypeReceipt}); // verify if the transaction receipt is valid pricerUtils.verifyTransactionReceipt(response1); @@ -132,7 +215,9 @@ describe('Set accepted margins', function() { constants.deployerPassphrase, constants.currencyUSD, 8, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); // verify if the transaction receipt is valid pricerUtils.verifyTransactionReceipt(response2); @@ -147,49 +232,64 @@ describe('Set accepted margins', function() { }); -/* + it('should pass when margin is 0', async function() { + // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set the accepted margin to 0 const response = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 0, - 0xBA43B7400); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if the accepted margin is set to 0 const amResult = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult.isSuccess(), true); assert.equal(0, amResult.data.acceptedMargins); }); + it('should pass when margin is 50', async function() { + // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set the accepted margin to 50 const response = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 50, - 0xBA43B7400); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if the accepted margin is set to 50 const amResult = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult.isSuccess(), true); assert.equal(50, amResult.data.acceptedMargins); }); -*/ + }); diff --git a/mocha_test/services/pricer/set_price_oracle.js b/mocha_test/services/pricer/set_price_oracle.js index 7f20835..8980f2e 100644 --- a/mocha_test/services/pricer/set_price_oracle.js +++ b/mocha_test/services/pricer/set_price_oracle.js @@ -25,37 +25,49 @@ describe('Set price oracle', function() { assert.notEqual(constants.deployer, constants.account1); assert.notEqual(constants.ops, constants.account1); + // unset the price oracle const response = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if value is changed const poResult1 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult1.isSuccess(), true); assert.equal(0x0, poResult1.data.priceOracles); + // unset the price oracle const response2 = await pricerOstEur.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyEUR, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response2); - assert.equal(response2.isSuccess(), true); - assert.exists(response2.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstEur, response2.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response2.data.transaction_hash); + // verify if value is changed const poResult2 = await pricerOstEur.priceOracles(constants.currencyEUR); assert.equal(poResult2.isSuccess(), true); assert.equal(0x0, poResult2.data.priceOracles); }); - +/* it('should fail when sender is not ops', async function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); @@ -216,6 +228,6 @@ describe('Set price oracle', function() { assert.equal(poResult.data.priceOracles, constants.priceOracles.OST.EUR); }); - +*/ }); diff --git a/mocha_test/services/pricer/unset_price_oracle.js b/mocha_test/services/pricer/unset_price_oracle.js index a24e0eb..6527da1 100644 --- a/mocha_test/services/pricer/unset_price_oracle.js +++ b/mocha_test/services/pricer/unset_price_oracle.js @@ -26,31 +26,43 @@ describe('Unset price oracle', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set the price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + + // verify if value is changed const poResult1 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult1.isSuccess(), true); assert.equal(poResult1.data.priceOracles, constants.priceOracles.OST.USD); + // unset the price oracle const response2 = await pricerOstUsd.unsetPriceOracle( constants.deployer, constants.deployerPassphrase, constants.currencyUSD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response2); - assert.equal(response2.isSuccess(), true); - assert.exists(response2.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response2.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response2.data.transaction_hash); + // verify if value is changed const poResult2 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult2.isSuccess(), true); assert.equal(poResult2.data.priceOracles, constants.priceOracles.OST.USD); @@ -60,11 +72,16 @@ describe('Unset price oracle', function() { it('should fail when currency is blank', async function() { + // unset the price oracle const response = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyBlank, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if it failed assert.equal(response.isFailure(), true); }); @@ -72,22 +89,32 @@ describe('Unset price oracle', function() { it('should fail when gas amount is 0', async function() { + // unset the price oracle const response = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, - 0); + 0, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if it failed assert.equal(response.isFailure(), true); }); it('should fail when sender address is 0', async function() { + // unset the price oracle const response = await pricerOstUsd.unsetPriceOracle( 0, constants.opsPassphrase, constants.currencyUSD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if it failed assert.equal(response.isFailure(), true); }); @@ -108,37 +135,110 @@ describe('Unset price oracle', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set the price oracle const setResponse = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(setResponse.isSuccess(), true); - assert.exists(setResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, setResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(setResponse); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, setResponse.data.transaction_hash); + + // verify if value is changed const poResult1 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult1.isSuccess(), true); assert.equal(poResult1.data.priceOracles, constants.priceOracles.OST.USD); + // unset the price oracle const unsetResponse = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(unsetResponse); - assert.equal(unsetResponse.isSuccess(), true); - assert.exists(unsetResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, unsetResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, unsetResponse.data.transaction_hash); + // verify if value is changed const poResult2 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult2.isSuccess(), true); assert.equal(poResult2.data.priceOracles, 0x0); }); + + it('should pass for interaction layer test when return type is uuid', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // unset the price oracle + const unsetResponse = await pricerOstUsd.unsetPriceOracle( + constants.ops, + constants.opsPassphrase, + constants.currencyUSD, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeUUID}); + + // verify if the transaction receipt is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionUUID(unsetResponse); + + }); + + it('should pass for interaction layer test when return type is txHash', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // unset the price oracle + const unsetResponse = await pricerOstUsd.unsetPriceOracle( + constants.ops, + constants.opsPassphrase, + constants.currencyUSD, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeHash}); + + // verify if the transaction hash is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionHash(unsetResponse); + + }); + + it('should pass for interaction layer test when return type is txReceipt', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // unset the price oracle + const unsetResponse = await pricerOstUsd.unsetPriceOracle( + constants.ops, + constants.opsPassphrase, + constants.currencyUSD, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid. + // We will not check here if the value is really set as its just interaction layer testing. + pricerUtils.verifyTransactionReceipt(unsetResponse); + + }); + }); From 9416c189c86518e8b3a4cf985a925edcfbb6675e Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 00:21:29 +0530 Subject: [PATCH 03/10] Updated mocha test cases for setPriceOracle for event notification --- .../services/pricer/set_price_oracle.js | 166 +++++++++++++++--- 1 file changed, 140 insertions(+), 26 deletions(-) diff --git a/mocha_test/services/pricer/set_price_oracle.js b/mocha_test/services/pricer/set_price_oracle.js index 8980f2e..94592f8 100644 --- a/mocha_test/services/pricer/set_price_oracle.js +++ b/mocha_test/services/pricer/set_price_oracle.js @@ -67,22 +67,28 @@ describe('Set price oracle', function() { }); -/* + it('should fail when sender is not ops', async function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set price oracle const setResponse = await pricerOstUsd.setPriceOracle( constants.deployer, constants.deployerPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(setResponse.isSuccess(), true); - assert.exists(setResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, setResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(setResponse); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, setResponse.data.transaction_hash); + // verify if its not set const poResult2 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult2.isSuccess(), true); assert.equal(0x0, poResult2.data.priceOracles); @@ -92,12 +98,17 @@ describe('Set price oracle', function() { it('should fail when currency is blank', async function() { + // set price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyBlank, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // check if the response is failure assert.equal(response.isFailure(), true); }); @@ -105,12 +116,17 @@ describe('Set price oracle', function() { it('should fail when oracleAddress is 0', async function() { + // set price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, 0, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // check if the response is failure assert.equal(response.isFailure(), true); }); @@ -118,12 +134,17 @@ describe('Set price oracle', function() { it('should fail when gas amount is 0', async function() { + // set price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0); + 0, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // check if the response is failure assert.equal(response.isFailure(), true); }); @@ -131,12 +152,17 @@ describe('Set price oracle', function() { it('should fail when sender address is 0', async function() { + // set price oracle const response = await pricerOstUsd.setPriceOracle( 0, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // check if the response is failure assert.equal(response.isFailure(), true); }); @@ -146,17 +172,23 @@ describe('Set price oracle', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.ETH.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if its not set const poResult = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult.isSuccess(), true); assert.notEqual(poResult.data.priceOracles, constants.priceOracles.ETH.USD); @@ -168,17 +200,23 @@ describe('Set price oracle', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set price oracle const response = await pricerOstUsd10Decimal.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd10Decimal, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if its not set const poResult = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult.isSuccess(), true); assert.equal(poResult.data.priceOracles, 0x0); @@ -190,17 +228,23 @@ describe('Set price oracle', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + // verify if its set const poResult = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult.isSuccess(), true); assert.equal(poResult.data.priceOracles, constants.priceOracles.OST.USD); @@ -212,22 +256,92 @@ describe('Set price oracle', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set price oracle const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyEUR, constants.priceOracles.OST.EUR, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + + // verify if its set const poResult = await pricerOstUsd.priceOracles(constants.currencyEUR); assert.equal(poResult.isSuccess(), true); assert.equal(poResult.data.priceOracles, constants.priceOracles.OST.EUR); }); -*/ + + + it('should pass for interaction layer test when return type is uuid', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // set price oracle + const response = await pricerOstUsd.setPriceOracle( + constants.ops, + constants.opsPassphrase, + constants.currencyEUR, + constants.priceOracles.OST.EUR, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeUUID}); + + // verify if the transaction receipt is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionUUID(response); + + }); + + it('should pass for interaction layer test when return type is txHash', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // set price oracle + const response = await pricerOstUsd.setPriceOracle( + constants.ops, + constants.opsPassphrase, + constants.currencyEUR, + constants.priceOracles.OST.EUR, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeHash}); + + // verify if the transaction hash is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionHash(response); + + }); + + it('should pass for interaction layer test when return type is txReceipt', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + // set price oracle + const response = await pricerOstUsd.setPriceOracle( + constants.ops, + constants.opsPassphrase, + constants.currencyEUR, + constants.priceOracles.OST.EUR, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid. + // We will not check here if the value is really set as its just interaction layer testing. + pricerUtils.verifyTransactionReceipt(response); + + }); + }); From 5cf703ef8cd9bb740c2d94f73f88c7faf5f48262 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 00:28:32 +0530 Subject: [PATCH 04/10] Updated mocha test for getPricePoint for event notification --- mocha_test/services/pricer/get_price_point.js | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/mocha_test/services/pricer/get_price_point.js b/mocha_test/services/pricer/get_price_point.js index fe390ff..de96e6e 100644 --- a/mocha_test/services/pricer/get_price_point.js +++ b/mocha_test/services/pricer/get_price_point.js @@ -46,34 +46,43 @@ describe('Get price point', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); - // set price point 1 + // set price oracle 1 const response = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyEUR, constants.priceOracles.OST.EUR, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); + + // verify if its set const poResult = await pricerOstUsd.priceOracles(constants.currencyEUR); assert.equal(poResult.isSuccess(), true); assert.equal(poResult.data.priceOracles, constants.priceOracles.OST.EUR); - // set price point 2 + // set price oracle 2 const response2 = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response2); - assert.equal(response2.isSuccess(), true); - assert.exists(response2.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response2.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response2.data.transaction_hash); const poResult2 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult2.isSuccess(), true); @@ -94,15 +103,20 @@ describe('Get price point', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // unset price oracle const response = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response); - assert.equal(response.isSuccess(), true); - assert.exists(response.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response.data.transaction_hash); const poResult = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult.isSuccess(), true); From c54fc87fd02eba4970e4d08f6fb173bf51950dca Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 00:32:45 +0530 Subject: [PATCH 05/10] Updated mocha test for getPricePointAndCalculatedAmounts for event notifications --- .../get_pricepoint_and_calculated_amounts.js | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/mocha_test/services/pricer/get_pricepoint_and_calculated_amounts.js b/mocha_test/services/pricer/get_pricepoint_and_calculated_amounts.js index 83e41b6..e5f5874 100644 --- a/mocha_test/services/pricer/get_pricepoint_and_calculated_amounts.js +++ b/mocha_test/services/pricer/get_pricepoint_and_calculated_amounts.js @@ -26,32 +26,44 @@ describe('Get price point and calculated amounts', function() { assert.notEqual(constants.deployer, constants.account1); assert.notEqual(constants.ops, constants.account1); + // set price oracle const response1 = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(response1.isSuccess(), true); - assert.exists(response1.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, response1.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response1); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, response1.data.transaction_hash); + + // verify if its set const poResult1 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult1.isSuccess(), true); assert.equal(constants.priceOracles.OST.USD, poResult1.data.priceOracles); + // set price oracle const response2 = await pricerOstEur.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyEUR, constants.priceOracles.OST.EUR, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(response2); - assert.equal(response2.isSuccess(), true); - assert.exists(response2.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstEur, response2.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstEur, response1.data.transaction_hash); + // verify if its set const poResult2 = await pricerOstEur.priceOracles(constants.currencyEUR); assert.equal(poResult2.isSuccess(), true); assert.equal(constants.priceOracles.OST.EUR, poResult2.data.priceOracles); From 1713dc9d37a2d403b5f7bf95f418b85cf8e5e313 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 00:39:07 +0530 Subject: [PATCH 06/10] Updated mocha test for priceOracle for event notifications --- mocha_test/services/pricer/price_oracles.js | 54 +++++++++++++++------ 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/mocha_test/services/pricer/price_oracles.js b/mocha_test/services/pricer/price_oracles.js index 2783575..7998061 100644 --- a/mocha_test/services/pricer/price_oracles.js +++ b/mocha_test/services/pricer/price_oracles.js @@ -38,33 +38,44 @@ describe('Get price oracles', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // set price oracle const setResponse = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(setResponse.isSuccess(), true); - assert.exists(setResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, setResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(setResponse); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, setResponse.data.transaction_hash); + // set price oracle const setResponse1 = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyEUR, constants.priceOracles.OST.EUR, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(setResponse1.isSuccess(), true); - assert.exists(setResponse1.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, setResponse1.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(setResponse); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, setResponse.data.transaction_hash); + + // verify result const poResult1 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult1.isSuccess(), true); assert.equal(poResult1.data.priceOracles, constants.priceOracles.OST.USD); + // verify result const poResult2 = await pricerOstUsd.priceOracles(constants.currencyEUR); assert.equal(poResult2.isSuccess(), true); assert.equal(poResult2.data.priceOracles, constants.priceOracles.OST.EUR); @@ -75,31 +86,42 @@ describe('Get price oracles', function() { // eslint-disable-next-line no-invalid-this this.timeout(100000); + // unset price oracle const unsetResponse = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(unsetResponse.isSuccess(), true); - assert.exists(unsetResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, unsetResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(unsetResponse); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, unsetResponse.data.transaction_hash); + // unset price oracle const unsetResponse1 = await pricerOstUsd.unsetPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyEUR, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(unsetResponse1); - assert.equal(unsetResponse1.isSuccess(), true); - assert.exists(unsetResponse1.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, unsetResponse1.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, unsetResponse1.data.transaction_hash); + // verify result const poResult1 = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult1.isSuccess(), true); assert.equal(poResult1.data.priceOracles, 0x0); + // verify result const poResult2 = await pricerOstUsd.priceOracles(constants.currencyEUR); assert.equal(poResult2.isSuccess(), true); assert.equal(poResult2.data.priceOracles, 0x0); From 909853208381395199a2689b79d1698eadcf0162 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 01:09:48 +0530 Subject: [PATCH 07/10] Updated mocha test for pay for event notification --- lib/contract_interact/pricer.js | 165 +++++++--- mocha_test/services/pricer/event_listner.js | 8 +- mocha_test/services/pricer/pay.js | 314 ++++++++++++++++---- 3 files changed, 398 insertions(+), 89 deletions(-) diff --git a/lib/contract_interact/pricer.js b/lib/contract_interact/pricer.js index b32a7a8..5f8d9de 100644 --- a/lib/contract_interact/pricer.js +++ b/lib/contract_interact/pricer.js @@ -286,22 +286,6 @@ Pricer.prototype = { return asyncSetPriceOracle(); } - /* currContract.options.address = this.contractAddress; - currContract.setProvider( web3RpcProvider.currentProvider ); - const transactionObject = currContract.methods.setPriceOracle(web3RpcProvider.utils.asciiToHex(currency), address); - const encodedABI = transactionObject.encodeABI(); - return helper.sendTxAsyncFromAddr( - web3RpcProvider, - this.contractAddress, - encodedABI, - senderAddress, - senderPassphrase, - { gasPrice: gasPrice, gas: GAS_LIMIT } - ).then(function(transactionHash) { - return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); - }); - - */ }, @@ -448,22 +432,6 @@ Pricer.prototype = { return asyncUnsetPriceOracle(); } - /* - currContract.options.address = this.contractAddress; - currContract.setProvider( web3RpcProvider.currentProvider ); - const transactionObject = currContract.methods.unsetPriceOracle(web3RpcProvider.utils.asciiToHex(currency)); - const encodedABI = transactionObject.encodeABI(); - return helper.sendTxAsyncFromAddr( - web3RpcProvider, - this.contractAddress, - encodedABI, - senderAddress, - senderPassphrase, - { gasPrice: gasPrice, gas: GAS_LIMIT } - ).then(function(transactionHash) { - return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); - }); - */ }, @@ -643,7 +611,9 @@ Pricer.prototype = { commissionAmount, currency, intendedPricePoint, - gasPrice) { + gasPrice, + chainId, + options) { if (gasPrice === undefined || gasPrice === '' || gasPrice == 0 || gasPrice === null) { return responseHelper.error('l_ci_p_p_1', 'gas price is mandatory'); @@ -666,7 +636,132 @@ Pricer.prototype = { } } - const transactionObject = currContract.methods.pay( + /*eslint-disable */ + options = options || {}; + + const oThis = this + , txUUID = uuid.v4() + , tag = options.tag + , returnType = basicHelper.getReturnType(options.returnType) + , notificationData = { + topics: ['payment.pricer.pay'], + message: { + kind: '', // populate later: with every stage + payload: { + contract_name: contractName, + contract_address: oThis.contractAddress, + method: 'pay', + params: {args: [], txParams: {}}, // populate later: when Tx params created + transaction_hash: '', // populate later: when Tx submitted + chain_id: web3RpcProvider.chainId, + chain_kind: web3RpcProvider.chainKind, + uuid: txUUID, + tag: tag, + error_data: {} // populate later: when error received + } + } + } + /*eslint-enable */ + + + const asyncPay = function() { + + const transactionObject = currContract.methods.pay( + beneficiaryAddress, + transferAmount, + commissionBeneficiaryAddress, + commissionAmount, + web3RpcProvider.utils.asciiToHex(currency), + intendedPricePoint); + + const encodedABI = transactionObject.encodeABI(); + + //TODO: calculate the gas limit + const txParams = { + from: senderAddress, + to: oThis.contractAddress, + data: encodedABI, + gasPrice: gasPrice, + gas: GAS_LIMIT + }; + + // set params in notification data + notificationData.message.payload.params.txParams = txParams; + + return new Promise(function (onResolve, onReject) { + // Unlock account + web3RpcProvider.eth.personal.unlockAccount( + senderAddress, + senderPassphrase) + .then(function() { + web3RpcProvider.eth.sendTransaction(txParams) + .on('transactionHash', function(transactionHash) { + // set transaction hash in notification data + // eslint-disable-next-line camelcase + notificationData.message.payload.transaction_hash = transactionHash; + // Publish event + notificationData.message.kind = 'transaction_initiated'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxHash(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: transactionHash, + transaction_receipt: {} + })); + } + }) + .on('receipt', function(receipt) { + // Publish event + notificationData.message.kind = 'transaction_mined'; + openSTNotification.publishEvent.perform(notificationData); + + // send response + if (basicHelper.isReturnTypeTxReceipt(returnType)) { + return onResolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: receipt.transactionHash, + transaction_receipt: receipt + })); + } + }); + }) + .catch(function(reason) { + // set error data in notification data + notificationData.message.payload.error_data = reason; + + // Publish event + notificationData.message.kind = 'error'; + openSTNotification.publishEvent.perform(notificationData); + + return Promise.resolve(responseHelper.error('l_ci_p_p_4', 'pay transaction failed')); + }); + + }); + + }; + + // Perform pay transaction as requested + if (basicHelper.isReturnTypeUUID(returnType)) { + asyncPay(); + return Promise.resolve( + responseHelper.successWithData( + { + transaction_uuid: txUUID, + transaction_hash: "", + transaction_receipt: {} + })); + } + else { + return asyncPay(); + } + + /*const transactionObject = currContract.methods.pay( beneficiaryAddress, transferAmount, commissionBeneficiaryAddress, @@ -686,7 +781,7 @@ Pricer.prototype = { ).then(function(transactionHash) { return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); }); - + */ }, diff --git a/mocha_test/services/pricer/event_listner.js b/mocha_test/services/pricer/event_listner.js index b1f1cf1..ac68df0 100644 --- a/mocha_test/services/pricer/event_listner.js +++ b/mocha_test/services/pricer/event_listner.js @@ -5,7 +5,7 @@ var allEvents = {}; module.exports.verifyIfEventFired = function(uuid, kind) { - const key = `${uuid}_${kind}`; + const key = `${uuid}_${kind}`; return allEvents[key] !== undefined && allEvents[key] !== "undefined" && allEvents[key] !== null; }; @@ -15,13 +15,13 @@ module.exports.startObserving = function() { [ "payment.pricer.setAcceptedMargin", "payment.pricer.setPriceOracle", - "payment.pricer.unsetPriceOracle" + "payment.pricer.unsetPriceOracle", + "payment.pricer.pay" ], - function(msgContent) { + function(msgContent) { const messageData = JSON.parse(msgContent); const key = `${messageData.message.payload.uuid}_${messageData.message.kind}`; allEvents[key] = messageData.message; - console.log("Event received: "+ key); }); notificationRef = openSTNotification; } diff --git a/mocha_test/services/pricer/pay.js b/mocha_test/services/pricer/pay.js index d8c35df..3c101c6 100644 --- a/mocha_test/services/pricer/pay.js +++ b/mocha_test/services/pricer/pay.js @@ -27,32 +27,44 @@ describe('Pay', function() { assert.notEqual(constants.deployer, constants.account1); assert.notEqual(constants.ops, constants.account1); + // set accepted margin const amResponse = await pricerOstUsd.setAcceptedMargin( constants.ops, constants.opsPassphrase, constants.currencyUSD, 50, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(amResponse); - assert.equal(amResponse.isSuccess(), true); - assert.exists(amResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, amResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, amResponse.data.transaction_hash); + // verify if its set const amResult = await pricerOstUsd.acceptedMargins(constants.currencyUSD); assert.equal(amResult.isSuccess(), true); assert.equal(50, amResult.data.acceptedMargins); + // set price oracle const spoResponse = await pricerOstUsd.setPriceOracle( constants.ops, constants.opsPassphrase, constants.currencyUSD, constants.priceOracles.OST.USD, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(spoResponse.isSuccess(), true); - assert.exists(spoResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, spoResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(spoResponse); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, spoResponse.data.transaction_hash); + + // verify if its set const poResult = await pricerOstUsd.priceOracles(constants.currencyUSD); assert.equal(poResult.isSuccess(), true); assert.equal(constants.priceOracles.OST.USD, poResult.data.priceOracles); @@ -148,12 +160,15 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -225,11 +240,15 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -292,11 +311,15 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -358,7 +381,10 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + assert.equal(payResponse.isFailure(), true); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) @@ -421,11 +447,15 @@ describe('Pay', function() { commissionAmount, constants.currencyINR, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -487,7 +517,10 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + assert.equal(payResponse.isFailure(), true); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) @@ -551,11 +584,15 @@ describe('Pay', function() { commissionAmount, currency, changedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -621,11 +658,15 @@ describe('Pay', function() { commissionAmount, currency, changedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -687,11 +728,15 @@ describe('Pay', function() { commissionAmount, constants.currencyINR, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -751,11 +796,15 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -803,11 +852,15 @@ describe('Pay', function() { commissionAmount, currency, intendedPricePoint, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -865,11 +918,15 @@ describe('Pay', function() { commissionAmount, constants.currencyUSD, 0, - 0xBA43B7400); + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); - assert.equal(payResponse.isSuccess(), true); - assert.exists(payResponse.data.transactionHash); - await pricerUtils.verifyReceipt(pricerOstUsd, payResponse.data.transactionHash); + // verify if the transaction receipt is valid + pricerUtils.verifyTransactionReceipt(payResponse); + + // verify if the transaction has was actually mined + await pricerUtils.verifyIfMined(pricerOstUsd, payResponse.data.transaction_hash); const account1Balance = new BigNumber(await TC5.balanceOf(constants.account1)) , account3Balance = new BigNumber(await TC5.balanceOf(constants.account3)) @@ -881,6 +938,163 @@ describe('Pay', function() { }); + + it('should pass for interaction layer test when return type is uuid', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + const beneficiary = constants.account3 + , commissionAmount = new BigNumber(pricerOstUsd.toWei('10')) + , commissionBeneficiary = constants.account4 + , currency = constants.currencyUSD + , transferAmount = new BigNumber(pricerOstUsd.toWei('5')) + ; + + const acceptedMarginData = await pricerOstUsd.acceptedMargins(currency); + assert.equal(acceptedMarginData.isSuccess(), true); + + const estimatedValues = await pricerOstUsd.getPricePointAndCalculatedAmounts( + transferAmount, + commissionAmount, + currency); + assert.equal(estimatedValues.isSuccess(), true); + + const estimatedTokenAmount = new BigNumber(estimatedValues.data.tokenAmount); + const estimatedCommissionTokenAmount = new BigNumber(estimatedValues.data.commissionTokenAmount); + const estimatedMargin = new BigNumber(acceptedMarginData.data.acceptedMargins); + + const total = estimatedTokenAmount.plus(estimatedCommissionTokenAmount).plus(estimatedMargin); + + await TC5.approve( + constants.account1, + constants.accountPassphrase1, + constants.pricerOstUsdAddress, + total, + 0xBA43B7400); + + const payResponse = await pricerOstUsd.pay( + constants.account1, + constants.accountPassphrase1, + beneficiary, + transferAmount, + commissionBeneficiary, + commissionAmount, + constants.currencyUSD, + 0, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeUUID}); + + // verify if the transaction receipt is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionUUID(payResponse); + + }); + + it('should pass for interaction layer test when return type is txHash', async function() { + + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + + const beneficiary = constants.account3 + , commissionAmount = new BigNumber(pricerOstUsd.toWei('10')) + , commissionBeneficiary = constants.account4 + , currency = constants.currencyUSD + , transferAmount = new BigNumber(pricerOstUsd.toWei('5')) + ; + + const acceptedMarginData = await pricerOstUsd.acceptedMargins(currency); + assert.equal(acceptedMarginData.isSuccess(), true); + + const estimatedValues = await pricerOstUsd.getPricePointAndCalculatedAmounts( + transferAmount, + commissionAmount, + currency); + assert.equal(estimatedValues.isSuccess(), true); + + const estimatedTokenAmount = new BigNumber(estimatedValues.data.tokenAmount); + const estimatedCommissionTokenAmount = new BigNumber(estimatedValues.data.commissionTokenAmount); + const estimatedMargin = new BigNumber(acceptedMarginData.data.acceptedMargins); + + const total = estimatedTokenAmount.plus(estimatedCommissionTokenAmount).plus(estimatedMargin); + + await TC5.approve( + constants.account1, + constants.accountPassphrase1, + constants.pricerOstUsdAddress, + total, + 0xBA43B7400); + + const payResponse = await pricerOstUsd.pay( + constants.account1, + constants.accountPassphrase1, + beneficiary, + transferAmount, + commissionBeneficiary, + commissionAmount, + constants.currencyUSD, + 0, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeHash}); + + // verify if the transaction hash is valid + // we will not verify if it got mined as its just interaction layer testing + pricerUtils.verifyTransactionHash(payResponse); + + }); + + it('should pass for interaction layer test when return type is txReceipt', async function() { + + const beneficiary = constants.account3 + , commissionAmount = new BigNumber(pricerOstUsd.toWei('10')) + , commissionBeneficiary = constants.account4 + , currency = constants.currencyUSD + , transferAmount = new BigNumber(pricerOstUsd.toWei('5')) + ; + + const acceptedMarginData = await pricerOstUsd.acceptedMargins(currency); + assert.equal(acceptedMarginData.isSuccess(), true); + + const estimatedValues = await pricerOstUsd.getPricePointAndCalculatedAmounts( + transferAmount, + commissionAmount, + currency); + assert.equal(estimatedValues.isSuccess(), true); + + const estimatedTokenAmount = new BigNumber(estimatedValues.data.tokenAmount); + const estimatedCommissionTokenAmount = new BigNumber(estimatedValues.data.commissionTokenAmount); + const estimatedMargin = new BigNumber(acceptedMarginData.data.acceptedMargins); + + const total = estimatedTokenAmount.plus(estimatedCommissionTokenAmount).plus(estimatedMargin); + + await TC5.approve( + constants.account1, + constants.accountPassphrase1, + constants.pricerOstUsdAddress, + total, + 0xBA43B7400); + + const payResponse = await pricerOstUsd.pay( + constants.account1, + constants.accountPassphrase1, + beneficiary, + transferAmount, + commissionBeneficiary, + commissionAmount, + constants.currencyUSD, + 0, + 0xBA43B7400, + constants.chainId, + {returnType: constants.returnTypeReceipt}); + + // verify if the transaction receipt is valid. + // We will not check here if the value is really set as its just interaction layer testing. + pricerUtils.verifyTransactionReceipt(payResponse); + + }); + }); From a9c04f280ace9519f36ddb7caa098d9702a0168e Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 01:33:39 +0530 Subject: [PATCH 08/10] Return type made consistant to Promise.resolve for validations --- lib/contract_interact/pricer.js | 61 ++++++++------------- mocha_test/services/pricer/event_listner.js | 2 +- mocha_test/services/pricer/pay.js | 3 + 3 files changed, 28 insertions(+), 38 deletions(-) diff --git a/lib/contract_interact/pricer.js b/lib/contract_interact/pricer.js index 5f8d9de..af44592 100644 --- a/lib/contract_interact/pricer.js +++ b/lib/contract_interact/pricer.js @@ -60,7 +60,7 @@ Pricer.prototype = { */ acceptedMargins: async function (currency) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_am_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_am_1', 'currency is mandatory')); } const transactionObject = currContract.methods.acceptedMargins(web3RpcProvider.utils.asciiToHex(currency)); const encodedABI = transactionObject.encodeABI(); @@ -81,7 +81,7 @@ Pricer.prototype = { */ priceOracles: async function (currency) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_po_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_po_1', 'currency is mandatory')); } const transactionObject = currContract.methods.priceOracles(web3RpcProvider.utils.asciiToHex(currency)); const encodedABI = transactionObject.encodeABI(); @@ -144,6 +144,8 @@ Pricer.prototype = { * @param {string} currency - quote currency * @param {string} address - address of price pracle * @param {BigNumber} gasPrice - gas price + * @param {number} chainId - chain Id + * @param {options} object - for params like returnType, tag. * * @return {Promise} * @@ -151,16 +153,16 @@ Pricer.prototype = { setPriceOracle: function (senderAddress, senderPassphrase, currency, address, gasPrice, chainId, options) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_spo_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_spo_1', 'currency is mandatory')); } if (gasPrice === undefined || gasPrice === '' || gasPrice == 0 || gasPrice === null) { - return responseHelper.error('l_ci_p_spo_2', 'gas is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_spo_2', 'gas is mandatory')); } if (!helper.isAddressValid(address)) { - return responseHelper.error('l_ci_p_spo_3', 'address is invalid'); + return Promise.resolve(responseHelper.error('l_ci_p_spo_3', 'address is invalid')); } if (!helper.isAddressValid(senderAddress)) { - return responseHelper.error('l_ci_p_spo_4', 'address is invalid'); + return Promise.resolve(responseHelper.error('l_ci_p_spo_4', 'address is invalid')); } /*eslint-disable */ @@ -296,19 +298,21 @@ Pricer.prototype = { * @param {string} senderPassphrase - passphrase of sender * @param {string} currency - quote currency * @param {BigNumber} gasPrice - gas price + * @param {number} chainId - chain Id + * @param {options} object - for params like returnType, tag. * * @return {Promise} * */ unsetPriceOracle: function (senderAddress, senderPassphrase, currency, gasPrice, chainId, options) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_uspo_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_uspo_1', 'currency is mandatory')); } if (gasPrice === undefined || gasPrice === '' || gasPrice == 0 || gasPrice === null) { - return responseHelper.error('l_ci_p_uspo_2', 'gas is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_uspo_2', 'gas is mandatory')); } if (!helper.isAddressValid(senderAddress)) { - return responseHelper.error('l_ci_p_spo_3', 'address is invalid'); + return Promise.resolve(responseHelper.error('l_ci_p_spo_3', 'address is invalid')); } /*eslint-disable */ @@ -443,6 +447,8 @@ Pricer.prototype = { * @param {string} currency - quote currency * @param {BigNumber} acceptedMargin - accepted margin for the given currency (in wei) * @param {BigNumber} gasPrice - gas price + * @param {number} chainId - chain Id + * @param {options} object - for params like returnType, tag. * * @return {Promise} * @@ -598,6 +604,8 @@ Pricer.prototype = { * @param {string} currency - quote currency * @param {BigNumber} intendedPricePoint - price point at which the pay is intended (in wei) * @param {BigNumber} gasPrice - gas price + * @param {number} chainId - chain Id + * @param {options} object - for params like returnType, tag. * * @return {Promise} * @@ -616,23 +624,23 @@ Pricer.prototype = { options) { if (gasPrice === undefined || gasPrice === '' || gasPrice == 0 || gasPrice === null) { - return responseHelper.error('l_ci_p_p_1', 'gas price is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_p_1', 'gas price is mandatory')); } if (transferAmount < 0) { - return responseHelper.error('l_ci_p_p_2', 'transfer amount cannot be negetive'); + return Promise.resolve(responseHelper.error('l_ci_p_p_2', 'transfer amount cannot be negetive')); } if (commissionAmount < 0) { - return responseHelper.error('l_ci_p_p_3', 'Commission amount cannot be negetive'); + return Promise.resolve(responseHelper.error('l_ci_p_p_3', 'Commission amount cannot be negetive')); } helper.assertAddress(senderAddress); if (transferAmount > 0 || (beneficiaryAddress !== undefined && beneficiaryAddress !== '')) { if (!helper.isAddressValid(beneficiaryAddress)) { - return responseHelper.error('l_ci_p_p_3', 'address is invalid'); + return Promise.resolve(responseHelper.error('l_ci_p_p_3', 'address is invalid')); } } if (commissionAmount > 0 || (commissionBeneficiaryAddress !== undefined && commissionBeneficiaryAddress !== '')) { if (!helper.isAddressValid(commissionBeneficiaryAddress)) { - return responseHelper.error('l_ci_p_p_4', 'address is invalid'); + return Promise.resolve(responseHelper.error('l_ci_p_p_4', 'address is invalid')); } } @@ -761,27 +769,6 @@ Pricer.prototype = { return asyncPay(); } - /*const transactionObject = currContract.methods.pay( - beneficiaryAddress, - transferAmount, - commissionBeneficiaryAddress, - commissionAmount, - web3RpcProvider.utils.asciiToHex(currency), - intendedPricePoint); - - const encodedABI = transactionObject.encodeABI(); - - return helper.sendTxAsyncFromAddr( - web3RpcProvider, - this.contractAddress, - encodedABI, - senderAddress, - senderPassphrase, - { gasPrice: gasPrice, gas: GAS_LIMIT } - ).then(function(transactionHash) { - return Promise.resolve(responseHelper.successWithData({transactionHash: transactionHash})); - }); - */ }, @@ -796,7 +783,7 @@ Pricer.prototype = { getPricePoint: function (currency) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_gpp_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_gpp_1', 'currency is mandatory')); } const transactionObject = currContract.methods.getPricePoint(web3RpcProvider.utils.asciiToHex(currency)); const encodedABI = transactionObject.encodeABI(); @@ -832,7 +819,7 @@ Pricer.prototype = { currency) { if (currency === undefined || currency === '' || currency === null) { - return responseHelper.error('l_ci_p_gppaca_1', 'currency is mandatory'); + return Promise.resolve(responseHelper.error('l_ci_p_gppaca_1', 'currency is mandatory')); } const transactionObject = currContract.methods.getPricePointAndCalculatedAmounts(transferAmount, commissionAmount, web3RpcProvider.utils.asciiToHex(currency)); diff --git a/mocha_test/services/pricer/event_listner.js b/mocha_test/services/pricer/event_listner.js index ac68df0..fc09ad8 100644 --- a/mocha_test/services/pricer/event_listner.js +++ b/mocha_test/services/pricer/event_listner.js @@ -6,7 +6,7 @@ var allEvents = {}; module.exports.verifyIfEventFired = function(uuid, kind) { const key = `${uuid}_${kind}`; - return allEvents[key] !== undefined && allEvents[key] !== "undefined" && allEvents[key] !== null; + return allEvents[key] !== undefined && allEvents[key] !== "undefined" && allEvents[key] !== null && allEvents[key] !== ''; }; module.exports.startObserving = function() { diff --git a/mocha_test/services/pricer/pay.js b/mocha_test/services/pricer/pay.js index 3c101c6..71c9399 100644 --- a/mocha_test/services/pricer/pay.js +++ b/mocha_test/services/pricer/pay.js @@ -1047,6 +1047,9 @@ describe('Pay', function() { it('should pass for interaction layer test when return type is txReceipt', async function() { + // eslint-disable-next-line no-invalid-this + this.timeout(100000); + const beneficiary = constants.account3 , commissionAmount = new BigNumber(pricerOstUsd.toWei('10')) , commissionBeneficiary = constants.account4 From de056e122a6ea4cff9c0e68ee68797adff5164b1 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 01:37:16 +0530 Subject: [PATCH 09/10] Updated package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 5bbb226..fdde599 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,6 @@ "description": "", "dependencies": { "@openstfoundation/openst-notification": "1.0.0-beta.1", - "@ostdotcom/ost-price-oracle": "^1.0.0-beta.3", "bignumber": "^1.1.0", "bignumber.js": "^4.1.0", "moment": "^2.19.2", From 2f35ae069f21d74f38fcbf0e68ebc90183d16cd8 Mon Sep 17 00:00:00 2001 From: deepesh KN Date: Wed, 14 Feb 2018 15:03:18 +0530 Subject: [PATCH 10/10] PR review changes --- lib/contract_interact/helper.js | 2 -- lib/contract_interact/pricer.js | 8 ++++---- .../pricer/{event_listner.js => event_listener.js} | 0 mocha_test/services/pricer/pricer_utils.js | 2 +- 4 files changed, 5 insertions(+), 7 deletions(-) rename mocha_test/services/pricer/{event_listner.js => event_listener.js} (100%) diff --git a/lib/contract_interact/helper.js b/lib/contract_interact/helper.js index 573b5d4..28f903a 100644 --- a/lib/contract_interact/helper.js +++ b/lib/contract_interact/helper.js @@ -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(); } }; diff --git a/lib/contract_interact/pricer.js b/lib/contract_interact/pricer.js index af44592..78eaea1 100644 --- a/lib/contract_interact/pricer.js +++ b/lib/contract_interact/pricer.js @@ -145,7 +145,7 @@ Pricer.prototype = { * @param {string} address - address of price pracle * @param {BigNumber} gasPrice - gas price * @param {number} chainId - chain Id - * @param {options} object - for params like returnType, tag. + * @param {object} options - for params like returnType, tag. * * @return {Promise} * @@ -299,7 +299,7 @@ Pricer.prototype = { * @param {string} currency - quote currency * @param {BigNumber} gasPrice - gas price * @param {number} chainId - chain Id - * @param {options} object - for params like returnType, tag. + * @param {object} options - for params like returnType, tag. * * @return {Promise} * @@ -448,7 +448,7 @@ Pricer.prototype = { * @param {BigNumber} acceptedMargin - accepted margin for the given currency (in wei) * @param {BigNumber} gasPrice - gas price * @param {number} chainId - chain Id - * @param {options} object - for params like returnType, tag. + * @param {object} options - for params like returnType, tag. * * @return {Promise} * @@ -605,7 +605,7 @@ Pricer.prototype = { * @param {BigNumber} intendedPricePoint - price point at which the pay is intended (in wei) * @param {BigNumber} gasPrice - gas price * @param {number} chainId - chain Id - * @param {options} object - for params like returnType, tag. + * @param {object} options - for params like returnType, tag. * * @return {Promise} * diff --git a/mocha_test/services/pricer/event_listner.js b/mocha_test/services/pricer/event_listener.js similarity index 100% rename from mocha_test/services/pricer/event_listner.js rename to mocha_test/services/pricer/event_listener.js diff --git a/mocha_test/services/pricer/pricer_utils.js b/mocha_test/services/pricer/pricer_utils.js index 5c71280..372e1b5 100644 --- a/mocha_test/services/pricer/pricer_utils.js +++ b/mocha_test/services/pricer/pricer_utils.js @@ -2,7 +2,7 @@ const chai = require('chai') , assert = chai.assert ,rootPrefix = '../../..' - , eventListner = require('./event_listner') + , eventListner = require('./event_listener') , helper = require(rootPrefix + '/lib/contract_interact/helper') ; /*eslint-enable */