light refactor

This commit is contained in:
David Sun 2019-07-08 15:28:37 -07:00
parent 1c6130a492
commit 82da33d742
8 changed files with 78 additions and 82 deletions

View File

@ -60,10 +60,20 @@ export class EthBalanceCheckerContract extends BaseContract {
// tslint:enable boolean-naming
return result;
},
<<<<<<< HEAD
getABIEncodedTransactionData(addresses: string[]): string {
assert.isArray('addresses', addresses);
const self = (this as any) as EthBalanceCheckerContract;
const abiEncodedTransactionData = self._strictEncodeArguments('getEthBalances(address[])', [addresses]);
=======
getABIEncodedTransactionData(
addresses: string[],
): string {
assert.isArray('addresses', addresses);
const self = this as any as EthBalanceCheckerContract;
const abiEncodedTransactionData = self._strictEncodeArguments('getEthBalances(address[])', [addresses
]);
>>>>>>> 70ce7fe82... light refactor
return abiEncodedTransactionData;
},
};

View File

@ -1,7 +1,7 @@
import { ContractWrappers } from '@0x/contract-wrappers';
import { schemas } from '@0x/json-schemas';
import { SignedOrder } from '@0x/order-utils';
import { ObjectMap } from '@0x/types';
import { MarketOperation, ObjectMap } from '@0x/types';
import { BigNumber, providerUtils } from '@0x/utils';
import { SupportedProvider, ZeroExProvider } from 'ethereum-types';
import * as _ from 'lodash';
@ -138,42 +138,8 @@ export class SwapQuoter {
takerAssetSellAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketSellSwapQuote> {
const { shouldForceOrderRefresh, slippagePercentage } = _.merge(
{},
constants.DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
options,
);
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assert.isBigNumber('takerAssetSellAmount', takerAssetSellAmount);
assert.isBoolean('shouldForceOrderRefresh', shouldForceOrderRefresh);
assert.isNumber('slippagePercentage', slippagePercentage);
const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
const isMakerAssetZrxToken = makerAssetData === zrxTokenAssetData;
// get the relevant orders for the makerAsset and fees
// if the requested assetData is ZRX, don't get the fee info
const [ordersAndFillableAmounts, feeOrdersAndFillableAmounts] = await Promise.all([
this.getOrdersAndFillableAmountsAsync(makerAssetData, takerAssetData, shouldForceOrderRefresh),
isMakerAssetZrxToken
? Promise.resolve(constants.EMPTY_ORDERS_AND_FILLABLE_AMOUNTS)
: this.getOrdersAndFillableAmountsAsync(zrxTokenAssetData, takerAssetData, shouldForceOrderRefresh),
shouldForceOrderRefresh,
]);
if (ordersAndFillableAmounts.orders.length === 0) {
throw new Error(
`${
SwapQuoterError.AssetUnavailable
}: For makerAssetdata ${makerAssetData} and takerAssetdata ${takerAssetData}`,
);
}
const swapQuote = swapQuoteCalculator.calculateMarketSellSwapQuote(
ordersAndFillableAmounts,
feeOrdersAndFillableAmounts,
takerAssetSellAmount,
slippagePercentage,
isMakerAssetZrxToken,
);
return swapQuote;
assert.isBigNumber('makerAssetBuyAmount', takerAssetSellAmount);
return await this._getSwapQuoteAsync(makerAssetData, takerAssetData, takerAssetSellAmount, 'marketSell', options) as MarketSellSwapQuote;
}
/**
@ -192,43 +158,8 @@ export class SwapQuoter {
makerAssetBuyAmount: BigNumber,
options: Partial<SwapQuoteRequestOpts> = {},
): Promise<MarketBuySwapQuote> {
const { shouldForceOrderRefresh, slippagePercentage } = _.merge(
{},
constants.DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
options,
);
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assert.isBigNumber('makerAssetBuyAmount', makerAssetBuyAmount);
assert.isBoolean('shouldForceOrderRefresh', shouldForceOrderRefresh);
assert.isNumber('slippagePercentage', slippagePercentage);
const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
const isMakerAssetZrxToken = makerAssetData === zrxTokenAssetData;
// get the relevant orders for the makerAsset and fees
// if the requested assetData is ZRX, don't get the fee info
const [ordersAndFillableAmounts, feeOrdersAndFillableAmounts] = await Promise.all([
this.getOrdersAndFillableAmountsAsync(makerAssetData, takerAssetData, shouldForceOrderRefresh),
isMakerAssetZrxToken
? Promise.resolve(constants.EMPTY_ORDERS_AND_FILLABLE_AMOUNTS)
: this.getOrdersAndFillableAmountsAsync(zrxTokenAssetData, takerAssetData, shouldForceOrderRefresh),
shouldForceOrderRefresh,
]);
if (ordersAndFillableAmounts.orders.length === 0) {
throw new Error(
`${
SwapQuoterError.AssetUnavailable
}: For makerAssetdata ${makerAssetData} and takerAssetdata ${takerAssetData}`,
);
}
const swapQuote = swapQuoteCalculator.calculateMarketBuySwapQuote(
ordersAndFillableAmounts,
feeOrdersAndFillableAmounts,
makerAssetBuyAmount,
slippagePercentage,
isMakerAssetZrxToken,
);
return swapQuote;
return await this._getSwapQuoteAsync(makerAssetData, takerAssetData, makerAssetBuyAmount, 'marketBuy', options) as MarketBuySwapQuote;
}
/**
* Get a `SwapQuote` containing all information relevant to fulfilling a swap between a desired ERC20 token address and ERC20 owned by a provided address.
@ -431,4 +362,62 @@ export class SwapQuoter {
private _getZrxTokenAssetDataOrThrow(): string {
return this._contractWrappers.exchange.getZRXAssetData();
}
private async _getSwapQuoteAsync(
makerAssetData: string,
takerAssetData: string,
assetFillAmount: BigNumber,
marketOperation: MarketOperation,
options: Partial<SwapQuoteRequestOpts>): Promise<SwapQuote> {
const { shouldForceOrderRefresh, slippagePercentage } = _.merge(
{},
constants.DEFAULT_SWAP_QUOTE_REQUEST_OPTS,
options,
);
assert.isString('makerAssetData', makerAssetData);
assert.isString('takerAssetData', takerAssetData);
assert.isBoolean('shouldForceOrderRefresh', shouldForceOrderRefresh);
assert.isNumber('slippagePercentage', slippagePercentage);
const zrxTokenAssetData = this._getZrxTokenAssetDataOrThrow();
const isMakerAssetZrxToken = makerAssetData === zrxTokenAssetData;
// get the relevant orders for the makerAsset and fees
// if the requested assetData is ZRX, don't get the fee info
const [ordersAndFillableAmounts, feeOrdersAndFillableAmounts] = await Promise.all([
this.getOrdersAndFillableAmountsAsync(makerAssetData, takerAssetData, shouldForceOrderRefresh),
isMakerAssetZrxToken
? Promise.resolve(constants.EMPTY_ORDERS_AND_FILLABLE_AMOUNTS)
: this.getOrdersAndFillableAmountsAsync(zrxTokenAssetData, takerAssetData, shouldForceOrderRefresh),
shouldForceOrderRefresh,
]);
if (ordersAndFillableAmounts.orders.length === 0) {
throw new Error(
`${
SwapQuoterError.AssetUnavailable
}: For makerAssetdata ${makerAssetData} and takerAssetdata ${takerAssetData}`,
);
}
let swapQuote: SwapQuote;
if (marketOperation === 'marketBuy') {
swapQuote = swapQuoteCalculator.calculateMarketBuySwapQuote(
ordersAndFillableAmounts,
feeOrdersAndFillableAmounts,
assetFillAmount,
slippagePercentage,
isMakerAssetZrxToken,
);
} else {
swapQuote = swapQuoteCalculator.calculateMarketSellSwapQuote(
ordersAndFillableAmounts,
feeOrdersAndFillableAmounts,
assetFillAmount,
slippagePercentage,
isMakerAssetZrxToken,
);
}
return swapQuote;
}
}

View File

@ -71,8 +71,6 @@ export interface SmartContractParamsBase {
signatures: string[];
}
export type MarketOperation = 'marketBuy' | 'marketSell';
/**
* orders: An array of objects conforming to SignedOrder. These orders can be used to cover the requested assetBuyAmount plus slippage.
* makerAssetFillAmount: The amount of makerAsset to swap for.

View File

@ -1,12 +1,12 @@
import { marketUtils, orderCalculationUtils, SignedOrder } from '@0x/order-utils';
import { BigNumber } from '@0x/utils';
import { MarketOperation } from '@0x/types';
import * as _ from 'lodash';
import { constants } from '../constants';
import { InsufficientAssetLiquidityError } from '../errors';
import {
MarketBuySwapQuote,
MarketOperation,
MarketSellSwapQuote,
OrdersAndFillableAmounts,
SwapQuote,

View File

@ -1,9 +1,9 @@
import { orderFactory } from '@0x/order-utils/lib/src/order_factory';
import { SignedOrder } from '@0x/types';
import { SignedOrder, MarketOperation } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
import { MarketOperation, SwapQuote } from '../../src/types';
import { SwapQuote } from '../../src/types';
const ZERO_BIG_NUMBER = new BigNumber(0);

View File

@ -1,5 +1,5 @@
import { schemas } from '@0x/json-schemas';
import { Order } from '@0x/types';
import { MarketOperation, Order } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as _ from 'lodash';
@ -10,7 +10,6 @@ import {
FindFeeOrdersThatCoverFeesForTargetOrdersOpts,
FindOrdersThatCoverMakerAssetFillAmountOpts,
FindOrdersThatCoverTakerAssetFillAmountOpts,
MarketOperation,
OrdersAndRemainingMakerFillAmount,
OrdersAndRemainingTakerFillAmount,
} from './types';

View File

@ -49,8 +49,6 @@ export interface FindOrdersThatCoverTakerAssetFillAmountOpts {
slippageBufferAmount?: BigNumber;
}
export type MarketOperation = 'marketSell' | 'marketBuy';
/**
* remainingFillableMakerAssetAmount: An array of BigNumbers corresponding to the `orders` parameter.
* You can use `OrderStateUtils` `@0x/order-utils` to perform blockchain lookups for these values.

View File

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