mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-22 18:00:18 +01:00
remove unused sufficient balance method (#11381)
this method relied on gasPrice being present and is safe to remove
This commit is contained in:
parent
ba3f51d6ed
commit
cb652c0fae
@ -1,4 +1,3 @@
|
|||||||
import { strict as assert } from 'assert';
|
|
||||||
import extension from 'extensionizer';
|
import extension from 'extensionizer';
|
||||||
import { stripHexPrefix } from 'ethereumjs-util';
|
import { stripHexPrefix } from 'ethereumjs-util';
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
@ -70,39 +69,6 @@ const getPlatform = (_) => {
|
|||||||
return PLATFORM_FIREFOX;
|
return PLATFORM_FIREFOX;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* 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',
|
|
||||||
);
|
|
||||||
assert.equal(
|
|
||||||
hexBalance.slice(0, 2),
|
|
||||||
'0x',
|
|
||||||
'sufficientBalance - hexBalance is not a hex string',
|
|
||||||
);
|
|
||||||
|
|
||||||
const balance = hexToBn(hexBalance);
|
|
||||||
const value = hexToBn(txParams.value);
|
|
||||||
const gasLimit = hexToBn(txParams.gas);
|
|
||||||
const gasPrice = hexToBn(txParams.gasPrice);
|
|
||||||
|
|
||||||
const maxCost = value.add(gasLimit.mul(gasPrice));
|
|
||||||
return balance.gte(maxCost);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts a hex string to a BN object
|
* Converts a hex string to a BN object
|
||||||
*
|
*
|
||||||
@ -183,7 +149,6 @@ function bnToHex(inputBn) {
|
|||||||
export {
|
export {
|
||||||
getPlatform,
|
getPlatform,
|
||||||
getEnvironmentType,
|
getEnvironmentType,
|
||||||
sufficientBalance,
|
|
||||||
hexToBn,
|
hexToBn,
|
||||||
BnMultiplyByFraction,
|
BnMultiplyByFraction,
|
||||||
checkForError,
|
checkForError,
|
||||||
|
@ -7,7 +7,7 @@ import {
|
|||||||
ENVIRONMENT_TYPE_FULLSCREEN,
|
ENVIRONMENT_TYPE_FULLSCREEN,
|
||||||
ENVIRONMENT_TYPE_BACKGROUND,
|
ENVIRONMENT_TYPE_BACKGROUND,
|
||||||
} from '../../../shared/constants/app';
|
} from '../../../shared/constants/app';
|
||||||
import { getEnvironmentType, sufficientBalance } from './util';
|
import { getEnvironmentType } from './util';
|
||||||
|
|
||||||
describe('app utils', function () {
|
describe('app utils', function () {
|
||||||
describe('getEnvironmentType', function () {
|
describe('getEnvironmentType', function () {
|
||||||
@ -68,44 +68,6 @@ describe('app utils', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('SufficientBalance', function () {
|
|
||||||
it('returns true if max tx cost is equal to balance.', function () {
|
|
||||||
const tx = {
|
|
||||||
value: '0x1',
|
|
||||||
gas: '0x2',
|
|
||||||
gasPrice: '0x3',
|
|
||||||
};
|
|
||||||
const balance = '0x8';
|
|
||||||
|
|
||||||
const result = sufficientBalance(tx, balance);
|
|
||||||
assert.ok(result, 'sufficient balance found.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns true if max tx cost is less than balance.', function () {
|
|
||||||
const tx = {
|
|
||||||
value: '0x1',
|
|
||||||
gas: '0x2',
|
|
||||||
gasPrice: '0x3',
|
|
||||||
};
|
|
||||||
const balance = '0x9';
|
|
||||||
|
|
||||||
const result = sufficientBalance(tx, balance);
|
|
||||||
assert.ok(result, 'sufficient balance found.');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('returns false if max tx cost is more than balance.', function () {
|
|
||||||
const tx = {
|
|
||||||
value: '0x1',
|
|
||||||
gas: '0x2',
|
|
||||||
gasPrice: '0x3',
|
|
||||||
};
|
|
||||||
const balance = '0x6';
|
|
||||||
|
|
||||||
const result = sufficientBalance(tx, balance);
|
|
||||||
assert.ok(!result, 'insufficient balance found.');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('isPrefixedFormattedHexString', function () {
|
describe('isPrefixedFormattedHexString', function () {
|
||||||
it('should return true for valid hex strings', function () {
|
it('should return true for valid hex strings', function () {
|
||||||
assert.equal(
|
assert.equal(
|
||||||
|
Loading…
Reference in New Issue
Block a user