fix(asset-buyer): output assetEthAmount instead of ethPerAssetPrice in BuyQuoteInfos

This commit is contained in:
Brandon Millman 2018-11-12 20:30:01 -08:00
parent add1cfd261
commit d73faf1b81
4 changed files with 31 additions and 31 deletions

View File

@ -54,12 +54,12 @@ export interface BuyQuote {
} }
/** /**
* ethPerAssetPrice: The price of one unit of the desired asset in ETH * assetEthAmount: The amount of eth required to pay for the requested asset.
* feeEthAmount: The amount of eth required to pay the affiliate fee. * feeEthAmount: The amount of eth required to pay the affiliate fee.
* totalEthAmount: the total amount of eth required to complete the buy. (Filling orders, feeOrders, and paying affiliate fee) * totalEthAmount: The total amount of eth required to complete the buy (filling orders, feeOrders, and paying affiliate fee).
*/ */
export interface BuyQuoteInfo { export interface BuyQuoteInfo {
ethPerAssetPrice: BigNumber; assetEthAmount: BigNumber;
feeEthAmount: BigNumber; feeEthAmount: BigNumber;
totalEthAmount: BigNumber; totalEthAmount: BigNumber;
} }

View File

@ -18,7 +18,7 @@ export const assert = {
} }
}, },
isValidBuyQuoteInfo(variableName: string, buyQuoteInfo: BuyQuoteInfo): void { isValidBuyQuoteInfo(variableName: string, buyQuoteInfo: BuyQuoteInfo): void {
sharedAssert.isBigNumber(`${variableName}.ethPerAssetPrice`, buyQuoteInfo.ethPerAssetPrice); sharedAssert.isBigNumber(`${variableName}.assetEthAmount`, buyQuoteInfo.assetEthAmount);
sharedAssert.isBigNumber(`${variableName}.feeEthAmount`, buyQuoteInfo.feeEthAmount); sharedAssert.isBigNumber(`${variableName}.feeEthAmount`, buyQuoteInfo.feeEthAmount);
sharedAssert.isBigNumber(`${variableName}.totalEthAmount`, buyQuoteInfo.totalEthAmount); sharedAssert.isBigNumber(`${variableName}.totalEthAmount`, buyQuoteInfo.totalEthAmount);
}, },

View File

@ -106,28 +106,28 @@ function calculateQuoteInfo(
isMakerAssetZrxToken: boolean, isMakerAssetZrxToken: boolean,
): BuyQuoteInfo { ): BuyQuoteInfo {
// find the total eth and zrx needed to buy assetAmount from the resultOrders from left to right // find the total eth and zrx needed to buy assetAmount from the resultOrders from left to right
let ethAmountToBuyAsset = constants.ZERO_AMOUNT; let assetEthAmount = constants.ZERO_AMOUNT;
let ethAmountToBuyZrx = constants.ZERO_AMOUNT; let zrxEthAmount = constants.ZERO_AMOUNT;
if (isMakerAssetZrxToken) { if (isMakerAssetZrxToken) {
ethAmountToBuyAsset = findEthAmountNeededToBuyZrx(ordersAndFillableAmounts, assetBuyAmount); assetEthAmount = findEthAmountNeededToBuyZrx(ordersAndFillableAmounts, assetBuyAmount);
} else { } else {
// find eth and zrx amounts needed to buy // find eth and zrx amounts needed to buy
const ethAndZrxAmountToBuyAsset = findEthAndZrxAmountNeededToBuyAsset(ordersAndFillableAmounts, assetBuyAmount); const ethAndZrxAmountToBuyAsset = findEthAndZrxAmountNeededToBuyAsset(ordersAndFillableAmounts, assetBuyAmount);
ethAmountToBuyAsset = ethAndZrxAmountToBuyAsset[0]; assetEthAmount = ethAndZrxAmountToBuyAsset[0];
const zrxAmountToBuyAsset = ethAndZrxAmountToBuyAsset[1]; const zrxAmountToBuyAsset = ethAndZrxAmountToBuyAsset[1];
// find eth amount needed to buy zrx // find eth amount needed to buy zrx
ethAmountToBuyZrx = findEthAmountNeededToBuyZrx(feeOrdersAndFillableAmounts, zrxAmountToBuyAsset); zrxEthAmount = findEthAmountNeededToBuyZrx(feeOrdersAndFillableAmounts, zrxAmountToBuyAsset);
} }
/// find the eth amount needed to buy the affiliate fee // eth amount needed to buy the affiliate fee
const ethAmountToBuyAffiliateFee = ethAmountToBuyAsset.mul(feePercentage).ceil(); const affiliateFeeEthAmount = assetEthAmount.mul(feePercentage).ceil();
const totalEthAmountWithoutAffiliateFee = ethAmountToBuyAsset.plus(ethAmountToBuyZrx); // eth amount needed for fees is the sum of affiliate fee and zrx fee
const ethAmountTotal = totalEthAmountWithoutAffiliateFee.plus(ethAmountToBuyAffiliateFee); const feeEthAmount = affiliateFeeEthAmount.plus(zrxEthAmount);
// divide into the assetBuyAmount in order to find rate of makerAsset / WETH // eth amount needed in total is the sum of the amount needed for the asset and the amount needed for fees
const ethPerAssetPrice = totalEthAmountWithoutAffiliateFee.div(assetBuyAmount); const totalEthAmount = assetEthAmount.plus(feeEthAmount);
return { return {
totalEthAmount: ethAmountTotal, assetEthAmount,
feeEthAmount: ethAmountToBuyAffiliateFee, feeEthAmount,
ethPerAssetPrice, totalEthAmount,
}; };
} }

