utils moved to SwapQuoter and polished Doc Strings
This commit is contained in:
parent
88ff38eca6
commit
dcf4eb2aaf
@ -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';
|
||||
|
@ -76,8 +76,6 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
|
||||
const { orders } = quote;
|
||||
|
||||
const optimizedOrders = calldataOptimizationUtils.optimizeForwarderOrders(orders);
|
||||
|
||||
const signatures = _.map(orders, o => o.signature);
|
||||
|
||||
let params: ExchangeSmartContractParams;
|
||||
@ -87,7 +85,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
const { makerAssetFillAmount } = quote;
|
||||
|
||||
params = {
|
||||
orders: optimizedOrders,
|
||||
orders,
|
||||
signatures,
|
||||
makerAssetFillAmount,
|
||||
type: MarketOperation.Buy,
|
||||
@ -98,7 +96,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
const { takerAssetFillAmount } = quote;
|
||||
|
||||
params = {
|
||||
orders: optimizedOrders,
|
||||
orders,
|
||||
signatures,
|
||||
takerAssetFillAmount,
|
||||
type: MarketOperation.Sell,
|
||||
|
@ -373,6 +373,21 @@ export class SwapQuoter {
|
||||
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.
|
||||
* Will throw if ZRX does not exist for the current network.
|
||||
|
@ -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 {
|
||||
|
@ -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),
|
||||
];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user