Skip to content

Commit

Permalink
Tidy up utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jinchung committed Apr 9, 2020
1 parent 413852b commit 2bf5fe3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 63 deletions.
32 changes: 12 additions & 20 deletions src/helpers/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const convertNumberToString = value => BigNumber(`${value}`).toFixed();
* @return {String}
*/
export const greaterThan = (numberOne, numberTwo) =>
BigNumber(`${numberOne}`).gt(BigNumber(`${numberTwo}`));
BigNumber(`${numberOne}`).gt(numberTwo);

/**
* @desc compares if numberOne is greater than or equal to numberTwo
Expand All @@ -57,7 +57,16 @@ export const greaterThan = (numberOne, numberTwo) =>
* @return {String}
*/
export const greaterThanOrEqualTo = (numberOne, numberTwo) =>
BigNumber(`${numberOne}`).gte(BigNumber(`${numberTwo}`));
BigNumber(`${numberOne}`).gte(numberTwo);

/**
* @desc compares if numberOne is equal to numberTwo
* @param {Number} numberOne
* @param {Number} numberTwo
* @return {String}
*/
export const isEqual = (numberOne, numberTwo) =>
BigNumber(`${numberOne}`).eq(numberTwo);

/**
* @desc format fixed number of decimals
Expand Down Expand Up @@ -97,23 +106,6 @@ export const feeCalculation = (amount, percentFee, fixedFee) =>
.plus(fixedFee)
.toFixed(2, BigNumber.ROUND_HALF_UP);

/**
* @desc compares if numberOne is greater than or equal to numberTwo
* @param {Number} numberOne
* @param {Number} numberTwo
* @return {String}
*/
export const greaterThanOrEqual = (numberOne, numberTwo) =>
BigNumber(`${numberOne}`).comparedTo(BigNumber(`${numberTwo}`)) >= 0;

/**
* @desc compares if numberOne is equal to numberTwo
* @param {Number} numberOne
* @param {Number} numberTwo
* @return {String}
*/
export const isEqual = (numberOne, numberTwo) =>
BigNumber(`${numberOne}`).comparedTo(BigNumber(`${numberTwo}`)) === 0;
/**
* @desc real floor divides two numbers
* @param {Number} numberOne
Expand Down Expand Up @@ -256,7 +248,7 @@ export const convertStringToNumber = value => BigNumber(`${value}`).toNumber();
* @return {String}
*/
export const smallerThan = (numberOne, numberTwo) =>
BigNumber(`${numberOne}`).comparedTo(BigNumber(`${numberTwo}`)) === -1;
BigNumber(`${numberOne}`).lt(numberTwo);

/**
* @desc handle signficant decimals
Expand Down
71 changes: 28 additions & 43 deletions src/utils/ethereumUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,61 +18,46 @@ const getEthPriceUnit = assets => {
return get(ethAsset, 'price.value', 0);
};

const getOnChainBalance = async (selected, accountAddress) => {
try {
let onChainBalance = 0;
if (selected.address === 'eth') {
onChainBalance = await web3Provider.getBalance(accountAddress);
} else {
const tokenContract = new ethers.Contract(
selected.address,
erc20ABI,
web3Provider
);
onChainBalance = await tokenContract.balanceOf(accountAddress);
}
return convertRawAmountToDecimalFormat(onChainBalance, selected.decimals);
} catch (e) {
// Default to current balance
// if something goes wrong
captureException(e);
return get(selected, 'balance.amount', 0);
}
};

const getBalanceAmount = async (
selectedGasPrice,
selected,
onchain = false,
accountAddress = null
) => {
let amount = '';
if (onchain) {
amount = getOnChainBalance(selected, accountAddress);
} else {
amount = get(selected, 'balance.amount', 0);
}
if (selected.address === 'eth') {
let balanceAmount;
if (onchain) {
try {
const onchainBalance = await web3Provider.getBalance(accountAddress);
balanceAmount = convertRawAmountToDecimalFormat(
onchainBalance,
selected.decimals
);
} catch (e) {
// Default to current balance
// if something goes wrong
captureException(e);
balanceAmount = get(selected, 'balance.amount', 0);
}
} else {
balanceAmount = get(selected, 'balance.amount', 0);
}

if (!isEmpty(selectedGasPrice)) {
const txFeeRaw = get(selectedGasPrice, 'txFee.value.amount');
const txFeeAmount = fromWei(txFeeRaw);
const remaining = subtract(balanceAmount, txFeeAmount);
const remaining = subtract(amount, txFeeAmount);
amount = convertNumberToString(greaterThan(remaining, 0) ? remaining : 0);
} else {
amount = balanceAmount;
}
} else {
if (onchain) {
try {
const tokenContract = new ethers.Contract(
selected.address,
erc20ABI,
web3Provider
);
const onchainAmount = await tokenContract.balanceOf(accountAddress);
amount = convertRawAmountToDecimalFormat(
onchainAmount,
selected.decimals
);
} catch (e) {
// Default to current balance
// if something goes wrong
captureException(e);
amount = get(selected, 'balance.amount', 0);
}
} else {
amount = get(selected, 'balance.amount', 0);
}
}
return amount;
Expand Down

0 comments on commit 2bf5fe3

Please sign in to comment.