diff options
author | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2018-04-19 04:39:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-04-19 04:39:31 +0800 |
commit | 6742a5b2722da7af9320f46b18e9f4b59c5666ba (patch) | |
tree | a967fd6d6bdcc2f163cdf6639f5da915c3aba042 /app/scripts/lib | |
parent | 061975cd4a92dfcff7c98c2ab34290b8680c5545 (diff) | |
parent | 164f9c4662072dc0960ee5dc2c021545a7b14d8a (diff) | |
download | tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.tar tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.tar.gz tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.tar.bz2 tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.tar.lz tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.tar.xz tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.tar.zst tangerine-wallet-browser-6742a5b2722da7af9320f46b18e9f4b59c5666ba.zip |
Merge pull request #3987 from MetaMask/dm-docs-1
Documentation for various controllers and and lib utils
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/buy-eth-url.js | 11 | ||||
-rw-r--r-- | app/scripts/lib/get-first-preferred-lang-code.js | 7 | ||||
-rw-r--r-- | app/scripts/lib/nodeify.js | 8 | ||||
-rw-r--r-- | app/scripts/lib/util.js | 49 |
4 files changed, 75 insertions, 0 deletions
diff --git a/app/scripts/lib/buy-eth-url.js b/app/scripts/lib/buy-eth-url.js index b9dde3c28..4e2d0bc79 100644 --- a/app/scripts/lib/buy-eth-url.js +++ b/app/scripts/lib/buy-eth-url.js @@ -1,5 +1,16 @@ module.exports = getBuyEthUrl +/** + * Gives the caller a url at which the user can acquire eth, depending on the network they are in + * + * @param {object} opts Options required to determine the correct url + * @param {string} opts.network The network for which to return a url + * @param {string} opts.amount The amount of ETH to buy on coinbase. Only relevant if network === '1'. + * @param {string} opts.address The address the bought ETH should be sent to. Only relevant if network === '1'. + * @returns {string|undefined} The url at which the user can access ETH, while in the given network. If the passed + * network does not match any of the specified cases, or if no network is given, returns undefined. + * + */ function getBuyEthUrl ({ network, amount, address }) { let url switch (network) { diff --git a/app/scripts/lib/get-first-preferred-lang-code.js b/app/scripts/lib/get-first-preferred-lang-code.js index e3635434e..5473fccf0 100644 --- a/app/scripts/lib/get-first-preferred-lang-code.js +++ b/app/scripts/lib/get-first-preferred-lang-code.js @@ -4,6 +4,13 @@ const allLocales = require('../../_locales/index.json') const existingLocaleCodes = allLocales.map(locale => locale.code.toLowerCase().replace('_', '-')) +/** + * Returns a preferred language code, based on settings within the user's browser. If we have no translations for the + * users preferred locales, 'en' is returned. + * + * @returns {Promise<string>} Promises a locale code, either one from the user's preferred list that we have a translation for, or 'en' + * + */ async function getFirstPreferredLangCode () { const userPreferredLocaleCodes = await promisify( extension.i18n.getAcceptLanguages, diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js index 9b595d93c..25be6537b 100644 --- a/app/scripts/lib/nodeify.js +++ b/app/scripts/lib/nodeify.js @@ -1,6 +1,14 @@ const promiseToCallback = require('promise-to-callback') const noop = function () {} +/** + * A generator that returns a function which, when passed a promise, can treat that promise as a node style callback. + * The prime advantage being that callbacks are better for error handling. + * + * @param {Function} fn The function to handle as a callback + * @param {Object} context The context in which the fn is to be called, most often a this reference + * + */ module.exports = function nodeify (fn, context) { return function () { const args = [].slice.call(arguments) diff --git a/app/scripts/lib/util.js b/app/scripts/lib/util.js index df815906f..431d1e59c 100644 --- a/app/scripts/lib/util.js +++ b/app/scripts/lib/util.js @@ -7,11 +7,26 @@ const { ENVIRONMENT_TYPE_FULLSCREEN, } = require('./enums') +/** + * Generates an example stack trace + * + * @returns {string} A stack trace + * + */ function getStack () { const stack = new Error('Stack trace generator - not an error').stack return stack } +/** + * Used to determine the window type through which the app is being viewed. + * - 'popup' refers to the extension opened through the browser app icon (in top right corner in chrome and firefox) + * - 'responsive' refers to the main browser window + * - 'notification' refers to the popup that appears in its own window when taking action outside of metamask + * + * @returns {string} A single word label that represents the type of window through which the app is being viewed + * + */ const getEnvironmentType = (url = window.location.href) => { if (url.match(/popup.html(?:\?.+)*$/)) { return ENVIRONMENT_TYPE_POPUP @@ -22,6 +37,17 @@ const getEnvironmentType = (url = window.location.href) => { } } +/** + * Checks whether a given balance of ETH, represented as a hex string, is sufficient to pay a value plus a gas fee + * + * @param {object} txParams Contains data about a transaction + * @param {string} txParams.gas The gas for a transaction + * @param {string} txParams.gasPrice The price per gas for the transaction + * @param {string} txParams.value The value of ETH to send + * @param {string} hexBalance A balance of ETH represented as a hex string + * @returns {boolean} Whether the balance is greater than or equal to the value plus the value of gas times gasPrice + * + */ function sufficientBalance (txParams, hexBalance) { // validate hexBalance is a hex string assert.equal(typeof hexBalance, 'string', 'sufficientBalance - hexBalance is not a hex string') @@ -36,14 +62,37 @@ function sufficientBalance (txParams, hexBalance) { return balance.gte(maxCost) } +/** + * Converts a BN object to a hex string with a '0x' prefix + * + * @param {BN} inputBn The BN to convert to a hex string + * @returns {string} A '0x' prefixed hex string + * + */ function bnToHex (inputBn) { return ethUtil.addHexPrefix(inputBn.toString(16)) } +/** + * Converts a hex string to a BN object + * + * @param {string} inputHex A number represented as a hex string + * @returns {Object} A BN object + * + */ function hexToBn (inputHex) { return new BN(ethUtil.stripHexPrefix(inputHex), 16) } +/** + * Used to multiply a BN by a fraction + * + * @param {BN} targetBN The number to multiply by a fraction + * @param {number|string} numerator The numerator of the fraction multiplier + * @param {number|string} denominator The denominator of the fraction multiplier + * @returns {BN} The product of the multiplication + * + */ function BnMultiplyByFraction (targetBN, numerator, denominator) { const numBN = new BN(numerator) const denomBN = new BN(denominator) |