diff options
author | Brandon Millman <brandon.millman@gmail.com> | 2018-10-18 01:30:29 +0800 |
---|---|---|
committer | Brandon Millman <brandon.millman@gmail.com> | 2018-10-18 01:30:29 +0800 |
commit | 84057934c6cda3fe3e4f86cf686b163db017cf9e (patch) | |
tree | 0160c132e134fb04106f232834f0ed021d06ebd5 /packages/instant/src/util | |
parent | aa1085c8f3866da4965c0102be27c0f5e19b3db6 (diff) | |
parent | c767404ad09ca9fdacf9ab43c0e6b13fc70bffb9 (diff) | |
download | dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.tar dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.tar.gz dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.tar.bz2 dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.tar.lz dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.tar.xz dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.tar.zst dexon-sol-tools-84057934c6cda3fe3e4f86cf686b163db017cf9e.zip |
Merge branch 'development' into feature/website/asset-buyer-docs
* development: (31 commits)
Update CODEOWNERS
Update CODEOWNERS
Update CODEOWNERS
Add leo to CODEOWNERS on some packages
fix(monorepo-scripts): Format date as UTC not local time.
Bump max bundle size for instant
fix: dont use enum string as type as typedoc gets confused
feat: export AssetData from order-utils
feat: export AssetData from utils
chore: temporarily increase the bundle size for instant
Remove order-utils from dependencies
Run tests on circle CI
Add tests for format and use toFixed instead of round for usd
Remove expiry buffer seconds option from AssetBuyer init
Add ts-optchain and use it instead of lodash get
Hide USD price when ETH-USD price is not available
Rename OrderDetailsRow to EthAmountRow
fix: add Steve's github account to about page, and capitalize AppFolio correctly
Put boundNoop in a util file
Add tnxHash to buy button callbacks
...
Diffstat (limited to 'packages/instant/src/util')
-rw-r--r-- | packages/instant/src/util/asset_buyer.ts | 9 | ||||
-rw-r--r-- | packages/instant/src/util/coinbase_api.ts | 10 | ||||
-rw-r--r-- | packages/instant/src/util/format.ts | 45 | ||||
-rw-r--r-- | packages/instant/src/util/provider.ts | 12 | ||||
-rw-r--r-- | packages/instant/src/util/util.ts | 5 | ||||
-rw-r--r-- | packages/instant/src/util/web3_wrapper.ts | 5 |
6 files changed, 86 insertions, 0 deletions
diff --git a/packages/instant/src/util/asset_buyer.ts b/packages/instant/src/util/asset_buyer.ts new file mode 100644 index 000000000..27d66d600 --- /dev/null +++ b/packages/instant/src/util/asset_buyer.ts @@ -0,0 +1,9 @@ +import { AssetBuyer } from '@0xproject/asset-buyer'; + +import { sraApiUrl } from '../constants'; + +import { getProvider } from './provider'; + +const provider = getProvider(); + +export const assetBuyer = AssetBuyer.getAssetBuyerForStandardRelayerAPIUrl(provider, sraApiUrl); diff --git a/packages/instant/src/util/coinbase_api.ts b/packages/instant/src/util/coinbase_api.ts new file mode 100644 index 000000000..94a5d3c80 --- /dev/null +++ b/packages/instant/src/util/coinbase_api.ts @@ -0,0 +1,10 @@ +import { BigNumber } from '@0xproject/utils'; + +const baseEndpoint = 'https://api.coinbase.com/v2'; +export const coinbaseApi = { + getEthUsdPrice: async (): Promise<BigNumber> => { + const res = await fetch(`${baseEndpoint}/prices/ETH-USD/buy`); + const resJson = await res.json(); + return new BigNumber(resJson.data.amount); + }, +}; diff --git a/packages/instant/src/util/format.ts b/packages/instant/src/util/format.ts new file mode 100644 index 000000000..b62c968fb --- /dev/null +++ b/packages/instant/src/util/format.ts @@ -0,0 +1,45 @@ +import { BigNumber } from '@0xproject/utils'; +import { Web3Wrapper } from '@0xproject/web3-wrapper'; +import * as _ from 'lodash'; + +import { ethDecimals } from '../constants'; + +export const format = { + ethBaseAmount: (ethBaseAmount?: BigNumber, decimalPlaces: number = 4, defaultText: string = '0 ETH'): string => { + if (_.isUndefined(ethBaseAmount)) { + return defaultText; + } + const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ethDecimals); + return format.ethUnitAmount(ethUnitAmount, decimalPlaces); + }, + ethUnitAmount: (ethUnitAmount?: BigNumber, decimalPlaces: number = 4, defaultText: string = '0 ETH'): string => { + if (_.isUndefined(ethUnitAmount)) { + return defaultText; + } + const roundedAmount = ethUnitAmount.round(decimalPlaces); + return `${roundedAmount} ETH`; + }, + ethBaseAmountInUsd: ( + ethBaseAmount?: BigNumber, + ethUsdPrice?: BigNumber, + decimalPlaces: number = 2, + defaultText: string = '$0.00', + ): string => { + if (_.isUndefined(ethBaseAmount) || _.isUndefined(ethUsdPrice)) { + return defaultText; + } + const ethUnitAmount = Web3Wrapper.toUnitAmount(ethBaseAmount, ethDecimals); + return format.ethUnitAmountInUsd(ethUnitAmount, ethUsdPrice, decimalPlaces); + }, + ethUnitAmountInUsd: ( + ethUnitAmount?: BigNumber, + ethUsdPrice?: BigNumber, + decimalPlaces: number = 2, + defaultText: string = '$0.00', + ): string => { + if (_.isUndefined(ethUnitAmount) || _.isUndefined(ethUsdPrice)) { + return defaultText; + } + return `$${ethUnitAmount.mul(ethUsdPrice).toFixed(decimalPlaces)}`; + }, +}; diff --git a/packages/instant/src/util/provider.ts b/packages/instant/src/util/provider.ts new file mode 100644 index 000000000..49705fd11 --- /dev/null +++ b/packages/instant/src/util/provider.ts @@ -0,0 +1,12 @@ +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/util.ts b/packages/instant/src/util/util.ts new file mode 100644 index 000000000..232a86850 --- /dev/null +++ b/packages/instant/src/util/util.ts @@ -0,0 +1,5 @@ +import * as _ from 'lodash'; + +export const util = { + boundNoop: _.noop.bind(_), +}; diff --git a/packages/instant/src/util/web3_wrapper.ts b/packages/instant/src/util/web3_wrapper.ts new file mode 100644 index 000000000..d7e43521f --- /dev/null +++ b/packages/instant/src/util/web3_wrapper.ts @@ -0,0 +1,5 @@ +import { Web3Wrapper } from '@0xproject/web3-wrapper'; + +import { getProvider } from './provider'; + +export const web3Wrapper = new Web3Wrapper(getProvider()); |