Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function findOrdersThatCoverAssetFillAmount(
orders: T[],
assetFillAmount: BigNumber,
operation: MarketOperation,
opts?: FindOrdersThatCoverTakerAssetFillAmountOpts | FindOrdersThatCoverMakerAssetFillAmountOpts,
): OrdersAndRemainingTakerFillAmount | OrdersAndRemainingMakerFillAmount {
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 === MarketOperation.Buy ? order.makerAssetAmount : order.takerAssetAmount)),
) as BigNumber[];
_.forEach(remainingFillableAssetAmounts, (amount, index) =>
assert.isValidBaseUnitAmount(`remainingFillable${variablePrefix}AssetAmount[${index}]`, amount),
);
assert.assert(
orders.length === remainingFillableAssetAmounts.length,
`Expected orders.length to equal opts.remainingFillable${variablePrefix}AssetAmounts.length`,
);
// try to get slippageBufferAmount from opts, if it's not there, default to 0public async getSmartContractParamsOrThrowAsync(
quote: SwapQuote,
_opts: Partial,
): Promise> {
assert.isValidSwapQuote('quote', quote);
const { orders } = quote;
const signatures = _.map(orders, o => o.signature);
const optimizedOrders = swapQuoteConsumerUtils.optimizeOrdersForMarketExchangeOperation(orders, quote.type);
let params: ExchangeSmartContractParams;
let methodName: string;
if (quote.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quote;
params = {
orders: optimizedOrders,
signatures,
makerAssetFillAmount,
type: MarketOperation.Buy,
};
methodName = 'marketBuyOrders';
} else {
const { takerAssetFillAmount } = quote;
params = {
orders: optimizedOrders,
signatures, _.map(orders, order => (operation === MarketOperation.Buy ? order.makerAssetAmount : order.takerAssetAmount)),
) as BigNumber[];assert.isETHAddressHex('takerAddress', takerAddress);
}
if (gasLimit !== undefined) {
assert.isNumber('gasLimit', gasLimit);
}
if (gasPrice !== undefined) {
assert.isBigNumber('gasPrice', gasPrice);
}
const { orders } = quote;
const finalTakerAddress = await swapQuoteConsumerUtils.getTakerAddressOrThrowAsync(this.provider, opts);
try {
let txHash: string;
if (quote.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quote;
txHash = await this._contractWrappers.exchange.marketBuyOrdersNoThrow.sendTransactionAsync(
orders,
makerAssetFillAmount,
orders.map(o => o.signature),
{
from: finalTakerAddress,
gas: gasLimit,
gasPrice,
},
);
} else {
const { takerAssetFillAmount } = quote;
txHash = await this._contractWrappers.exchange.marketSellOrdersNoThrow.sendTransactionAsync(
orders,
takerAssetFillAmount,findOrdersThatCoverMakerAssetFillAmount(
orders: T[],
makerAssetFillAmount: BigNumber,
opts?: FindOrdersThatCoverMakerAssetFillAmountOpts,
): OrdersAndRemainingMakerFillAmount {
return findOrdersThatCoverAssetFillAmount(
orders,
makerAssetFillAmount,
MarketOperation.Buy,
opts,
) as OrdersAndRemainingMakerFillAmount;
},
/**public async getCalldataOrThrowAsync(
quote: SwapQuote,
opts: Partial,
): Promise {
assert.isValidSwapQuote('quote', quote);
const { toAddress, methodAbi, ethAmount, params } = await this.getSmartContractParamsOrThrowAsync(quote, opts);
const abiEncoder = new AbiEncoder.Method(methodAbi);
const { orders, signatures } = params;
let args: any[];
if (params.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = params;
args = [orders, makerAssetFillAmount, signatures];
} else {
const { takerAssetFillAmount } = params;
args = [orders, takerAssetFillAmount, signatures];
}
const calldataHexString = abiEncoder.encode(args, { shouldOptimize: true });
return {
calldataHexString,
methodAbi,
toAddress,
ethAmount,
};
}const signatures = _.map(orders, o => o.signature);
const optimizedOrders = swapQuoteConsumerUtils.optimizeOrdersForMarketExchangeOperation(orders, quote.type);
let params: ExchangeSmartContractParams;
let methodName: string;
if (quote.type === MarketOperation.Buy) {
const { makerAssetFillAmount } = quote;
params = {
orders: optimizedOrders,
signatures,
makerAssetFillAmount,
type: MarketOperation.Buy,
};
methodName = 'marketBuyOrders';
} else {
const { takerAssetFillAmount } = quote;
params = {
orders: optimizedOrders,
signatures,
takerAssetFillAmount,
type: MarketOperation.Sell,
};
methodName = 'marketSellOrders';
}