diff --git a/packages/asset-swapper/src/index.ts b/packages/asset-swapper/src/index.ts index f6de50303e..af5ab2c12a 100644 --- a/packages/asset-swapper/src/index.ts +++ b/packages/asset-swapper/src/index.ts @@ -23,7 +23,6 @@ export { export { SignedOrder } from '@0x/types'; export { BigNumber } from '@0x/utils'; -export { SwapQuoteUtils } from './utils/swap_quote_utils'; export { SwapQuoteConsumer } from './quote_consumers/swap_quote_consumer'; export { SwapQuoter } from './swap_quoter'; export { InsufficientAssetLiquidityError } from './errors'; diff --git a/packages/asset-swapper/src/quote_consumers/exchange_swap_quote_consumer.ts b/packages/asset-swapper/src/quote_consumers/exchange_swap_quote_consumer.ts index 23d26dfdcd..a88b383310 100644 --- a/packages/asset-swapper/src/quote_consumers/exchange_swap_quote_consumer.ts +++ b/packages/asset-swapper/src/quote_consumers/exchange_swap_quote_consumer.ts @@ -76,8 +76,6 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase o.signature); let params: ExchangeSmartContractParams; @@ -87,7 +85,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase { + 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. * Will throw if ZRX does not exist for the current network. diff --git a/packages/asset-swapper/src/types.ts b/packages/asset-swapper/src/types.ts index d3ec592da2..0d76d285c9 100644 --- a/packages/asset-swapper/src/types.ts +++ b/packages/asset-swapper/src/types.ts @@ -94,9 +94,11 @@ export interface ExchangeMarketSellSmartContractParams extends SmartContractPara type: MarketOperation.Sell; } +/** + * Represents the varying smart contracts that can consume a valid swap quote + */ export enum ConsumerType { - Forwarder, - Exchange, + Forwarder, Exchange, } /** @@ -162,10 +164,6 @@ export interface SwapQuoteConsumerOpts { networkId: number; } -export interface SwapQuoteUtilsOpts { - networkId: number; -} - /** * Represents the options provided to a generic SwapQuoteConsumer */ @@ -193,34 +191,27 @@ export interface ForwarderSwapQuoteGetOutputOpts extends SwapQuoteGetOutputOptsB ethAmount?: BigNumber; } -/** - * Represents the options for executing a swap quote with ForwarderSwapQuoteConusmer - */ -export interface ForwarderSwapQuoteExecutionOpts extends ForwarderSwapQuoteGetOutputOpts, SwapQuoteExecutionOptsBase {} - export type SwapQuote = MarketBuySwapQuote | MarketSellSwapQuote; /** - * feePercentage: percentage (up to 5%) of the taker asset paid to feeRecipient - * feeRecipient: address of the receiver of the feePercentage of taker asset - * ethAmount: The amount of eth (in Wei) sent to the forwarder contract. + * takerAddress: The address to perform the buy. Defaults to the first available address from the provider. + * useConsumerType: If provided, defaults the SwapQuoteConsumer to create output consumed by ConsumerType. */ export interface SwapQuoteGetOutputOpts extends ForwarderSwapQuoteGetOutputOpts { takerAddress?: string; useConsumerType?: ConsumerType; } -/** - * Represents the options for executing a swap quote with ForwarderSwapQuoteConusmer - */ export interface ForwarderSwapQuoteExecutionOpts extends ForwarderSwapQuoteGetOutputOpts, SwapQuoteExecutionOptsBase {} +/** + * Represents the options for executing a swap quote with SwapQuoteConsumer + */ 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). * 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. * 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. @@ -235,11 +226,19 @@ export interface SwapQuoteBase { worstCaseQuoteInfo: SwapQuoteInfo; } +/** + * takerAssetFillAmount: The amount of takerAsset sold for makerAsset. + * type: Specified MarketOperation the SwapQuote is provided for + */ export interface MarketSellSwapQuote extends SwapQuoteBase { takerAssetFillAmount: BigNumber; type: MarketOperation.Sell; } +/** + * makerAssetFillAmount: The amount of makerAsset bought with takerAsset. + * type: Specified MarketOperation the SwapQuote is provided for + */ export interface MarketBuySwapQuote extends SwapQuoteBase { makerAssetFillAmount: BigNumber; type: MarketOperation.Buy; @@ -254,6 +253,7 @@ export interface MarketSellSwapQuoteWithAffiliateFee extends SwapQuoteWithAffili export interface MarketBuySwapQuoteWithAffiliateFee extends SwapQuoteWithAffiliateFeeBase, MarketBuySwapQuote {} export type SwapQuoteWithAffiliateFee = MarketBuySwapQuoteWithAffiliateFee | MarketSellSwapQuoteWithAffiliateFee; + /** * 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. @@ -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. + * 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%). */ export interface SwapQuoteRequestOpts { diff --git a/packages/asset-swapper/src/utils/swap_quote_utils.ts b/packages/asset-swapper/src/utils/swap_quote_utils.ts deleted file mode 100644 index 6ea855aa10..0000000000 --- a/packages/asset-swapper/src/utils/swap_quote_utils.ts +++ /dev/null @@ -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 = {}) { - 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), - ]; - } -}