Skip to content

Commit

Permalink
fix(affiliates): exclude liquidation fees in referred fee calculation…
Browse files Browse the repository at this point in the history
… (backport #2585) (#2587)

Co-authored-by: Teddy Ding <teddy@dydx.exchange>
  • Loading branch information
mergify[bot] and teddyding authored Nov 20, 2024
1 parent a2ea55f commit bf515a6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
AffiliateInfoFromDatabase, Liquidity,
AffiliateInfoFromDatabase, Liquidity, FillType,
} from '../../src/types';
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers';
import {
Expand Down Expand Up @@ -138,15 +138,15 @@ describe('Affiliate info store', () => {
);
const expectedAffiliateInfo1: AffiliateInfoFromDatabase = {
address: defaultWallet2.address,
affiliateEarnings: '1000',
referredMakerTrades: 2,
referredTakerTrades: 0,
totalReferredMakerFees: '2000',
affiliateEarnings: '1005',
referredMakerTrades: 3,
referredTakerTrades: 1,
totalReferredMakerFees: '2100',
totalReferredTakerFees: '0',
totalReferredMakerRebates: '0',
totalReferredUsers: 1,
firstReferralBlockHeight: '1',
referredTotalVolume: '2',
referredTotalVolume: '4',
};
expect(updatedInfo1).toEqual(expectedAffiliateInfo1);

Expand All @@ -161,15 +161,15 @@ describe('Affiliate info store', () => {
);
const expectedAffiliateInfo2: AffiliateInfoFromDatabase = {
address: defaultWallet2.address,
affiliateEarnings: '2000',
referredMakerTrades: 3,
referredTakerTrades: 1,
totalReferredMakerFees: '2000',
affiliateEarnings: '2005',
referredMakerTrades: 4,
referredTakerTrades: 2,
totalReferredMakerFees: '2100',
totalReferredTakerFees: '1000',
totalReferredMakerRebates: '-1000',
totalReferredUsers: 1,
firstReferralBlockHeight: '1',
referredTotalVolume: '4',
referredTotalVolume: '6',
};
expect(updatedInfo2).toEqual(expectedAffiliateInfo2);

Expand All @@ -188,15 +188,15 @@ describe('Affiliate info store', () => {
);
const expectedAffiliateInfo3: AffiliateInfoFromDatabase = {
address: defaultWallet2.address,
affiliateEarnings: '2000',
referredMakerTrades: 3,
referredTakerTrades: 1,
totalReferredMakerFees: '2000',
affiliateEarnings: '2005',
referredMakerTrades: 4,
referredTakerTrades: 2,
totalReferredMakerFees: '2100',
totalReferredTakerFees: '1000',
totalReferredMakerRebates: '-1000',
totalReferredUsers: 2,
firstReferralBlockHeight: '1',
referredTotalVolume: '4',
referredTotalVolume: '6',
};
expect(updatedInfo3).toEqual(expectedAffiliateInfo3);
});
Expand Down Expand Up @@ -303,7 +303,7 @@ describe('Affiliate info store', () => {
}));
});

it('Successfully uses offset and limit', async () => {
it('Successfully uses offset (default to sorted) and limit', async () => {
const infos: AffiliateInfoFromDatabase[] = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
[],
Expand All @@ -315,13 +315,15 @@ describe('Affiliate info store', () => {
expect(infos!.length).toEqual(2);
expect(infos![0]).toEqual(expect.objectContaining({
...defaultAffiliateInfo,
address: 'address_5',
affiliateEarnings: '5',
address: 'address_4',
// affiliateEarnings in DB: 9, 8, 7, 6, 5, 4, ...
// so we get 4 with offset = 5.
affiliateEarnings: '4',
}));
expect(infos![1]).toEqual(expect.objectContaining({
...defaultAffiliateInfo,
address: 'address_6',
affiliateEarnings: '6',
address: 'address_3',
affiliateEarnings: '3',
}));
});

Expand Down Expand Up @@ -358,6 +360,34 @@ describe('Affiliate info store', () => {
expect(infos).toBeDefined();
expect(infos!.length).toEqual(0);
});

it('Successfully use sorted - equal earnings between affiliates', async () => {
await AffiliateInfoTable.create({
...defaultAffiliateInfo,
address: 'address_10',
affiliateEarnings: '9', // same as address_9
});
const infos: AffiliateInfoFromDatabase[] = await AffiliateInfoTable
.paginatedFindWithAddressFilter(
[],
0,
100,
true,
);
expect(infos).toBeDefined();
expect(infos!.length).toEqual(11);
expect(infos![0]).toEqual(expect.objectContaining({
...defaultAffiliateInfo,
address: 'address_10', // '10' < '9' in lexicographical order
affiliateEarnings: '9',
}));
expect(infos![1]).toEqual(expect.objectContaining({
...defaultAffiliateInfo,
address: 'address_9',
affiliateEarnings: '9',
}));
});

});
});

