This commit is contained in:
David Sun
2019-07-02 12:05:08 -07:00
parent 6a8197a4e8
commit 44806106db
2 changed files with 29 additions and 9 deletions

View File

@@ -24,12 +24,15 @@ import { swapQuoteConsumerUtils } from '../utils/swap_quote_consumer_utils';
import { ExchangeSwapQuoteConsumer } from './exchange_swap_quote_consumer';
import { ForwarderSwapQuoteConsumer } from './forwarder_swap_quote_consumer';
type SmartContractParams = ExchangeMarketBuySmartContractParams | ExchangeMarketSellSmartContractParams | ForwarderMarketBuySmartContractParams | ForwarderMarketSellSmartContractParams;
type SmartContractParams =
| ExchangeMarketBuySmartContractParams
| ExchangeMarketSellSmartContractParams
| ForwarderMarketBuySmartContractParams
| ForwarderMarketSellSmartContractParams;
type ValidSwapQuoteConsumer = ExchangeSwapQuoteConsumer | ForwarderSwapQuoteConsumer;
export class DynamicSwapQuoteConsumer
implements SwapQuoteConsumer<SmartContractParams> {
export class DynamicSwapQuoteConsumer implements SwapQuoteConsumer<SmartContractParams> {
public readonly provider: ZeroExProvider;
public readonly networkId: number;
@@ -79,7 +82,10 @@ export class DynamicSwapQuoteConsumer
return consumer.executeSwapQuoteOrThrowAsync(quote, opts);
}
private async _getConsumerForSwapQuoteAsync(quote: SwapQuote, opts: Partial<DynamicSwapQuoteGetOutputOpts>): Promise<ValidSwapQuoteConsumer> {
private async _getConsumerForSwapQuoteAsync(
quote: SwapQuote,
opts: Partial<DynamicSwapQuoteGetOutputOpts>,
): Promise<ValidSwapQuoteConsumer> {
const wethAssetData = assetDataUtils.getEtherTokenAssetData(this._contractWrappers);
if (swapQuoteConsumerUtils.isValidForwarderSwapQuote(quote, wethAssetData)) {
if (opts.takerAddress !== undefined) {
@@ -87,10 +93,17 @@ export class DynamicSwapQuoteConsumer
}
const ethAmount = opts.ethAmount || quote.worstCaseQuoteInfo.totalTakerTokenAmount;
const takerAddress = await swapQuoteConsumerUtils.getTakerAddressOrThrowAsync(this.provider, opts);
const takerEthAndWethBalance = await swapQuoteConsumerUtils.getEthAndWethBalanceAsync(this.provider, this._contractWrappers, takerAddress);
const takerEthAndWethBalance = await swapQuoteConsumerUtils.getEthAndWethBalanceAsync(
this.provider,
this._contractWrappers,
takerAddress,
);
// 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.
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;

View File

@@ -24,7 +24,11 @@ export const swapQuoteConsumerUtils = {
}
}
},
async getEthAndWethBalanceAsync(provider: SupportedProvider, contractWrappers: ContractWrappers, takerAddress: string): Promise<[BigNumber, BigNumber]> {
async getEthAndWethBalanceAsync(
provider: SupportedProvider,
contractWrappers: ContractWrappers,
takerAddress: string,
): Promise<[BigNumber, BigNumber]> {
const web3Wrapper = new Web3Wrapper(provider);
const wethAddress = contractWrappers.forwarder.etherTokenAddress;
const ethBalance = await web3Wrapper.getBalanceInWeiAsync(takerAddress);
@@ -32,7 +36,10 @@ export const swapQuoteConsumerUtils = {
return [ethBalance, wethBalance];
},
isValidForwarderSwapQuote(swapQuote: SwapQuote, wethAssetData: string): boolean {
return swapQuoteConsumerUtils.isValidForwarderSignedOrders(swapQuote.orders, wethAssetData) && swapQuoteConsumerUtils.isValidForwarderSignedOrders(swapQuote.feeOrders, wethAssetData);
return (
swapQuoteConsumerUtils.isValidForwarderSignedOrders(swapQuote.orders, wethAssetData) &&
swapQuoteConsumerUtils.isValidForwarderSignedOrders(swapQuote.feeOrders, wethAssetData)
);
},
isValidForwarderSignedOrders(orders: SignedOrder[], wethAssetData: string): boolean {
return _.every(orders, order => swapQuoteConsumerUtils.isValidForwarderSignedOrder(order, wethAssetData));