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

[OTE-272] update DB and APIs to add OI caps info #1305

Merged
merged 12 commits into from
Apr 9, 2024
Merged
2 changes: 2 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,8 @@ export const defaultLiquidityTier2: LiquidityTiersCreateObject = {
name: 'Mid-Cap',
initialMarginPpm: '100000', // 10%
maintenanceFractionPpm: '500000', // 50%
openInterestLowerCap: '0',
openInterestUpperCap: '5000000',
};

// ============== OraclePrices ==============
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as Knex from "knex";

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('liquidity_tiers', (table) => {
table.string('openInterestLowerCap').nullable().defaultTo(null);
table.string('openInterestUpperCap').nullable().defaultTo(null);
});

}


export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('liquidity_tiers', (table) => {
table.dropColumn('openInterestLowerCap');
table.dropColumn('openInterestUpperCap');
});
}

11 changes: 10 additions & 1 deletion indexer/packages/postgres/src/models/liquidity-tiers-model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IntegerPattern } from '../lib/validators';
import { IntegerPattern, NumericPattern } from '../lib/validators';
import UpsertQueryBuilder from '../query-builders/upsert';
import BaseModel from './base-model';

Expand Down Expand Up @@ -27,6 +27,9 @@ export default class LiquidityTiersModel extends BaseModel {
name: { type: 'string' },
initialMarginPpm: { type: 'string', pattern: IntegerPattern },
maintenanceFractionPpm: { type: 'string', pattern: IntegerPattern },
// optional fields which are nullable
openInterestLowerCap: {type: ['string', 'null'], pattern: NumericPattern},
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
openInterestUpperCap: {type: ['string', 'null'], pattern: NumericPattern},
},
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
};
}
Expand All @@ -43,6 +46,8 @@ export default class LiquidityTiersModel extends BaseModel {
name: 'string',
initialMarginPpm: 'string',
maintenanceFractionPpm: 'string',
openInterestLowerCap: 'string',
openInterestUpperCap: 'string',
};
}

Expand All @@ -55,4 +60,8 @@ export default class LiquidityTiersModel extends BaseModel {
initialMarginPpm!: string;

maintenanceFractionPpm!: string;

openInterestLowerCap?: string;

openInterestUpperCap?: string;
}
2 changes: 2 additions & 0 deletions indexer/packages/postgres/src/types/db-model-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ export interface LiquidityTiersFromDatabase {
name: string;
initialMarginPpm: string;
maintenanceFractionPpm: string;
openInterestLowerCap?: string;
openInterestUpperCap?: string;
}

export interface CandleFromDatabase extends IdBasedModelFromDatabase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,24 @@ export interface LiquidityTiersCreateObject {
name: string,
initialMarginPpm: string,
maintenanceFractionPpm: string,
openInterestLowerCap?: string,
openInterestUpperCap?: string,
}

export interface LiquidityTiersUpdateObject {
id: number,
name?: string,
initialMarginPpm?: string,
maintenanceFractionPpm?: string,
openInterestLowerCap?: string,
openInterestUpperCap?: string,
}

