made get Consumer private
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { ContractWrappers } from '@0x/contract-wrappers';
|
||||
import { BigNumber, providerUtils } from '@0x/utils';
|
||||
import { providerUtils } from '@0x/utils';
|
||||
import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
@@ -15,7 +15,6 @@ import {
|
||||
SwapQuoteConsumerOpts,
|
||||
} from '../types';
|
||||
import { assert } from '../utils/assert';
|
||||
import { assetDataUtils } from '../utils/asset_data_utils';
|
||||
import { swapQuoteConsumerUtils } from '../utils/swap_quote_consumer_utils';
|
||||
|
||||
import { ExchangeSwapQuoteConsumer } from './exchange_swap_quote_consumer';
|
||||
@@ -49,7 +48,7 @@ export class SwapQuoteConsumer implements SwapQuoteConsumerBase<SmartContractPar
|
||||
opts: Partial<DynamicSwapQuoteGetOutputOpts>,
|
||||
): Promise<CalldataInfo> {
|
||||
assert.isValidSwapQuote('quote', quote);
|
||||
const consumer = await this.getConsumerForSwapQuoteAsync(quote, opts);
|
||||
const consumer = await this._getConsumerForSwapQuoteAsync(quote, opts);
|
||||
return consumer.getCalldataOrThrowAsync(quote, opts);
|
||||
}
|
||||
|
||||
@@ -58,7 +57,7 @@ export class SwapQuoteConsumer implements SwapQuoteConsumerBase<SmartContractPar
|
||||
opts: Partial<DynamicSwapQuoteGetOutputOpts>,
|
||||
): Promise<SmartContractParamsInfo<SmartContractParams>> {
|
||||
assert.isValidSwapQuote('quote', quote);
|
||||
const consumer = await this.getConsumerForSwapQuoteAsync(quote, opts);
|
||||
const consumer = await this._getConsumerForSwapQuoteAsync(quote, opts);
|
||||
return consumer.getSmartContractParamsOrThrowAsync(quote, opts);
|
||||
}
|
||||
|
||||
@@ -67,40 +66,21 @@ export class SwapQuoteConsumer implements SwapQuoteConsumerBase<SmartContractPar
|
||||
opts: Partial<DynamicSwapQuoteExecutionOpts>,
|
||||
): Promise<string> {
|
||||
assert.isValidSwapQuote('quote', quote);
|
||||
const consumer = await this.getConsumerForSwapQuoteAsync(quote, opts);
|
||||
const consumer = await this._getConsumerForSwapQuoteAsync(quote, opts);
|
||||
return consumer.executeSwapQuoteOrThrowAsync(quote, opts);
|
||||
}
|
||||
|
||||
public async getConsumerForSwapQuoteAsync(
|
||||
private async _getConsumerForSwapQuoteAsync(
|
||||
quote: SwapQuote,
|
||||
opts: Partial<DynamicSwapQuoteGetOutputOpts>,
|
||||
): Promise<SwapQuoteConsumerBase<SmartContractParams>> {
|
||||
const wethAssetData = assetDataUtils.getEtherTokenAssetData(this._contractWrappers);
|
||||
if (swapQuoteConsumerUtils.isValidForwarderSwapQuote(quote, wethAssetData)) {
|
||||
if (opts.takerAddress !== undefined) {
|
||||
assert.isETHAddressHex('takerAddress', opts.takerAddress);
|
||||
}
|
||||
const ethAmount = opts.ethAmount || quote.worstCaseQuoteInfo.totalTakerTokenAmount;
|
||||
const takerAddress = await swapQuoteConsumerUtils.getTakerAddressAsync(this.provider, opts);
|
||||
const takerEthAndWethBalance = takerAddress !== undefined ? await swapQuoteConsumerUtils.getEthAndWethBalanceAsync(
|
||||
this.provider,
|
||||
this._contractWrappers,
|
||||
takerAddress,
|
||||
) : [constants.ZERO_AMOUNT, constants.ZERO_AMOUNT];
|
||||
// TODO(david): when considering if there is enough Eth balance, should account for gas costs.
|
||||
const isEnoughEthAndWethBalance = _.map(takerEthAndWethBalance, (balance: BigNumber) =>
|
||||
balance.isGreaterThanOrEqualTo(ethAmount),
|
||||
);
|
||||
if (isEnoughEthAndWethBalance[1]) {
|
||||
// should be more gas efficient to use exchange consumer, so if possible use it.
|
||||
return this._exchangeConsumer;
|
||||
} else if (isEnoughEthAndWethBalance[0] && !isEnoughEthAndWethBalance[1]) {
|
||||
return this._forwarderConsumer;
|
||||
}
|
||||
// Note: defaulting to forwarderConsumer if takerAddress is null or not enough balance of either wEth or Eth
|
||||
return this._forwarderConsumer;
|
||||
} else {
|
||||
return this._exchangeConsumer;
|
||||
}
|
||||
return swapQuoteConsumerUtils.getConsumerForSwapQuoteAsync(
|
||||
quote,
|
||||
this._contractWrappers,
|
||||
this.provider,
|
||||
this._exchangeConsumer,
|
||||
this._forwarderConsumer,
|
||||
opts,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -2,9 +2,23 @@ import { ContractWrappers } from '@0x/contract-wrappers';
|
||||
import { SignedOrder } from '@0x/types';
|
||||
import { BigNumber } from '@0x/utils';
|
||||
import { SupportedProvider, Web3Wrapper } from '@0x/web3-wrapper';
|
||||
import { Provider } from 'ethereum-types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { SwapQuote, SwapQuoteConsumerError, SwapQuoteExecutionOpts } from '../types';
|
||||
import { constants } from '../constants';
|
||||
import { ExchangeSwapQuoteConsumer } from '../quote_consumers/exchange_swap_quote_consumer';
|
||||
import { ForwarderSwapQuoteConsumer } from '../quote_consumers/forwarder_swap_quote_consumer';
|
||||
import {
|
||||
DynamicSwapQuoteGetOutputOpts,
|
||||
SmartContractParams,
|
||||
SwapQuote,
|
||||
SwapQuoteConsumerBase,
|
||||
SwapQuoteConsumerError,
|
||||
SwapQuoteExecutionOpts,
|
||||
} from '../types';
|
||||
|
||||
import { assert } from './assert';
|
||||
import { assetDataUtils } from './asset_data_utils';
|
||||
|
||||
export const swapQuoteConsumerUtils = {
|
||||
async getTakerAddressOrThrowAsync(
|
||||
@@ -58,4 +72,39 @@ export const swapQuoteConsumerUtils = {
|
||||
isValidForwarderSignedOrder(order: SignedOrder, wethAssetData: string): boolean {
|
||||
return order.takerAssetData === wethAssetData;
|
||||
},
|
||||
async getConsumerForSwapQuoteAsync(
|
||||
quote: SwapQuote,
|
||||
contractWrappers: ContractWrappers,
|
||||
provider: Provider,
|
||||
exchangeConsumer: ExchangeSwapQuoteConsumer,
|
||||
forwarderConsumer: ForwarderSwapQuoteConsumer,
|
||||
opts: Partial<DynamicSwapQuoteGetOutputOpts>,
|
||||
): Promise<SwapQuoteConsumerBase<SmartContractParams>> {
|
||||
const wethAssetData = assetDataUtils.getEtherTokenAssetData(contractWrappers);
|
||||
if (swapQuoteConsumerUtils.isValidForwarderSwapQuote(quote, wethAssetData)) {
|
||||
if (opts.takerAddress !== undefined) {
|
||||
assert.isETHAddressHex('takerAddress', opts.takerAddress);
|
||||
}
|
||||
const ethAmount = opts.ethAmount || quote.worstCaseQuoteInfo.totalTakerTokenAmount;
|
||||
const takerAddress = await swapQuoteConsumerUtils.getTakerAddressAsync(provider, opts);
|
||||
const takerEthAndWethBalance =
|
||||
takerAddress !== undefined
|
||||
? await swapQuoteConsumerUtils.getEthAndWethBalanceAsync(provider, contractWrappers, takerAddress)
|
||||
: [constants.ZERO_AMOUNT, constants.ZERO_AMOUNT];
|
||||
// TODO(david): when considering if there is enough Eth balance, should account for gas costs.
|
||||
const isEnoughEthAndWethBalance = _.map(takerEthAndWethBalance, (balance: BigNumber) =>
|
||||
balance.isGreaterThanOrEqualTo(ethAmount),
|
||||
);
|
||||
if (isEnoughEthAndWethBalance[1]) {
|
||||
// should be more gas efficient to use exchange consumer, so if possible use it.
|
||||
return exchangeConsumer;
|
||||
} else if (isEnoughEthAndWethBalance[0] && !isEnoughEthAndWethBalance[1]) {
|
||||
return forwarderConsumer;
|
||||
}
|
||||
// Note: defaulting to forwarderConsumer if takerAddress is null or not enough balance of either wEth or Eth
|
||||
return forwarderConsumer;
|
||||
} else {
|
||||
return exchangeConsumer;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
Reference in New Issue
Block a user