utils moved to SwapQuoter and polished Doc Strings

This commit is contained in:
David Sun 2019-07-17 15:12:59 -07:00
parent 88ff38eca6
commit dcf4eb2aaf
5 changed files with 36 additions and 64 deletions

View File

@ -23,7 +23,6 @@ export {
export { SignedOrder } from '@0x/types'; export { SignedOrder } from '@0x/types';
export { BigNumber } from '@0x/utils'; export { BigNumber } from '@0x/utils';
export { SwapQuoteUtils } from './utils/swap_quote_utils';
export { SwapQuoteConsumer } from './quote_consumers/swap_quote_consumer'; export { SwapQuoteConsumer } from './quote_consumers/swap_quote_consumer';
export { SwapQuoter } from './swap_quoter'; export { SwapQuoter } from './swap_quoter';
export { InsufficientAssetLiquidityError } from './errors'; export { InsufficientAssetLiquidityError } from './errors';

View File

@ -76,8 +76,6 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
const { orders } = quote; const { orders } = quote;
const optimizedOrders = calldataOptimizationUtils.optimizeForwarderOrders(orders);
const signatures = _.map(orders, o => o.signature); const signatures = _.map(orders, o => o.signature);
let params: ExchangeSmartContractParams; let params: ExchangeSmartContractParams;
@ -87,7 +85,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
const { makerAssetFillAmount } = quote; const { makerAssetFillAmount } = quote;
params = { params = {
orders: optimizedOrders, orders,
signatures, signatures,
makerAssetFillAmount, makerAssetFillAmount,
type: MarketOperation.Buy, type: MarketOperation.Buy,
@ -98,7 +96,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
const { takerAssetFillAmount } = quote; const { takerAssetFillAmount } = quote;
params = { params = {
orders: optimizedOrders, orders,
signatures, signatures,
takerAssetFillAmount, takerAssetFillAmount,
type: MarketOperation.Sell, type: MarketOperation.Sell,

View File

@ -373,6 +373,21 @@ export class SwapQuoter {
return ordersAndFillableAmounts; return ordersAndFillableAmounts;
} }
public async isTakerAddressAllowanceEnoughForBestAndWorstQuoteInfoAsync(
swapQuote: SwapQuote,
takerAddress: string,
): Promise<[boolean, boolean]> {
const orderValidatorWrapper = this._contractWrappers.orderValidator;
const balanceAndAllowance = await orderValidatorWrapper.getBalanceAndAllowanceAsync(
takerAddress,
swapQuote.takerAssetData,
);
return [
balanceAndAllowance.allowance.isGreaterThanOrEqualTo(swapQuote.bestCaseQuoteInfo.totalTakerTokenAmount),
balanceAndAllowance.allowance.isGreaterThanOrEqualTo(swapQuote.worstCaseQuoteInfo.totalTakerTokenAmount),
];
}
/** /**
* Get the assetData that represents the ZRX token. * Get the assetData that represents the ZRX token.
* Will throw if ZRX does not exist for the current network. * Will throw if ZRX does not exist for the current network.

View File

@ -94,9 +94,11 @@ export interface ExchangeMarketSellSmartContractParams extends SmartContractPara
type: MarketOperation.Sell; type: MarketOperation.Sell;
} }
/**
* Represents the varying smart contracts that can consume a valid swap quote
*/
export enum ConsumerType { export enum ConsumerType {
Forwarder, Forwarder, Exchange,
Exchange,
} }
/** /**
@ -162,10 +164,6 @@ export interface SwapQuoteConsumerOpts {
networkId: number; networkId: number;
} }
export interface SwapQuoteUtilsOpts {
networkId: number;
}
/** /**
* Represents the options provided to a generic SwapQuoteConsumer * Represents the options provided to a generic SwapQuoteConsumer
*/ */
@ -193,34 +191,27 @@ export interface ForwarderSwapQuoteGetOutputOpts extends SwapQuoteGetOutputOptsB
ethAmount?: BigNumber; ethAmount?: BigNumber;
} }
/**
* Represents the options for executing a swap quote with ForwarderSwapQuoteConusmer
*/
export interface ForwarderSwapQuoteExecutionOpts extends ForwarderSwapQuoteGetOutputOpts, SwapQuoteExecutionOptsBase {}
export type SwapQuote = MarketBuySwapQuote | MarketSellSwapQuote; export type SwapQuote = MarketBuySwapQuote | MarketSellSwapQuote;
/** /**
* feePercentage: percentage (up to 5%) of the taker asset paid to feeRecipient * takerAddress: The address to perform the buy. Defaults to the first available address from the provider.
* feeRecipient: address of the receiver of the feePercentage of taker asset * useConsumerType: If provided, defaults the SwapQuoteConsumer to create output consumed by ConsumerType.
* ethAmount: The amount of eth (in Wei) sent to the forwarder contract.
*/ */
export interface SwapQuoteGetOutputOpts extends ForwarderSwapQuoteGetOutputOpts { export interface SwapQuoteGetOutputOpts extends ForwarderSwapQuoteGetOutputOpts {
takerAddress?: string; takerAddress?: string;
useConsumerType?: ConsumerType; useConsumerType?: ConsumerType;
} }
/**
* Represents the options for executing a swap quote with ForwarderSwapQuoteConusmer
*/
export interface ForwarderSwapQuoteExecutionOpts extends ForwarderSwapQuoteGetOutputOpts, SwapQuoteExecutionOptsBase {} export interface ForwarderSwapQuoteExecutionOpts extends ForwarderSwapQuoteGetOutputOpts, SwapQuoteExecutionOptsBase {}
/**
* Represents the options for executing a swap quote with SwapQuoteConsumer
*/
export interface SwapQuoteExecutionOpts extends SwapQuoteGetOutputOpts, ForwarderSwapQuoteExecutionOpts {} export interface SwapQuoteExecutionOpts extends SwapQuoteGetOutputOpts, ForwarderSwapQuoteExecutionOpts {}
/** /**
* takerAssetData: String that represents a specific taker asset (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md). * takerAssetData: String that represents a specific taker asset (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* makerAssetData: String that represents a specific maker asset (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md). * makerAssetData: String that represents a specific maker asset (for more info: https://github.com/0xProject/0x-protocol-specification/blob/master/v2/v2-specification.md).
* makerAssetFillAmount: The amount of makerAsset to swap for.
* orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested assetBuyAmount plus slippage. * orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested assetBuyAmount plus slippage.
* feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above. * feeOrders: An array of objects conforming to SignedOrder. These orders can be used to cover the fees for the orders param above.
* bestCaseQuoteInfo: Info about the best case price for the asset. * bestCaseQuoteInfo: Info about the best case price for the asset.
@ -235,11 +226,19 @@ export interface SwapQuoteBase {
worstCaseQuoteInfo: SwapQuoteInfo; worstCaseQuoteInfo: SwapQuoteInfo;
} }
/**
* takerAssetFillAmount: The amount of takerAsset sold for makerAsset.
* type: Specified MarketOperation the SwapQuote is provided for
*/
export interface MarketSellSwapQuote extends SwapQuoteBase { export interface MarketSellSwapQuote extends SwapQuoteBase {
takerAssetFillAmount: BigNumber; takerAssetFillAmount: BigNumber;
type: MarketOperation.Sell; type: MarketOperation.Sell;
} }
/**
* makerAssetFillAmount: The amount of makerAsset bought with takerAsset.
* type: Specified MarketOperation the SwapQuote is provided for
*/
export interface MarketBuySwapQuote extends SwapQuoteBase { export interface MarketBuySwapQuote extends SwapQuoteBase {
makerAssetFillAmount: BigNumber; makerAssetFillAmount: BigNumber;
type: MarketOperation.Buy; type: MarketOperation.Buy;
@ -254,6 +253,7 @@ export interface MarketSellSwapQuoteWithAffiliateFee extends SwapQuoteWithAffili
export interface MarketBuySwapQuoteWithAffiliateFee extends SwapQuoteWithAffiliateFeeBase, MarketBuySwapQuote {} export interface MarketBuySwapQuoteWithAffiliateFee extends SwapQuoteWithAffiliateFeeBase, MarketBuySwapQuote {}
export type SwapQuoteWithAffiliateFee = MarketBuySwapQuoteWithAffiliateFee | MarketSellSwapQuoteWithAffiliateFee; export type SwapQuoteWithAffiliateFee = MarketBuySwapQuoteWithAffiliateFee | MarketSellSwapQuoteWithAffiliateFee;
/** /**
* assetEthAmount: The amount of eth required to pay for the requested asset. * assetEthAmount: The amount of eth required to pay for the requested asset.
* feeEthAmount: The amount of eth required to pay any fee concerned with completing the swap. * feeEthAmount: The amount of eth required to pay any fee concerned with completing the swap.
@ -268,6 +268,7 @@ export interface SwapQuoteInfo {
/** /**
* shouldForceOrderRefresh: If set to true, new orders and state will be fetched instead of waiting for the next orderRefreshIntervalMs. Defaults to false. * shouldForceOrderRefresh: If set to true, new orders and state will be fetched instead of waiting for the next orderRefreshIntervalMs. Defaults to false.
* shouldDisableRequestingFeeOrders: If set to true, requesting a swapQuote will not perform any computation or requests for fees.
* slippagePercentage: The percentage buffer to add to account for slippage. Affects max ETH price estimates. Defaults to 0.2 (20%). * slippagePercentage: The percentage buffer to add to account for slippage. Affects max ETH price estimates. Defaults to 0.2 (20%).
*/ */
export interface SwapQuoteRequestOpts { export interface SwapQuoteRequestOpts {

View File

@ -1,41 +0,0 @@
import { ContractWrappers, SupportedProvider, ZeroExProvider } from '@0x/contract-wrappers';
import { providerUtils } from '@0x/utils';
import * as _ from 'lodash';
import { constants } from '../constants';
import { SwapQuote, SwapQuoteUtilsOpts } from '../types';
import { assert } from '../utils/assert';
export class SwapQuoteUtils {
public readonly provider: ZeroExProvider;
public readonly networkId: number;
private readonly _contractWrappers: ContractWrappers;
constructor(supportedProvider: SupportedProvider, options: Partial<SwapQuoteUtilsOpts> = {}) {
const { networkId } = _.merge({}, constants.DEFAULT_SWAP_QUOTE_UTILS_OPTS, options);
assert.isNumber('networkId', networkId);
const provider = providerUtils.standardizeOrThrow(supportedProvider);
this.provider = provider;
this.networkId = networkId;
this._contractWrappers = new ContractWrappers(this.provider, {
networkId,
});
}
public async isTakerAddressAllowanceEnoughForBestAndWorstQuoteInfoAsync(
swapQuote: SwapQuote,
takerAddress: string,
): Promise<[boolean, boolean]> {
const orderValidatorWrapper = this._contractWrappers.orderValidator;
const balanceAndAllowance = await orderValidatorWrapper.getBalanceAndAllowanceAsync(
takerAddress,
swapQuote.takerAssetData,
);
return [
balanceAndAllowance.allowance.isGreaterThanOrEqualTo(swapQuote.bestCaseQuoteInfo.totalTakerTokenAmount),
balanceAndAllowance.allowance.isGreaterThanOrEqualTo(swapQuote.worstCaseQuoteInfo.totalTakerTokenAmount),
];
}
}