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

Backport changes to support default funding on indexer #2702

Merged
merged 2 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 All @@ -36,6 +37,7 @@ export interface PerpetualMarketUpdateObject {
subticksPerTick?: number,
stepBaseQuantums?: number,
liquidityTierId?: number,
defaultFundingRate1H?: string,
}

export enum PerpetualMarketColumns {
Expand All @@ -54,6 +56,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 @@ -17,8 +17,14 @@ describe('nextFundingCache', () => {
await addFundingSample('BTC', new Big('0.0001'), client);
await addFundingSample('BTC', new Big('0.0002'), client); // avg = 0.00015
await addFundingSample('ETH', new Big('0.0005'), client); // avg = 0.0005
expect(await getNextFunding(client, ['BTC', 'ETH'])).toEqual(
{ BTC: new Big('0.00015'), ETH: new Big('0.0005') },
expect(await getNextFunding(client, [
['BTC', '0.0001'],
['ETH', '0'],
])).toEqual(
{
BTC: new Big('0.00025'), // 0.00015 + 0.0001
ETH: new Big('0.0005'), // 0.0005 + 0
},
);
});

Expand All @@ -27,13 +33,23 @@ describe('nextFundingCache', () => {
await addFundingSample('BTC', new Big('0.0002'), client); // avg = 0.00015
await clearFundingSamples('BTC', client);
await addFundingSample('ETH', new Big('0.0005'), client); // avg = 0.0005
expect(await getNextFunding(client, ['BTC', 'ETH'])).toEqual(
{ BTC: undefined, ETH: new Big('0.0005') },
expect(await getNextFunding(client, [
['BTC', '0.0001'],
['ETH', '0.00015'],
])).toEqual(
{
BTC: undefined, // no samples
ETH: new Big('0.00065'), // 0.0005 + 0.00015
},
);
});

it('get next funding with no values', async () => {
expect(await getNextFunding(client, ['BTC'])).toEqual(
expect(await getNextFunding(client, [
['BTC', '0.001'],
])).toEqual(
// Even though default funding rate is 0.001,
// return undefined since there are no samples
{ BTC: undefined },
);
});
Expand Down
7 changes: 4 additions & 3 deletions indexer/packages/redis/src/caches/next-funding-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ function getKey(ticker: string): string {
*/
export async function getNextFunding(
client: RedisClient,
tickers: string[],
tickerDefaultFundingRate1HPairs: [string, string][],
): Promise<{ [ticker: string]: Big | undefined }> {
const fundingRates: { [ticker: string]: Big | undefined } = {};

await Promise.all(
tickers.map(async (ticker: string) => {
tickerDefaultFundingRate1HPairs.map(async ([ticker, defaultFundingRate1H]) => {
const rates: string[] = await lRangeAsync(
getKey(ticker),
client,
Expand All @@ -36,7 +37,7 @@ export async function getNextFunding(
new Big(0),
);
const avg: Big = sum.div(rates.length);
fundingRates[ticker] = avg;
fundingRates[ticker] = avg.plus(new Big(defaultFundingRate1H));
} else {
fundingRates[ticker] = undefined;
}
Expand Down
Loading
Loading