Skip to content

Commit

Permalink
Split affiliate info fees by taker and maker
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryfan01234 committed Oct 2, 2024
1 parent e2438dd commit 5a15dab
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 62 deletions.
12 changes: 6 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,9 @@ export const defaultAffiliateInfo: AffiliateInfoCreateObject = {
affiliateEarnings: '10',
referredMakerTrades: 10,
referredTakerTrades: 20,
totalReferredFees: '10',
totalReferredMakerFees: '-10',
totalReferredTakerFees: '10',
totalReferredUsers: 5,
referredNetProtocolEarnings: '20',
firstReferralBlockHeight: '1',
referredTotalVolume: '1000',
};
Expand All @@ -1002,9 +1002,9 @@ export const defaultAffiliateInfo2: AffiliateInfoCreateObject = {
affiliateEarnings: '11',
referredMakerTrades: 11,
referredTakerTrades: 21,
totalReferredFees: '11',
totalReferredMakerFees: '-11',
totalReferredTakerFees: '11',
totalReferredUsers: 5,
referredNetProtocolEarnings: '21',
firstReferralBlockHeight: '11',
referredTotalVolume: '1000',
};
Expand All @@ -1014,9 +1014,9 @@ export const defaultAffiliateInfo3: AffiliateInfoCreateObject = {
affiliateEarnings: '12',
referredMakerTrades: 12,
referredTakerTrades: 22,
totalReferredFees: '12',
totalReferredMakerFees: '-12',
totalReferredTakerFees: '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,9 @@ describe('Affiliate info store', () => {
affiliateEarnings: '1000',
referredMakerTrades: 1,
referredTakerTrades: 1,
totalReferredFees: '2000',
totalReferredMakerFees: '-1000',
totalReferredTakerFees: '1000',
totalReferredUsers: 1,
referredNetProtocolEarnings: '1000',
firstReferralBlockHeight: '1',
referredTotalVolume: '2',
};
Expand All @@ -140,9 +140,9 @@ describe('Affiliate info store', () => {
affiliateEarnings: '1000',
referredMakerTrades: 2,
referredTakerTrades: 0,
totalReferredFees: '2000',
totalReferredMakerFees: '-2000',
totalReferredTakerFees: '0',
totalReferredUsers: 1,
referredNetProtocolEarnings: '1000',
firstReferralBlockHeight: '1',
referredTotalVolume: '2',
};
Expand All @@ -157,14 +157,14 @@ 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: '-3000',
totalReferredTakerFees: '1000',
totalReferredUsers: 1,
referredNetProtocolEarnings: '2000',
firstReferralBlockHeight: '1',
referredTotalVolume: '4',
};
Expand All @@ -183,14 +183,14 @@ 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: '-3000',
totalReferredTakerFees: '1000',
totalReferredUsers: 2,
referredNetProtocolEarnings: '2000',
firstReferralBlockHeight: '1',
referredTotalVolume: '4',
};
Expand Down Expand Up @@ -236,9 +236,9 @@ describe('Affiliate info store', () => {
affiliateEarnings: '0',
referredMakerTrades: 0,
referredTakerTrades: 0,
totalReferredFees: '0',
totalReferredMakerFees: '0',
totalReferredTakerFees: '0',
totalReferredUsers: 1,
referredNetProtocolEarnings: '0',
firstReferralBlockHeight: '2',
referredTotalVolume: '0',
};
Expand Down Expand Up @@ -391,7 +391,7 @@ async function populateFillsAndReferrals(): Promise<DateTime> {
eventId: defaultTendermintEventId2,
price: '1',
size: '1',
fee: '1000',
fee: '-1000',
affiliateRevShare: '500',
}),
FillTable.create({
Expand All @@ -402,7 +402,7 @@ async function populateFillsAndReferrals(): Promise<DateTime> {
eventId: defaultTendermintEventId3,
price: '1',
size: '1',
fee: '1000',
fee: '-1000',
affiliateRevShare: '500',
}),
FillTable.create({
Expand All @@ -413,7 +413,7 @@ async function populateFillsAndReferrals(): Promise<DateTime> {
eventId: defaultTendermintEventId4,
price: '1',
size: '1',
fee: '1000',
fee: '-1000',
affiliateRevShare: '500',
}),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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);
});
}

