changed market-operation type to enum from literal
This commit is contained in:
parent
ac82b2622c
commit
542255332d
@ -12,6 +12,7 @@ import {
|
||||
const NULL_ADDRESS = '0x0000000000000000000000000000000000000000';
|
||||
const MAINNET_NETWORK_ID = 1;
|
||||
const ONE_SECOND_MS = 1000;
|
||||
const DEFAULT_PER_PAGE = 1000;
|
||||
|
||||
const DEFAULT_SWAP_QUOTER_OPTS: SwapQuoterOpts = {
|
||||
networkId: MAINNET_NETWORK_ID,
|
||||
@ -48,4 +49,5 @@ export const constants = {
|
||||
DEFAULT_FORWARDER_SWAP_QUOTE_EXECUTE_OPTS,
|
||||
DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
|
||||
EMPTY_ORDERS_AND_FILLABLE_AMOUNTS,
|
||||
DEFAULT_PER_PAGE,
|
||||
};
|
||||
|
@ -4,6 +4,7 @@ import { APIOrder, AssetPairsResponse, OrderbookResponse } from '@0x/types';
|
||||
import { BigNumber } from '@0x/utils';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { constants } from '../constants';
|
||||
import {
|
||||
OrderProvider,
|
||||
OrderProviderRequest,
|
||||
@ -117,9 +118,7 @@ export class StandardRelayerAPIOrderProvider implements OrderProvider {
|
||||
* @return An array of asset data strings that can be used to purchased makerAssetData.
|
||||
*/
|
||||
public async getAvailableTakerAssetDatasAsync(makerAssetData: string): Promise<string[]> {
|
||||
// Return a maximum of 1000 asset datas
|
||||
const maxPerPage = 1000;
|
||||
const requestOpts = { networkId: this.networkId, perPage: maxPerPage };
|
||||
const requestOpts = { networkId: this.networkId, perPage: constants.DEFAULT_PER_PAGE };
|
||||
const assetPairsRequest = { assetDataA: makerAssetData };
|
||||
const fullRequest = {
|
||||
...requestOpts,
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers';
|
||||
import { MarketOperation } from '@0x/types';
|
||||
import { AbiEncoder, providerUtils } from '@0x/utils';
|
||||
import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper';
|
||||
import { MethodAbi } from 'ethereum-types';
|
||||
@ -50,7 +51,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
|
||||
const { orders, signatures } = params;
|
||||
let args: any[];
|
||||
if (params.type === 'marketBuy') {
|
||||
if (params.type === MarketOperation.Buy) {
|
||||
const { makerAssetFillAmount } = params;
|
||||
args = [orders, makerAssetFillAmount, signatures];
|
||||
} else {
|
||||
@ -79,14 +80,14 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
let params: ExchangeSmartContractParams;
|
||||
let methodName: string;
|
||||
|
||||
if (quote.type === 'marketBuy') {
|
||||
if (quote.type === MarketOperation.Buy) {
|
||||
const { makerAssetFillAmount } = quote;
|
||||
|
||||
params = {
|
||||
orders,
|
||||
signatures,
|
||||
makerAssetFillAmount,
|
||||
type: 'marketBuy',
|
||||
type: MarketOperation.Buy,
|
||||
};
|
||||
|
||||
methodName = 'marketBuyOrders';
|
||||
@ -97,7 +98,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
orders,
|
||||
signatures,
|
||||
takerAssetFillAmount,
|
||||
type: 'marketSell',
|
||||
type: MarketOperation.Sell,
|
||||
};
|
||||
|
||||
methodName = 'marketSellOrders';
|
||||
@ -139,7 +140,7 @@ export class ExchangeSwapQuoteConsumer implements SwapQuoteConsumerBase<Exchange
|
||||
|
||||
try {
|
||||
let txHash: string;
|
||||
if (quote.type === 'marketBuy') {
|
||||
if (quote.type === MarketOperation.Buy) {
|
||||
const { makerAssetFillAmount } = quote;
|
||||
txHash = await this._contractWrappers.exchange.marketBuyOrdersNoThrowAsync(
|
||||
orders,
|
||||
|
@ -1,4 +1,5 @@
|
||||
import { ContractWrappers, ContractWrappersError, ForwarderWrapperError } from '@0x/contract-wrappers';
|
||||
import { MarketOperation } from '@0x/types';
|
||||
import { AbiEncoder, providerUtils } from '@0x/utils';
|
||||
import { SupportedProvider, ZeroExProvider } from '@0x/web3-wrapper';
|
||||
import { MethodAbi } from 'ethereum-types';
|
||||
@ -58,7 +59,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
|
||||
const { orders, signatures, feeOrders, feeSignatures, feePercentage, feeRecipient } = params;
|
||||
|
||||
let args: any[];
|
||||
if (params.type === 'marketBuy') {
|
||||
if (params.type === MarketOperation.Buy) {
|
||||
const { makerAssetFillAmount } = params;
|
||||
args = [orders, makerAssetFillAmount, signatures, feeOrders, feeSignatures, feePercentage, feeRecipient];
|
||||
} else {
|
||||
@ -108,7 +109,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
|
||||
let params: ForwarderSmartContractParams;
|
||||
let methodName: string;
|
||||
|
||||
if (quoteWithAffiliateFee.type === 'marketBuy') {
|
||||
if (quoteWithAffiliateFee.type === MarketOperation.Buy) {
|
||||
const { makerAssetFillAmount } = quoteWithAffiliateFee;
|
||||
|
||||
params = {
|
||||
@ -119,7 +120,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
|
||||
feeSignatures,
|
||||
feePercentage,
|
||||
feeRecipient,
|
||||
type: 'marketBuy',
|
||||
type: MarketOperation.Buy,
|
||||
};
|
||||
|
||||
methodName = 'marketBuyOrdersWithEth';
|
||||
@ -131,7 +132,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
|
||||
feeSignatures,
|
||||
feePercentage,
|
||||
feeRecipient,
|
||||
type: 'marketSell',
|
||||
type: MarketOperation.Sell,
|
||||
};
|
||||
methodName = 'marketSellOrdersWithEth';
|
||||
}
|
||||
@ -188,7 +189,7 @@ export class ForwarderSwapQuoteConsumer implements SwapQuoteConsumerBase<Forward
|
||||
|
||||
try {
|
||||
let txHash: string;
|
||||
if (quoteWithAffiliateFee.type === 'marketBuy') {
|
||||
if (quoteWithAffiliateFee.type === MarketOperation.Buy) {
|
||||
const { makerAssetFillAmount } = quoteWithAffiliateFee;
|
||||
txHash = await this._contractWrappers.forwarder.marketBuyOrdersWithEthAsync(
|
||||
orders,
|
||||
|
@ -144,7 +144,7 @@ export class SwapQuoter {
|
||||
makerAssetData,
|
||||
takerAssetData,
|
||||
takerAssetSellAmount,
|
||||
'marketSell',
|
||||
MarketOperation.Sell,
|
||||
options,
|
||||
)) as MarketSellSwapQuote;
|
||||
}
|
||||
@ -170,7 +170,7 @@ export class SwapQuoter {
|
||||
makerAssetData,
|
||||
takerAssetData,
|
||||
makerAssetBuyAmount,
|
||||
'marketBuy',
|
||||
MarketOperation.Buy,
|
||||
options,
|
||||
)) as MarketBuySwapQuote;
|
||||
}
|
||||
@ -423,7 +423,7 @@ export class SwapQuoter {
|
||||
|
||||
let swapQuote: SwapQuote;
|
||||
|
||||
if (marketOperation === 'marketBuy') {
|
||||
if (marketOperation === MarketOperation.Buy) {
|
||||
swapQuote = swapQuoteCalculator.calculateMarketBuySwapQuote(
|
||||
ordersAndFillableAmounts,
|
||||
feeOrdersAndFillableAmounts,
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { SignedOrder } from '@0x/types';
|
||||
import { MarketOperation, SignedOrder } from '@0x/types';
|
||||
import { BigNumber } from '@0x/utils';
|
||||
import { MethodAbi } from 'ethereum-types';
|
||||
|
||||
@ -82,7 +82,7 @@ export interface SmartContractParamsBase {
|
||||
*/
|
||||
export interface ExchangeMarketBuySmartContractParams extends SmartContractParamsBase {
|
||||
makerAssetFillAmount: BigNumber;
|
||||
type: 'marketBuy';
|
||||
type: MarketOperation.Buy;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -91,7 +91,7 @@ export interface ExchangeMarketBuySmartContractParams extends SmartContractParam
|
||||
*/
|
||||
export interface ExchangeMarketSellSmartContractParams extends SmartContractParamsBase {
|
||||
takerAssetFillAmount: BigNumber;
|
||||
type: 'marketSell';
|
||||
type: MarketOperation.Sell;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -227,12 +227,12 @@ export interface SwapQuoteBase {
|
||||
|
||||
export interface MarketSellSwapQuote extends SwapQuoteBase {
|
||||
takerAssetFillAmount: BigNumber;
|
||||
type: 'marketSell';
|
||||
type: MarketOperation.Sell;
|
||||
}
|
||||
|
||||
export interface MarketBuySwapQuote extends SwapQuoteBase {
|
||||
makerAssetFillAmount: BigNumber;
|
||||
type: 'marketBuy';
|
||||
type: MarketOperation.Buy;
|
||||
}
|
||||
|
||||
export interface SwapQuoteWithAffiliateFeeBase {
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { assert as sharedAssert } from '@0x/assert';
|
||||
import { schemas } from '@0x/json-schemas';
|
||||
import { SignedOrder } from '@0x/types';
|
||||
import { MarketOperation, SignedOrder } from '@0x/types';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { OrderProvider, OrderProviderRequest, SwapQuote, SwapQuoteInfo } from '../types';
|
||||
@ -14,7 +14,7 @@ export const assert = {
|
||||
sharedAssert.doesConformToSchema(`${variableName}.feeOrders`, swapQuote.feeOrders, schemas.signedOrdersSchema);
|
||||
assert.isValidSwapQuoteInfo(`${variableName}.bestCaseQuoteInfo`, swapQuote.bestCaseQuoteInfo);
|
||||
assert.isValidSwapQuoteInfo(`${variableName}.worstCaseQuoteInfo`, swapQuote.worstCaseQuoteInfo);
|
||||
if (swapQuote.type === 'marketBuy') {
|
||||
if (swapQuote.type === MarketOperation.Buy) {
|
||||
sharedAssert.isBigNumber(`${variableName}.makerAssetFillAmount`, swapQuote.makerAssetFillAmount);
|
||||
} else {
|
||||
sharedAssert.isBigNumber(`${variableName}.takerAssetFillAmount`, swapQuote.takerAssetFillAmount);
|
||||
|
@ -29,7 +29,7 @@ export const swapQuoteCalculator = {
|
||||
takerAssetFillAmount,
|
||||
slippagePercentage,
|
||||
isMakerAssetZrxToken,
|
||||
'marketSell',
|
||||
MarketOperation.Sell,
|
||||
) as MarketSellSwapQuote;
|
||||
},
|
||||
calculateMarketBuySwapQuote(
|
||||
@ -45,7 +45,7 @@ export const swapQuoteCalculator = {
|
||||
makerAssetFillAmount,
|
||||
slippagePercentage,
|
||||
isMakerAssetZrxToken,
|
||||
'marketBuy',
|
||||
MarketOperation.Buy,
|
||||
) as MarketBuySwapQuote;
|
||||
},
|
||||
};
|
||||
@ -74,7 +74,7 @@ function calculateSwapQuote(
|
||||
let remainingFillAmount: BigNumber;
|
||||
let ordersRemainingFillableMakerAssetAmounts: BigNumber[];
|
||||
|
||||
if (marketOperation === 'marketBuy') {
|
||||
if (marketOperation === MarketOperation.Buy) {
|
||||
// find the orders that cover the desired assetBuyAmount (with slippage)
|
||||
({
|
||||
resultOrders,
|
||||
@ -185,16 +185,16 @@ function calculateSwapQuote(
|
||||
worstCaseQuoteInfo,
|
||||
};
|
||||
|
||||
if (marketOperation === 'marketBuy') {
|
||||
if (marketOperation === MarketOperation.Buy) {
|
||||
return {
|
||||
...quoteBase,
|
||||
type: 'marketBuy',
|
||||
type: MarketOperation.Buy,
|
||||
makerAssetFillAmount: assetFillAmount,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
...quoteBase,
|
||||
type: 'marketSell',
|
||||
type: MarketOperation.Sell,
|
||||
takerAssetFillAmount: assetFillAmount,
|
||||
};
|
||||
}
|
||||
@ -208,12 +208,12 @@ function calculateQuoteInfo(
|
||||
marketOperation: MarketOperation,
|
||||
): SwapQuoteInfo {
|
||||
// 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 takerTokenAmount = marketOperation === 'marketSell' ? tokenAmount : constants.ZERO_AMOUNT;
|
||||
let makerTokenAmount = marketOperation === MarketOperation.Buy ? tokenAmount : constants.ZERO_AMOUNT;
|
||||
let takerTokenAmount = marketOperation === MarketOperation.Sell ? tokenAmount : constants.ZERO_AMOUNT;
|
||||
let zrxTakerTokenAmount = constants.ZERO_AMOUNT;
|
||||
|
||||
if (isMakerAssetZrxToken) {
|
||||
if (marketOperation === 'marketBuy') {
|
||||
if (marketOperation === MarketOperation.Buy) {
|
||||
takerTokenAmount = findTakerTokenAmountNeededToBuyZrx(ordersAndFillableAmounts, makerTokenAmount);
|
||||
} else {
|
||||
makerTokenAmount = findZrxTokenAmountFromSellingTakerTokenAmount(
|
||||
@ -223,15 +223,15 @@ function calculateQuoteInfo(
|
||||
}
|
||||
} else {
|
||||
const findTokenAndZrxAmount =
|
||||
marketOperation === 'marketBuy'
|
||||
marketOperation === MarketOperation.Buy
|
||||
? findTakerTokenAndZrxAmountNeededToBuyAsset
|
||||
: findMakerTokenAmountReceivedAndZrxAmountNeededToSellAsset;
|
||||
// find eth and zrx amounts needed to buy
|
||||
const tokenAndZrxAmountToBuyAsset = findTokenAndZrxAmount(
|
||||
ordersAndFillableAmounts,
|
||||
marketOperation === 'marketBuy' ? makerTokenAmount : takerTokenAmount,
|
||||
marketOperation === MarketOperation.Buy ? makerTokenAmount : takerTokenAmount,
|
||||
);
|
||||
if (marketOperation === 'marketBuy') {
|
||||
if (marketOperation === MarketOperation.Buy) {
|
||||
takerTokenAmount = tokenAndZrxAmountToBuyAsset[0];
|
||||
} else {
|
||||
makerTokenAmount = tokenAndZrxAmountToBuyAsset[0];
|
||||
|
@ -57,16 +57,16 @@ export const getFullyFillableSwapQuoteWithNoFees = (
|
||||
worstCaseQuoteInfo: quoteInfo,
|
||||
};
|
||||
|
||||
if (operation === 'marketBuy') {
|
||||
if (operation === MarketOperation.Buy) {
|
||||
return {
|
||||
...quoteBase,
|
||||
type: 'marketBuy',
|
||||
type: MarketOperation.Buy,
|
||||
makerAssetFillAmount,
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
...quoteBase,
|
||||
type: 'marketSell',
|
||||
type: MarketOperation.Sell,
|
||||
takerAssetFillAmount: totalTakerTokenAmount,
|
||||
};
|
||||
}
|
||||
|
@ -23,7 +23,7 @@ export const marketUtils = {
|
||||
return findOrdersThatCoverAssetFillAmount<T>(
|
||||
orders,
|
||||
takerAssetFillAmount,
|
||||
'marketSell',
|
||||
MarketOperation.Sell,
|
||||
opts,
|
||||
) as OrdersAndRemainingTakerFillAmount<T>;
|
||||
},
|
||||
@ -45,7 +45,7 @@ export const marketUtils = {
|
||||
return findOrdersThatCoverAssetFillAmount<T>(
|
||||
orders,
|
||||
makerAssetFillAmount,
|
||||
'marketBuy',
|
||||
MarketOperation.Buy,
|
||||
opts,
|
||||
) as OrdersAndRemainingMakerFillAmount<T>;
|
||||
},
|
||||
@ -133,14 +133,14 @@ function findOrdersThatCoverAssetFillAmount<T extends Order>(
|
||||
operation: MarketOperation,
|
||||
opts?: FindOrdersThatCoverTakerAssetFillAmountOpts | FindOrdersThatCoverMakerAssetFillAmountOpts,
|
||||
): OrdersAndRemainingTakerFillAmount<T> | OrdersAndRemainingMakerFillAmount<T> {
|
||||
const variablePrefix = operation === 'marketBuy' ? 'Maker' : 'Taker';
|
||||
const variablePrefix = operation === MarketOperation.Buy ? 'Maker' : 'Taker';
|
||||
assert.doesConformToSchema('orders', orders, schemas.ordersSchema);
|
||||
assert.isValidBaseUnitAmount('assetFillAmount', assetFillAmount);
|
||||
// try to get remainingFillableTakerAssetAmounts from opts, if it's not there, use takerAssetAmount values from orders
|
||||
const remainingFillableAssetAmounts = _.get(
|
||||
opts,
|
||||
`remainingFillable${variablePrefix}AssetAmounts`,
|
||||
_.map(orders, order => (operation === 'marketBuy' ? order.makerAssetAmount : order.takerAssetAmount)),
|
||||
_.map(orders, order => (operation === MarketOperation.Buy ? order.makerAssetAmount : order.takerAssetAmount)),
|
||||
) as BigNumber[];
|
||||
_.forEach(remainingFillableAssetAmounts, (amount, index) =>
|
||||
assert.isValidBaseUnitAmount(`remainingFillable${variablePrefix}AssetAmount[${index}]`, amount),
|
||||
@ -194,7 +194,7 @@ function findOrdersThatCoverAssetFillAmount<T extends Order>(
|
||||
...ordersAndRemainingFillAmount
|
||||
} = result;
|
||||
|
||||
if (operation === 'marketBuy') {
|
||||
if (operation === MarketOperation.Buy) {
|
||||
return {
|
||||
...ordersAndRemainingFillAmount,
|
||||
ordersRemainingFillableMakerAssetAmounts: resultOrdersRemainingFillableAssetAmounts,
|
||||
|
@ -41,7 +41,10 @@ export interface SignedOrder extends Order {
|
||||
signature: string;
|
||||
}
|
||||
|
||||
export type MarketOperation = 'marketBuy' | 'marketSell';
|
||||
export enum MarketOperation {
|
||||
Sell = 'Sell',
|
||||
Buy = 'Buy',
|
||||
}
|
||||
|
||||
/**
|
||||
* ZeroExTransaction for use with 0x Exchange executeTransaction
|
||||
|
Loading…
x
Reference in New Issue
Block a user