From c198d0079e63fd243692abfef537eb932dc8843f Mon Sep 17 00:00:00 2001 From: David Sun Date: Sat, 16 Nov 2019 11:41:19 -0600 Subject: [PATCH] prettier + minor changes --- .../forwarder_swap_quote_consumer.ts | 6 +--- .../src/utils/protocol_fee_utils.ts | 9 +++--- .../src/utils/swap_quote_calculator.ts | 16 +++++++--- .../instant/src/components/buy_button.tsx | 30 ++++++++++++++--- .../components/buy_order_state_buttons.tsx | 20 ++++++++++-- .../instant/src/components/order_details.tsx | 15 ++++++--- .../components/zero_ex_instant_provider.tsx | 3 +- .../selected_asset_buy_order_state_buttons.ts | 32 ++++++++++++++----- .../selected_erc20_asset_amount_input.ts | 6 +--- packages/instant/src/index.umd.ts | 7 ++-- packages/instant/src/redux/actions.ts | 3 +- packages/instant/src/redux/reducer.ts | 4 +-- packages/instant/src/types.ts | 1 - packages/instant/src/util/analytics.ts | 30 ++++++++++++++--- .../instant/src/util/asset_swapper_factory.ts | 2 +- .../instant/src/util/swap_quote_updater.ts | 12 ++++--- 16 files changed, 139 insertions(+), 57 deletions(-) diff --git a/packages/asset-swapper/src/quote_consumers/forwarder_swap_quote_consumer.ts b/packages/asset-swapper/src/quote_consumers/forwarder_swap_quote_consumer.ts index aec674c5aa..92b2159088 100644 --- a/packages/asset-swapper/src/quote_consumers/forwarder_swap_quote_consumer.ts +++ b/packages/asset-swapper/src/quote_consumers/forwarder_swap_quote_consumer.ts @@ -89,11 +89,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase> { assert.isValidForwarderSwapQuote('quote', quote, await this._getEtherTokenAssetDataOrThrowAsync()); - const { extensionContractOpts } = _.merge( - {}, - constants.DEFAULT_FORWARDER_SWAP_QUOTE_GET_OPTS, - opts, - ); + const { extensionContractOpts } = _.merge({}, constants.DEFAULT_FORWARDER_SWAP_QUOTE_GET_OPTS, opts); assert.isValidForwarderExtensionContractOpts('extensionContractOpts', extensionContractOpts); diff --git a/packages/asset-swapper/src/utils/protocol_fee_utils.ts b/packages/asset-swapper/src/utils/protocol_fee_utils.ts index 7d8eead1a4..b51ed6846d 100644 --- a/packages/asset-swapper/src/utils/protocol_fee_utils.ts +++ b/packages/asset-swapper/src/utils/protocol_fee_utils.ts @@ -9,9 +9,7 @@ import { SwapQuoterError } from '../types'; export class ProtocolFeeUtils { private readonly _exchangeContract: ExchangeContract; - constructor( - exchangeContract: ExchangeContract, - ) { + constructor(exchangeContract: ExchangeContract) { this._exchangeContract = exchangeContract; } @@ -41,7 +39,10 @@ export class ProtocolFeeUtils { /** * Calculates protocol fee with protofol fee multiplier for each fill. */ - public async calculateWorstCaseProtocolFeeAsync(orders: T[], gasPrice: BigNumber): Promise { + public async calculateWorstCaseProtocolFeeAsync( + orders: T[], + gasPrice: BigNumber, + ): Promise { const protocolFeeMultiplier = await this.getProtocolFeeMultiplierAsync(); const protocolFee = new BigNumber(orders.length).times(protocolFeeMultiplier).times(gasPrice); return protocolFee; diff --git a/packages/asset-swapper/src/utils/swap_quote_calculator.ts b/packages/asset-swapper/src/utils/swap_quote_calculator.ts index 328def75b0..72d5ae2926 100644 --- a/packages/asset-swapper/src/utils/swap_quote_calculator.ts +++ b/packages/asset-swapper/src/utils/swap_quote_calculator.ts @@ -26,14 +26,14 @@ export const swapQuoteCalculator = { gasPrice: BigNumber, protocolFeeUtils: ProtocolFeeUtils, ): Promise { - return await calculateSwapQuoteAsync( + return (await calculateSwapQuoteAsync( prunedOrders, takerAssetFillAmount, slippagePercentage, gasPrice, protocolFeeUtils, MarketOperation.Sell, - ) as MarketSellSwapQuote; + )) as MarketSellSwapQuote; }, async calculateMarketBuySwapQuoteAsync( prunedOrders: PrunedSignedOrder[], @@ -42,14 +42,14 @@ export const swapQuoteCalculator = { gasPrice: BigNumber, protocolFeeUtils: ProtocolFeeUtils, ): Promise { - return await calculateSwapQuoteAsync( + return (await calculateSwapQuoteAsync( prunedOrders, takerAssetFillAmount, slippagePercentage, gasPrice, protocolFeeUtils, MarketOperation.Buy, - ) as MarketBuySwapQuote; + )) as MarketBuySwapQuote; }, }; @@ -103,7 +103,13 @@ async function calculateSwapQuoteAsync( const takerAssetData = resultOrders[0].takerAssetData; const makerAssetData = resultOrders[0].makerAssetData; - const bestCaseQuoteInfo = await calculateQuoteInfoAsync(resultOrders, assetFillAmount, gasPrice, protocolFeeUtils, marketOperation); + const bestCaseQuoteInfo = await calculateQuoteInfoAsync( + resultOrders, + assetFillAmount, + gasPrice, + protocolFeeUtils, + marketOperation, + ); // in order to calculate the maxRate, reverse the ordersAndFillableAmounts such that they are sorted from worst rate to best rate const worstCaseQuoteInfo = await calculateQuoteInfoAsync( _.reverse(_.clone(resultOrders)), diff --git a/packages/instant/src/components/buy_button.tsx b/packages/instant/src/components/buy_button.tsx index e621039703..9294741a3a 100644 --- a/packages/instant/src/components/buy_button.tsx +++ b/packages/instant/src/components/buy_button.tsx @@ -1,4 +1,11 @@ -import { affiliateFeeUtils, ExtensionContractType, MarketBuySwapQuote, SwapQuoteConsumer, SwapQuoteConsumerError, SwapQuoter } from '@0x/asset-swapper'; +import { + affiliateFeeUtils, + ExtensionContractType, + MarketBuySwapQuote, + SwapQuoteConsumer, + SwapQuoteConsumerError, + SwapQuoter, +} from '@0x/asset-swapper'; import { AssetProxyId } from '@0x/types'; import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; @@ -26,9 +33,17 @@ export interface BuyButtonProps { affiliateInfo?: AffiliateInfo; selectedAsset?: Asset; onValidationPending: (swapQuote: MarketBuySwapQuote) => void; - onValidationFail: (swapQuote: MarketBuySwapQuote, errorMessage: SwapQuoteConsumerError | ZeroExInstantError) => void; + onValidationFail: ( + swapQuote: MarketBuySwapQuote, + errorMessage: SwapQuoteConsumerError | ZeroExInstantError, + ) => void; onSignatureDenied: (swapQuote: MarketBuySwapQuote) => void; - onBuyProcessing: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => void; + onBuyProcessing: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => void; onBuySuccess: (swapQuote: MarketBuySwapQuote, txHash: string) => void; onBuyFailure: (swapQuote: MarketBuySwapQuote, txHash: string) => void; } @@ -59,7 +74,14 @@ export class BuyButton extends React.PureComponent { } private readonly _handleClick = async () => { // The button is disabled when there is no buy quote anyway. - const { swapQuote, swapQuoteConsumer, affiliateInfo, accountAddress, accountEthBalanceInWei, web3Wrapper } = this.props; + const { + swapQuote, + swapQuoteConsumer, + affiliateInfo, + accountAddress, + accountEthBalanceInWei, + web3Wrapper, + } = this.props; if (swapQuote === undefined || accountAddress === undefined) { return; } diff --git a/packages/instant/src/components/buy_order_state_buttons.tsx b/packages/instant/src/components/buy_order_state_buttons.tsx index 89d1b1699a..e04832fbe2 100644 --- a/packages/instant/src/components/buy_order_state_buttons.tsx +++ b/packages/instant/src/components/buy_order_state_buttons.tsx @@ -1,4 +1,10 @@ -import { MarketBuySwapQuote, SwapQuoteConsumer, SwapQuoter, SwapQuoterError, SwapQuoteConsumerError } from '@0x/asset-swapper'; +import { + MarketBuySwapQuote, + SwapQuoteConsumer, + SwapQuoter, + SwapQuoterError, + SwapQuoteConsumerError, +} from '@0x/asset-swapper'; import { BigNumber } from '@0x/utils'; import { Web3Wrapper } from '@0x/web3-wrapper'; import * as React from 'react'; @@ -25,9 +31,17 @@ export interface BuyOrderStateButtonProps { selectedAsset?: Asset; onViewTransaction: () => void; onValidationPending: (swapQuote: MarketBuySwapQuote) => void; - onValidationFail: (swapQuote: MarketBuySwapQuote, errorMessage: SwapQuoteConsumerError | ZeroExInstantError) => void; + onValidationFail: ( + swapQuote: MarketBuySwapQuote, + errorMessage: SwapQuoteConsumerError | ZeroExInstantError, + ) => void; onSignatureDenied: (swapQuote: MarketBuySwapQuote) => void; - onBuyProcessing: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => void; + onBuyProcessing: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => void; onBuySuccess: (swapQuote: MarketBuySwapQuote, txHash: string) => void; onBuyFailure: (swapQuote: MarketBuySwapQuote, txHash: string) => void; onRetry: () => void; diff --git a/packages/instant/src/components/order_details.tsx b/packages/instant/src/components/order_details.tsx index 28074791e2..028af3662e 100644 --- a/packages/instant/src/components/order_details.tsx +++ b/packages/instant/src/components/order_details.tsx @@ -37,7 +37,7 @@ export class OrderDetails extends React.PureComponent { ); } - // TODO(dave4506) adjust fees for the new system (add affiliate fees) + // TODO(dave4506) currently instant has affiliate fees disabled, until we resolve that, this displays only takerFees + protocolFees private _renderRows(): React.ReactNode { const { swapQuoteInfo } = this.props; return ( @@ -48,12 +48,16 @@ export class OrderDetails extends React.PureComponent { /> @@ -89,7 +93,10 @@ export class OrderDetails extends React.PureComponent { (secondaryCurrency === BaseCurrency.USD && this.props.ethUsdPrice && !this._hadErrorFetchingUsdPrice()); if (this.props.swapQuoteInfo && canDisplayCurrency) { - return this._displayAmount(secondaryCurrency, this.props.swapQuoteInfo.totalTakerAssetAmount.plus(this.props.swapQuoteInfo.protocolFeeInEthAmount)); + return this._displayAmount( + secondaryCurrency, + this.props.swapQuoteInfo.totalTakerAssetAmount.plus(this.props.swapQuoteInfo.protocolFeeInEthAmount), + ); } else { return undefined; } diff --git a/packages/instant/src/components/zero_ex_instant_provider.tsx b/packages/instant/src/components/zero_ex_instant_provider.tsx index 38a58c531f..59cf8476bd 100644 --- a/packages/instant/src/components/zero_ex_instant_provider.tsx +++ b/packages/instant/src/components/zero_ex_instant_provider.tsx @@ -84,7 +84,6 @@ export class ZeroExInstantProvider extends React.PureComponent void; onSignatureDenied: (swapQuote: MarketBuySwapQuote) => void; - onBuyProcessing: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => void; + onBuyProcessing: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => void; onBuySuccess: (swapQuote: MarketBuySwapQuote, txHash: string) => void; onBuyFailure: (swapQuote: MarketBuySwapQuote, txHash: string) => void; onRetry: () => void; - onValidationFail: (swapQuote: MarketBuySwapQuote, errorMessage: SwapQuoteConsumerError | ZeroExInstantError) => void; + onValidationFail: ( + swapQuote: MarketBuySwapQuote, + errorMessage: SwapQuoteConsumerError | ZeroExInstantError, + ) => void; } export interface SelectedAssetBuyOrderStateButtons {} const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyOrderStateButtons): ConnectedState => { @@ -63,10 +77,7 @@ const mapStateToProps = (state: State, _ownProps: SelectedAssetBuyOrderStateButt state.swapOrderState.processState === OrderProcessState.Success || state.swapOrderState.processState === OrderProcessState.Failure ) { - const etherscanUrl = etherscanUtil.getEtherScanTxnAddressIfExists( - state.swapOrderState.txHash, - chainId, - ); + const etherscanUrl = etherscanUtil.getEtherScanTxnAddressIfExists(state.swapOrderState.txHash, chainId); if (etherscanUrl) { analytics.trackTransactionViewed(state.swapOrderState.processState); @@ -85,7 +96,12 @@ const mapDispatchToProps = ( onValidationPending: (swapQuote: MarketBuySwapQuote) => { dispatch(actions.setSwapOrderStateValidating()); }, - onBuyProcessing: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => { + onBuyProcessing: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => { dispatch(actions.setSwapOrderStateProcessing(txHash, startTimeUnix, expectedEndTimeUnix)); }, onBuySuccess: (swapQuote: MarketBuySwapQuote, txHash: string) => dispatch(actions.setSwapOrderStateSuccess(txHash)), diff --git a/packages/instant/src/containers/selected_erc20_asset_amount_input.ts b/packages/instant/src/containers/selected_erc20_asset_amount_input.ts index dd6a3f5e2b..191da44bb3 100644 --- a/packages/instant/src/containers/selected_erc20_asset_amount_input.ts +++ b/packages/instant/src/containers/selected_erc20_asset_amount_input.ts @@ -29,11 +29,7 @@ interface ConnectedState { } interface ConnectedDispatch { - updateSwapQuote: ( - swapQuoter: SwapQuoter, - value?: BigNumber, - asset?: ERC20Asset, - ) => void; + updateSwapQuote: (swapQuoter: SwapQuoter, value?: BigNumber, asset?: ERC20Asset) => void; } type ConnectedProps = Omit; diff --git a/packages/instant/src/index.umd.ts b/packages/instant/src/index.umd.ts index d430f5c056..344a8de9b5 100644 --- a/packages/instant/src/index.umd.ts +++ b/packages/instant/src/index.umd.ts @@ -48,9 +48,10 @@ const validateInstantRenderConfig = (config: ZeroExInstantConfig, selector: stri if (config.zIndex !== undefined) { assert.isNumber('zIndex', config.zIndex); } - if (config.affiliateInfo !== undefined) { - assert.isValidAffiliateInfo('affiliateInfo', config.affiliateInfo); - } + // TODO(dave4506) reenable affiliate fees once we figure out how to work with it + // if (config.affiliateInfo !== undefined) { + // assert.isValidAffiliateInfo('affiliateInfo', config.affiliateInfo); + // } if (config.provider !== undefined) { providerUtils.standardizeOrThrow(config.provider); } diff --git a/packages/instant/src/redux/actions.ts b/packages/instant/src/redux/actions.ts index 1bd23851db..47e0e45276 100644 --- a/packages/instant/src/redux/actions.ts +++ b/packages/instant/src/redux/actions.ts @@ -59,7 +59,8 @@ export const actions = { createAction(ActionTypes.SetSwapOrderStateProcessing, { txHash, startTimeUnix, expectedEndTimeUnix }), setSwapOrderStateFailure: (txHash: string) => createAction(ActionTypes.SetSwapOrderStateFailure, txHash), setSwapOrderStateSuccess: (txHash: string) => createAction(ActionTypes.SetSwapOrderStateSuccess, txHash), - updateLatestSwapQuote: (swapQuote?: MarketBuySwapQuote) => createAction(ActionTypes.UpdateLatestSwapQuote, swapQuote), + updateLatestSwapQuote: (swapQuote?: MarketBuySwapQuote) => + createAction(ActionTypes.UpdateLatestSwapQuote, swapQuote), updateSelectedAsset: (asset: Asset) => createAction(ActionTypes.UpdateSelectedAsset, asset), setAvailableAssets: (availableAssets: Asset[]) => createAction(ActionTypes.SetAvailableAssets, availableAssets), setQuoteRequestStatePending: () => createAction(ActionTypes.SetQuoteRequestStatePending), diff --git a/packages/instant/src/redux/reducer.ts b/packages/instant/src/redux/reducer.ts index 46f68d1877..6583615c89 100644 --- a/packages/instant/src/redux/reducer.ts +++ b/packages/instant/src/redux/reducer.ts @@ -118,7 +118,7 @@ export const createReducer = (initialState: State) => { case ActionTypes.UpdateLatestSwapQuote: const newSwapQuoteIfExists = action.data; const shouldUpdate = - newSwapQuoteIfExists === undefined || doesSwapQuoteMatchState(newSwapQuoteIfExists, state); + newSwapQuoteIfExists === undefined || doesSwapQuoteMatchState(newSwapQuoteIfExists, state); if (shouldUpdate) { return { ...state, @@ -271,7 +271,7 @@ const reduceStateWithAccount = (state: State, account: Account) => { }; }; -const doesSwapQuoteMatchState = (swapQuote: MarketBuySwapQuote , state: State): boolean => { +const doesSwapQuoteMatchState = (swapQuote: MarketBuySwapQuote, state: State): boolean => { const selectedAssetIfExists = state.selectedAsset; const selectedAssetUnitAmountIfExists = state.selectedAssetUnitAmount; // if no selectedAsset or selectedAssetAmount exists on the current state, return false diff --git a/packages/instant/src/types.ts b/packages/instant/src/types.ts index 0656a7374c..d41cdd117e 100644 --- a/packages/instant/src/types.ts +++ b/packages/instant/src/types.ts @@ -200,7 +200,6 @@ export interface ZeroExInstantOptionalBaseConfig { defaultSelectedAssetData: string; additionalAssetMetaDataMap: ObjectMap; networkId: Network; - affiliateInfo: AffiliateInfo; shouldDisableAnalyticsTracking: boolean; onSuccess?: (txHash: string) => void; } diff --git a/packages/instant/src/util/analytics.ts b/packages/instant/src/util/analytics.ts index 9827460190..eeaed40fd0 100644 --- a/packages/instant/src/util/analytics.ts +++ b/packages/instant/src/util/analytics.ts @@ -85,13 +85,18 @@ function trackingEventFnWithPayload(eventName: EventNames): (eventProperties: Ev const swapQuoteEventProperties = (swapQuote: MarketBuySwapQuote) => { const makerAssetFillAmount = swapQuote.makerAssetFillAmount.toString(); const assetEthAmount = swapQuote.worstCaseQuoteInfo.takerAssetAmount.toString(); - const feeEthAmount = swapQuote.worstCaseQuoteInfo.protocolFeeInEthAmount.plus(swapQuote.worstCaseQuoteInfo.feeTakerAssetAmount).toString(); - const totalEthAmount = swapQuote.worstCaseQuoteInfo.totalTakerAssetAmount.toString(); + const feeEthAmount = swapQuote.worstCaseQuoteInfo.protocolFeeInEthAmount + .plus(swapQuote.worstCaseQuoteInfo.feeTakerAssetAmount) + .toString(); + const totalEthAmount = swapQuote.worstCaseQuoteInfo.totalTakerAssetAmount + .plus(swapQuote.worstCaseQuoteInfo.protocolFeeInEthAmount) + .toString(); return { makerAssetFillAmount, assetEthAmount, feeEthAmount, totalEthAmount, + gasPrice: swapQuote.gasPrice, }; }; @@ -191,20 +196,35 @@ export const analytics = { ...swapQuoteEventProperties(swapQuote), errorMessage, }), - trackBuyTxSubmitted: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => + trackBuyTxSubmitted: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => trackingEventFnWithPayload(EventNames.BuyTxSubmitted)({ ...swapQuoteEventProperties(swapQuote), txHash, expectedTxTimeMs: expectedEndTimeUnix - startTimeUnix, }), - trackBuyTxSucceeded: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => + trackBuyTxSucceeded: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => trackingEventFnWithPayload(EventNames.BuyTxSucceeded)({ ...swapQuoteEventProperties(swapQuote), txHash, expectedTxTimeMs: expectedEndTimeUnix - startTimeUnix, actualTxTimeMs: new Date().getTime() - startTimeUnix, }), - trackBuyTxFailed: (swapQuote: MarketBuySwapQuote, txHash: string, startTimeUnix: number, expectedEndTimeUnix: number) => + trackBuyTxFailed: ( + swapQuote: MarketBuySwapQuote, + txHash: string, + startTimeUnix: number, + expectedEndTimeUnix: number, + ) => trackingEventFnWithPayload(EventNames.BuyTxFailed)({ ...swapQuoteEventProperties(swapQuote), txHash, diff --git a/packages/instant/src/util/asset_swapper_factory.ts b/packages/instant/src/util/asset_swapper_factory.ts index 18b5185ecb..71b56848a0 100644 --- a/packages/instant/src/util/asset_swapper_factory.ts +++ b/packages/instant/src/util/asset_swapper_factory.ts @@ -18,6 +18,6 @@ export const assetSwapperFactory = { const swapQuoteConsumerOptions: Partial = { chainId: network, }; - return new SwapQuoteConsumer(supportedProvider, swapQuoteConsumerOptions ); + return new SwapQuoteConsumer(supportedProvider, swapQuoteConsumerOptions); }, }; diff --git a/packages/instant/src/util/swap_quote_updater.ts b/packages/instant/src/util/swap_quote_updater.ts index 9fd83b978b..c7f80c01b4 100644 --- a/packages/instant/src/util/swap_quote_updater.ts +++ b/packages/instant/src/util/swap_quote_updater.ts @@ -35,7 +35,6 @@ export const swapQuoteUpdater = { // mark quote as pending dispatch(actions.setQuoteRequestStatePending()); } - // TODO(dave4506) expose wethAssetData + feePercentage utils const wethAssetData = await swapQuoter.getEtherTokenAssetDataOrThrowAsync(); let newSwapQuote: MarketBuySwapQuote | undefined; const slippagePercentage = @@ -43,9 +42,14 @@ export const swapQuoteUpdater = { ? ERC20_SWAP_QUOTE_SLIPPAGE_PERCENTAGE : ERC721_SWAP_QUOTE_SLIPPAGE_PERCENTAGE; try { - newSwapQuote = await swapQuoter.getMarketBuySwapQuoteAsync(wethAssetData, asset.assetData, assetUnitAmount, { - slippagePercentage, - }); + newSwapQuote = await swapQuoter.getMarketBuySwapQuoteAsync( + wethAssetData, + asset.assetData, + assetUnitAmount, + { + slippagePercentage, + }, + ); } catch (error) { const errorMessage = assetUtils.swapQuoterErrorMessage(asset, error);