export async function down(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('affiliate_info', (table) => {
table.decimal('totalReferredFees', null).notNullable().defaultTo(0).alter();
table.decimal('referredNetProtocolEarnings', null).notNullable().defaultTo(0).alter();
table.dropColumn('totalReferredTakerFees');
table.dropColumn('totalReferredMakerFees');
});
}
20 changes: 10 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,9 @@ export default class AffiliateInfoModel extends BaseModel {
'affiliateEarnings',
'referredMakerTrades',
'referredTakerTrades',
'totalReferredFees',
'totalReferredMakerFees',
'totalReferredTakerFees',
'totalReferredUsers',
'referredNetProtocolEarnings',
'firstReferralBlockHeight',
'referredTotalVolume',
],
Expand All @@ -30,9 +30,9 @@ 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: NumericPattern },
totalReferredTakerFees: { 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 +51,9 @@ export default class AffiliateInfoModel extends BaseModel {
affiliateEarnings: 'string',
referredMakerTrades: 'int',
referredTakerTrades: 'int',
totalReferredFees: 'string',
totalReferredMakerFees: 'string',
totalReferredTakerFees: 'string',
totalReferredUsers: 'int',
referredNetProtocolEarnings: 'string',
firstReferralBlockHeight: 'string',
referredTotalVolume: 'string',
};
Expand All @@ -69,11 +69,11 @@ export default class AffiliateInfoModel extends BaseModel {

referredTakerTrades!: number;

totalReferredFees!: string;
totalReferredMakerFees!: string;

totalReferredUsers!: number;
totalReferredTakerFees!: string;

referredNetProtocolEarnings!: string;
totalReferredUsers!: number;

firstReferralBlockHeight!: string;

Expand Down
19 changes: 10 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,8 @@ 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}' 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",
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 +211,9 @@ 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."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 +232,8 @@ INSERT INTO affiliate_info (
"affiliateEarnings",
"referredMakerTrades",
"referredTakerTrades",
"totalReferredFees",
"referredNetProtocolEarnings",
"totalReferredMakerFees",
"totalReferredTakerFees",
"referredTotalVolume"
)
SELECT
Expand All @@ -242,8 +243,8 @@ SELECT
"affiliateEarnings",
"referredMakerTrades",
"referredTakerTrades",
"totalReferredFees",
"referredNetProtocolEarnings",
"totalReferredMakerFees",
"totalReferredTakerFees",
"referredTotalVolume"
FROM
affiliate_info_update
Expand All @@ -254,8 +255,8 @@ 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",
"referredTotalVolume" = affiliate_info."referredTotalVolume" + EXCLUDED."referredTotalVolume";
`;

Expand Down
8 changes: 4 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,9 @@ export interface AffiliateInfoCreateObject {
affiliateEarnings: string,
referredMakerTrades: number,
referredTakerTrades: number,
totalReferredFees: string,
totalReferredMakerFees: string,
totalReferredTakerFees: string,
totalReferredUsers: number,
referredNetProtocolEarnings: string,
firstReferralBlockHeight: string,
referredTotalVolume: string,
}
Expand All @@ -15,9 +15,9 @@ export enum AffiliateInfoColumns {
affiliateEarnings = 'affiliateEarnings',
referredMakerTrades = 'referredMakerTrades',
referredTakerTrades = 'referredTakerTrades',
totalReferredFees = 'totalReferredFees',
totalReferredMakerFees = 'totalReferredMakerFees',
totalReferredTakerFees = 'totalReferredTakerFees',
totalReferredUsers = 'totalReferredUsers',
referredNetProtocolEarnings = 'referredNetProtocolEarnings',
firstReferralBlockHeight = 'firstReferralBlockHeight',
referredTotalVolume = 'referredTotalVolume',
}
4 changes: 2 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,9 @@ export interface AffiliateInfoFromDatabase {
affiliateEarnings: string,
referredMakerTrades: number,
referredTakerTrades: number,
totalReferredFees: string,
totalReferredMakerFees: string,
totalReferredTakerFees: 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,14 @@ function affiliateInfoCreateToResponseObject(
affiliateEarnings: Number(info.affiliateEarnings),
affiliateReferredTrades:
Number(info.referredTakerTrades) + Number(info.referredMakerTrades),
affiliateTotalReferredFees: Number(info.totalReferredFees),
affiliateTotalReferredFees: Number(info.totalReferredMakerFees) +
Number(info.totalReferredTakerFees),
affiliateReferredUsers: Number(info.totalReferredUsers),
affiliateReferredNetProtocolEarnings: Number(info.referredNetProtocolEarnings),
affiliateReferredNetProtocolEarnings: Number(info.totalReferredMakerFees) +
Number(info.totalReferredTakerFees) -
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 5a15dab

Please sign in to comment.