Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e1c9323e3c | ||
|
|
b83c10a532 | ||
|
|
70d748fb15 | ||
|
|
6551c2b02b | ||
|
|
dcf36acb0b | ||
|
|
44719c9ea5 | ||
|
|
773b1cdc13 | ||
|
|
8d9f2cdda3 | ||
|
|
3b4fa06927 | ||
|
|
6de1e0b83d | ||
|
|
85e9c1300c | ||
|
|
4e9c47c27e | ||
|
|
9c3c0a30f6 |
@@ -4,14 +4,8 @@ import { getDateTimeFormat } from '@vegaprotocol/utils';
|
||||
import * as Schema from '@vegaprotocol/types';
|
||||
import type { PartialDeep } from 'type-fest';
|
||||
import type { Trade } from './fills-data-provider';
|
||||
import {
|
||||
FeesDiscountBreakdownTooltip,
|
||||
FillsTable,
|
||||
getFeesBreakdown,
|
||||
getTotalFeesDiscounts,
|
||||
} from './fills-table';
|
||||
import { FeesDiscountBreakdownTooltip, FillsTable } from './fills-table';
|
||||
import { generateFill } from './test-helpers';
|
||||
import type { TradeFeeFieldsFragment } from './__generated__/Fills';
|
||||
|
||||
const partyId = 'party-id';
|
||||
const defaultFill: PartialDeep<Trade> = {
|
||||
@@ -35,6 +29,7 @@ const defaultFill: PartialDeep<Trade> = {
|
||||
},
|
||||
createdAt: new Date('2022-02-02T14:00:00').toISOString(),
|
||||
};
|
||||
|
||||
describe('FillsTable', () => {
|
||||
it('correct columns are rendered', async () => {
|
||||
// 7005-FILL-001
|
||||
@@ -65,7 +60,7 @@ describe('FillsTable', () => {
|
||||
expect(headers.map((h) => h.textContent?.trim())).toEqual(expectedHeaders);
|
||||
});
|
||||
|
||||
it('formats cells correctly for buyer fill', async () => {
|
||||
it('formats cells correctly for buyer fill for maker', async () => {
|
||||
const buyerFill = generateFill({
|
||||
...defaultFill,
|
||||
buyer: {
|
||||
@@ -89,7 +84,7 @@ describe('FillsTable', () => {
|
||||
'3.00 BTC',
|
||||
'Maker',
|
||||
'2.00 BTC',
|
||||
'0.27 BTC',
|
||||
'0.09 BTC',
|
||||
getDateTimeFormat().format(new Date(buyerFill.createdAt)),
|
||||
'', // action column
|
||||
];
|
||||
@@ -271,96 +266,48 @@ describe('FillsTable', () => {
|
||||
.find((c) => c.getAttribute('col-id') === 'size');
|
||||
expect(sizeCell).toHaveTextContent('3,000,000,000');
|
||||
});
|
||||
});
|
||||
|
||||
describe('FeesDiscountBreakdownTooltip', () => {
|
||||
it('shows all discounts', () => {
|
||||
const data = generateFill({
|
||||
...defaultFill,
|
||||
buyer: {
|
||||
id: partyId,
|
||||
},
|
||||
});
|
||||
const props = {
|
||||
data,
|
||||
partyId,
|
||||
value: data.market,
|
||||
} as Parameters<typeof FeesDiscountBreakdownTooltip>['0'];
|
||||
const { container } = render(<FeesDiscountBreakdownTooltip {...props} />);
|
||||
const dt = container.querySelectorAll('dt');
|
||||
const dd = container.querySelectorAll('dd');
|
||||
const expectedDt = [
|
||||
'Infrastructure Fee',
|
||||
'Referral Discount',
|
||||
'Volume Discount',
|
||||
'Liquidity Fee',
|
||||
'Referral Discount',
|
||||
'Volume Discount',
|
||||
'Maker Fee',
|
||||
'Referral Discount',
|
||||
'Volume Discount',
|
||||
];
|
||||
const expectedDD = [
|
||||
'0.05 BTC',
|
||||
'0.06 BTC',
|
||||
'0.01 BTC',
|
||||
'0.02 BTC',
|
||||
'0.03 BTC',
|
||||
'0.04 BTC',
|
||||
];
|
||||
expectedDt.forEach((label, i) => {
|
||||
expect(dt[i]).toHaveTextContent(label);
|
||||
});
|
||||
expectedDD.forEach((label, i) => {
|
||||
expect(dd[i]).toHaveTextContent(label);
|
||||
describe('FeesDiscountBreakdownTooltip', () => {
|
||||
it('shows all discounts', () => {
|
||||
const data = generateFill({
|
||||
...defaultFill,
|
||||
buyer: {
|
||||
id: partyId,
|
||||
},
|
||||
});
|
||||
const props = {
|
||||
data,
|
||||
partyId,
|
||||
value: data.market,
|
||||
} as Parameters<typeof FeesDiscountBreakdownTooltip>['0'];
|
||||
const { container } = render(<FeesDiscountBreakdownTooltip {...props} />);
|
||||
const dt = container.querySelectorAll('dt');
|
||||
const dd = container.querySelectorAll('dd');
|
||||
const expectedDt = [
|
||||
'Infrastructure Fee',
|
||||
'Referral Discount',
|
||||
'Volume Discount',
|
||||
'Liquidity Fee',
|
||||
'Referral Discount',
|
||||
'Volume Discount',
|
||||
'Maker Fee',
|
||||
'Referral Discount',
|
||||
'Volume Discount',
|
||||
];
|
||||
const expectedDD = [
|
||||
'0.05 BTC',
|
||||
'0.06 BTC',
|
||||
'0.01 BTC',
|
||||
'0.02 BTC',
|
||||
'0.03 BTC',
|
||||
'0.04 BTC',
|
||||
];
|
||||
expectedDt.forEach((label, i) => {
|
||||
expect(dt[i]).toHaveTextContent(label);
|
||||
});
|
||||
expectedDD.forEach((label, i) => {
|
||||
expect(dd[i]).toHaveTextContent(label);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getFeesBreakdown', () => {
|
||||
it('should return correct fees breakdown for a taker', () => {
|
||||
const fees = {
|
||||
makerFee: '1000',
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
makerFee: '1000',
|
||||
totalFee: '6000',
|
||||
};
|
||||
expect(getFeesBreakdown('Taker', fees)).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct fees breakdown for a maker', () => {
|
||||
const fees = {
|
||||
makerFee: '1000',
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
makerFee: '-1000',
|
||||
totalFee: '4000',
|
||||
};
|
||||
expect(getFeesBreakdown('Maker', fees)).toEqual(expectedBreakdown);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTotalFeesDiscounts', () => {
|
||||
it('should return correct total value', () => {
|
||||
const fees = {
|
||||
infrastructureFeeReferralDiscount: '1',
|
||||
infrastructureFeeVolumeDiscount: '2',
|
||||
liquidityFeeReferralDiscount: '3',
|
||||
liquidityFeeVolumeDiscount: '4',
|
||||
makerFeeReferralDiscount: '5',
|
||||
makerFeeVolumeDiscount: '6',
|
||||
};
|
||||
expect(getTotalFeesDiscounts(fees as TradeFeeFieldsFragment)).toEqual(
|
||||
(1 + 2 + 3 + 4 + 5 + 6).toString()
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { useMemo } from 'react';
|
||||
import type {
|
||||
AgGridReact,
|
||||
AgGridReactProps,
|
||||
AgReactUiProps,
|
||||
import {
|
||||
type AgGridReact,
|
||||
type AgGridReactProps,
|
||||
type AgReactUiProps,
|
||||
} from 'ag-grid-react';
|
||||
import type { ITooltipParams, ColDef } from 'ag-grid-community';
|
||||
import type { ColDef } from 'ag-grid-community';
|
||||
import type { ITooltipParams } from 'ag-grid-community';
|
||||
import {
|
||||
addDecimal,
|
||||
addDecimalsFormatNumber,
|
||||
@@ -29,19 +30,11 @@ import type {
|
||||
import { forwardRef } from 'react';
|
||||
import BigNumber from 'bignumber.js';
|
||||
import type { Trade } from './fills-data-provider';
|
||||
import type {
|
||||
FillFieldsFragment,
|
||||
TradeFeeFieldsFragment,
|
||||
} from './__generated__/Fills';
|
||||
import { FillActionsDropdown } from './fill-actions-dropdown';
|
||||
import { getAsset } from '@vegaprotocol/markets';
|
||||
import { MAKER, TAKER, getFeesBreakdown, getRoleAndFees } from './fills-utils';
|
||||
|
||||
const TAKER = 'Taker';
|
||||
const MAKER = 'Maker';
|
||||
|
||||
export type Role = typeof TAKER | typeof MAKER | '-';
|
||||
|
||||
export type Props = (AgGridReactProps | AgReactUiProps) & {
|
||||
type Props = (AgGridReactProps | AgReactUiProps) & {
|
||||
partyId: string;
|
||||
onMarketClick?: (marketId: string, metaKey?: boolean) => void;
|
||||
};
|
||||
@@ -261,63 +254,13 @@ const formatFeeDiscount = (partyId: string) => {
|
||||
}: VegaValueFormatterParams<Trade, 'market'>) => {
|
||||
if (!market || !data) return '-';
|
||||
const asset = getAsset(market);
|
||||
const { fees } = getRoleAndFees({ data, partyId });
|
||||
if (!fees) return '-';
|
||||
|
||||
const total = getTotalFeesDiscounts(fees);
|
||||
return addDecimalsFormatNumber(total, asset.decimals);
|
||||
const { fees: roleFees, role } = getRoleAndFees({ data, partyId });
|
||||
if (!roleFees) return '-';
|
||||
const { totalFeeDiscount } = getFeesBreakdown(role, roleFees);
|
||||
return addDecimalsFormatNumber(totalFeeDiscount, asset.decimals);
|
||||
};
|
||||
};
|
||||
|
||||
export const isEmptyFeeObj = (feeObj: Schema.TradeFee) => {
|
||||
if (!feeObj) return true;
|
||||
return (
|
||||
feeObj.liquidityFee === '0' &&
|
||||
feeObj.makerFee === '0' &&
|
||||
feeObj.infrastructureFee === '0'
|
||||
);
|
||||
};
|
||||
|
||||
export const getRoleAndFees = ({
|
||||
data,
|
||||
partyId,
|
||||
}: {
|
||||
data: Pick<
|
||||
FillFieldsFragment,
|
||||
'buyerFee' | 'sellerFee' | 'buyer' | 'seller' | 'aggressor'
|
||||
>;
|
||||
partyId?: string;
|
||||
}) => {
|
||||
let role: Role;
|
||||
let fees;
|
||||
if (data?.buyer.id === partyId) {
|
||||
if (data.aggressor === Schema.Side.SIDE_BUY) {
|
||||
role = TAKER;
|
||||
fees = data?.buyerFee;
|
||||
} else if (data.aggressor === Schema.Side.SIDE_SELL) {
|
||||
role = MAKER;
|
||||
fees = data?.sellerFee;
|
||||
} else {
|
||||
role = '-';
|
||||
fees = !isEmptyFeeObj(data?.buyerFee) ? data.buyerFee : data.sellerFee;
|
||||
}
|
||||
} else if (data?.seller.id === partyId) {
|
||||
if (data.aggressor === Schema.Side.SIDE_SELL) {
|
||||
role = TAKER;
|
||||
fees = data?.sellerFee;
|
||||
} else if (data.aggressor === Schema.Side.SIDE_BUY) {
|
||||
role = MAKER;
|
||||
fees = data?.buyerFee;
|
||||
} else {
|
||||
role = '-';
|
||||
fees = !isEmptyFeeObj(data.sellerFee) ? data.sellerFee : data.buyerFee;
|
||||
}
|
||||
} else {
|
||||
return { role: '-', fees: undefined };
|
||||
}
|
||||
return { role, fees };
|
||||
};
|
||||
|
||||
const FeesBreakdownTooltip = ({
|
||||
data,
|
||||
value: market,
|
||||
@@ -329,16 +272,23 @@ const FeesBreakdownTooltip = ({
|
||||
|
||||
const asset = getAsset(market);
|
||||
|
||||
const { role, fees } = getRoleAndFees({ data, partyId }) ?? {};
|
||||
const { role, fees, marketState } = getRoleAndFees({ data, partyId }) ?? {};
|
||||
if (!fees) return null;
|
||||
const { infrastructureFee, liquidityFee, makerFee, totalFee } =
|
||||
getFeesBreakdown(role, fees);
|
||||
getFeesBreakdown(role, fees, marketState);
|
||||
|
||||
return (
|
||||
<div
|
||||
data-testid="fee-breakdown-tooltip"
|
||||
className="z-20 max-w-sm px-4 py-2 text-sm text-black border rounded bg-vega-light-100 dark:bg-vega-dark-100 border-vega-light-200 dark:border-vega-dark-200 break-word dark:text-white"
|
||||
className="z-20 max-w-sm px-4 py-2 text-xs text-black border rounded bg-vega-light-100 dark:bg-vega-dark-100 border-vega-light-200 dark:border-vega-dark-200 break-word dark:text-white"
|
||||
>
|
||||
{marketState && (
|
||||
<p className="mb-1 italic">
|
||||
{t('If the market was %s', [
|
||||
Schema.MarketStateMapping[marketState].toLowerCase(),
|
||||
])}
|
||||
</p>
|
||||
)}
|
||||
{role === MAKER && (
|
||||
<>
|
||||
<p className="mb-1">{t('The maker will receive the maker fee.')}</p>
|
||||
@@ -352,7 +302,7 @@ const FeesBreakdownTooltip = ({
|
||||
{role === TAKER && (
|
||||
<p className="mb-1">{t('Fees to be paid by the taker.')}</p>
|
||||
)}
|
||||
{role === '-' && (
|
||||
{(role === '-' || marketState === Schema.MarketState.STATE_SUSPENDED) && (
|
||||
<p className="mb-1">
|
||||
{t(
|
||||
'If the market is in monitoring auction, half of the infrastructure and liquidity fees will be paid.'
|
||||
@@ -393,8 +343,8 @@ const FeesDiscountBreakdownTooltipItem = ({
|
||||
}) =>
|
||||
value && value !== '0' ? (
|
||||
<>
|
||||
<dt className="col-span-1">{label}</dt>
|
||||
<dd className="text-right col-span-1">
|
||||
<dt className="col-span-2">{label}</dt>
|
||||
<dd className="text-right col-span-2">
|
||||
{addDecimalsFormatNumber(value, asset.decimals)} {asset.symbol}
|
||||
</dd>
|
||||
</>
|
||||
@@ -409,15 +359,19 @@ export const FeesDiscountBreakdownTooltip = ({
|
||||
}
|
||||
const asset = getAsset(data.market);
|
||||
|
||||
const { fees } = getRoleAndFees({ data, partyId }) ?? {};
|
||||
if (!fees) return null;
|
||||
|
||||
const {
|
||||
fees: roleFees,
|
||||
marketState,
|
||||
role,
|
||||
} = getRoleAndFees({ data, partyId }) ?? {};
|
||||
if (!roleFees) return null;
|
||||
const fees = getFeesBreakdown(role, roleFees, marketState);
|
||||
return (
|
||||
<div
|
||||
data-testid="fee-discount-breakdown-tooltip"
|
||||
className="max-w-sm bg-vega-light-100 dark:bg-vega-dark-100 border border-vega-light-200 dark:border-vega-dark-200 px-4 py-2 z-20 rounded text-sm break-word text-black dark:text-white"
|
||||
>
|
||||
<dl className="grid grid-cols-2 gap-x-1">
|
||||
<dl className="grid grid-cols-6 gap-x-1 text-xs">
|
||||
{(fees.infrastructureFeeReferralDiscount || '0') !== '0' ||
|
||||
(fees.infrastructureFeeVolumeDiscount || '0') !== '0' ? (
|
||||
<dt className="col-span-2">{t('Infrastructure Fee')}</dt>
|
||||
@@ -461,42 +415,14 @@ export const FeesDiscountBreakdownTooltip = ({
|
||||
label={t('Volume Discount')}
|
||||
asset={asset}
|
||||
/>
|
||||
|
||||
<dt className="col-span-2">{t('Total Fee Discount')}</dt>
|
||||
<FeesDiscountBreakdownTooltipItem
|
||||
value={fees.totalFeeDiscount}
|
||||
label={''}
|
||||
asset={asset}
|
||||
/>
|
||||
</dl>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export const getTotalFeesDiscounts = (fees: TradeFeeFieldsFragment) => {
|
||||
return (
|
||||
BigInt(fees.infrastructureFeeReferralDiscount || '0') +
|
||||
BigInt(fees.infrastructureFeeVolumeDiscount || '0') +
|
||||
BigInt(fees.liquidityFeeReferralDiscount || '0') +
|
||||
BigInt(fees.liquidityFeeVolumeDiscount || '0') +
|
||||
BigInt(fees.makerFeeReferralDiscount || '0') +
|
||||
BigInt(fees.makerFeeVolumeDiscount || '0')
|
||||
).toString();
|
||||
};
|
||||
|
||||
export const getFeesBreakdown = (
|
||||
role: Role,
|
||||
feesObj: TradeFeeFieldsFragment
|
||||
) => {
|
||||
const makerFee =
|
||||
role === MAKER
|
||||
? new BigNumber(feesObj.makerFee).times(-1).toString()
|
||||
: feesObj.makerFee;
|
||||
|
||||
const infrastructureFee = feesObj.infrastructureFee;
|
||||
const liquidityFee = feesObj.liquidityFee;
|
||||
|
||||
const totalFee = new BigNumber(infrastructureFee)
|
||||
.plus(makerFee)
|
||||
.plus(liquidityFee)
|
||||
.toString();
|
||||
return {
|
||||
infrastructureFee,
|
||||
liquidityFee,
|
||||
makerFee,
|
||||
totalFee,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
import { getFeesBreakdown } from './fills-utils';
|
||||
import * as Schema from '@vegaprotocol/types';
|
||||
|
||||
describe('getFeesBreakdown', () => {
|
||||
it('should return correct fees breakdown for a taker', () => {
|
||||
const fees = {
|
||||
makerFee: '1000',
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
makerFee: '1000',
|
||||
totalFee: '6000',
|
||||
totalFeeDiscount: '0',
|
||||
};
|
||||
expect(getFeesBreakdown('Taker', fees)).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct fees breakdown for a maker if market is active', () => {
|
||||
const fees = {
|
||||
makerFee: '1000',
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '0',
|
||||
liquidityFee: '0',
|
||||
makerFee: '-1000',
|
||||
totalFee: '-1000',
|
||||
totalFeeDiscount: '0',
|
||||
};
|
||||
expect(
|
||||
getFeesBreakdown('Maker', fees, Schema.MarketState.STATE_ACTIVE)
|
||||
).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct fees breakdown for a maker if the market is suspended', () => {
|
||||
const fees = {
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
makerFee: '0',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '1000',
|
||||
liquidityFee: '1500',
|
||||
makerFee: '0',
|
||||
totalFee: '2500',
|
||||
totalFeeDiscount: '0',
|
||||
};
|
||||
expect(
|
||||
getFeesBreakdown('Maker', fees, Schema.MarketState.STATE_SUSPENDED)
|
||||
).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct fees breakdown for a taker if the market is suspended', () => {
|
||||
const fees = {
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
makerFee: '0',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '1000',
|
||||
liquidityFee: '1500',
|
||||
makerFee: '0',
|
||||
totalFee: '2500',
|
||||
totalFeeDiscount: '0',
|
||||
};
|
||||
expect(
|
||||
getFeesBreakdown('Taker', fees, Schema.MarketState.STATE_SUSPENDED)
|
||||
).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct fees breakdown for a taker if market is active', () => {
|
||||
const fees = {
|
||||
makerFee: '1000',
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
makerFee: '1000',
|
||||
totalFee: '6000',
|
||||
totalFeeDiscount: '0',
|
||||
};
|
||||
expect(
|
||||
getFeesBreakdown('Taker', fees, Schema.MarketState.STATE_ACTIVE)
|
||||
).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct fees breakdown for a maker', () => {
|
||||
const fees = {
|
||||
makerFee: '1000',
|
||||
infrastructureFee: '2000',
|
||||
liquidityFee: '3000',
|
||||
};
|
||||
const expectedBreakdown = {
|
||||
infrastructureFee: '0',
|
||||
liquidityFee: '0',
|
||||
makerFee: '-1000',
|
||||
totalFee: '-1000',
|
||||
totalFeeDiscount: '0',
|
||||
};
|
||||
expect(getFeesBreakdown('Maker', fees)).toEqual(expectedBreakdown);
|
||||
});
|
||||
|
||||
it('should return correct total fees discount value for a taker (if the market is active - default)', () => {
|
||||
const fees = {
|
||||
infrastructureFeeReferralDiscount: '1',
|
||||
infrastructureFeeVolumeDiscount: '2',
|
||||
liquidityFeeReferralDiscount: '3',
|
||||
liquidityFeeVolumeDiscount: '4',
|
||||
makerFeeReferralDiscount: '5',
|
||||
makerFeeVolumeDiscount: '6',
|
||||
infrastructureFee: '1000',
|
||||
liquidityFee: '2000',
|
||||
makerFee: '3000',
|
||||
};
|
||||
const { totalFeeDiscount } = getFeesBreakdown('Taker', fees);
|
||||
expect(totalFeeDiscount).toEqual((1 + 2 + 3 + 4 + 5 + 6).toString());
|
||||
});
|
||||
|
||||
it('should return correct total fees discount value for a maker (if the market is active - default)', () => {
|
||||
const fees = {
|
||||
infrastructureFeeReferralDiscount: '1',
|
||||
infrastructureFeeVolumeDiscount: '2',
|
||||
liquidityFeeReferralDiscount: '3',
|
||||
liquidityFeeVolumeDiscount: '4',
|
||||
makerFeeReferralDiscount: '5',
|
||||
makerFeeVolumeDiscount: '6',
|
||||
infrastructureFee: '1000',
|
||||
liquidityFee: '2000',
|
||||
makerFee: '3000',
|
||||
};
|
||||
const { totalFeeDiscount } = getFeesBreakdown('Maker', fees);
|
||||
// makerFeeReferralDiscount and makerFeeVolumeDiscount are added, infra and liq. fees are zeroed
|
||||
expect(totalFeeDiscount).toEqual((5 + 6).toString());
|
||||
});
|
||||
|
||||
it('should return correct total fees discount value for a maker (if the market is suspended)', () => {
|
||||
const fees = {
|
||||
infrastructureFeeReferralDiscount: '1',
|
||||
infrastructureFeeVolumeDiscount: '2',
|
||||
liquidityFeeReferralDiscount: '3',
|
||||
liquidityFeeVolumeDiscount: '4',
|
||||
makerFeeReferralDiscount: '5',
|
||||
makerFeeVolumeDiscount: '6',
|
||||
infrastructureFee: '1000',
|
||||
liquidityFee: '2000',
|
||||
makerFee: '3000',
|
||||
};
|
||||
const { totalFeeDiscount } = getFeesBreakdown(
|
||||
'Maker',
|
||||
fees,
|
||||
Schema.MarketState.STATE_SUSPENDED
|
||||
);
|
||||
// makerFeeReferralDiscount and makerFeeVolumeDiscount are zeroed, infra and liq. fees are halved
|
||||
expect(totalFeeDiscount).toEqual(((1 + 2 + 3 + 4) / 2).toString());
|
||||
});
|
||||
|
||||
it('should return correct total fees discount value for a taker (if the market is suspended)', () => {
|
||||
const fees = {
|
||||
infrastructureFeeReferralDiscount: '1',
|
||||
infrastructureFeeVolumeDiscount: '2',
|
||||
liquidityFeeReferralDiscount: '3',
|
||||
liquidityFeeVolumeDiscount: '4',
|
||||
makerFeeReferralDiscount: '5',
|
||||
makerFeeVolumeDiscount: '6',
|
||||
infrastructureFee: '1000',
|
||||
liquidityFee: '2000',
|
||||
makerFee: '3000',
|
||||
};
|
||||
const { totalFeeDiscount } = getFeesBreakdown(
|
||||
'Taker',
|
||||
fees,
|
||||
Schema.MarketState.STATE_SUSPENDED
|
||||
);
|
||||
// makerFeeReferralDiscount and makerFeeVolumeDiscount are zeroed, infra and liq. fees are halved
|
||||
expect(totalFeeDiscount).toEqual(((1 + 2 + 3 + 4) / 2).toString());
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,164 @@
|
||||
import BigNumber from 'bignumber.js';
|
||||
import type {
|
||||
FillFieldsFragment,
|
||||
TradeFeeFieldsFragment,
|
||||
} from './__generated__/Fills';
|
||||
import * as Schema from '@vegaprotocol/types';
|
||||
|
||||
export const TAKER = 'Taker';
|
||||
export const MAKER = 'Maker';
|
||||
|
||||
export type Role = typeof TAKER | typeof MAKER | '-';
|
||||
|
||||
export const getRoleAndFees = ({
|
||||
data,
|
||||
partyId,
|
||||
}: {
|
||||
data: Pick<
|
||||
FillFieldsFragment,
|
||||
'buyerFee' | 'sellerFee' | 'buyer' | 'seller' | 'aggressor'
|
||||
>;
|
||||
partyId?: string;
|
||||
}): {
|
||||
role: Role;
|
||||
fees?: TradeFeeFieldsFragment;
|
||||
marketState?: Schema.MarketState;
|
||||
} => {
|
||||
let role: Role;
|
||||
let fees;
|
||||
|
||||
if (data?.buyer.id === partyId) {
|
||||
if (data.aggressor === Schema.Side.SIDE_BUY) {
|
||||
role = TAKER;
|
||||
fees = data?.buyerFee;
|
||||
} else if (data.aggressor === Schema.Side.SIDE_SELL) {
|
||||
role = MAKER;
|
||||
fees = data?.sellerFee;
|
||||
} else {
|
||||
role = '-';
|
||||
fees = !isEmptyFeeObj(data?.buyerFee) ? data.buyerFee : data.sellerFee;
|
||||
}
|
||||
} else if (data?.seller.id === partyId) {
|
||||
if (data.aggressor === Schema.Side.SIDE_SELL) {
|
||||
role = TAKER;
|
||||
fees = data?.sellerFee;
|
||||
} else if (data.aggressor === Schema.Side.SIDE_BUY) {
|
||||
role = MAKER;
|
||||
fees = data?.buyerFee;
|
||||
} else {
|
||||
role = '-';
|
||||
fees = !isEmptyFeeObj(data.sellerFee) ? data.sellerFee : data.buyerFee;
|
||||
}
|
||||
} else {
|
||||
return { role: '-', fees: undefined };
|
||||
}
|
||||
|
||||
// We make the assumption that the market state is active if the maker fee is zero on both sides
|
||||
// This needs to be updated when we have a way to get the correct market state when that fill happened from the API
|
||||
// because the maker fee factor can be set to 0 via governance
|
||||
const marketState =
|
||||
data?.buyerFee.makerFee === data.sellerFee.makerFee &&
|
||||
new BigNumber(data?.buyerFee.makerFee).isZero()
|
||||
? Schema.MarketState.STATE_SUSPENDED
|
||||
: Schema.MarketState.STATE_ACTIVE;
|
||||
return { role, fees, marketState };
|
||||
};
|
||||
|
||||
export const getFeesBreakdown = (
|
||||
role: Role,
|
||||
fees: TradeFeeFieldsFragment,
|
||||
marketState: Schema.MarketState = Schema.MarketState.STATE_ACTIVE
|
||||
) => {
|
||||
// If market is in auction we assume maker fee is zero
|
||||
const isMarketActive = marketState === Schema.MarketState.STATE_ACTIVE;
|
||||
|
||||
// If role is taker, then these are the fees to be paid
|
||||
let { makerFee, infrastructureFee, liquidityFee } = fees;
|
||||
// If role is taker, then these are the fees discounts to be applied
|
||||
let {
|
||||
makerFeeVolumeDiscount,
|
||||
makerFeeReferralDiscount,
|
||||
infrastructureFeeVolumeDiscount,
|
||||
infrastructureFeeReferralDiscount,
|
||||
liquidityFeeVolumeDiscount,
|
||||
liquidityFeeReferralDiscount,
|
||||
} = fees;
|
||||
|
||||
if (isMarketActive) {
|
||||
if (role === MAKER) {
|
||||
makerFee = new BigNumber(fees.makerFee).times(-1).toString();
|
||||
infrastructureFee = '0';
|
||||
liquidityFee = '0';
|
||||
|
||||
// discounts are also zero or we can leave them undefined
|
||||
infrastructureFeeReferralDiscount =
|
||||
infrastructureFeeReferralDiscount && '0';
|
||||
infrastructureFeeVolumeDiscount = infrastructureFeeVolumeDiscount && '0';
|
||||
liquidityFeeReferralDiscount = liquidityFeeReferralDiscount && '0';
|
||||
liquidityFeeVolumeDiscount = liquidityFeeVolumeDiscount && '0';
|
||||
|
||||
// we leave maker discount fees as they are defined
|
||||
}
|
||||
} else {
|
||||
// If market is suspended (in monitoring auction), then half of the fees are paid
|
||||
infrastructureFee = new BigNumber(infrastructureFee)
|
||||
.dividedBy(2)
|
||||
.toString();
|
||||
liquidityFee = new BigNumber(liquidityFee).dividedBy(2).toString();
|
||||
// maker fee is already zero
|
||||
makerFee = '0';
|
||||
|
||||
// discounts are also halved
|
||||
infrastructureFeeReferralDiscount =
|
||||
infrastructureFeeReferralDiscount &&
|
||||
new BigNumber(infrastructureFeeReferralDiscount).dividedBy(2).toString();
|
||||
infrastructureFeeVolumeDiscount =
|
||||
infrastructureFeeVolumeDiscount &&
|
||||
new BigNumber(infrastructureFeeVolumeDiscount).dividedBy(2).toString();
|
||||
liquidityFeeReferralDiscount =
|
||||
liquidityFeeReferralDiscount &&
|
||||
new BigNumber(liquidityFeeReferralDiscount).dividedBy(2).toString();
|
||||
liquidityFeeVolumeDiscount =
|
||||
liquidityFeeVolumeDiscount &&
|
||||
new BigNumber(liquidityFeeVolumeDiscount).dividedBy(2).toString();
|
||||
// maker discount fees should already be zero
|
||||
makerFeeReferralDiscount = makerFeeReferralDiscount && '0';
|
||||
makerFeeVolumeDiscount = makerFeeVolumeDiscount && '0';
|
||||
}
|
||||
|
||||
const totalFee = new BigNumber(infrastructureFee)
|
||||
.plus(makerFee)
|
||||
.plus(liquidityFee)
|
||||
.toString();
|
||||
|
||||
const totalFeeDiscount = new BigNumber(makerFeeVolumeDiscount || '0')
|
||||
.plus(makerFeeReferralDiscount || '0')
|
||||
.plus(infrastructureFeeReferralDiscount || '0')
|
||||
.plus(infrastructureFeeVolumeDiscount || '0')
|
||||
.plus(liquidityFeeReferralDiscount || '0')
|
||||
.plus(liquidityFeeVolumeDiscount || '0')
|
||||
.toString();
|
||||
|
||||
return {
|
||||
infrastructureFee,
|
||||
infrastructureFeeReferralDiscount,
|
||||
infrastructureFeeVolumeDiscount,
|
||||
liquidityFee,
|
||||
liquidityFeeReferralDiscount,
|
||||
liquidityFeeVolumeDiscount,
|
||||
makerFee,
|
||||
makerFeeReferralDiscount,
|
||||
makerFeeVolumeDiscount,
|
||||
totalFee,
|
||||
totalFeeDiscount,
|
||||
};
|
||||
};
|
||||
|
||||
export const isEmptyFeeObj = (feeObj: Schema.TradeFee) => {
|
||||
if (!feeObj) return true;
|
||||
return (
|
||||
feeObj.liquidityFee === '0' &&
|
||||
feeObj.makerFee === '0' &&
|
||||
feeObj.infrastructureFee === '0'
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user