diff options
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/address.ts | 4 | ||||
-rw-r--r-- | packages/instant/src/util/assert.ts | 2 | ||||
-rw-r--r-- | packages/instant/src/util/big_number_input.ts | 34 | ||||
-rw-r--r-- | packages/instant/src/util/injected_provider.ts | 16 | ||||
-rw-r--r-- | packages/instant/src/util/maybe_big_number.ts | 25 | ||||
-rw-r--r-- | packages/instant/src/util/provider.ts | 12 | ||||
-rw-r--r-- | packages/instant/src/util/web3_wrapper.ts | 5 |
7 files changed, 44 insertions, 54 deletions
diff --git a/packages/instant/src/util/address.ts b/packages/instant/src/util/address.ts index 14d42d8c0..b21863a8e 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'; +import { Web3Wrapper } from '@0x/web3-wrapper'; -export const getBestAddress = async (): Promise<string | undefined> => { +export const getBestAddress = async (web3Wrapper: Web3Wrapper): Promise<string | undefined> => { const addresses = await web3Wrapper.getAvailableAddressesAsync(); return addresses[0]; }; diff --git a/packages/instant/src/util/assert.ts b/packages/instant/src/util/assert.ts index d02f58625..971c1eb96 100644 --- a/packages/instant/src/util/assert.ts +++ b/packages/instant/src/util/assert.ts @@ -44,7 +44,7 @@ export const assert = { assert.isUri(`${variableName}.imageUrl`, metaData.imageUrl); } }, - isValidaffiliateInfo(variableName: string, affiliateInfo: AffiliateInfo): void { + isValidAffiliateInfo(variableName: string, affiliateInfo: AffiliateInfo): void { assert.isETHAddressHex(`${variableName}.recipientAddress`, affiliateInfo.feeRecipient); assert.isNumber(`${variableName}.percentage`, affiliateInfo.feePercentage); assert.assert( diff --git a/packages/instant/src/util/big_number_input.ts b/packages/instant/src/util/big_number_input.ts deleted file mode 100644 index 370d91a0a..000000000 --- a/packages/instant/src/util/big_number_input.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { BigNumber } from '@0x/utils'; -import * as _ from 'lodash'; - -/** - * A BigNumber extension that is more flexible about decimal strings. - * Such as allowing: - * new BigNumberInput('0.') => 0 - * new BigNumberInput('1.') => 1 - * new BigNumberInput('1..') => still throws - */ -export class BigNumberInput extends BigNumber { - private readonly _isEndingWithDecimal: boolean; - constructor(numberOrString: string | number) { - if (_.isString(numberOrString)) { - const hasDecimalPeriod = _.endsWith(numberOrString, '.'); - let internalString = numberOrString; - if (hasDecimalPeriod) { - internalString = numberOrString.slice(0, -1); - } - super(internalString); - this._isEndingWithDecimal = hasDecimalPeriod; - } else { - super(numberOrString); - this._isEndingWithDecimal = false; - } - } - public toDisplayString(): string { - const internalString = super.toString(); - if (this._isEndingWithDecimal) { - return `${internalString}.`; - } - return internalString; - } -} diff --git a/packages/instant/src/util/injected_provider.ts b/packages/instant/src/util/injected_provider.ts new file mode 100644 index 000000000..40f9e2da5 --- /dev/null +++ b/packages/instant/src/util/injected_provider.ts @@ -0,0 +1,16 @@ +import { Provider } from 'ethereum-types'; +import * as _ from 'lodash'; + +export const getInjectedProvider = (): Provider => { + const injectedProviderIfExists = (window as any).ethereum; + if (!_.isUndefined(injectedProviderIfExists)) { + // TODO: call enable here when implementing wallet connection flow + return injectedProviderIfExists; + } + const injectedWeb3IfExists = (window as any).web3; + if (!_.isUndefined(injectedWeb3IfExists.currentProvider)) { + return injectedWeb3IfExists.currentProvider; + } else { + throw new Error(`No injected web3 found`); + } +}; diff --git a/packages/instant/src/util/maybe_big_number.ts b/packages/instant/src/util/maybe_big_number.ts new file mode 100644 index 000000000..9d3746e10 --- /dev/null +++ b/packages/instant/src/util/maybe_big_number.ts @@ -0,0 +1,25 @@ +import { BigNumber } from '@0x/utils'; +import * as _ from 'lodash'; + +import { Maybe } from '../types'; + +export const maybeBigNumberUtil = { + // converts a string to a Maybe<BigNumber> + // if string is a NaN, considered undefined + stringToMaybeBigNumber: (stringValue: string): Maybe<BigNumber> => { + let validBigNumber: BigNumber; + try { + validBigNumber = new BigNumber(stringValue); + } catch { + return undefined; + } + + return validBigNumber.isNaN() ? undefined : validBigNumber; + }, + areMaybeBigNumbersEqual: (val1: Maybe<BigNumber>, val2: Maybe<BigNumber>): boolean => { + if (!_.isUndefined(val1) && !_.isUndefined(val2)) { + return val1.equals(val2); + } + return _.isUndefined(val1) && _.isUndefined(val2); + }, +}; diff --git a/packages/instant/src/util/provider.ts b/packages/instant/src/util/provider.ts deleted file mode 100644 index 49705fd11..000000000 --- a/packages/instant/src/util/provider.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { Provider } from 'ethereum-types'; - -export const getProvider = (): Provider => { - const injectedWeb3 = (window as any).web3 || undefined; - try { - // Use MetaMask/Mist provider - return injectedWeb3.currentProvider; - } catch (err) { - // Throws when user doesn't have MetaMask/Mist running - throw new Error(`No injected web3 found: ${err}`); - } -}; diff --git a/packages/instant/src/util/web3_wrapper.ts b/packages/instant/src/util/web3_wrapper.ts deleted file mode 100644 index 24dcd9076..000000000 --- a/packages/instant/src/util/web3_wrapper.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { Web3Wrapper } from '@0x/web3-wrapper'; - -import { getProvider } from './provider'; - -export const web3Wrapper = new Web3Wrapper(getProvider()); |