add affiliate fee utils tests

This commit is contained in:
David Sun
2019-07-17 11:02:14 -07:00
parent 805131cf1e
commit c375199daa
2 changed files with 128 additions and 30 deletions

View File

@@ -1,38 +1,86 @@
// import { SignedOrder } from '@0x/types';
// import { BigNumber } from '@0x/utils';
// import * as chai from 'chai';
// import 'mocha';
// import * as TypeMoq from 'typemoq';
import { MarketOperation, SignedOrder } from '@0x/types';
import { BigNumber } from '@0x/utils';
import * as chai from 'chai';
import 'mocha';
import * as TypeMoq from 'typemoq';
// import { constants } from '../src/constants';
// import { } from '../src/types';
// import { affiliateFeeUtils } from '../src/utils/affiliate_fee_utils';
import { constants } from '../src/constants';
import { } from '../src/types';
import { affiliateFeeUtils } from '../src/utils/affiliate_fee_utils';
// import { chaiSetup } from './utils/chai_setup';
// import { getDummySwapQuotesWithNoFees } from './utils/swap_quote';
import { chaiSetup } from './utils/chai_setup';
import { getFullyFillableSwapQuoteWithNoFees, getSignedOrdersWithNoFees, getSignedOrdersWithFees, getFullyFillableSwapQuoteWithFees } from './utils/swap_quote';
// chaiSetup.configure();
// const expect = chai.expect;
chaiSetup.configure();
const expect = chai.expect;
// const FAKE_TAKER_ASSET_DATA = '0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48';
// const FAKE_MAKER_ASSET_DATA = '0xf47261b00000000000000000000000009f5B0C7e1623793bF0620569b9749e79DF6D0bC5';
// const NULL_ADDRESS = constants.NULL_ADDRESS;
// const FEE_PERCENTAGE = 0.05;
const FAKE_TAKER_ASSET_DATA = '0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48';
const FAKE_MAKER_ASSET_DATA = '0xf47261b00000000000000000000000009f5B0C7e1623793bF0620569b9749e79DF6D0bC5';
const NULL_ADDRESS = constants.NULL_ADDRESS;
const FEE_PERCENTAGE = 0.1;
const FILLABLE_AMOUNTS = [new BigNumber(2), new BigNumber(3), new BigNumber(5)];
const FILLABLE_FEE_AMOUNTS = [new BigNumber(1), new BigNumber(1), new BigNumber(1)];
const MARKET_OPERATION = MarketOperation.Sell;
// describe('affiliateFeeUtils', () => {
// const dummySwapQuotes = getDummySwapQuotesWithNoFees(FAKE_MAKER_ASSET_DATA, FAKE_TAKER_ASSET_DATA, NULL_ADDRESS);
describe('affiliateFeeUtils', () => {
const fakeFeeOrders = getSignedOrdersWithNoFees(
FAKE_MAKER_ASSET_DATA,
FAKE_TAKER_ASSET_DATA,
NULL_ADDRESS,
NULL_ADDRESS,
FILLABLE_FEE_AMOUNTS,
)
const fakeOrders = getSignedOrdersWithNoFees(
FAKE_MAKER_ASSET_DATA,
FAKE_TAKER_ASSET_DATA,
NULL_ADDRESS,
NULL_ADDRESS,
FILLABLE_AMOUNTS,
);
// describe('getSwapQuoteWithAffiliateFee', () => {
// it('should return unchanged swapQuote if feePercentage is 0', () => {
// const updatedSwapQuote = affiliateFeeUtils.getSwapQuoteWithAffiliateFee(dummySwapQuotes.fullyFilled, 0);
// expect(updatedSwapQuote).to.deep.equal(dummySwapQuotes.fullyFilled);
// });
// it('should return correct feeTakerToken and totalTakerToken amounts when provides SwapQuote with no fees', () => {
// const updatedSwapQuote = affiliateFeeUtils.getSwapQuoteWithAffiliateFee(dummySwapQuotes.partiallyFilled, FEE_PERCENTAGE);
const fakeOrdersWithFees = getSignedOrdersWithFees(
FAKE_MAKER_ASSET_DATA,
FAKE_TAKER_ASSET_DATA,
NULL_ADDRESS,
NULL_ADDRESS,
FILLABLE_AMOUNTS,
FILLABLE_FEE_AMOUNTS,
);
// });
// it('should return correct feeTakerToken and totalTakerToken amounts when provides SwapQuote with fees', () => {
const fakeSwapQuote = getFullyFillableSwapQuoteWithNoFees(
FAKE_MAKER_ASSET_DATA,
FAKE_TAKER_ASSET_DATA,
fakeOrders,
MARKET_OPERATION,
);
// });
// });
// });
const fakeSwapQuoteWithFees = getFullyFillableSwapQuoteWithFees(
FAKE_MAKER_ASSET_DATA,
FAKE_TAKER_ASSET_DATA,
fakeOrders,
fakeFeeOrders,
MARKET_OPERATION,
);
describe('getSwapQuoteWithAffiliateFee', () => {
it('should return unchanged swapQuote if feePercentage is 0', () => {
const updatedSwapQuote = affiliateFeeUtils.getSwapQuoteWithAffiliateFee(fakeSwapQuote, 0);
const fakeSwapQuoteWithAffiliateFees = { ...fakeSwapQuote, ...{ feePercentage: 0 }};
expect(updatedSwapQuote).to.deep.equal(fakeSwapQuoteWithAffiliateFees);
});
it('should return correct feeTakerToken and totalTakerToken amounts when provided SwapQuote with no fees', () => {
const updatedSwapQuote = affiliateFeeUtils.getSwapQuoteWithAffiliateFee(fakeSwapQuote, FEE_PERCENTAGE);
expect(updatedSwapQuote.bestCaseQuoteInfo.feeTakerTokenAmount).to.deep.equal(new BigNumber(1));
expect(updatedSwapQuote.bestCaseQuoteInfo.totalTakerTokenAmount).to.deep.equal(new BigNumber(11));
expect(updatedSwapQuote.worstCaseQuoteInfo.feeTakerTokenAmount).to.deep.equal(new BigNumber(1));
expect(updatedSwapQuote.worstCaseQuoteInfo.totalTakerTokenAmount).to.deep.equal(new BigNumber(11));
});
it('should return correct feeTakerToken and totalTakerToken amounts when provides SwapQuote with fees', () => {
const updatedSwapQuote = affiliateFeeUtils.getSwapQuoteWithAffiliateFee(fakeSwapQuoteWithFees, FEE_PERCENTAGE);
expect(updatedSwapQuote.bestCaseQuoteInfo.feeTakerTokenAmount).to.deep.equal(new BigNumber(4));
expect(updatedSwapQuote.bestCaseQuoteInfo.totalTakerTokenAmount).to.deep.equal(new BigNumber(14));
expect(updatedSwapQuote.worstCaseQuoteInfo.feeTakerTokenAmount).to.deep.equal(new BigNumber(4));
expect(updatedSwapQuote.worstCaseQuoteInfo.totalTakerTokenAmount).to.deep.equal(new BigNumber(14));
});
});
})

