changed market-operation type to enum from literal

This commit is contained in:
David Sun 2019-07-15 15:29:13 -07:00
parent ac82b2622c
commit 542255332d
11 changed files with 50 additions and 44 deletions

View File

@ -12,6 +12,7 @@ import {
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000'; const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
const MAINNET_NETWORK_ID = 1; const MAINNET_NETWORK_ID = 1;
const ONE_SECOND_MS = 1000; const ONE_SECOND_MS = 1000;
const DEFAULT_PER_PAGE = 1000;
const DEFAULT_SWAP_QUOTER_OPTS: SwapQuoterOpts = { const DEFAULT_SWAP_QUOTER_OPTS: SwapQuoterOpts = {
networkId: MAINNET_NETWORK_ID, networkId: MAINNET_NETWORK_ID,
@ -48,4 +49,5 @@ export const constants = {
DEFAULT_FORWARDER_SWAP_QUOTE_EXECUTE_OPTS, DEFAULT_FORWARDER_SWAP_QUOTE_EXECUTE_OPTS,
DEFAULT_SWAP_QUOTE_REQUEST_OPTS, DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
EMPTY_ORDERS_AND_FILLABLE_AMOUNTS, EMPTY_ORDERS_AND_FILLABLE_AMOUNTS,
DEFAULT_PER_PAGE,
}; };

View File

@ -4,6 +4,7 @@ import { APIOrder, AssetPairsResponse, OrderbookResponse } from '@0x/types';
import { BigNumber } from '@0x/utils'; import { BigNumber } from '@0x/utils';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { constants } from '../constants';
import { import {
OrderProvider, OrderProvider,
OrderProviderRequest, OrderProviderRequest,
@ -117,9 +118,7 @@ export class StandardRelayerAPIOrderProvider implements OrderProvider {
* @return An array of asset data strings that can be used to purchased makerAssetData. * @return An array of asset data strings that can be used to purchased makerAssetData.
*/ */
public async getAvailableTakerAssetDatasAsync(makerAssetData: string): Promise<string[]> { public async getAvailableTakerAssetDatasAsync(makerAssetData: string): Promise<string[]> {
// Return a maximum of 1000 asset datas const requestOpts = { networkId: this.networkId, perPage: constants.DEFAULT_PER_PAGE };
const maxPerPage = 1000;
const requestOpts = { networkId: this.networkId, perPage: maxPerPage };
const assetPairsRequest = { assetDataA: makerAssetData }; const assetPairsRequest = { assetDataA: makerAssetData };
const fullRequest = { const fullRequest = {
...requestOpts, ...requestOpts,

View File

@ -1,4 +1,5 @@
import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers'; import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers';
import { MarketOperation } from '@0x/types';
import { AbiEncoder, providerUtils } from '@0x/utils'; import { AbiEncoder, providerUtils } from '@0x/utils';
import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper'; import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper';
import { MethodAbi } from 'ethereum-types'; import { MethodAbi } from 'ethereum-types';
@ -50,7 +51,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
const { orders, signatures } = params; const { orders, signatures } = params;
let args: any[]; let args: any[];
if (params.type === 'marketBuy') { if (params.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = params; const { makerAssetFillAmount } = params;
args = [orders, makerAssetFillAmount, signatures]; args = [orders, makerAssetFillAmount, signatures];
} else { } else {
@ -79,14 +80,14 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
let params: ExchangeSmartContractParams; let params: ExchangeSmartContractParams;
let methodName: string; let methodName: string;
if (quote.type === 'marketBuy') { if (quote.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quote; const { makerAssetFillAmount } = quote;
params = { params = {
orders, orders,
signatures, signatures,
makerAssetFillAmount, makerAssetFillAmount,
type: 'marketBuy', type: MarketOperation.Buy,
}; };
methodName = 'marketBuyOrders'; methodName = 'marketBuyOrders';
@ -97,7 +98,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
orders, orders,
signatures, signatures,
takerAssetFillAmount, takerAssetFillAmount,
type: 'marketSell', type: MarketOperation.Sell,
}; };
methodName = 'marketSellOrders'; methodName = 'marketSellOrders';
@ -139,7 +140,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
try { try {
let txHash: string; let txHash: string;
if (quote.type === 'marketBuy') { if (quote.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quote; const { makerAssetFillAmount } = quote;
txHash = await this._contractWrappers.exchange.marketBuyOrdersNoThrowAsync( txHash = await this._contractWrappers.exchange.marketBuyOrdersNoThrowAsync(
orders, orders,

View File

@ -1,4 +1,5 @@
import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers'; import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers';
import { MarketOperation } from '@0x/types';
import { AbiEncoder, providerUtils } from '@0x/utils'; import { AbiEncoder, providerUtils } from '@0x/utils';
import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper'; import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper';
import { MethodAbi } from 'ethereum-types'; import { MethodAbi } from 'ethereum-types';
@ -58,7 +59,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
const { orders, signatures, feeOrders, feeSignatures, feePercentage, feeRecipient } = params; const { orders, signatures, feeOrders, feeSignatures, feePercentage, feeRecipient } = params;
let args: any[]; let args: any[];
if (params.type === 'marketBuy') { if (params.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = params; const { makerAssetFillAmount } = params;
args = [orders, makerAssetFillAmount, signatures, feeOrders, feeSignatures, feePercentage, feeRecipient]; args = [orders, makerAssetFillAmount, signatures, feeOrders, feeSignatures, feePercentage, feeRecipient];
} else { } else {
@ -108,7 +109,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
let params: ForwarderSmartContractParams; let params: ForwarderSmartContractParams;
let methodName: string; let methodName: string;
if (quoteWithAffiliateFee.type === 'marketBuy') { if (quoteWithAffiliateFee.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quoteWithAffiliateFee; const { makerAssetFillAmount } = quoteWithAffiliateFee;
params = { params = {
@ -119,7 +120,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
feeSignatures, feeSignatures,
feePercentage, feePercentage,
feeRecipient, feeRecipient,
type: 'marketBuy', type: MarketOperation.Buy,
}; };
methodName = 'marketBuyOrdersWithEth'; methodName = 'marketBuyOrdersWithEth';
@ -131,7 +132,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
feeSignatures, feeSignatures,
feePercentage, feePercentage,
feeRecipient, feeRecipient,
type: 'marketSell', type: MarketOperation.Sell,
}; };
methodName = 'marketSellOrdersWithEth'; methodName = 'marketSellOrdersWithEth';
} }
@ -188,7 +189,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
try { try {
let txHash: string; let txHash: string;
if (quoteWithAffiliateFee.type === 'marketBuy') { if (quoteWithAffiliateFee.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quoteWithAffiliateFee; const { makerAssetFillAmount } = quoteWithAffiliateFee;
txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync( txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync(
orders, orders,

View File

@ -144,7 +144,7 @@ export class SwapQuoter {
makerAssetData, makerAssetData,
takerAssetData, takerAssetData,
takerAssetSellAmount, takerAssetSellAmount,
'marketSell', MarketOperation.Sell,
options, options,
)) as MarketSellSwapQuote; )) as MarketSellSwapQuote;
} }
@ -170,7 +170,7 @@ export class SwapQuoter {
makerAssetData, makerAssetData,
takerAssetData, takerAssetData,
makerAssetBuyAmount, makerAssetBuyAmount,
'marketBuy', MarketOperation.Buy,
options, options,
)) as MarketBuySwapQuote; )) as MarketBuySwapQuote;
} }
@ -423,7 +423,7 @@ export class SwapQuoter {
let swapQuote: SwapQuote; let swapQuote: SwapQuote;
if (marketOperation === 'marketBuy') { if (marketOperation === MarketOperation.Buy) {
swapQuote = swapQuoteCalculator.calculateMarketBuySwapQuote( swapQuote = swapQuoteCalculator.calculateMarketBuySwapQuote(
ordersAndFillableAmounts, ordersAndFillableAmounts,
feeOrdersAndFillableAmounts, feeOrdersAndFillableAmounts,

View File

@ -1,4 +1,4 @@
import { SignedOrder } from '@0x/types'; import { MarketOperation, SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils'; import { BigNumber } from '@0x/utils';
import { MethodAbi } from 'ethereum-types'; import { MethodAbi } from 'ethereum-types';
@ -82,7 +82,7 @@ export interface SmartContractParamsBase {
*/ */
export interface ExchangeMarketBuySmartContractParams extends SmartContractParamsBase { export interface ExchangeMarketBuySmartContractParams extends SmartContractParamsBase {
makerAssetFillAmount: BigNumber; makerAssetFillAmount: BigNumber;
type: 'marketBuy'; type: MarketOperation.Buy;
} }
/** /**
@ -91,7 +91,7 @@ export interface ExchangeMarketBuySmartContractParams extends SmartContractParam
*/ */
export interface ExchangeMarketSellSmartContractParams extends SmartContractParamsBase { export interface ExchangeMarketSellSmartContractParams extends SmartContractParamsBase {
takerAssetFillAmount: BigNumber; takerAssetFillAmount: BigNumber;
type: 'marketSell'; type: MarketOperation.Sell;
} }
/** /**
@ -227,12 +227,12 @@ export interface SwapQuoteBase {
export interface MarketSellSwapQuote extends SwapQuoteBase { export interface MarketSellSwapQuote extends SwapQuoteBase {
takerAssetFillAmount: BigNumber; takerAssetFillAmount: BigNumber;
type: 'marketSell'; type: MarketOperation.Sell;
} }
export interface MarketBuySwapQuote extends SwapQuoteBase { export interface MarketBuySwapQuote extends SwapQuoteBase {
makerAssetFillAmount: BigNumber; makerAssetFillAmount: BigNumber;
type: 'marketBuy'; type: MarketOperation.Buy;
} }
export interface SwapQuoteWithAffiliateFeeBase { export interface SwapQuoteWithAffiliateFeeBase {

View File

@ -1,6 +1,6 @@
import { assert as sharedAssert } from '@0x/assert'; import { assert as sharedAssert } from '@0x/assert';
import { schemas } from '@0x/json-schemas'; import { schemas } from '@0x/json-schemas';
import { SignedOrder } from '@0x/types'; import { MarketOperation, SignedOrder } from '@0x/types';
import * as _ from 'lodash'; import * as _ from 'lodash';
import { OrderProvider, OrderProviderRequest, SwapQuote, SwapQuoteInfo } from '../types'; import { OrderProvider, OrderProviderRequest, SwapQuote, SwapQuoteInfo } from '../types';
@ -14,7 +14,7 @@ export const assert = {
sharedAssert.doesConformToSchema(`${variableName}.feeOrders`, swapQuote.feeOrders, schemas.signedOrdersSchema); sharedAssert.doesConformToSchema(`${variableName}.feeOrders`, swapQuote.feeOrders, schemas.signedOrdersSchema);
assert.isValidSwapQuoteInfo(`${variableName}.bestCaseQuoteInfo`, swapQuote.bestCaseQuoteInfo); assert.isValidSwapQuoteInfo(`${variableName}.bestCaseQuoteInfo`, swapQuote.bestCaseQuoteInfo);
assert.isValidSwapQuoteInfo(`${variableName}.worstCaseQuoteInfo`, swapQuote.worstCaseQuoteInfo); assert.isValidSwapQuoteInfo(`${variableName}.worstCaseQuoteInfo`, swapQuote.worstCaseQuoteInfo);
if (swapQuote.type === 'marketBuy') { if (swapQuote.type === MarketOperation.Buy) {
sharedAssert.isBigNumber(`${variableName}.makerAssetFillAmount`, swapQuote.makerAssetFillAmount); sharedAssert.isBigNumber(`${variableName}.makerAssetFillAmount`, swapQuote.makerAssetFillAmount);
} else { } else {
sharedAssert.isBigNumber(`${variableName}.takerAssetFillAmount`, swapQuote.takerAssetFillAmount); sharedAssert.isBigNumber(`${variableName}.takerAssetFillAmount`, swapQuote.takerAssetFillAmount);

View File

@ -29,7 +29,7 @@ export const swapQuoteCalculator = {
takerAssetFillAmount, takerAssetFillAmount,
slippagePercentage, slippagePercentage,
isMakerAssetZrxToken, isMakerAssetZrxToken,
'marketSell', MarketOperation.Sell,
) as MarketSellSwapQuote; ) as MarketSellSwapQuote;
}, },
calculateMarketBuySwapQuote( calculateMarketBuySwapQuote(
@ -45,7 +45,7 @@ export const swapQuoteCalculator = {
makerAssetFillAmount, makerAssetFillAmount,
slippagePercentage, slippagePercentage,
isMakerAssetZrxToken, isMakerAssetZrxToken,
'marketBuy', MarketOperation.Buy,
) as MarketBuySwapQuote; ) as MarketBuySwapQuote;
}, },
}; };
@ -74,7 +74,7 @@ function calculateSwapQuote(
let remainingFillAmount: BigNumber; let remainingFillAmount: BigNumber;
let ordersRemainingFillableMakerAssetAmounts: BigNumber[]; let ordersRemainingFillableMakerAssetAmounts: BigNumber[];
if (marketOperation === 'marketBuy') { if (marketOperation === MarketOperation.Buy) {
// find the orders that cover the desired assetBuyAmount (with slippage) // find the orders that cover the desired assetBuyAmount (with slippage)
({ ({
resultOrders, resultOrders,
@ -185,16 +185,16 @@ function calculateSwapQuote(
worstCaseQuoteInfo, worstCaseQuoteInfo,
}; };
if (marketOperation === 'marketBuy') { if (marketOperation === MarketOperation.Buy) {
return { return {
...quoteBase, ...quoteBase,
type: 'marketBuy', type: MarketOperation.Buy,
makerAssetFillAmount: assetFillAmount, makerAssetFillAmount: assetFillAmount,
}; };
} else { } else {
return { return {
...quoteBase, ...quoteBase,
type: 'marketSell', type: MarketOperation.Sell,
takerAssetFillAmount: assetFillAmount, takerAssetFillAmount: assetFillAmount,
}; };
} }
@ -208,12 +208,12 @@ function calculateQuoteInfo(
marketOperation: MarketOperation, marketOperation: MarketOperation,
): SwapQuoteInfo { ): SwapQuoteInfo {
// find the total eth and zrx needed to buy assetAmount from the resultOrders from left to right // find the total eth and zrx needed to buy assetAmount from the resultOrders from left to right
let makerTokenAmount = marketOperation === 'marketBuy' ? tokenAmount : constants.ZERO_AMOUNT; let makerTokenAmount = marketOperation === MarketOperation.Buy ? tokenAmount : constants.ZERO_AMOUNT;
let takerTokenAmount = marketOperation === 'marketSell' ? tokenAmount : constants.ZERO_AMOUNT; let takerTokenAmount = marketOperation === MarketOperation.Sell ? tokenAmount : constants.ZERO_AMOUNT;
let zrxTakerTokenAmount = constants.ZERO_AMOUNT; let zrxTakerTokenAmount = constants.ZERO_AMOUNT;
if (isMakerAssetZrxToken) { if (isMakerAssetZrxToken) {
if (marketOperation === 'marketBuy') { if (marketOperation === MarketOperation.Buy) {
takerTokenAmount = findTakerTokenAmountNeededToBuyZrx(ordersAndFillableAmounts, makerTokenAmount); takerTokenAmount = findTakerTokenAmountNeededToBuyZrx(ordersAndFillableAmounts, makerTokenAmount);
} else { } else {
makerTokenAmount = findZrxTokenAmountFromSellingTakerTokenAmount( makerTokenAmount = findZrxTokenAmountFromSellingTakerTokenAmount(
@ -223,15 +223,15 @@ function calculateQuoteInfo(
} }
} else { } else {
const findTokenAndZrxAmount = const findTokenAndZrxAmount =
marketOperation === 'marketBuy' marketOperation === MarketOperation.Buy
? findTakerTokenAndZrxAmountNeededToBuyAsset ? findTakerTokenAndZrxAmountNeededToBuyAsset
: findMakerTokenAmountReceivedAndZrxAmountNeededToSellAsset; : findMakerTokenAmountReceivedAndZrxAmountNeededToSellAsset;
// find eth and zrx amounts needed to buy // find eth and zrx amounts needed to buy
const tokenAndZrxAmountToBuyAsset = findTokenAndZrxAmount( const tokenAndZrxAmountToBuyAsset = findTokenAndZrxAmount(
ordersAndFillableAmounts, ordersAndFillableAmounts,
marketOperation === 'marketBuy' ? makerTokenAmount : takerTokenAmount, marketOperation === MarketOperation.Buy ? makerTokenAmount : takerTokenAmount,
); );
if (marketOperation === 'marketBuy') { if (marketOperation === MarketOperation.Buy) {
takerTokenAmount = tokenAndZrxAmountToBuyAsset[0]; takerTokenAmount = tokenAndZrxAmountToBuyAsset[0];
} else { } else {
makerTokenAmount = tokenAndZrxAmountToBuyAsset[0]; makerTokenAmount = tokenAndZrxAmountToBuyAsset[0];

View File

@ -57,16 +57,16 @@ export const getFullyFillableSwapQuoteWithNoFees = (
worstCaseQuoteInfo: quoteInfo, worstCaseQuoteInfo: quoteInfo,
}; };
if (operation === 'marketBuy') { if (operation === MarketOperation.Buy) {
return { return {
...quoteBase, ...quoteBase,
type: 'marketBuy', type: MarketOperation.Buy,
makerAssetFillAmount, makerAssetFillAmount,
}; };
} else { } else {
return { return {
...quoteBase, ...quoteBase,
type: 'marketSell', type: MarketOperation.Sell,
takerAssetFillAmount: totalTakerTokenAmount, takerAssetFillAmount: totalTakerTokenAmount,
}; };
} }

View File

@ -23,7 +23,7 @@ export const marketUtils = {
return findOrdersThatCoverAssetFillAmount<T>( return findOrdersThatCoverAssetFillAmount<T>(
orders, orders,
takerAssetFillAmount, takerAssetFillAmount,
'marketSell', MarketOperation.Sell,
opts, opts,
) as OrdersAndRemainingTakerFillAmount<T>; ) as OrdersAndRemainingTakerFillAmount<T>;
}, },
@ -45,7 +45,7 @@ export const marketUtils = {
return findOrdersThatCoverAssetFillAmount<T>( return findOrdersThatCoverAssetFillAmount<T>(
orders, orders,
makerAssetFillAmount, makerAssetFillAmount,
'marketBuy', MarketOperation.Buy,
opts, opts,
) as OrdersAndRemainingMakerFillAmount<T>; ) as OrdersAndRemainingMakerFillAmount<T>;
}, },
@ -133,14 +133,14 @@ function findOrdersThatCoverAssetFillAmount<T extends Order>(
operation: MarketOperation, operation: MarketOperation,
opts?: FindOrdersThatCoverTakerAssetFillAmountOpts | FindOrdersThatCoverMakerAssetFillAmountOpts, opts?: FindOrdersThatCoverTakerAssetFillAmountOpts | FindOrdersThatCoverMakerAssetFillAmountOpts,
): OrdersAndRemainingTakerFillAmount<T> | OrdersAndRemainingMakerFillAmount<T> { ): OrdersAndRemainingTakerFillAmount<T> | OrdersAndRemainingMakerFillAmount<T> {
const variablePrefix = operation === 'marketBuy' ? 'Maker' : 'Taker'; const variablePrefix = operation === MarketOperation.Buy ? 'Maker' : 'Taker';
assert.doesConformToSchema('orders', orders, schemas.ordersSchema); assert.doesConformToSchema('orders', orders, schemas.ordersSchema);
assert.isValidBaseUnitAmount('assetFillAmount', assetFillAmount); assert.isValidBaseUnitAmount('assetFillAmount', assetFillAmount);
// try to get remainingFillableTakerAssetAmounts from opts, if it's not there, use takerAssetAmount values from orders // try to get remainingFillableTakerAssetAmounts from opts, if it's not there, use takerAssetAmount values from orders
const remainingFillableAssetAmounts = _.get( const remainingFillableAssetAmounts = _.get(
opts, opts,
`remainingFillable${variablePrefix}AssetAmounts`, `remainingFillable${variablePrefix}AssetAmounts`,
_.map(orders, order => (operation === 'marketBuy' ? order.makerAssetAmount : order.takerAssetAmount)), _.map(orders, order => (operation === MarketOperation.Buy ? order.makerAssetAmount : order.takerAssetAmount)),
) as BigNumber[]; ) as BigNumber[];
_.forEach(remainingFillableAssetAmounts, (amount, index) => _.forEach(remainingFillableAssetAmounts, (amount, index) =>
assert.isValidBaseUnitAmount(`remainingFillable${variablePrefix}AssetAmount[${index}]`, amount), assert.isValidBaseUnitAmount(`remainingFillable${variablePrefix}AssetAmount[${index}]`, amount),
@ -194,7 +194,7 @@ function findOrdersThatCoverAssetFillAmount<T extends Order>(
...ordersAndRemainingFillAmount ...ordersAndRemainingFillAmount
} = result; } = result;
if (operation === 'marketBuy') { if (operation === MarketOperation.Buy) {
return { return {
...ordersAndRemainingFillAmount, ...ordersAndRemainingFillAmount,
ordersRemainingFillableMakerAssetAmounts: resultOrdersRemainingFillableAssetAmounts, ordersRemainingFillableMakerAssetAmounts: resultOrdersRemainingFillableAssetAmounts,

View File

@ -41,7 +41,10 @@ export interface SignedOrder extends Order {
signature: string; signature: string;
} }
export type MarketOperation = 'marketBuy' | 'marketSell'; export enum MarketOperation {
Sell = 'Sell',
Buy = 'Buy',
}
/** /**
* ZeroExTransaction for use with 0x Exchange executeTransaction * ZeroExTransaction for use with 0x Exchange executeTransaction