aboutsummaryrefslogtreecommitdiffstats
path: root/packages/instant/src/util
diff options
context:
space:
mode:
authorSteve Klebanoff <steve@0xproject.com>2018-10-30 01:12:44 +0800
committerGitHub <noreply@github.com>2018-10-30 01:12:44 +0800
commit4e4291eccdd6c837bbec70603aa6eb64d3aa8d85 (patch)
treeedf8006576143d5cd1c32b052164ac898c57c8b2 /packages/instant/src/util
parentae6202ed3d777605a3fd02cd29141a3ba40f4b34 (diff)
parent7ed44f7b2f0224b4169f0a8f34bae09e8c6b986f (diff)
downloaddexon-0x-contracts-4e4291eccdd6c837bbec70603aa6eb64d3aa8d85.tar.gz
dexon-0x-contracts-4e4291eccdd6c837bbec70603aa6eb64d3aa8d85.tar.zst
dexon-0x-contracts-4e4291eccdd6c837bbec70603aa6eb64d3aa8d85.zip
Merge pull request #1193 from 0xProject/feature/not-enough-eth
[instant] Not enough ETH error message
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r--packages/instant/src/util/address.ts6
-rw-r--r--packages/instant/src/util/balance.ts13
-rw-r--r--packages/instant/src/util/error.ts5
-rw-r--r--packages/instant/src/util/format.ts6
4 files changed, 26 insertions, 4 deletions
diff --git a/packages/instant/src/util/address.ts b/packages/instant/src/util/address.ts
new file mode 100644
index 000000000..14d42d8c0
--- /dev/null
+++ b/packages/instant/src/util/address.ts
@@ -0,0 +1,6 @@
+import { web3Wrapper } from '../util/web3_wrapper';
+
+export const getBestAddress = async (): Promise<string | undefined> => {
+ const addresses = await web3Wrapper.getAvailableAddressesAsync();
+ return addresses[0];
+};
diff --git a/packages/instant/src/util/balance.ts b/packages/instant/src/util/balance.ts
new file mode 100644
index 000000000..533656858
--- /dev/null
+++ b/packages/instant/src/util/balance.ts
@@ -0,0 +1,13 @@
+import { BuyQuote } from '@0x/asset-buyer';
+import { Web3Wrapper } from '@0x/web3-wrapper';
+import * as _ from 'lodash';
+
+export const balanceUtil = {
+ hasSufficentEth: async (takerAddress: string | undefined, buyQuote: BuyQuote, web3Wrapper: Web3Wrapper) => {
+ if (_.isUndefined(takerAddress)) {
+ return false;
+ }
+ const balanceWei = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
+ return balanceWei >= buyQuote.worstCaseQuoteInfo.totalEthAmount;
+ },
+};
diff --git a/packages/instant/src/util/error.ts b/packages/instant/src/util/error.ts
index 64c1f4885..39c563c75 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.InsufficientETH) {
+ return "You don't have enough ETH";
+ }
return undefined;
};
diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts
index ca7c01359..4a48dec9d 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: (