View File

@ -108,17 +108,17 @@ describe('buyQuoteCalculator', () => {
// 50 eth to fill the first order + 100 eth for fees // 50 eth to fill the first order + 100 eth for fees
const expectedEthAmountForAsset = new BigNumber(50); const expectedEthAmountForAsset = new BigNumber(50);
const expectedEthAmountForZrxFees = new BigNumber(100); const expectedEthAmountForZrxFees = new BigNumber(100);
const expectedFillEthAmount = expectedEthAmountForAsset.plus(expectedEthAmountForZrxFees); const expectedFillEthAmount = expectedEthAmountForAsset;
const expectedFeeEthAmount = expectedEthAmountForAsset.mul(feePercentage); const expectedAffiliateFeeEthAmount = expectedEthAmountForAsset.mul(feePercentage);
const expectedFeeEthAmount = expectedAffiliateFeeEthAmount.plus(expectedEthAmountForZrxFees);
const expectedTotalEthAmount = expectedFillEthAmount.plus(expectedFeeEthAmount); const expectedTotalEthAmount = expectedFillEthAmount.plus(expectedFeeEthAmount);
const expectedEthPerAssetPrice = expectedFillEthAmount.div(assetBuyAmount); expect(buyQuote.bestCaseQuoteInfo.assetEthAmount).to.bignumber.equal(expectedFillEthAmount);
expect(buyQuote.bestCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount); expect(buyQuote.bestCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount);
expect(buyQuote.bestCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount); expect(buyQuote.bestCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount);
expect(buyQuote.bestCaseQuoteInfo.ethPerAssetPrice).to.bignumber.equal(expectedEthPerAssetPrice);
// because we have no slippage protection, minRate is equal to maxRate // because we have no slippage protection, minRate is equal to maxRate
expect(buyQuote.worstCaseQuoteInfo.assetEthAmount).to.bignumber.equal(expectedFillEthAmount);
expect(buyQuote.worstCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount); expect(buyQuote.worstCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount);
expect(buyQuote.worstCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount); expect(buyQuote.worstCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount);
expect(buyQuote.worstCaseQuoteInfo.ethPerAssetPrice).to.bignumber.equal(expectedEthPerAssetPrice);
// test if feePercentage gets passed through // test if feePercentage gets passed through
expect(buyQuote.feePercentage).to.equal(feePercentage); expect(buyQuote.feePercentage).to.equal(feePercentage);
}); });
@ -146,23 +146,23 @@ describe('buyQuoteCalculator', () => {
// 50 eth to fill the first order + 100 eth for fees // 50 eth to fill the first order + 100 eth for fees
const expectedEthAmountForAsset = new BigNumber(50); const expectedEthAmountForAsset = new BigNumber(50);
const expectedEthAmountForZrxFees = new BigNumber(100); const expectedEthAmountForZrxFees = new BigNumber(100);
const expectedFillEthAmount = expectedEthAmountForAsset.plus(expectedEthAmountForZrxFees); const expectedFillEthAmount = expectedEthAmountForAsset;
const expectedFeeEthAmount = expectedEthAmountForAsset.mul(feePercentage); const expectedAffiliateFeeEthAmount = expectedEthAmountForAsset.mul(feePercentage);
const expectedFeeEthAmount = expectedAffiliateFeeEthAmount.plus(expectedEthAmountForZrxFees);
const expectedTotalEthAmount = expectedFillEthAmount.plus(expectedFeeEthAmount); const expectedTotalEthAmount = expectedFillEthAmount.plus(expectedFeeEthAmount);
const expectedEthPerAssetPrice = expectedFillEthAmount.div(assetBuyAmount); expect(buyQuote.bestCaseQuoteInfo.assetEthAmount).to.bignumber.equal(expectedFillEthAmount);
expect(buyQuote.bestCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount); expect(buyQuote.bestCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedFeeEthAmount);
expect(buyQuote.bestCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount); expect(buyQuote.bestCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedTotalEthAmount);
expect(buyQuote.bestCaseQuoteInfo.ethPerAssetPrice).to.bignumber.equal(expectedEthPerAssetPrice);
// 100 eth to fill the first order + 208 eth for fees // 100 eth to fill the first order + 208 eth for fees
const expectedWorstEthAmountForAsset = new BigNumber(100); const expectedWorstEthAmountForAsset = new BigNumber(100);
const expectedWorstEthAmountForZrxFees = new BigNumber(208); const expectedWorstEthAmountForZrxFees = new BigNumber(208);
const expectedWorstFillEthAmount = expectedWorstEthAmountForAsset.plus(expectedWorstEthAmountForZrxFees); const expectedWorstFillEthAmount = expectedWorstEthAmountForAsset;
const expectedWorstFeeEthAmount = expectedWorstEthAmountForAsset.mul(feePercentage); const expectedWorstAffiliateFeeEthAmount = expectedWorstEthAmountForAsset.mul(feePercentage);
const expectedWorstFeeEthAmount = expectedWorstAffiliateFeeEthAmount.plus(expectedWorstEthAmountForZrxFees);
const expectedWorstTotalEthAmount = expectedWorstFillEthAmount.plus(expectedWorstFeeEthAmount); const expectedWorstTotalEthAmount = expectedWorstFillEthAmount.plus(expectedWorstFeeEthAmount);
const expectedWorstEthPerAssetPrice = expectedWorstFillEthAmount.div(assetBuyAmount); expect(buyQuote.worstCaseQuoteInfo.assetEthAmount).to.bignumber.equal(expectedWorstFillEthAmount);
expect(buyQuote.worstCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedWorstFeeEthAmount); expect(buyQuote.worstCaseQuoteInfo.feeEthAmount).to.bignumber.equal(expectedWorstFeeEthAmount);
expect(buyQuote.worstCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedWorstTotalEthAmount); expect(buyQuote.worstCaseQuoteInfo.totalEthAmount).to.bignumber.equal(expectedWorstTotalEthAmount);
expect(buyQuote.worstCaseQuoteInfo.ethPerAssetPrice).to.bignumber.equal(expectedWorstEthPerAssetPrice);
// test if feePercentage gets passed through // test if feePercentage gets passed through
expect(buyQuote.feePercentage).to.equal(feePercentage); expect(buyQuote.feePercentage).to.equal(feePercentage);
}); });