Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split affiliate info fees by taker and maker #2439

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we separate positive maker fees from negative maker fees?

Copy link
Contributor Author

@jerryfan01234 jerryfan01234 Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add totalReferredMakerRebates for negative, and keep totalReferredMakerFees for positive

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' },
jerryfan01234 marked this conversation as resolved.
Show resolved Hide resolved
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,
jerryfan01234 marked this conversation as resolved.
Show resolved Hide resolved
totalReferredMakerRebates: string,
Comment on lines +6 to +8
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

Action Required: Residual References Found for referredNetProtocolEarnings

The property referredNetProtocolEarnings is still referenced in several migration files:

  • indexer/packages/postgres/src/db/migrations/migration_files/20240919142157_change_affiliate_info_default_value.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240830165511_create_affiliate_info_table.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts
  • indexer/packages/postgres/src/db/migrations/migration_files/20240913142157_change_affiliate_info_decimal_precision_and_add_total_referred_volume.ts

Please address these residual references to ensure database migrations function correctly without the removed property.

🔗 Analysis chain

LGTM! Changes align with PR objectives.

The addition of totalReferredMakerFees, totalReferredTakerFees, and totalReferredMakerRebates aligns well with the PR objective of splitting affiliate info fees by taker and maker. This change provides more granular tracking of fees and rebates.

Please verify the impact of removing the referredNetProtocolEarnings property. A previous review comment indicated its usage in migration scripts. Run the following script to check for any remaining usage of this property in the codebase:

If there are any occurrences, ensure they are updated accordingly.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for usage of removed property 'referredNetProtocolEarnings'

# Test: Search for 'referredNetProtocolEarnings' in TypeScript files
rg --type typescript 'referredNetProtocolEarnings'

Length of output: 85


Script:

#!/bin/bash
# Description: Check for usage of removed property 'referredNetProtocolEarnings'

# Test: Search for 'referredNetProtocolEarnings' in TypeScript files
rg 'referredNetProtocolEarnings' --glob '*.ts' --glob '*.tsx'

Length of output: 1396

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),
jerryfan01234 marked this conversation as resolved.
Show resolved Hide resolved
};
}
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
Loading