Expand Down Expand Up @@ -421,6 +451,30 @@ async function populateFillsAndReferrals(): Promise<DateTime> {
fee: '1000',
affiliateRevShare: '500',
}),
FillTable.create({
...defaultFill,
liquidity: Liquidity.TAKER,
subaccountId: defaultOrder.subaccountId,
createdAt: referenceDt.minus({ minutes: 2 }).toISO(),
eventId: defaultTendermintEventId4,
price: '1',
size: '1',
fee: '1000',
affiliateRevShare: '0',
type: FillType.LIQUIDATED,
}),
FillTable.create({
...defaultFill,
liquidity: Liquidity.MAKER,
subaccountId: defaultOrder.subaccountId,
createdAt: referenceDt.minus({ minutes: 2 }).toISO(),
eventId: defaultTendermintEventId,
price: '1',
size: '1',
fee: '100',
affiliateRevShare: '5',
type: FillType.LIQUIDATION,
}),
]);

return referenceDt;
Expand Down
12 changes: 8 additions & 4 deletions indexer/packages/postgres/src/stores/affiliate-info-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
AffiliateInfoFromDatabase,
AffiliateInfoQueryConfig,
Liquidity,
FillType,
} from '../types';

export async function findAll(
Expand Down Expand Up @@ -156,7 +157,8 @@ filtered_fills AS (
fills."affiliateRevShare",
fills."createdAtHeight",
fills."price",
fills."size"
fills."size",
fills."type"
FROM
fills
WHERE
Expand All @@ -174,6 +176,7 @@ affiliate_fills AS (
filtered_fills."affiliateRevShare",
filtered_fills."price",
filtered_fills."size",
filtered_fills."type",
affiliate_referred_subaccounts."affiliateAddress",
affiliate_referred_subaccounts."referredAtBlock"
FROM
Expand All @@ -193,7 +196,7 @@ affiliate_stats AS (
SUM(affiliate_fills."fee") AS "totalReferredFees",
SUM(affiliate_fills."affiliateRevShare") AS "affiliateEarnings",
SUM(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.MAKER}' AND affiliate_fills."fee" > 0 THEN affiliate_fills."fee" ELSE 0 END) AS "totalReferredMakerFees",
SUM(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.TAKER}' THEN affiliate_fills."fee" ELSE 0 END) AS "totalReferredTakerFees",
SUM(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.TAKER}' AND affiliate_fills."type" = '${FillType.LIMIT}' THEN affiliate_fills."fee" ELSE 0 END) AS "totalReferredTakerFees",
SUM(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.MAKER}' AND affiliate_fills."fee" < 0 THEN affiliate_fills."fee" ELSE 0 END) AS "totalReferredMakerRebates",
COUNT(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.MAKER}' THEN 1 END) AS "referredMakerTrades",
COUNT(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.TAKER}' THEN 1 END) AS "referredTakerTrades",
Expand Down Expand Up @@ -301,8 +304,9 @@ export async function paginatedFindWithAddressFilter(
}

// Sorting by affiliate earnings or default sorting by address
if (sortByAffiliateEarning) {
baseQuery = baseQuery.orderBy(AffiliateInfoColumns.affiliateEarnings, Ordering.DESC);
if (sortByAffiliateEarning || offset !== 0) {
baseQuery = baseQuery.orderBy(AffiliateInfoColumns.affiliateEarnings, Ordering.DESC)
.orderBy(AffiliateInfoColumns.address, Ordering.ASC);
}

// Apply pagination using offset and limit
Expand Down

0 comments on commit bf515a6

Please sign in to comment.