Skip to content

Commit

Permalink
Split affiliate info fees by taker and maker (#2439)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryfan01234 authored Oct 3, 2024
1 parent af18d0c commit 12c2ee9
Show file tree
Hide file tree
Showing 13 changed files with 145 additions and 60 deletions.
15 changes: 9 additions & 6 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -990,9 +990,10 @@ export const defaultAffiliateInfo: AffiliateInfoCreateObject = {
affiliateEarnings: '10',
referredMakerTrades: 10,
referredTakerTrades: 20,
totalReferredFees: '10',
totalReferredMakerFees: '10',
totalReferredTakerFees: '10',
totalReferredMakerRebates: '-10',
totalReferredUsers: 5,
referredNetProtocolEarnings: '20',
firstReferralBlockHeight: '1',
referredTotalVolume: '1000',
};
Expand All @@ -1002,9 +1003,10 @@ export const defaultAffiliateInfo2: AffiliateInfoCreateObject = {
affiliateEarnings: '11',
referredMakerTrades: 11,
referredTakerTrades: 21,
totalReferredFees: '11',
totalReferredMakerFees: '11',
totalReferredTakerFees: '11',
totalReferredMakerRebates: '-11',
totalReferredUsers: 5,
referredNetProtocolEarnings: '21',
firstReferralBlockHeight: '11',
referredTotalVolume: '1000',
};
Expand All @@ -1014,9 +1016,10 @@ export const defaultAffiliateInfo3: AffiliateInfoCreateObject = {
affiliateEarnings: '12',
referredMakerTrades: 12,
referredTakerTrades: 22,
totalReferredFees: '12',
totalReferredMakerFees: '12',
totalReferredTakerFees: '12',
totalReferredMakerRebates: '-12',
totalReferredUsers: 10,
referredNetProtocolEarnings: '22',
firstReferralBlockHeight: '12',
referredTotalVolume: '1111111',
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ describe('Affiliate info store', () => {
affiliateEarnings: '1000',
referredMakerTrades: 1,
referredTakerTrades: 1,
totalReferredFees: '2000',
totalReferredMakerFees: '0',
totalReferredTakerFees: '1000',
totalReferredMakerRebates: '-1000',
totalReferredUsers: 1,
referredNetProtocolEarnings: '1000',
firstReferralBlockHeight: '1',
referredTotalVolume: '2',
};
Expand All @@ -140,9 +141,10 @@ describe('Affiliate info store', () => {
affiliateEarnings: '1000',
referredMakerTrades: 2,
referredTakerTrades: 0,
totalReferredFees: '2000',
totalReferredMakerFees: '2000',
totalReferredTakerFees: '0',
totalReferredMakerRebates: '0',
totalReferredUsers: 1,
referredNetProtocolEarnings: '1000',
firstReferralBlockHeight: '1',
referredTotalVolume: '2',
};
Expand All @@ -157,14 +159,15 @@ describe('Affiliate info store', () => {
const updatedInfo2 = await AffiliateInfoTable.findById(
defaultWallet2.address,
);
const expectedAffiliateInfo2 = {
const expectedAffiliateInfo2: AffiliateInfoFromDatabase = {
address: defaultWallet2.address,
affiliateEarnings: '2000',
referredMakerTrades: 3,
referredTakerTrades: 1,
totalReferredFees: '4000',
totalReferredMakerFees: '2000',
totalReferredTakerFees: '1000',
totalReferredMakerRebates: '-1000',
totalReferredUsers: 1,
referredNetProtocolEarnings: '2000',
firstReferralBlockHeight: '1',
referredTotalVolume: '4',
};
Expand All @@ -183,14 +186,15 @@ describe('Affiliate info store', () => {
const updatedInfo3 = await AffiliateInfoTable.findById(
defaultWallet2.address,
);
const expectedAffiliateInfo3 = {
const expectedAffiliateInfo3: AffiliateInfoFromDatabase = {
address: defaultWallet2.address,
affiliateEarnings: '2000',
referredMakerTrades: 3,
referredTakerTrades: 1,
totalReferredFees: '4000',
totalReferredMakerFees: '2000',
totalReferredTakerFees: '1000',
totalReferredMakerRebates: '-1000',
totalReferredUsers: 2,
referredNetProtocolEarnings: '2000',
firstReferralBlockHeight: '1',
referredTotalVolume: '4',
};
Expand Down Expand Up @@ -236,9 +240,10 @@ describe('Affiliate info store', () => {
affiliateEarnings: '0',
referredMakerTrades: 0,
referredTakerTrades: 0,
totalReferredFees: '0',
totalReferredMakerFees: '0',
totalReferredTakerFees: '0',
totalReferredMakerRebates: '0',
totalReferredUsers: 1,
referredNetProtocolEarnings: '0',
firstReferralBlockHeight: '2',
referredTotalVolume: '0',
};
Expand Down Expand Up @@ -391,7 +396,7 @@ async function populateFillsAndReferrals(): Promise<DateTime> {
eventId: defaultTendermintEventId2,
price: '1',
size: '1',
fee: '1000',
fee: '-1000',
affiliateRevShare: '500',
}),
FillTable.create({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('affiliate_info', (table) => {
table.dropColumn('totalReferredFees');
table.dropColumn('referredNetProtocolEarnings');
table.decimal('totalReferredTakerFees', null).notNullable().defaultTo(0);
table.decimal('totalReferredMakerFees', null).notNullable().defaultTo(0);
table.decimal('totalReferredMakerRebates', null).notNullable().defaultTo(0);
});
}

export async function down(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('affiliate_info', (table) => {
table.decimal('totalReferredFees', null).notNullable().defaultTo(0);
table.decimal('referredNetProtocolEarnings', null).notNullable().defaultTo(0);
table.dropColumn('totalReferredTakerFees');
table.dropColumn('totalReferredMakerFees');
table.dropColumn('totalReferredMakerRebates');
});
}
25 changes: 15 additions & 10 deletions indexer/packages/postgres/src/models/affiliate-info-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NonNegativeNumericPattern } from '../lib/validators';
import { NonNegativeNumericPattern, NumericPattern } from '../lib/validators';
import UpsertQueryBuilder from '../query-builders/upsert';
import BaseModel from './base-model';

Expand All @@ -19,9 +19,10 @@ export default class AffiliateInfoModel extends BaseModel {
'affiliateEarnings',
'referredMakerTrades',
'referredTakerTrades',
'totalReferredFees',
'totalReferredMakerFees',
'totalReferredTakerFees',
'totalReferredMakerRebates',
'totalReferredUsers',
'referredNetProtocolEarnings',
'firstReferralBlockHeight',
'referredTotalVolume',
],
Expand All @@ -30,9 +31,10 @@ export default class AffiliateInfoModel extends BaseModel {
affiliateEarnings: { type: 'string', pattern: NonNegativeNumericPattern },
referredMakerTrades: { type: 'int' },
referredTakerTrades: { type: 'int' },
totalReferredFees: { type: 'string', pattern: NonNegativeNumericPattern },
totalReferredMakerFees: { type: 'string', pattern: NonNegativeNumericPattern },
totalReferredTakerFees: { type: 'string', pattern: NonNegativeNumericPattern },
totalReferredMakerRebates: { type: 'string', pattern: NumericPattern },
totalReferredUsers: { type: 'int' },
referredNetProtocolEarnings: { type: 'string', pattern: NonNegativeNumericPattern },
firstReferralBlockHeight: { type: 'string', pattern: NonNegativeNumericPattern },
referredTotalVolume: { type: 'string', pattern: NonNegativeNumericPattern },
},
Expand All @@ -51,9 +53,10 @@ export default class AffiliateInfoModel extends BaseModel {
affiliateEarnings: 'string',
referredMakerTrades: 'int',
referredTakerTrades: 'int',
totalReferredFees: 'string',
totalReferredMakerFees: 'string',
totalReferredTakerFees: 'string',
totalReferredMakerRebates: 'string',
totalReferredUsers: 'int',
referredNetProtocolEarnings: 'string',
firstReferralBlockHeight: 'string',
referredTotalVolume: 'string',
};
Expand All @@ -69,11 +72,13 @@ export default class AffiliateInfoModel extends BaseModel {

referredTakerTrades!: number;

totalReferredFees!: string;
totalReferredMakerFees!: string;

totalReferredUsers!: number;
totalReferredTakerFees!: string;

totalReferredMakerRebates!: string;

referredNetProtocolEarnings!: string;
totalReferredUsers!: number;

firstReferralBlockHeight!: string;

Expand Down
24 changes: 15 additions & 9 deletions indexer/packages/postgres/src/stores/affiliate-info-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ affiliate_stats AS (
affiliate_fills."affiliateAddress",
SUM(affiliate_fills."fee") AS "totalReferredFees",
SUM(affiliate_fills."affiliateRevShare") AS "affiliateEarnings",
SUM(affiliate_fills."fee") - SUM(affiliate_fills."affiliateRevShare") AS "referredNetProtocolEarnings",
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.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",
SUM(affiliate_fills."price" * affiliate_fills."size") AS "referredTotalVolume"
Expand All @@ -210,9 +212,10 @@ affiliate_info_update AS (
affiliate_metadata."affiliateAddress",
affiliate_metadata."totalReferredUsers",
affiliate_metadata."firstReferralBlockHeight",
COALESCE(affiliate_stats."totalReferredFees", 0) AS "totalReferredFees",
COALESCE(affiliate_stats."totalReferredMakerFees", 0) AS "totalReferredMakerFees",
COALESCE(affiliate_stats."totalReferredTakerFees", 0) AS "totalReferredTakerFees",
COALESCE(affiliate_stats."totalReferredMakerRebates", 0) AS "totalReferredMakerRebates",
COALESCE(affiliate_stats."affiliateEarnings", 0) AS "affiliateEarnings",
COALESCE(affiliate_stats."referredNetProtocolEarnings", 0) AS "referredNetProtocolEarnings",
COALESCE(affiliate_stats."referredMakerTrades", 0) AS "referredMakerTrades",
COALESCE(affiliate_stats."referredTakerTrades", 0) AS "referredTakerTrades",
COALESCE(affiliate_stats."referredTotalVolume", 0) AS "referredTotalVolume"
Expand All @@ -231,8 +234,9 @@ INSERT INTO affiliate_info (
"affiliateEarnings",
"referredMakerTrades",
"referredTakerTrades",
"totalReferredFees",
"referredNetProtocolEarnings",
"totalReferredMakerFees",
"totalReferredTakerFees",
"totalReferredMakerRebates",
"referredTotalVolume"
)
SELECT
Expand All @@ -242,8 +246,9 @@ SELECT
"affiliateEarnings",
"referredMakerTrades",
"referredTakerTrades",
"totalReferredFees",
"referredNetProtocolEarnings",
"totalReferredMakerFees",
"totalReferredTakerFees",
"totalReferredMakerRebates",
"referredTotalVolume"
FROM
affiliate_info_update
Expand All @@ -254,8 +259,9 @@ DO UPDATE SET
"affiliateEarnings" = affiliate_info."affiliateEarnings" + EXCLUDED."affiliateEarnings",
"referredMakerTrades" = affiliate_info."referredMakerTrades" + EXCLUDED."referredMakerTrades",
"referredTakerTrades" = affiliate_info."referredTakerTrades" + EXCLUDED."referredTakerTrades",
"totalReferredFees" = affiliate_info."totalReferredFees" + EXCLUDED."totalReferredFees",
"referredNetProtocolEarnings" = affiliate_info."referredNetProtocolEarnings" + EXCLUDED."referredNetProtocolEarnings",
"totalReferredMakerFees" = affiliate_info."totalReferredMakerFees" + EXCLUDED."totalReferredMakerFees",
"totalReferredTakerFees" = affiliate_info."totalReferredTakerFees" + EXCLUDED."totalReferredTakerFees",
"totalReferredMakerRebates" = affiliate_info."totalReferredMakerRebates" + EXCLUDED."totalReferredMakerRebates",
"referredTotalVolume" = affiliate_info."referredTotalVolume" + EXCLUDED."referredTotalVolume";
`;

Expand Down
10 changes: 6 additions & 4 deletions indexer/packages/postgres/src/types/affiliate-info-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ export interface AffiliateInfoCreateObject {
affiliateEarnings: string,
referredMakerTrades: number,
referredTakerTrades: number,
totalReferredFees: string,
totalReferredMakerFees: string,
totalReferredTakerFees: string,
totalReferredMakerRebates: string,
totalReferredUsers: number,
referredNetProtocolEarnings: string,
firstReferralBlockHeight: string,
referredTotalVolume: string,
}
Expand All @@ -15,9 +16,10 @@ export enum AffiliateInfoColumns {
affiliateEarnings = 'affiliateEarnings',
referredMakerTrades = 'referredMakerTrades',
referredTakerTrades = 'referredTakerTrades',
totalReferredFees = 'totalReferredFees',
totalReferredMakerFees = 'totalReferredMakerFees',
totalReferredTakerFees = 'totalReferredTakerFees',
totalReferredMakerRebates = 'totalReferredMakerRebates',
totalReferredUsers = 'totalReferredUsers',
referredNetProtocolEarnings = 'referredNetProtocolEarnings',
firstReferralBlockHeight = 'firstReferralBlockHeight',
referredTotalVolume = 'referredTotalVolume',
}
5 changes: 3 additions & 2 deletions indexer/packages/postgres/src/types/db-model-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,10 @@ export interface AffiliateInfoFromDatabase {
affiliateEarnings: string,
referredMakerTrades: number,
referredTakerTrades: number,
totalReferredFees: string,
totalReferredMakerFees: string,
totalReferredTakerFees: string,
totalReferredMakerRebates: string,
totalReferredUsers: number,
referredNetProtocolEarnings: string,
firstReferralBlockHeight: string,
referredTotalVolume: string,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,9 +369,16 @@ function affiliateInfoCreateToResponseObject(
affiliateEarnings: Number(info.affiliateEarnings),
affiliateReferredTrades:
Number(info.referredTakerTrades) + Number(info.referredMakerTrades),
affiliateTotalReferredFees: Number(info.totalReferredFees),
affiliateTotalReferredFees: Number(info.totalReferredMakerFees) +
Number(info.totalReferredTakerFees) +
Number(info.totalReferredMakerRebates),
affiliateReferredUsers: Number(info.totalReferredUsers),
affiliateReferredNetProtocolEarnings: Number(info.referredNetProtocolEarnings),
affiliateReferredNetProtocolEarnings: Number(info.totalReferredMakerFees) +
Number(info.totalReferredTakerFees) +
Number(info.totalReferredMakerRebates) -
Number(info.affiliateEarnings),
affiliateReferredTotalVolume: Number(info.referredTotalVolume),
affiliateReferredMakerFees: Number(info.totalReferredMakerFees),
affiliateReferredTakerFees: Number(info.totalReferredTakerFees),
};
}
14 changes: 11 additions & 3 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,9 @@ fetch(`${baseURL}/affiliates/snapshot`,
"affiliateTotalReferredFees": 0.1,
"affiliateReferredUsers": 0.1,
"affiliateReferredNetProtocolEarnings": 0.1,
"affiliateReferredTotalVolume": 0.1
"affiliateReferredTotalVolume": 0.1,
"affiliateReferredMakerFees": 0.1,
"affiliateReferredTakerFees": 0.1
}
],
"total": 0.1,
Expand Down Expand Up @@ -4244,7 +4246,9 @@ This operation does not require authentication
"affiliateTotalReferredFees": 0.1,
"affiliateReferredUsers": 0.1,
"affiliateReferredNetProtocolEarnings": 0.1,
"affiliateReferredTotalVolume": 0.1
"affiliateReferredTotalVolume": 0.1,
"affiliateReferredMakerFees": 0.1,
"affiliateReferredTakerFees": 0.1
}

```
Expand All @@ -4261,6 +4265,8 @@ This operation does not require authentication
|affiliateReferredUsers|number(double)|true|none|none|
|affiliateReferredNetProtocolEarnings|number(double)|true|none|none|
|affiliateReferredTotalVolume|number(double)|true|none|none|
|affiliateReferredMakerFees|number(double)|true|none|none|
|affiliateReferredTakerFees|number(double)|true|none|none|

## AffiliateSnapshotResponse

Expand All @@ -4280,7 +4286,9 @@ This operation does not require authentication
"affiliateTotalReferredFees": 0.1,
"affiliateReferredUsers": 0.1,
"affiliateReferredNetProtocolEarnings": 0.1,
"affiliateReferredTotalVolume": 0.1
"affiliateReferredTotalVolume": 0.1,
"affiliateReferredMakerFees": 0.1,
"affiliateReferredTakerFees": 0.1
}
],
"total": 0.1,
Expand Down
12 changes: 11 additions & 1 deletion indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@
"affiliateReferredTotalVolume": {
"type": "number",
"format": "double"
},
"affiliateReferredMakerFees": {
"type": "number",
"format": "double"
},
"affiliateReferredTakerFees": {
"type": "number",
"format": "double"
}
},
"required": [
Expand All @@ -313,7 +321,9 @@
"affiliateTotalReferredFees",
"affiliateReferredUsers",
"affiliateReferredNetProtocolEarnings",
"affiliateReferredTotalVolume"
"affiliateReferredTotalVolume",
"affiliateReferredMakerFees",
"affiliateReferredTakerFees"
],
"type": "object",
"additionalProperties": false
Expand Down
Loading

0 comments on commit 12c2ee9

Please sign in to comment.