Merge pull request #1959 from 0xProject/feature/asset-swapper/minor-changes
Minor changes to Asset-swapper
This commit is contained in:
@@ -4,6 +4,7 @@ import { BigNumber } from '@0x/utils';
|
||||
import {
|
||||
ForwarderSwapQuoteExecutionOpts,
|
||||
ForwarderSwapQuoteGetOutputOpts,
|
||||
LiquidityRequestOpts,
|
||||
OrdersAndFillableAmounts,
|
||||
SwapQuoteRequestOpts,
|
||||
SwapQuoterOpts,
|
||||
@@ -12,6 +13,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,
|
||||
@@ -36,6 +38,10 @@ const EMPTY_ORDERS_AND_FILLABLE_AMOUNTS: OrdersAndFillableAmounts = {
|
||||
remainingFillableMakerAssetAmounts: [] as BigNumber[],
|
||||
};
|
||||
|
||||
const DEFAULT_LIQUIDITY_REQUEST_OPTS: LiquidityRequestOpts = {
|
||||
shouldForceOrderRefresh: false,
|
||||
};
|
||||
|
||||
export const constants = {
|
||||
ZERO_AMOUNT: new BigNumber(0),
|
||||
NULL_ADDRESS,
|
||||
@@ -48,4 +54,6 @@ export const constants = {
|
||||
DEFAULT_FORWARDER_SWAP_QUOTE_EXECUTE_OPTS,
|
||||
DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
|
||||
EMPTY_ORDERS_AND_FILLABLE_AMOUNTS,
|
||||
DEFAULT_PER_PAGE,
|
||||
DEFAULT_LIQUIDITY_REQUEST_OPTS,
|
||||
};
|
||||
|
@@ -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;
|
||||
}
|
||||
@@ -248,8 +248,7 @@ export class SwapQuoter {
|
||||
takerAssetData: string,
|
||||
options: Partial<LiquidityRequestOpts> = {},
|
||||
): Promise<LiquidityForAssetData> {
|
||||
const shouldForceOrderRefresh =
|
||||
options.shouldForceOrderRefresh !== undefined ? options.shouldForceOrderRefresh : false;
|
||||
const { shouldForceOrderRefresh } = _.merge({}, constants.DEFAULT_LIQUIDITY_REQUEST_OPTS, options);
|
||||
assert.isString('makerAssetData', makerAssetData);
|
||||
assert.isString('takerAssetData', takerAssetData);
|
||||
assetDataUtils.decodeAssetDataOrThrow(makerAssetData);
|
||||
@@ -423,7 +422,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,
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user