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/util.js | |
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/util.js')
-rw-r--r-- | app/scripts/lib/util.js | 49 |
1 files changed, 49 insertions, 0 deletions
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) |