Documentation pt2

This commit is contained in:
Richard Ramos 2018-08-27 16:16:00 -04:00
parent 00a203896f
commit a26b834dab
7 changed files with 81 additions and 27 deletions

View File

@ -275,7 +275,7 @@
"error", "error",
"never" "never"
], ],
"valid-jsdoc": "error", "valid-jsdoc": "off",
"vars-on-top": "off", "vars-on-top": "off",
"wrap-iife": "error", "wrap-iife": "error",
"wrap-regex": "error", "wrap-regex": "error",

View File

@ -1,5 +1,13 @@
/**
* Configuration Settings related to contracts
*/
class ContractSettings { class ContractSettings {
/**
* @param {object} config - Configuration object obtained from `./config/config.js`
* @param {object} web3 - Web3 object already configured
* @param {object} eventEmitter - Event Emitter
*/
constructor(config, web3, eventEmitter){ constructor(config, web3, eventEmitter){
this.tokens = config.tokens; this.tokens = config.tokens;
this.topics = []; this.topics = [];
@ -12,11 +20,17 @@ class ContractSettings {
this.pendingToLoad = 0; this.pendingToLoad = 0;
} }
/**
* Process configuration file
*/
process(){ process(){
this._setTokenPricePlugin(); this._setTokenPricePlugin();
this._processContracts(); this._processContracts();
} }
/**
* Set price plugin for token
*/
_setTokenPricePlugin(){ _setTokenPricePlugin(){
for(let token in this.tokens){ for(let token in this.tokens){
if(this.tokens[token].pricePlugin !== undefined){ if(this.tokens[token].pricePlugin !== undefined){
@ -26,16 +40,30 @@ class ContractSettings {
} }
} }
/**
* Get allowed tokens
* @return {object} - Dictionary with allowed tokens (address as key)
*/
getTokens(){ getTokens(){
return this.tokens; return this.tokens;
} }
/**
* Get token by address
* @param {string} - Token address
* @return {object} - Token details
*/
getToken(token){ getToken(token){
const tokenObj = this.tokens[token]; const tokenObj = this.tokens[token];
tokenObj.address = token; tokenObj.address = token;
return tokenObj; return tokenObj;
} }
/**
* Get token by symbol
* @param {string} - Token symbol
* @return {object} - Token details
*/
getTokenBySymbol(symbol){ getTokenBySymbol(symbol){
for(let token in this.tokens){ for(let token in this.tokens){
if(this.tokens[token].symbol == symbol){ if(this.tokens[token].symbol == symbol){
@ -46,14 +74,28 @@ class ContractSettings {
} }
} }
/**
* Get contract by topicName
* @param {string} topicName - Topic name that represents a contract
* @return {object} - Contract details
*/
getContractByTopic(topicName){ getContractByTopic(topicName){
return this.contracts[topicName]; return this.contracts[topicName];
} }
/**
* Calculate the topic based on the contract's name
* @param {string} contractName - Name of the contract as it appears in the configuration
* @return {string} - Topic
*/
getTopicName(contractName){ getTopicName(contractName){
return this.web3.utils.toHex(contractName).slice(0, 10); return this.web3.utils.toHex(contractName).slice(0, 10);
} }
/**
* Set contract's bytecode in the configuration
* @param {string} topicName - Topic name that represents a contract
*/
async _obtainContractBytecode(topicName){ async _obtainContractBytecode(topicName){
if(this.contracts[topicName].isIdentity) return; if(this.contracts[topicName].isIdentity) return;
@ -71,6 +113,10 @@ class ContractSettings {
} }
} }
/**
* Extract function details based on topicName
* @param {string} topicName - Topic name that represents a contract
*/
_extractFunctions(topicName){ _extractFunctions(topicName){
const contract = this.getContractByTopic(topicName); const contract = this.getContractByTopic(topicName);
@ -90,6 +136,9 @@ class ContractSettings {
this.contracts[topicName] = contract; this.contracts[topicName] = contract;
} }
/**
* Process contracts and setup the settings object
*/
_processContracts(){ _processContracts(){
for(let contractName in this.contracts){ for(let contractName in this.contracts){
// Obtaining the abis // Obtaining the abis
@ -114,6 +163,11 @@ class ContractSettings {
} }
} }
/**
* Create strategy object based on source code and topicName
* @param {string} strategyFile - Souce code path of strategy to build
* @param {string} topicName - Hex string that represents a contract's topic
*/
buildStrategy(strategyFile, topicName){ buildStrategy(strategyFile, topicName){
const strategy = require(strategyFile); const strategy = require(strategyFile);
return new strategy(this.web3, this.config, this, this.contracts[topicName]); return new strategy(this.web3, this.config, this, this.contracts[topicName]);

View File

@ -4,10 +4,10 @@
class MessageProcessor { class MessageProcessor {
/** /**
* @param {object} config Configuration object obtained from `./config/config.js` * @param {object} config - Configuration object obtained from `./config/config.js`
* @param {object} settings Settings obtained from parsing the configuration object * @param {object} settings - Settings obtained from parsing the configuration object
* @param {object} web3 Web3 object already configured * @param {object} web3 - Web3 object already configured
* @param {object} events Event emitter * @param {object} events - Event emitter
*/ */
constructor(config, settings, web3, events){ constructor(config, settings, web3, events){
this.config = config; this.config = config;
@ -18,8 +18,8 @@ class MessageProcessor {
/** /**
* Validate input message content * Validate input message content
* @param {object} contract Contract object obtained from the settings based on the message topic * @param {object} contract - Object obtained from the settings based on the message topic
* @param {object} input Input object obtained from a message. * @param {object} input - Object obtained from a message.
* @returns {object} State of validation * @returns {object} State of validation
*/ */
async _validateInput(contract, input){ async _validateInput(contract, input){
@ -54,10 +54,10 @@ class MessageProcessor {
/** /**
* Process strategy and return validation result * Process strategy and return validation result
* @param {object} contract Contract object obtained from the settings based on the message topic * @param {object} contract - Object obtained from the settings based on the message topic
* @param {object} input Input object obtained from a message. * @param {object} input - Object obtained from a message.
* @param {function} reply Reply function to return message * @param {function} reply - Function to reply a message
* @param {object} strategy Strategy to apply. If undefined, it will use a strategy based on the contract * @param {object} strategy - Strategy to apply. If undefined, it will use a strategy based on the contract
* @returns {object} State of validation * @returns {object} State of validation
*/ */
async processStrategy(contract, input, reply, strategy){ async processStrategy(contract, input, reply, strategy){
@ -87,9 +87,9 @@ class MessageProcessor {
/** /**
* Process strategy and based on its result, send a transaction to the blockchain * Process strategy and based on its result, send a transaction to the blockchain
* @param {object} contract Contract object obtained from the settings based on the message topic * @param {object} contract - Object obtained from the settings based on the message topic
* @param {object} input Input object obtained from a message. * @param {object} input - Object obtained from a message.
* @param {function} reply Reply function to return message * @param {function} reply - function to reply a message
* @returns {undefined} * @returns {undefined}
*/ */
async processTransaction(contract, input, reply){ async processTransaction(contract, input, reply){

View File

@ -8,7 +8,7 @@ class AvailabilityStrategy extends Strategy {
/** /**
* Process availability strategy * Process availability strategy
* @param {object} input Input object obtained from an 'availability' request. It expects an object with this structure `{contract, address, action, gasToken, gasPrice}` * @param {object} input - Object obtained from an 'availability' request. It expects an object with this structure `{contract, address, action, gasToken, gasPrice}`
* @returns {object} Status of validation, and minimum price * @returns {object} Status of validation, and minimum price
*/ */
execute(input){ execute(input){

View File

@ -8,10 +8,10 @@ const erc20ABI = require('../../abi/ERC20Token.json');
class BaseStrategy { class BaseStrategy {
/** /**
* @param {object} web3 Web3 object already configured * @param {object} web3 - Web3 object already configured
* @param {object} config Configuration object obtained from `./config/config.js` * @param {object} config - Configuration object obtained from `./config/config.js`
* @param {object} settings Settings obtained from parsing the configuration object * @param {object} settings - Settings obtained from parsing the configuration object
* @param {object} contract Contract object obtained from the settings based on the message topic * @param {object} contract - Object obtained from the settings based on the message topic
*/ */
constructor(web3, config, settings, contract){ constructor(web3, config, settings, contract){
this.web3 = web3; this.web3 = web3;
@ -22,8 +22,8 @@ class BaseStrategy {
/** /**
* Obtain the balance in tokens or ETH from an address * Obtain the balance in tokens or ETH from an address
* @param {string} address ETH address to obtain the balance from * @param {string} address - ETH address to obtain the balance from
* @param {object} token Token obtained from `settings.getToken(tokenSymbol)` * @param {object} token - Obtained from `settings.getToken(tokenSymbol)`
* @returns {web3.utils.BN} Balance * @returns {web3.utils.BN} Balance
*/ */
async getBalance(address, token){ async getBalance(address, token){
@ -39,7 +39,7 @@ class BaseStrategy {
/** /**
* Build Parameters Function * Build Parameters Function
* @param {object} input Input object obtained from an `transaction` request. * @param {object} input - Object obtained from an `transaction` request.
* @returns {function} Function that simplifies accessing contract functions' parameters * @returns {function} Function that simplifies accessing contract functions' parameters
*/ */
_obtainParametersFunc(input){ _obtainParametersFunc(input){
@ -51,7 +51,7 @@ class BaseStrategy {
/** /**
* Estimate gas using web3 * Estimate gas using web3
* @param {object} input Input object obtained from an `transaction` request. * @param {object} input - Object obtained from an `transaction` request.
* @returns {web3.utils.toBN} Estimated gas fees * @returns {web3.utils.toBN} Estimated gas fees
*/ */
async _estimateGas(input){ async _estimateGas(input){
@ -66,7 +66,7 @@ class BaseStrategy {
/** /**
* Simulate transaction using ganache. Useful for obtaining events * Simulate transaction using ganache. Useful for obtaining events
* @param {object} input Input object obtained from an `transaction` request. * @param {object} input - Object obtained from an `transaction` request.
* @returns {object} Simulated transaction receipt * @returns {object} Simulated transaction receipt
*/ */
async _simulateTransaction(input){ async _simulateTransaction(input){

View File

@ -9,7 +9,7 @@ class IdentityStrategy extends Strategy {
/** /**
* Validates if the contract being invoked represents an Identity instance * Validates if the contract being invoked represents an Identity instance
* @param {object} input Input object obtained from a `transaction` request. * @param {object} input - Object obtained from a `transaction` request.
* @returns {bool} Valid instance or not * @returns {bool} Valid instance or not
*/ */
async _validateInstance(input){ async _validateInstance(input){
@ -26,7 +26,7 @@ class IdentityStrategy extends Strategy {
/** /**
* Process Identity strategy * Process Identity strategy
* @param {object} input Input object obtained from an 'transaction' request. It expects an object with this structure `{contract, address, action, functionName, functionParameters, payload}` * @param {object} input - Object obtained from an 'transaction' request. It expects an object with this structure `{contract, address, action, functionName, functionParameters, payload}`
* @returns {object} Status of validation and estimated gas * @returns {object} Status of validation and estimated gas
*/ */
async execute(input){ async execute(input){

View File

@ -11,7 +11,7 @@ class SNTStrategy extends Strategy {
/** /**
* Process SNTController strategy * Process SNTController strategy
* @param {object} input Input object obtained from an 'transaction' request. It expects an object with this structure `{contract, address, action, functionName, functionParameters, payload}` * @param {object} input - Object obtained from an 'transaction' request. It expects an object with this structure `{contract, address, action, functionName, functionParameters, payload}`
* @returns {object} Status of validation and estimated gas * @returns {object} Status of validation and estimated gas
*/ */
async execute(input){ async execute(input){