From ffecba21f4e9dcda961a3e8432e70e6605174de5 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 12:43:08 -0700 Subject: ethDecimals -> ETH_DECIMALS --- packages/instant/src/util/format.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts index 8482b1526..2e0ccc03b 100644 --- a/packages/instant/src/util/format.ts +++ b/packages/instant/src/util/format.ts @@ -2,7 +2,7 @@ import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; -import { ethDecimals } from '../constants'; +import { ETH_DECIMALS } from '../constants'; export const format = { ethBaseAmount: ( @@ -13,7 +13,7 @@ export const format = { if (_.isUndefined(ethBaseAmount)) { return defaultText; } - const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ethDecimals); + const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ETH_DECIMALS); return format.ethUnitAmount(ethUnitAmount, decimalPlaces); }, ethUnitAmount: ( @@ -36,7 +36,7 @@ export const format = { if (_.isUndefined(ethBaseAmount) || _.isUndefined(ethUsdPrice)) { return defaultText; } - const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ethDecimals); + const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ETH_DECIMALS); return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces); }, ethUnitAmountInUsd: ( -- cgit From 9512978de9aee1953930d4f16a105ec5ef7f048e Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 12:43:44 -0700 Subject: feat(instant): Show message if user doesn't have enough ETH --- packages/instant/src/util/address.ts | 6 ++++++ packages/instant/src/util/error.ts | 5 ++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 packages/instant/src/util/address.ts (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/address.ts b/packages/instant/src/util/address.ts new file mode 100644 index 000000000..97ed30a00 --- /dev/null +++ b/packages/instant/src/util/address.ts @@ -0,0 +1,6 @@ +import { web3Wrapper } from '../util/web3_wrapper'; + +export const getBestAddress = async (): Promise => { + const addresses = await web3Wrapper.getAvailableAddressesAsync(); + return addresses[0]; +}; diff --git a/packages/instant/src/util/error.ts b/packages/instant/src/util/error.ts index 64c1f4885..5db0c66d2 100644 --- a/packages/instant/src/util/error.ts +++ b/packages/instant/src/util/error.ts @@ -2,7 +2,7 @@ import { AssetBuyerError } from '@0x/asset-buyer'; import { Dispatch } from 'redux'; import { Action, actions } from '../redux/actions'; -import { Asset } from '../types'; +import { Asset, ZeroExInstantError } from '../types'; import { assetUtils } from './asset'; @@ -49,6 +49,9 @@ const humanReadableMessageForError = (error: Error, asset?: Asset): string | und if (error.message === AssetBuyerError.SignatureRequestDenied) { return 'You denied this transaction'; } + if (error.message === ZeroExInstantError.InsufficientBalance) { + return "You don't have enough ETH"; + } return undefined; }; -- cgit From e1ae551560ad12501256fd5702dcd929baa84100 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 15:20:04 -0700 Subject: move funct into util --- packages/instant/src/util/balance.ts | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 packages/instant/src/util/balance.ts (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts new file mode 100644 index 000000000..d442da9e0 --- /dev/null +++ b/packages/instant/src/util/balance.ts @@ -0,0 +1,30 @@ +import { BuyQuote } from '@0x/asset-buyer'; +import { Web3Wrapper } from '@0x/web3-wrapper'; +import { Dispatch } from 'redux'; + +import { ZeroExInstantError } from '../types'; + +import { errorUtil } from './error'; + +export const balanceUtil = { + /** + * Checks to see if user has enough balance to buy assets + * If they do not, flash an error and return false + * If they do, return true + */ + checkSufficientBalanceAndFlashError: async ( + takerAddress: string, + buyQuote: BuyQuote, + web3Wrapper: Web3Wrapper, + dispatch: Dispatch, + ) => { + const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); + + if (balanceWei < buyQuote.worstCaseQuoteInfo.totalEthAmount) { + const balanceError = new Error(ZeroExInstantError.InsufficientBalance); + errorUtil.errorFlasher.flashNewError(dispatch, balanceError); + return false; + } + return true; + }, +}; -- cgit From 86febc3cce1135d2345f0048c205c3c524a66733 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 15:34:35 -0700 Subject: acccount for no address --- packages/instant/src/util/address.ts | 2 +- packages/instant/src/util/balance.ts | 26 +++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/address.ts b/packages/instant/src/util/address.ts index 97ed30a00..14d42d8c0 100644 --- a/packages/instant/src/util/address.ts +++ b/packages/instant/src/util/address.ts @@ -1,6 +1,6 @@ import { web3Wrapper } from '../util/web3_wrapper'; -export const getBestAddress = async (): Promise => { +export const getBestAddress = async (): Promise => { const addresses = await web3Wrapper.getAvailableAddressesAsync(); return addresses[0]; }; diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts index d442da9e0..954dc7156 100644 --- a/packages/instant/src/util/balance.ts +++ b/packages/instant/src/util/balance.ts @@ -1,30 +1,38 @@ import { BuyQuote } from '@0x/asset-buyer'; import { Web3Wrapper } from '@0x/web3-wrapper'; +import * as _ from 'lodash'; import { Dispatch } from 'redux'; import { ZeroExInstantError } from '../types'; import { errorUtil } from './error'; +const hasSufficientFunds = async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => { + if (_.isUndefined(takerAddress)) { + return false; + } + const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); + return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount; +}; + export const balanceUtil = { /** * Checks to see if user has enough balance to buy assets * If they do not, flash an error and return false * If they do, return true */ - checkSufficientBalanceAndFlashError: async ( - takerAddress: string, + checkInsufficientEthBalanceAndFlashError: async ( + takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper, dispatch: Dispatch, ) => { - const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); - - if (balanceWei < buyQuote.worstCaseQuoteInfo.totalEthAmount) { - const balanceError = new Error(ZeroExInstantError.InsufficientBalance); - errorUtil.errorFlasher.flashNewError(dispatch, balanceError); - return false; + const hasEnoughFunds = await hasSufficientFunds(takerAddress, buyQuote, web3Wrapper); + if (hasEnoughFunds) { + return true; } - return true; + const balanceError = new Error(ZeroExInstantError.InsufficientBalance); + errorUtil.errorFlasher.flashNewError(dispatch, balanceError); + return false; }, }; -- cgit From ff295daa5c56b5c056a5faa5ca8875c317524070 Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 19:53:15 -0700 Subject: Simpler way of validaitng has enough eth --- packages/instant/src/util/balance.ts | 40 +++++++++--------------------------- 1 file changed, 10 insertions(+), 30 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts index 954dc7156..9cb8b8987 100644 --- a/packages/instant/src/util/balance.ts +++ b/packages/instant/src/util/balance.ts @@ -1,38 +1,18 @@ import { BuyQuote } from '@0x/asset-buyer'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; -import { Dispatch } from 'redux'; - -import { ZeroExInstantError } from '../types'; - -import { errorUtil } from './error'; - -const hasSufficientFunds = async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => { - if (_.isUndefined(takerAddress)) { - return false; - } - const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); - return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount; -}; export const balanceUtil = { - /** - * Checks to see if user has enough balance to buy assets - * If they do not, flash an error and return false - * If they do, return true - */ - checkInsufficientEthBalanceAndFlashError: async ( - takerAddress: string | undefined, - buyQuote: BuyQuote, - web3Wrapper: Web3Wrapper, - dispatch: Dispatch, - ) => { - const hasEnoughFunds = await hasSufficientFunds(takerAddress, buyQuote, web3Wrapper); - if (hasEnoughFunds) { - return true; + hasSufficientFunds: async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => { + if (_.isUndefined(takerAddress)) { + return false; } - const balanceError = new Error(ZeroExInstantError.InsufficientBalance); - errorUtil.errorFlasher.flashNewError(dispatch, balanceError); - return false; + const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); + console.log('balanceWei', balanceWei.toString()); + console.log( + 'buyQuote.worstCaseQuoteInfo.totalEthAmount', + buyQuote.worstCaseQuoteInfo.totalEthAmount.toString(), + ); + return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount; }, }; -- cgit From 3052c8d303bdbaf57e762345d5ee8428d2194d0a Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Fri, 26 Oct 2018 19:55:42 -0700 Subject: linting --- packages/instant/src/util/balance.ts | 5 ----- 1 file changed, 5 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts index 9cb8b8987..24b7b99c3 100644 --- a/packages/instant/src/util/balance.ts +++ b/packages/instant/src/util/balance.ts @@ -8,11 +8,6 @@ export const balanceUtil = { return false; } const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress); - console.log('balanceWei', balanceWei.toString()); - console.log( - 'buyQuote.worstCaseQuoteInfo.totalEthAmount', - buyQuote.worstCaseQuoteInfo.totalEthAmount.toString(), - ); return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount; }, }; -- cgit From 7ed44f7b2f0224b4169f0a8f34bae09e8c6b986f Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Mon, 29 Oct 2018 09:07:40 -0700 Subject: Has Sufficient Funds/Balance -> Has Sufficient ETH --- packages/instant/src/util/balance.ts | 2 +- packages/instant/src/util/error.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'packages/instant/src/util') diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts index 24b7b99c3..533656858 100644 --- a/packages/instant/src/util/balance.ts +++ b/packages/instant/src/util/balance.ts @@ -3,7 +3,7 @@ import { Web3Wrapper } from '@0x/web3-wrapper'; import * as _ from 'lodash'; export const balanceUtil = { - hasSufficientFunds: async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => { + hasSufficentEth: async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => { if (_.isUndefined(takerAddress)) { return false; } diff --git a/packages/instant/src/util/error.ts b/packages/instant/src/util/error.ts index 5db0c66d2..39c563c75 100644 --- a/packages/instant/src/util/error.ts +++ b/packages/instant/src/util/error.ts @@ -49,7 +49,7 @@ const humanReadableMessageForError = (error: Error, asset?: Asset): string | und if (error.message === AssetBuyerError.SignatureRequestDenied) { return 'You denied this transaction'; } - if (error.message === ZeroExInstantError.InsufficientBalance) { + if (error.message === ZeroExInstantError.InsufficientETH) { return "You don't have enough ETH"; } -- cgit