export enum LiquidityTiersColumns {
id = 'id',
name = 'name',
initialMarginPpm = 'initialMarginPpm',
maintenanceFractionPpm = 'maintenanceFractionPpm',
openInterestLowerCap = 'openInterestLowerCap',
openInterestUpperCap = 'openInterestUpperCap',
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ export interface TradingPerpetualMarketMessage {
subticksPerTick?: number;
stepBaseQuantums?: number;
marketType?: PerpetualMarketType;
openInterestLowerCap?: string;
openInterestUpperCap?: string;

// Fields that are likely to change
priceChange24H?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function expectResponseWithMarkets(
expect(_.size(response.body.markets)).toEqual(perpetualMarkets.length);
expect(_.size(response.body.markets)).toEqual(markets.length);
expect(_.size(response.body.markets)).toEqual(liquidityTiers.length);

_.each(_.zip(perpetualMarkets, liquidityTiers, markets), (
[perpetualMarket, liquidityTier, market]:
[PerpetualMarketFromDatabase | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ describe('request-transformer', () => {
stepBaseQuantums: perpetualMarket.stepBaseQuantums,
subticksPerTick: perpetualMarket.subticksPerTick,
marketType: perpetualMarket.marketType,
openInterestLowerCap: liquidityTier.openInterestLowerCap,
openInterestUpperCap: liquidityTier.openInterestUpperCap,
},
);
});
Expand Down
22 changes: 17 additions & 5 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -1730,7 +1730,9 @@ fetch('https://dydx-testnet.imperator.co/v4/perpetualMarkets',
"stepSize": "string",
"stepBaseQuantums": 0,
"subticksPerTick": 0,
"marketType": "CROSS"
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string"
},
"property2": {
"clobPairId": "string",
Expand All @@ -1750,7 +1752,9 @@ fetch('https://dydx-testnet.imperator.co/v4/perpetualMarkets',
"stepSize": "string",
"stepBaseQuantums": 0,
"subticksPerTick": 0,
"marketType": "CROSS"
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string"
}
}
}
Expand Down Expand Up @@ -3894,7 +3898,9 @@ or
"stepSize": "string",
"stepBaseQuantums": 0,
"subticksPerTick": 0,
"marketType": "CROSS"
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string"
}

```
Expand All @@ -3921,6 +3927,8 @@ or
|stepBaseQuantums|number(double)|true|none|none|
|subticksPerTick|number(double)|true|none|none|
|marketType|[PerpetualMarketType](#schemaperpetualmarkettype)|true|none|none|
|openInterestLowerCap|string|false|none|none|
|openInterestUpperCap|string|false|none|none|

## PerpetualMarketResponse

Expand Down Expand Up @@ -3950,7 +3958,9 @@ or
"stepSize": "string",
"stepBaseQuantums": 0,
"subticksPerTick": 0,
"marketType": "CROSS"
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string"
},
"property2": {
"clobPairId": "string",
Expand All @@ -3970,7 +3980,9 @@ or
"stepSize": "string",
"stepBaseQuantums": 0,
"subticksPerTick": 0,
"marketType": "CROSS"
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,12 @@
},
"marketType": {
"$ref": "#/components/schemas/PerpetualMarketType"
},
"openInterestLowerCap": {
"type": "string"
},
"openInterestUpperCap": {
"type": "string"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,8 @@ export function perpetualMarketToResponseObject(
stepBaseQuantums: perpetualMarket.stepBaseQuantums,
subticksPerTick: perpetualMarket.subticksPerTick,
marketType: perpetualMarket.marketType,
openInterestLowerCap: liquidityTier.openInterestLowerCap,
openInterestUpperCap: liquidityTier.openInterestUpperCap,
};
}

Expand Down
2 changes: 2 additions & 0 deletions indexer/services/comlink/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ export interface PerpetualMarketResponseObject {
stepBaseQuantums: number;
subticksPerTick: number;
marketType: PerpetualMarketType;
openInterestLowerCap?: string;
openInterestUpperCap?: string;
}

/* ------- ORDERBOOK TYPES ------- */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ export function expectLiquidityTier(
liquidityTierFromDb: LiquidityTiersFromDatabase,
event: LiquidityTierUpsertEventV1,
): void {
expect(liquidityTierFromDb).toEqual(expect.objectContaining({
id: event.id,
name: event.name,
initialMarginPpm: event.initialMarginPpm.toString(),
maintenanceFractionPpm: event.maintenanceFractionPpm.toString(),
}));
expect(liquidityTierFromDb.id).toEqual(event.id);
expect(liquidityTierFromDb.name).toEqual(event.name);
expect(liquidityTierFromDb.initialMarginPpm).toEqual(event.initialMarginPpm.toString());
expect(liquidityTierFromDb.maintenanceFractionPpm).toEqual(event.maintenanceFractionPpm.toString());
expect(Number(liquidityTierFromDb.openInterestLowerCap)).toEqual(event.openInterestLowerCap.toNumber());
expect(Number(liquidityTierFromDb.openInterestUpperCap)).toEqual(event.openInterestUpperCap.toNumber());
}

function createKafkaMessageFromLiquidityTiersEvent({
Expand Down Expand Up @@ -230,12 +230,12 @@ function validateLiquidityTierRefresher(
liquidityTierEvent.id,
);

expect(liquidityTier).toEqual({
id: liquidityTierEvent.id,
name: liquidityTierEvent.name,
initialMarginPpm: liquidityTierEvent.initialMarginPpm.toString(),
maintenanceFractionPpm: liquidityTierEvent.maintenanceFractionPpm.toString(),
});
expect(liquidityTier.id).toEqual(liquidityTierEvent.id);
expect(liquidityTier.name).toEqual(liquidityTierEvent.name);
expect(liquidityTier.initialMarginPpm).toEqual(liquidityTierEvent.initialMarginPpm.toString());
expect(liquidityTier.maintenanceFractionPpm).toEqual(liquidityTierEvent.maintenanceFractionPpm.toString());
expect(Number(liquidityTier.openInterestLowerCap)).toEqual(liquidityTierEvent.openInterestLowerCap.toNumber());
expect(Number(liquidityTier.openInterestUpperCap)).toEqual(liquidityTierEvent.openInterestUpperCap.toNumber());
}

function expectKafkaMessages(
Expand Down
2 changes: 1 addition & 1 deletion indexer/services/ender/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export const defaultLiquidityTierUpsertEvent: LiquidityTierUpsertEventV1 = {
maintenanceFractionPpm: 600000, // 60% of IM = 3%
basePositionNotional: Long.fromValue(1_000_000_000_000, true), // 1_000_000 USDC
openInterestLowerCap: Long.fromValue(0, true),
openInterestUpperCap: Long.fromValue(1_000_000_000, true),
openInterestUpperCap: Long.fromValue(1_000_000_000_000, true),
};

export const defaultUpdatePerpetualEvent: UpdatePerpetualEventV1 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export class LiquidityTierHandler extends Handler<LiquidityTierUpsertEventV1> {
// eslint-disable-next-line @typescript-eslint/require-await
public async internalHandle(resultRow: pg.QueryResultRow): Promise<ConsolidatedKafkaEvent[]> {
const liquidityTier: LiquidityTiersFromDatabase = LiquidityTiersModel.fromJson(
resultRow.liquidity_tier) as LiquidityTiersFromDatabase;
resultRow.liquidity_tier
) as LiquidityTiersFromDatabase;

liquidityTierRefresher.upsertLiquidityTier(liquidityTier);
return this.generateWebsocketEventsForLiquidityTier(liquidityTier);
}
Expand Down
2 changes: 2 additions & 0 deletions indexer/services/ender/src/helpers/kafka-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ export function generatePerpetualMarketMessage(
Number(liquidityTier.maintenanceFractionPpm),
),
),
openInterestLowerCap: liquidityTier.openInterestLowerCap,
openInterestUpperCap: liquidityTier.openInterestUpperCap,
};
})
.value();
Expand Down
2 changes: 1 addition & 1 deletion indexer/services/ender/src/lib/block-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ export class BlockProcessor {
};
}
});

affanv14 marked this conversation as resolved.
Show resolved Hide resolved
const start: number = Date.now();
let success = false;
let resultRow: pg.QueryResultRow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ BEGIN
liquidity_tier_record."name" = event_data->>'name';
liquidity_tier_record."initialMarginPpm" = (event_data->'initialMarginPpm')::bigint;
liquidity_tier_record."maintenanceFractionPpm" = (event_data->'maintenanceFractionPpm')::bigint;
liquidity_tier_record."openInterestLowerCap" = dydx_from_jsonlib_long(event_data->'openInterestLowerCap');
liquidity_tier_record."openInterestUpperCap" = dydx_from_jsonlib_long(event_data->'openInterestUpperCap');

INSERT INTO liquidity_tiers
VALUES (liquidity_tier_record.*)
Expand All @@ -23,7 +25,9 @@ BEGIN
SET
"name" = liquidity_tier_record."name",
"initialMarginPpm" = liquidity_tier_record."initialMarginPpm",
"maintenanceFractionPpm" = liquidity_tier_record."maintenanceFractionPpm"
"maintenanceFractionPpm" = liquidity_tier_record."maintenanceFractionPpm",
"openInterestLowerCap" = liquidity_tier_record."openInterestLowerCap",
"openInterestUpperCap" = liquidity_tier_record."openInterestUpperCap"
RETURNING * INTO liquidity_tier_record;

RETURN jsonb_build_object(
Expand Down
Loading