Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const traderInfo = orderAndTraderInfo.traderInfo;
const minSet = [];
// Calculate min of balance & allowance of taker's takerAsset
if (signedOrder.takerAddress !== NULL_ADDRESS) {
const maxTakerAssetFillAmountGivenTakerConstraints = BigNumber.min(
traderInfo.takerBalance,
traderInfo.takerAllowance,
);
minSet.push(maxTakerAssetFillAmountGivenTakerConstraints);
}
// Calculate min of balance & allowance of maker's makerAsset -> translate into takerAsset amount
const maxMakerAssetFillAmount = BigNumber.min(traderInfo.makerBalance, traderInfo.makerAllowance);
const maxTakerAssetFillAmountGivenMakerConstraints = orderCalculationUtils.getTakerFillAmount(
signedOrder,
maxMakerAssetFillAmount,
);
minSet.push(maxTakerAssetFillAmountGivenMakerConstraints);
// Calculate min of balance & allowance of taker's Fee -> translate into takerAsset amount
if (!signedOrder.takerFee.eq(0)) {
const takerFeeAvailable = BigNumber.min(traderInfo.takerFeeBalance, traderInfo.takerFeeAllowance);
const maxTakerAssetFillAmountGivenTakerFeeConstraints = takerFeeAvailable
.multipliedBy(signedOrder.takerAssetAmount)
.div(signedOrder.takerFee)
.integerValue(BigNumber.ROUND_FLOOR);
minSet.push(maxTakerAssetFillAmountGivenTakerFeeConstraints);
}
// Calculate min of balance & allowance of maker's Fee -> translate into takerAsset amount
_.each(batchOrderAndTraderInfo, (orderAndTraderInfo: OrderAndTraderInfo, i: number) => {
const signedOrder = signedOrders[i];
const remainingFillableTakerAssetAmount = Handlers._calculateRemainingFillableTakerAssetAmount(
signedOrder,
orderAndTraderInfo,
);
const totalTakerAssetAmountAtOrderExchangeRate = orderCalculationUtils.getTakerFillAmount(
signedOrder,
totalMakerAssetAmount,
);
const takerAssetFillAmount = totalTakerAssetAmountAtOrderExchangeRate.isLessThan(
remainingFillableTakerAssetAmount,
)
? totalTakerAssetAmountAtOrderExchangeRate
: remainingFillableTakerAssetAmount;
const remainingTotalTakerAssetAmount = totalTakerAssetAmountAtOrderExchangeRate.minus(takerAssetFillAmount);
totalMakerAssetAmount = orderCalculationUtils.getMakerFillAmount(
signedOrder,
remainingTotalTakerAssetAmount,
);
takerAssetFillAmounts.push(takerAssetFillAmount);
});
(acc, order, curIndex) => {
const availableMakerAssetAmount = remainingFillableMakerAssetAmounts[curIndex];
if (availableMakerAssetAmount === undefined) {
throw new Error(`No corresponding fillableMakerAssetAmounts at index ${curIndex}`);
}
const makerTokensAvailableForCurrentOrder = availableMakerAssetAmount;
const takerTokensAvailableForCurrentOrder = orderCalculationUtils.getTakerFillAmount(
order,
makerTokensAvailableForCurrentOrder,
);
return {
makerTokensAvailableInBaseUnits: acc.makerTokensAvailableInBaseUnits.plus(
makerTokensAvailableForCurrentOrder,
),
takerTokensAvailableInBaseUnits: acc.takerTokensAvailableInBaseUnits.plus(
takerTokensAvailableForCurrentOrder,
),
};
},
{
(order: SignedOrder, i: number): SignedOrderWithFillableAmounts => {
const fillableAmount = fillableAmounts[i];
const fillableMakerAssetAmount =
operation === MarketOperation.Buy
? fillableAmount
: orderCalculationUtils.getMakerFillAmount(order, fillableAmount);
const fillableTakerAssetAmount =
operation === MarketOperation.Sell
? fillableAmount
: orderCalculationUtils.getTakerFillAmount(order, fillableAmount);
const fillableTakerFeeAmount = orderCalculationUtils.getTakerFeeAmount(order, fillableTakerAssetAmount);
return {
fillableMakerAssetAmount,
fillableTakerAssetAmount,
fillableTakerFeeAmount,
...order,
};
},
)