Skip to content

Commit

Permalink
Default funding
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyding committed Jan 14, 2025
1 parent 2a79ca0 commit 84dc866
Show file tree
Hide file tree
Showing 62 changed files with 2,189 additions and 244 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', 'null'], default: null, 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
Loading

0 comments on commit 84dc866

Please sign in to comment.