Skip to content

Commit

Permalink
Add defaultFunding8hrPpm field to perpetual indexer events
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Jan 9, 2025
1 parent 2a79ca0 commit a5349cd
Show file tree
Hide file tree
Showing 47 changed files with 447 additions and 190 deletions.
2 changes: 2 additions & 0 deletions indexer/packages/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Add a knex migration by running `pnpm run migrate:make <create_fake_table>`

Run the migration with `pnpm run migrate`

In `__tests__/db/migrations.test.ts`, test cases may be expected to fail (and hence should be commented out) if a model is modified due to the latest migration.

In order to migrate in dev and staging, you must redeploy and run bazooka.

TODO(CORE-512): Add info/resources around bazooka. [Doc](https://www.notion.so/dydx/Engineering-Runbook-15064661da9643188ce33e341b68e7bb#cb2283d80ef14a51924f3bd1a538fd82).
25 changes: 19 additions & 6 deletions indexer/packages/postgres/__tests__/db/migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import {
} from '../helpers/constants';
import { seedData } from '../helpers/mock-generators';

// NOTE: If a model is modified for a migration then these
// tests must be skipped until the following migration
describe('Test new migration', () => {
beforeEach(async () => {
await migrate();
Expand All @@ -25,29 +23,44 @@ describe('Test new migration', () => {
await teardown();
});

it('test adding most recent migration', async () => {
it('test UP and DOWN for most recent migration without seed data', async () => {
// remove latest migration
await multiDown(1);

// re-add latest migration
await knexPrimary.migrate.latest({ loadExtensions: ['.js'] });

// re-remove latest migration
await multiDown(1);
});

// NOTE: If a model is modified for a migration then these
// tests must be skipped until the following migration
it.skip('[Will fail if a model is modified for migration - see README] test adding most recent migration', async () => {
// remove latest migration
await multiDown(1);

// add data to verify you can roll up and then later roll down
await seedData();

// readd latest migration
// re-add latest migration
await knexPrimary.migrate.latest({ loadExtensions: ['.js'] });

// re-remove latest migration
await multiDown(1);
});

it('test adding most recent migration with rows that fail index that should only be applied going forward', async () => {
// NOTE: If a model is modified for a migration then these
// tests must be skipped until the following migration
it.skip('[Will fail if a model is modified for migration - see README] test adding most recent migration with rows that fail index that should only be applied going forward', async () => {
// remove latest migration
await multiDown(1);

// add data to verify you can roll up and then later roll down
await seedData();
await OrderTable.create(defaultOrder);

// readd latest migration
// re-add latest migration
await knexPrimary.migrate.latest({ loadExtensions: ['.js'] });

// re-remove latest migration
Expand Down
5 changes: 5 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ export const defaultPerpetualMarket: PerpetualMarketCreateObject = {
liquidityTierId: 0,
marketType: PerpetualMarketType.CROSS,
baseOpenInterest: '100000',
defaultFundingRate1H: '0',
};
export const defaultPerpetualMarket2: PerpetualMarketCreateObject = {
id: '1',
Expand All @@ -304,6 +305,7 @@ export const defaultPerpetualMarket2: PerpetualMarketCreateObject = {
liquidityTierId: 0,
marketType: PerpetualMarketType.CROSS,
baseOpenInterest: '100000',
defaultFundingRate1H: '0',
};
export const defaultPerpetualMarket3: PerpetualMarketCreateObject = {
id: '2',
Expand All @@ -323,6 +325,7 @@ export const defaultPerpetualMarket3: PerpetualMarketCreateObject = {
liquidityTierId: 0,
marketType: PerpetualMarketType.CROSS,
baseOpenInterest: '100000',
defaultFundingRate1H: '0',
};

export const isolatedPerpetualMarket: PerpetualMarketCreateObject = {
Expand All @@ -343,6 +346,7 @@ export const isolatedPerpetualMarket: PerpetualMarketCreateObject = {
liquidityTierId: 0,
marketType: PerpetualMarketType.ISOLATED,
baseOpenInterest: '100000',
defaultFundingRate1H: '0.0001',
};

export const isolatedPerpetualMarket2: PerpetualMarketCreateObject = {
Expand All @@ -363,6 +367,7 @@ export const isolatedPerpetualMarket2: PerpetualMarketCreateObject = {
liquidityTierId: 0,
marketType: PerpetualMarketType.ISOLATED,
baseOpenInterest: '100000',
defaultFundingRate1H: '0.0001',
};

// ============== Orders ==============
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('perpetual_markets', (table) => {
table.decimal('defaultFundingRate1H', null).defaultTo(0);
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('perpetual_markets', (table) => {
table.dropColumn('defaultFundingRate1H');
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export default class PerpetualMarketModel extends Model {
liquidityTierId: { type: 'integer' },
marketType: { type: 'string' },
baseOpenInterest: { type: 'string', pattern: NumericPattern },
defaultFundingRate1H: { type: 'string', pattern: NumericPattern },
},
};
}
Expand Down Expand Up @@ -115,6 +116,7 @@ export default class PerpetualMarketModel extends Model {
liquidityTierId: 'integer',
marketType: 'string',
baseOpenInterest: 'string',
defaultFundingRate1H: 'string',
};
}

Expand Down Expand Up @@ -151,4 +153,6 @@ export default class PerpetualMarketModel extends Model {
marketType!: PerpetualMarketType;

baseOpenInterest!: string;

defaultFundingRate1H!: string;
}
1 change: 1 addition & 0 deletions indexer/packages/postgres/src/types/db-model-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface PerpetualMarketFromDatabase {
liquidityTierId: number,
marketType: PerpetualMarketType,
baseOpenInterest: string,
defaultFundingRate1H: string,
}

export interface FillFromDatabase {
Expand Down
2 changes: 2 additions & 0 deletions indexer/packages/postgres/src/types/perpetual-market-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface PerpetualMarketCreateObject {
liquidityTierId: number,
marketType: PerpetualMarketType,
baseOpenInterest: string,
defaultFundingRate1H: string,
}

export interface PerpetualMarketUpdateObject {
Expand Down Expand Up @@ -54,6 +55,7 @@ export enum PerpetualMarketColumns {
subticksPerTick = 'subticksPerTick',
stepBaseQuantums = 'stepBaseQuantums',
liquidityTierId = 'liquidityTierId',
defaultFundingRate1H = 'defaultFundingRate1H',
}

export enum PerpetualMarketStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export interface TradingPerpetualMarketMessage {
openInterestLowerCap?: string,
openInterestUpperCap?: string,
baseOpenInterest?: string,
defaultFundingRate1H?: string,

// Fields that are likely to change
priceChange24H?: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,12 @@ export interface PerpetualMarketCreateEventV2 {
/** Market type of the perpetual. */

marketType: PerpetualMarketType;
/**
* Since: v8.x
* Default 8hr funding rate in parts-per-million.
*/

defaultFunding8hrPpm: number;
}
/**
* PerpetualMarketCreateEventV2 message contains all the information about a
Expand Down Expand Up @@ -1101,6 +1107,12 @@ export interface PerpetualMarketCreateEventV2SDKType {
/** Market type of the perpetual. */

market_type: PerpetualMarketTypeSDKType;
/**
* Since: v8.x
* Default 8hr funding rate in parts-per-million.
*/

default_funding8hr_ppm: number;
}
/**
* LiquidityTierUpsertEventV1 message contains all the information to
Expand Down Expand Up @@ -1373,6 +1385,12 @@ export interface UpdatePerpetualEventV2 {
/** Market type of the perpetual. */

marketType: PerpetualMarketType;
/**
* Since: v8.x
* Default 8hr funding rate in parts-per-million.
*/

defaultFunding8hrPpm: number;
}
/**
* UpdatePerpetualEventV2 message contains all the information about an update
Expand Down Expand Up @@ -1415,6 +1433,12 @@ export interface UpdatePerpetualEventV2SDKType {
/** Market type of the perpetual. */

market_type: PerpetualMarketTypeSDKType;
/**
* Since: v8.x
* Default 8hr funding rate in parts-per-million.
*/

default_funding8hr_ppm: number;
}
/**
* TradingRewardsEventV1 is communicates all trading rewards for all accounts
Expand Down Expand Up @@ -3189,7 +3213,8 @@ function createBasePerpetualMarketCreateEventV2(): PerpetualMarketCreateEventV2
subticksPerTick: 0,
stepBaseQuantums: Long.UZERO,
liquidityTier: 0,
marketType: 0
marketType: 0,
defaultFunding8hrPpm: 0
};
}

Expand Down Expand Up @@ -3239,6 +3264,10 @@ export const PerpetualMarketCreateEventV2 = {
writer.uint32(88).int32(message.marketType);
}

if (message.defaultFunding8hrPpm !== 0) {
writer.uint32(96).int32(message.defaultFunding8hrPpm);
}

return writer;
},

Expand Down Expand Up @@ -3295,6 +3324,10 @@ export const PerpetualMarketCreateEventV2 = {
message.marketType = (reader.int32() as any);
break;

case 12:
message.defaultFunding8hrPpm = reader.int32();
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -3317,6 +3350,7 @@ export const PerpetualMarketCreateEventV2 = {
message.stepBaseQuantums = object.stepBaseQuantums !== undefined && object.stepBaseQuantums !== null ? Long.fromValue(object.stepBaseQuantums) : Long.UZERO;
message.liquidityTier = object.liquidityTier ?? 0;
message.marketType = object.marketType ?? 0;
message.defaultFunding8hrPpm = object.defaultFunding8hrPpm ?? 0;
return message;
}

Expand Down Expand Up @@ -3584,7 +3618,8 @@ function createBaseUpdatePerpetualEventV2(): UpdatePerpetualEventV2 {
marketId: 0,
atomicResolution: 0,
liquidityTier: 0,
marketType: 0
marketType: 0,
defaultFunding8hrPpm: 0
};
}

Expand Down Expand Up @@ -3614,6 +3649,10 @@ export const UpdatePerpetualEventV2 = {
writer.uint32(48).int32(message.marketType);
}

if (message.defaultFunding8hrPpm !== 0) {
writer.uint32(56).int32(message.defaultFunding8hrPpm);
}

return writer;
},

Expand Down Expand Up @@ -3650,6 +3689,10 @@ export const UpdatePerpetualEventV2 = {
message.marketType = (reader.int32() as any);
break;

case 7:
message.defaultFunding8hrPpm = reader.int32();
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -3667,6 +3710,7 @@ export const UpdatePerpetualEventV2 = {
message.atomicResolution = object.atomicResolution ?? 0;
message.liquidityTier = object.liquidityTier ?? 0;
message.marketType = object.marketType ?? 0;
message.defaultFunding8hrPpm = object.defaultFunding8hrPpm ?? 0;
return message;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface PerpetualParams {

atomicResolution: number;
/**
* The default funding payment if there is no price premium. In
* The default (8hr) funding payment if there is no price premium. In
* parts-per-million.
*/

Expand Down Expand Up @@ -154,7 +154,7 @@ export interface PerpetualParamsSDKType {

atomic_resolution: number;
/**
* The default funding payment if there is no price premium. In
* The default (8hr) funding payment if there is no price premium. In
* parts-per-million.
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ describe('request-transformer', () => {
openInterestLowerCap: liquidityTier.openInterestLowerCap,
openInterestUpperCap: liquidityTier.openInterestUpperCap,
baseOpenInterest: perpetualMarket.baseOpenInterest,
defaultFundingRate1H: perpetualMarket.defaultFundingRate1H,
},
);
});
Expand Down
16 changes: 11 additions & 5 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -2506,7 +2506,8 @@ fetch(`${baseURL}/perpetualMarkets`,
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string",
"baseOpenInterest": "string"
"baseOpenInterest": "string",
"defaultFundingRate1H": "string"
},
"property2": {
"clobPairId": "string",
Expand All @@ -2529,7 +2530,8 @@ fetch(`${baseURL}/perpetualMarkets`,
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string",
"baseOpenInterest": "string"
"baseOpenInterest": "string",
"defaultFundingRate1H": "string"
}
}
}
Expand Down Expand Up @@ -5387,7 +5389,8 @@ or
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string",
"baseOpenInterest": "string"
"baseOpenInterest": "string",
"defaultFundingRate1H": "string"
}

```
Expand Down Expand Up @@ -5417,6 +5420,7 @@ or
|openInterestLowerCap|string|false|none|none|
|openInterestUpperCap|string|false|none|none|
|baseOpenInterest|string|true|none|none|
|defaultFundingRate1H|string|true|none|none|

## PerpetualMarketResponse

Expand Down Expand Up @@ -5449,7 +5453,8 @@ or
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string",
"baseOpenInterest": "string"
"baseOpenInterest": "string",
"defaultFundingRate1H": "string"
},
"property2": {
"clobPairId": "string",
Expand All @@ -5472,7 +5477,8 @@ or
"marketType": "CROSS",
"openInterestLowerCap": "string",
"openInterestUpperCap": "string",
"baseOpenInterest": "string"
"baseOpenInterest": "string",
"defaultFundingRate1H": "string"
}
}
}
Expand Down
Loading

0 comments on commit a5349cd

Please sign in to comment.