View File

@@ -25,6 +25,56 @@ export const getSignedOrdersWithNoFees = (
);
};
export const getSignedOrdersWithFees = (
makerAssetData: string,
takerAssetData: string,
makerAddress: string,
takerAddress: string,
fillableAmounts: BigNumber[],
takerFees: BigNumber[],
): SignedOrder[] => {
const orders = getSignedOrdersWithNoFees(
makerAssetData,
takerAssetData,
makerAddress,
takerAddress,
fillableAmounts,
);
return _.map(orders, (order: SignedOrder, index: number) =>
orderFactory.createSignedOrderFromPartial({
...order,
...{ takerFee: takerFees[index] },
}),
);
};
export const getFullyFillableSwapQuoteWithFees = (
makerAssetData: string,
takerAssetData: string,
orders: SignedOrder[],
feeOrders: SignedOrder[],
operation: MarketOperation,
) => {
const swapQuote = getFullyFillableSwapQuoteWithNoFees(
makerAssetData,
takerAssetData,
orders,
operation,
);
swapQuote.feeOrders = feeOrders;
const totalFeeTakerTokenAmount = _.reduce(
feeOrders,
(a: BigNumber, c: SignedOrder) => a.plus(c.takerAssetAmount),
ZERO_BIG_NUMBER,
);
// Adds fees to the SwapQuoteInfos assuming all feeOrders will be filled
swapQuote.bestCaseQuoteInfo.feeTakerTokenAmount = totalFeeTakerTokenAmount;
swapQuote.worstCaseQuoteInfo.feeTakerTokenAmount = totalFeeTakerTokenAmount;
swapQuote.bestCaseQuoteInfo.totalTakerTokenAmount = swapQuote.bestCaseQuoteInfo.totalTakerTokenAmount.plus(totalFeeTakerTokenAmount);
swapQuote.worstCaseQuoteInfo.totalTakerTokenAmount = swapQuote.worstCaseQuoteInfo.totalTakerTokenAmount.plus(totalFeeTakerTokenAmount);
return swapQuote;
};
export const getFullyFillableSwapQuoteWithNoFees = (
makerAssetData: string,
takerAssetData: string,