From 5a15dab4c9e856f35dc01d4051857a70564d5bba Mon Sep 17 00:00:00 2001 From: Jerry Fan Date: Wed, 2 Oct 2024 16:05:43 -0400 Subject: [PATCH 1/2] Split affiliate info fees by taker and maker --- .../postgres/__tests__/helpers/constants.ts | 12 ++++---- .../stores/affiliate-info-table.test.ts | 30 +++++++++---------- ...44813_change_affiliate_info_fee_columns.ts | 23 ++++++++++++++ .../src/models/affiliate-info-model.ts | 20 ++++++------- .../src/stores/affiliate-info-table.ts | 19 ++++++------ .../src/types/affiliate-info-types.ts | 8 ++--- .../postgres/src/types/db-model-types.ts | 4 +-- .../api/v4/affiliates-controller.test.ts | 9 ++++-- .../comlink/public/api-documentation.md | 14 +++++++-- indexer/services/comlink/public/swagger.json | 12 +++++++- .../api/v4/affiliates-controller.ts | 9 ++++-- indexer/services/comlink/src/types.ts | 2 ++ .../tasks/update-affiliate-info.test.ts | 16 +++++----- 13 files changed, 116 insertions(+), 62 deletions(-) create mode 100644 indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index 56e2865227..118663429f 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -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', }; @@ -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', }; @@ -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', }; diff --git a/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts b/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts index 04790001c8..f2c26c1475 100644 --- a/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts +++ b/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts @@ -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', }; @@ -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', }; @@ -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', }; @@ -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', }; @@ -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', }; @@ -391,7 +391,7 @@ async function populateFillsAndReferrals(): Promise { eventId: defaultTendermintEventId2, price: '1', size: '1', - fee: '1000', + fee: '-1000', affiliateRevShare: '500', }), FillTable.create({ @@ -402,7 +402,7 @@ async function populateFillsAndReferrals(): Promise { eventId: defaultTendermintEventId3, price: '1', size: '1', - fee: '1000', + fee: '-1000', affiliateRevShare: '500', }), FillTable.create({ @@ -413,7 +413,7 @@ async function populateFillsAndReferrals(): Promise { eventId: defaultTendermintEventId4, price: '1', size: '1', - fee: '1000', + fee: '-1000', affiliateRevShare: '500', }), ]); diff --git a/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts b/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts new file mode 100644 index 0000000000..db951a7dc3 --- /dev/null +++ b/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts @@ -0,0 +1,23 @@ +import * as Knex from 'knex'; + +export async function up(knex: Knex): Promise { + 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 { + 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'); + }); +} diff --git a/indexer/packages/postgres/src/models/affiliate-info-model.ts b/indexer/packages/postgres/src/models/affiliate-info-model.ts index 7fcbefac39..304004a1d5 100644 --- a/indexer/packages/postgres/src/models/affiliate-info-model.ts +++ b/indexer/packages/postgres/src/models/affiliate-info-model.ts @@ -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'; @@ -19,9 +19,9 @@ export default class AffiliateInfoModel extends BaseModel { 'affiliateEarnings', 'referredMakerTrades', 'referredTakerTrades', - 'totalReferredFees', + 'totalReferredMakerFees', + 'totalReferredTakerFees', 'totalReferredUsers', - 'referredNetProtocolEarnings', 'firstReferralBlockHeight', 'referredTotalVolume', ], @@ -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 }, }, @@ -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', }; @@ -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; diff --git a/indexer/packages/postgres/src/stores/affiliate-info-table.ts b/indexer/packages/postgres/src/stores/affiliate-info-table.ts index 6c2ab2adc4..61b5a9d284 100644 --- a/indexer/packages/postgres/src/stores/affiliate-info-table.ts +++ b/indexer/packages/postgres/src/stores/affiliate-info-table.ts @@ -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" @@ -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" @@ -231,8 +232,8 @@ INSERT INTO affiliate_info ( "affiliateEarnings", "referredMakerTrades", "referredTakerTrades", - "totalReferredFees", - "referredNetProtocolEarnings", + "totalReferredMakerFees", + "totalReferredTakerFees", "referredTotalVolume" ) SELECT @@ -242,8 +243,8 @@ SELECT "affiliateEarnings", "referredMakerTrades", "referredTakerTrades", - "totalReferredFees", - "referredNetProtocolEarnings", + "totalReferredMakerFees", + "totalReferredTakerFees", "referredTotalVolume" FROM affiliate_info_update @@ -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"; `; diff --git a/indexer/packages/postgres/src/types/affiliate-info-types.ts b/indexer/packages/postgres/src/types/affiliate-info-types.ts index 885de8b9b7..82885c44cf 100644 --- a/indexer/packages/postgres/src/types/affiliate-info-types.ts +++ b/indexer/packages/postgres/src/types/affiliate-info-types.ts @@ -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, } @@ -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', } diff --git a/indexer/packages/postgres/src/types/db-model-types.ts b/indexer/packages/postgres/src/types/db-model-types.ts index b400153118..d4e203d739 100644 --- a/indexer/packages/postgres/src/types/db-model-types.ts +++ b/indexer/packages/postgres/src/types/db-model-types.ts @@ -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, } diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts index ea89899e15..edac09dcf5 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts @@ -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), }; } diff --git a/indexer/services/comlink/public/api-documentation.md b/indexer/services/comlink/public/api-documentation.md index 4d08bf1430..eaec0a2fbe 100644 --- a/indexer/services/comlink/public/api-documentation.md +++ b/indexer/services/comlink/public/api-documentation.md @@ -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, @@ -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 } ``` @@ -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 @@ -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, diff --git a/indexer/services/comlink/public/swagger.json b/indexer/services/comlink/public/swagger.json index 23de12c60c..e94a095d65 100644 --- a/indexer/services/comlink/public/swagger.json +++ b/indexer/services/comlink/public/swagger.json @@ -303,6 +303,14 @@ "affiliateReferredTotalVolume": { "type": "number", "format": "double" + }, + "affiliateReferredMakerFees": { + "type": "number", + "format": "double" + }, + "affiliateReferredTakerFees": { + "type": "number", + "format": "double" } }, "required": [ @@ -313,7 +321,9 @@ "affiliateTotalReferredFees", "affiliateReferredUsers", "affiliateReferredNetProtocolEarnings", - "affiliateReferredTotalVolume" + "affiliateReferredTotalVolume", + "affiliateReferredMakerFees", + "affiliateReferredTakerFees" ], "type": "object", "additionalProperties": false diff --git a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts index 76e2433255..7eb6850b7d 100644 --- a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts @@ -164,10 +164,15 @@ class AffiliatesController extends Controller { info.address in addressUsernameMap ? addressUsernameMap[info.address] : '', affiliateEarnings: Number(info.affiliateEarnings), affiliateReferredTrades: Number(info.referredMakerTrades) + Number(info.referredTakerTrades), - 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), })); const response: AffiliateSnapshotResponse = { diff --git a/indexer/services/comlink/src/types.ts b/indexer/services/comlink/src/types.ts index c777521f48..81eace3303 100644 --- a/indexer/services/comlink/src/types.ts +++ b/indexer/services/comlink/src/types.ts @@ -731,6 +731,8 @@ export interface AffiliateSnapshotResponseObject { affiliateReferredUsers: number, affiliateReferredNetProtocolEarnings: number, affiliateReferredTotalVolume: number, + affiliateReferredMakerFees: number, + affiliateReferredTakerFees: number, } export interface AffiliateTotalVolumeResponse { diff --git a/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts b/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts index adc63c68e3..9abaa91940 100644 --- a/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts +++ b/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts @@ -72,9 +72,9 @@ describe('update-affiliate-info', () => { affiliateEarnings: '0', referredMakerTrades: 0, referredTakerTrades: 0, - totalReferredFees: '0', + totalReferredMakerFees: '0', + totalReferredTakerFees: '0', totalReferredUsers: 1, - referredNetProtocolEarnings: '0', firstReferralBlockHeight: '1', referredTotalVolume: '0', }; @@ -121,9 +121,9 @@ describe('update-affiliate-info', () => { affiliateEarnings: '500', referredMakerTrades: 0, referredTakerTrades: 1, - totalReferredFees: '1000', + totalReferredMakerFees: '0', + totalReferredTakerFees: '1000', totalReferredUsers: 2, - referredNetProtocolEarnings: '500', firstReferralBlockHeight: '1', referredTotalVolume: '1', }; @@ -189,9 +189,9 @@ describe('update-affiliate-info', () => { affiliateEarnings: '1000', referredMakerTrades: 0, referredTakerTrades: 2, - totalReferredFees: '2000', + totalReferredMakerFees: '0', + totalReferredTakerFees: '2000', totalReferredUsers: 1, - referredNetProtocolEarnings: '1000', firstReferralBlockHeight: '1', referredTotalVolume: '2', }; @@ -255,9 +255,9 @@ describe('update-affiliate-info', () => { affiliateEarnings: '1000', referredMakerTrades: 0, referredTakerTrades: 2, - totalReferredFees: '2000', + totalReferredMakerFees: '0', + totalReferredTakerFees: '2000', totalReferredUsers: 1, - referredNetProtocolEarnings: '1000', firstReferralBlockHeight: '1', referredTotalVolume: '2', }; From 70f0585296195809cb3b6faf308bc1dca82ee3aa Mon Sep 17 00:00:00 2001 From: Jerry Fan Date: Wed, 2 Oct 2024 17:00:38 -0400 Subject: [PATCH 2/2] revision --- .../postgres/__tests__/helpers/constants.ts | 9 ++++++--- .../stores/affiliate-info-table.test.ts | 17 +++++++++++------ ...2144813_change_affiliate_info_fee_columns.ts | 6 ++++-- .../postgres/src/models/affiliate-info-model.ts | 9 +++++++-- .../postgres/src/stores/affiliate-info-table.ts | 7 ++++++- .../postgres/src/types/affiliate-info-types.ts | 2 ++ .../postgres/src/types/db-model-types.ts | 1 + .../api/v4/affiliates-controller.test.ts | 6 ++++-- .../controllers/api/v4/affiliates-controller.ts | 6 ++++-- .../tasks/update-affiliate-info.test.ts | 4 ++++ 10 files changed, 49 insertions(+), 18 deletions(-) diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index 118663429f..5de24209bd 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -990,8 +990,9 @@ export const defaultAffiliateInfo: AffiliateInfoCreateObject = { affiliateEarnings: '10', referredMakerTrades: 10, referredTakerTrades: 20, - totalReferredMakerFees: '-10', + totalReferredMakerFees: '10', totalReferredTakerFees: '10', + totalReferredMakerRebates: '-10', totalReferredUsers: 5, firstReferralBlockHeight: '1', referredTotalVolume: '1000', @@ -1002,8 +1003,9 @@ export const defaultAffiliateInfo2: AffiliateInfoCreateObject = { affiliateEarnings: '11', referredMakerTrades: 11, referredTakerTrades: 21, - totalReferredMakerFees: '-11', + totalReferredMakerFees: '11', totalReferredTakerFees: '11', + totalReferredMakerRebates: '-11', totalReferredUsers: 5, firstReferralBlockHeight: '11', referredTotalVolume: '1000', @@ -1014,8 +1016,9 @@ export const defaultAffiliateInfo3: AffiliateInfoCreateObject = { affiliateEarnings: '12', referredMakerTrades: 12, referredTakerTrades: 22, - totalReferredMakerFees: '-12', + totalReferredMakerFees: '12', totalReferredTakerFees: '12', + totalReferredMakerRebates: '-12', totalReferredUsers: 10, firstReferralBlockHeight: '12', referredTotalVolume: '1111111', diff --git a/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts b/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts index f2c26c1475..9c9eddc6eb 100644 --- a/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts +++ b/indexer/packages/postgres/__tests__/stores/affiliate-info-table.test.ts @@ -113,8 +113,9 @@ describe('Affiliate info store', () => { affiliateEarnings: '1000', referredMakerTrades: 1, referredTakerTrades: 1, - totalReferredMakerFees: '-1000', + totalReferredMakerFees: '0', totalReferredTakerFees: '1000', + totalReferredMakerRebates: '-1000', totalReferredUsers: 1, firstReferralBlockHeight: '1', referredTotalVolume: '2', @@ -140,8 +141,9 @@ describe('Affiliate info store', () => { affiliateEarnings: '1000', referredMakerTrades: 2, referredTakerTrades: 0, - totalReferredMakerFees: '-2000', + totalReferredMakerFees: '2000', totalReferredTakerFees: '0', + totalReferredMakerRebates: '0', totalReferredUsers: 1, firstReferralBlockHeight: '1', referredTotalVolume: '2', @@ -162,8 +164,9 @@ describe('Affiliate info store', () => { affiliateEarnings: '2000', referredMakerTrades: 3, referredTakerTrades: 1, - totalReferredMakerFees: '-3000', + totalReferredMakerFees: '2000', totalReferredTakerFees: '1000', + totalReferredMakerRebates: '-1000', totalReferredUsers: 1, firstReferralBlockHeight: '1', referredTotalVolume: '4', @@ -188,8 +191,9 @@ describe('Affiliate info store', () => { affiliateEarnings: '2000', referredMakerTrades: 3, referredTakerTrades: 1, - totalReferredMakerFees: '-3000', + totalReferredMakerFees: '2000', totalReferredTakerFees: '1000', + totalReferredMakerRebates: '-1000', totalReferredUsers: 2, firstReferralBlockHeight: '1', referredTotalVolume: '4', @@ -238,6 +242,7 @@ describe('Affiliate info store', () => { referredTakerTrades: 0, totalReferredMakerFees: '0', totalReferredTakerFees: '0', + totalReferredMakerRebates: '0', totalReferredUsers: 1, firstReferralBlockHeight: '2', referredTotalVolume: '0', @@ -402,7 +407,7 @@ async function populateFillsAndReferrals(): Promise { eventId: defaultTendermintEventId3, price: '1', size: '1', - fee: '-1000', + fee: '1000', affiliateRevShare: '500', }), FillTable.create({ @@ -413,7 +418,7 @@ async function populateFillsAndReferrals(): Promise { eventId: defaultTendermintEventId4, price: '1', size: '1', - fee: '-1000', + fee: '1000', affiliateRevShare: '500', }), ]); diff --git a/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts b/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts index db951a7dc3..877b2524c0 100644 --- a/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts +++ b/indexer/packages/postgres/src/db/migrations/migration_files/20241002144813_change_affiliate_info_fee_columns.ts @@ -8,6 +8,7 @@ export async function up(knex: Knex): Promise { table.dropColumn('referredNetProtocolEarnings'); table.decimal('totalReferredTakerFees', null).notNullable().defaultTo(0); table.decimal('totalReferredMakerFees', null).notNullable().defaultTo(0); + table.decimal('totalReferredMakerRebates', null).notNullable().defaultTo(0); }); } @@ -15,9 +16,10 @@ export async function down(knex: Knex): Promise { return knex .schema .alterTable('affiliate_info', (table) => { - table.decimal('totalReferredFees', null).notNullable().defaultTo(0).alter(); - table.decimal('referredNetProtocolEarnings', null).notNullable().defaultTo(0).alter(); + table.decimal('totalReferredFees', null).notNullable().defaultTo(0); + table.decimal('referredNetProtocolEarnings', null).notNullable().defaultTo(0); table.dropColumn('totalReferredTakerFees'); table.dropColumn('totalReferredMakerFees'); + table.dropColumn('totalReferredMakerRebates'); }); } diff --git a/indexer/packages/postgres/src/models/affiliate-info-model.ts b/indexer/packages/postgres/src/models/affiliate-info-model.ts index 304004a1d5..49dd0dfdbd 100644 --- a/indexer/packages/postgres/src/models/affiliate-info-model.ts +++ b/indexer/packages/postgres/src/models/affiliate-info-model.ts @@ -21,6 +21,7 @@ export default class AffiliateInfoModel extends BaseModel { 'referredTakerTrades', 'totalReferredMakerFees', 'totalReferredTakerFees', + 'totalReferredMakerRebates', 'totalReferredUsers', 'firstReferralBlockHeight', 'referredTotalVolume', @@ -30,8 +31,9 @@ export default class AffiliateInfoModel extends BaseModel { affiliateEarnings: { type: 'string', pattern: NonNegativeNumericPattern }, referredMakerTrades: { type: 'int' }, referredTakerTrades: { type: 'int' }, - totalReferredMakerFees: { type: 'string', pattern: NumericPattern }, - totalReferredTakerFees: { type: 'string', pattern: NumericPattern }, + totalReferredMakerFees: { type: 'string', pattern: NonNegativeNumericPattern }, + totalReferredTakerFees: { type: 'string', pattern: NonNegativeNumericPattern }, + totalReferredMakerRebates: { type: 'string', pattern: NumericPattern }, totalReferredUsers: { type: 'int' }, firstReferralBlockHeight: { type: 'string', pattern: NonNegativeNumericPattern }, referredTotalVolume: { type: 'string', pattern: NonNegativeNumericPattern }, @@ -53,6 +55,7 @@ export default class AffiliateInfoModel extends BaseModel { referredTakerTrades: 'int', totalReferredMakerFees: 'string', totalReferredTakerFees: 'string', + totalReferredMakerRebates: 'string', totalReferredUsers: 'int', firstReferralBlockHeight: 'string', referredTotalVolume: 'string', @@ -73,6 +76,8 @@ export default class AffiliateInfoModel extends BaseModel { totalReferredTakerFees!: string; + totalReferredMakerRebates!: string; + totalReferredUsers!: number; firstReferralBlockHeight!: string; diff --git a/indexer/packages/postgres/src/stores/affiliate-info-table.ts b/indexer/packages/postgres/src/stores/affiliate-info-table.ts index 61b5a9d284..d661a01d70 100644 --- a/indexer/packages/postgres/src/stores/affiliate-info-table.ts +++ b/indexer/packages/postgres/src/stores/affiliate-info-table.ts @@ -192,8 +192,9 @@ affiliate_stats AS ( affiliate_fills."affiliateAddress", SUM(affiliate_fills."fee") AS "totalReferredFees", SUM(affiliate_fills."affiliateRevShare") AS "affiliateEarnings", - SUM(CASE WHEN affiliate_fills."liquidity" = '${Liquidity.MAKER}' THEN affiliate_fills."fee" ELSE 0 END) AS "totalReferredMakerFees", + 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" @@ -213,6 +214,7 @@ affiliate_info_update AS ( affiliate_metadata."firstReferralBlockHeight", 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."referredMakerTrades", 0) AS "referredMakerTrades", COALESCE(affiliate_stats."referredTakerTrades", 0) AS "referredTakerTrades", @@ -234,6 +236,7 @@ INSERT INTO affiliate_info ( "referredTakerTrades", "totalReferredMakerFees", "totalReferredTakerFees", + "totalReferredMakerRebates", "referredTotalVolume" ) SELECT @@ -245,6 +248,7 @@ SELECT "referredTakerTrades", "totalReferredMakerFees", "totalReferredTakerFees", + "totalReferredMakerRebates", "referredTotalVolume" FROM affiliate_info_update @@ -257,6 +261,7 @@ DO UPDATE SET "referredTakerTrades" = affiliate_info."referredTakerTrades" + EXCLUDED."referredTakerTrades", "totalReferredMakerFees" = affiliate_info."totalReferredMakerFees" + EXCLUDED."totalReferredMakerFees", "totalReferredTakerFees" = affiliate_info."totalReferredTakerFees" + EXCLUDED."totalReferredTakerFees", + "totalReferredMakerRebates" = affiliate_info."totalReferredMakerRebates" + EXCLUDED."totalReferredMakerRebates", "referredTotalVolume" = affiliate_info."referredTotalVolume" + EXCLUDED."referredTotalVolume"; `; diff --git a/indexer/packages/postgres/src/types/affiliate-info-types.ts b/indexer/packages/postgres/src/types/affiliate-info-types.ts index 82885c44cf..a1dcc61be6 100644 --- a/indexer/packages/postgres/src/types/affiliate-info-types.ts +++ b/indexer/packages/postgres/src/types/affiliate-info-types.ts @@ -5,6 +5,7 @@ export interface AffiliateInfoCreateObject { referredTakerTrades: number, totalReferredMakerFees: string, totalReferredTakerFees: string, + totalReferredMakerRebates: string, totalReferredUsers: number, firstReferralBlockHeight: string, referredTotalVolume: string, @@ -17,6 +18,7 @@ export enum AffiliateInfoColumns { referredTakerTrades = 'referredTakerTrades', totalReferredMakerFees = 'totalReferredMakerFees', totalReferredTakerFees = 'totalReferredTakerFees', + totalReferredMakerRebates = 'totalReferredMakerRebates', totalReferredUsers = 'totalReferredUsers', firstReferralBlockHeight = 'firstReferralBlockHeight', referredTotalVolume = 'referredTotalVolume', diff --git a/indexer/packages/postgres/src/types/db-model-types.ts b/indexer/packages/postgres/src/types/db-model-types.ts index d4e203d739..9557be8ede 100644 --- a/indexer/packages/postgres/src/types/db-model-types.ts +++ b/indexer/packages/postgres/src/types/db-model-types.ts @@ -290,6 +290,7 @@ export interface AffiliateInfoFromDatabase { referredTakerTrades: number, totalReferredMakerFees: string, totalReferredTakerFees: string, + totalReferredMakerRebates: string, totalReferredUsers: number, firstReferralBlockHeight: string, referredTotalVolume: string, diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts index edac09dcf5..30ceebd062 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/affiliates-controller.test.ts @@ -370,10 +370,12 @@ function affiliateInfoCreateToResponseObject( affiliateReferredTrades: Number(info.referredTakerTrades) + Number(info.referredMakerTrades), affiliateTotalReferredFees: Number(info.totalReferredMakerFees) + - Number(info.totalReferredTakerFees), + Number(info.totalReferredTakerFees) + + Number(info.totalReferredMakerRebates), affiliateReferredUsers: Number(info.totalReferredUsers), affiliateReferredNetProtocolEarnings: Number(info.totalReferredMakerFees) + - Number(info.totalReferredTakerFees) - + Number(info.totalReferredTakerFees) + + Number(info.totalReferredMakerRebates) - Number(info.affiliateEarnings), affiliateReferredTotalVolume: Number(info.referredTotalVolume), affiliateReferredMakerFees: Number(info.totalReferredMakerFees), diff --git a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts index 7eb6850b7d..ac246bbfdf 100644 --- a/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/affiliates-controller.ts @@ -165,10 +165,12 @@ class AffiliatesController extends Controller { affiliateEarnings: Number(info.affiliateEarnings), affiliateReferredTrades: Number(info.referredMakerTrades) + Number(info.referredTakerTrades), affiliateTotalReferredFees: Number(info.totalReferredMakerFees) + - Number(info.totalReferredTakerFees), + Number(info.totalReferredTakerFees) + + Number(info.totalReferredMakerRebates), affiliateReferredUsers: Number(info.totalReferredUsers), affiliateReferredNetProtocolEarnings: Number(info.totalReferredMakerFees) + - Number(info.totalReferredTakerFees) - + Number(info.totalReferredTakerFees) + + Number(info.totalReferredMakerRebates) - Number(info.affiliateEarnings), affiliateReferredTotalVolume: Number(info.referredTotalVolume), affiliateReferredMakerFees: Number(info.totalReferredMakerFees), diff --git a/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts b/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts index 9abaa91940..26b139cdb8 100644 --- a/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts +++ b/indexer/services/roundtable/__tests__/tasks/update-affiliate-info.test.ts @@ -74,6 +74,7 @@ describe('update-affiliate-info', () => { referredTakerTrades: 0, totalReferredMakerFees: '0', totalReferredTakerFees: '0', + totalReferredMakerRebates: '0', totalReferredUsers: 1, firstReferralBlockHeight: '1', referredTotalVolume: '0', @@ -123,6 +124,7 @@ describe('update-affiliate-info', () => { referredTakerTrades: 1, totalReferredMakerFees: '0', totalReferredTakerFees: '1000', + totalReferredMakerRebates: '0', totalReferredUsers: 2, firstReferralBlockHeight: '1', referredTotalVolume: '1', @@ -191,6 +193,7 @@ describe('update-affiliate-info', () => { referredTakerTrades: 2, totalReferredMakerFees: '0', totalReferredTakerFees: '2000', + totalReferredMakerRebates: '0', totalReferredUsers: 1, firstReferralBlockHeight: '1', referredTotalVolume: '2', @@ -257,6 +260,7 @@ describe('update-affiliate-info', () => { referredTakerTrades: 2, totalReferredMakerFees: '0', totalReferredTakerFees: '2000', + totalReferredMakerRebates: '0', totalReferredUsers: 1, firstReferralBlockHeight: '1', referredTotalVolume: '2',