Skip to content

Commit

Permalink
[IND-498]: Add TradingRewardsHandler (#902)
Browse files Browse the repository at this point in the history
* [IND-498]: Add TradingRewardHandler

* fix everything

* nits, add helper functions, update precision and scale

* lint
  • Loading branch information
Christopher-Li authored Dec 22, 2023
1 parent 0459917 commit b1ac285
Show file tree
Hide file tree
Showing 24 changed files with 599 additions and 10 deletions.
12 changes: 9 additions & 3 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
TransferCreateObject,
WalletCreateObject,
} from '../../src/types';
import { denomToHumanReadableConversion } from './conversion-helpers';

export const createdDateTime: DateTime = DateTime.utc();
export const createdHeight: string = '2';
Expand Down Expand Up @@ -100,7 +101,12 @@ export const defaultSubaccountId3: string = SubaccountTable.uuid(
// ============== Wallets ==============
export const defaultWallet: WalletCreateObject = {
address: defaultAddress,
totalTradingRewards: '0',
totalTradingRewards: denomToHumanReadableConversion(0),
};

export const defaultWallet2: WalletCreateObject = {
address: defaultWalletAddress,
totalTradingRewards: denomToHumanReadableConversion(1),
};

// ============== Assets ==============
Expand Down Expand Up @@ -592,7 +598,7 @@ export const defaultTradingReward: TradingRewardCreateObject = {
address: defaultAddress,
blockHeight: createdHeight,
blockTime: createdDateTime.toISO(),
amount: '1.00',
amount: denomToHumanReadableConversion(1),
};

// ========= Trading Reward Aggregation Data ==========
Expand All @@ -602,7 +608,7 @@ export const defaultTradingRewardAggregation: TradingRewardAggregationCreateObje
startedAtHeight: createdHeight,
startedAt: createdDateTime.toISO(),
period: TradingRewardAggregationPeriod.DAILY,
amount: '1.00',
amount: denomToHumanReadableConversion(1),
};
export const defaultTradingRewardAggregationId: string = TradingRewardAggregationTable.uuid(
defaultTradingRewardAggregation.address,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Big from 'big.js';

export const DENOM_TO_COIN_CONVERSION: number = 1e-18;
export const DENOM_COIN_SCALE: number = 18;

export function denomToHumanReadableConversion(denom: number): string {
return Big(denom).times(DENOM_TO_COIN_CONVERSION).toFixed(DENOM_COIN_SCALE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import * as TradingRewardAggregationTable from '../../src/stores/trading-reward-aggregation-table';
import { WalletTable } from '../../src';
import { seedData } from '../helpers/mock-generators';
import { denomToHumanReadableConversion } from '../helpers/conversion-helpers';

describe('TradingRewardAggregation store', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -82,7 +83,7 @@ describe('TradingRewardAggregation store', () => {
it('Successfully updates an existing TradingRewardAggregation', async () => {
await TradingRewardAggregationTable.create(defaultTradingRewardAggregation);

const amount: string = '100000.00';
const amount: string = denomToHumanReadableConversion(100000);
const endedAt: string = '2021-01-01T00:00:00.000Z';
const endedAtHeight: string = '1000';
const update:
Expand Down
2 changes: 2 additions & 0 deletions indexer/packages/postgres/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import OrderModel from './models/order-model';
import PerpetualMarketModel from './models/perpetual-market-model';
import PerpetualPositionModel from './models/perpetual-position-model';
import SubaccountModel from './models/subaccount-model';
import TradingRewardModel from './models/trading-reward-model';
import TransferModel from './models/transfer-model';
import {
APITimeInForce,
Expand Down Expand Up @@ -99,6 +100,7 @@ export const SQL_TO_JSON_DEFINED_MODELS = [
PerpetualPositionModel,
SubaccountModel,
TransferModel,
TradingRewardModel,
];

export type SpecifiedClobPairStatus =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('trading_rewards', (table) => {
// 27 is the max precision and 18 is scale, which means 9 digits before the decimal point
// and 18 after
table.decimal('amount', 27, 18).notNullable().alter();
});
}

export async function down(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('trading_rewards', (table) => {
table.decimal('amount').notNullable().alter();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('wallets', (table) => {
// 27 is the max precision and 18 is scale, which means 9 digits before the decimal point
// and 18 after
table.decimal('totalTradingRewards', 27, 18).notNullable().alter();
});
}

export async function down(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('wallets', (table) => {
table.decimal('totalTradingRewards').notNullable().alter();
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('trading_reward_aggregations', (table) => {
// 27 is the max precision and 18 is scale, which means 9 digits before the decimal point
// and 18 after
table.decimal('amount', 27, 18).notNullable().alter();
});
}

export async function down(knex: Knex): Promise<void> {
return knex
.schema
.alterTable('trading_reward_aggregations', (table) => {
table.decimal('amount').notNullable().alter();
});
}
3 changes: 3 additions & 0 deletions indexer/packages/postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export { default as OrderModel } from './models/order-model';
export { default as PerpetualMarketModel } from './models/perpetual-market-model';
export { default as PerpetualPositionModel } from './models/perpetual-position-model';
export { default as TransferModel } from './models/transfer-model';
export { default as TradingRewardModel } from './models/trading-reward-model';
export { default as TradingRewardAggregationModel } from './models/trading-reward-aggregation-model';

export * as AssetTable from './stores/asset-table';
export * as AssetPositionTable from './stores/asset-position-table';
Expand Down Expand Up @@ -50,5 +52,6 @@ export * as storeHelpers from './helpers/stores-helpers';

export * as testMocks from '../__tests__/helpers/mock-generators';
export * as testConstants from '../__tests__/helpers/constants';
export * as testConversionHelpers from '../__tests__/helpers/conversion-helpers';

export * as helpers from './db/helpers';
4 changes: 2 additions & 2 deletions indexer/packages/postgres/src/stores/trading-reward-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ export async function create(
}

export async function findById(
address: string,
id: string,
options: Options = DEFAULT_POSTGRES_OPTIONS,
): Promise<TradingRewardFromDatabase | undefined> {
const baseQuery: QueryBuilder<TradingRewardModel> = setupBaseQuery<TradingRewardModel>(
TradingRewardModel,
options,
);
return baseQuery
.findById(address)
.findById(id)
.returning('*');
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface SubaccountMessageContents {
orders?: OrderSubaccountMessageContents[],
fills?: FillSubaccountMessageContents[],
transfers?: TransferSubaccountMessageContents,
tradingReward?: TradingRewardSubaccountMessageContents,
}

export interface PerpetualPositionSubaccountMessageContents {
Expand Down Expand Up @@ -163,6 +164,12 @@ export interface TransferSubaccountMessageContents {
createdAtHeight: string;
}

export interface TradingRewardSubaccountMessageContents {
tradingReward: string;
createdAtHeight: string;
createdAt: string;
}

/* ------- TradeMessageContents ------- */

export interface TradeMessageContents {
Expand Down
Loading

0 comments on commit b1ac285

Please sign in to comment.