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 7, 2025
1 parent 6973144 commit 76278b2
Show file tree
Hide file tree
Showing 38 changed files with 398 additions and 172 deletions.
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 @@ -389,6 +389,7 @@ export function perpetualMarketToResponseObject(
openInterestLowerCap: liquidityTier.openInterestLowerCap,
openInterestUpperCap: liquidityTier.openInterestUpperCap,
baseOpenInterest: perpetualMarket.baseOpenInterest,
defaultFundingRate1H: perpetualMarket.defaultFundingRate1H,
};
}

Expand Down
1 change: 1 addition & 0 deletions indexer/services/comlink/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ export interface PerpetualMarketResponseObject {
openInterestLowerCap?: string,
openInterestUpperCap?: string,
baseOpenInterest: string,
defaultFundingRate1H: string,
}

/* ------- ORDERBOOK TYPES ------- */
Expand Down
1 change: 1 addition & 0 deletions indexer/services/ender/src/helpers/kafka-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ export function generatePerpetualMarketMessage(
nextFundingRate: perpetualMarket.nextFundingRate,
openInterest: perpetualMarket.openInterest,
baseOpenInterest: perpetualMarket.baseOpenInterest,
defaultFundingRate1H: perpetualMarket.defaultFundingRate1H,
};
})
.value();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CREATE OR REPLACE FUNCTION dydx_perpetual_market_v2_handler(event_data jsonb) RE
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
DECLARE
PPM_EXPONENT constant numeric = -6;
FUNDING_RATE_FROM_PROTOCOL_IN_HOURS constant numeric = 8;
perpetual_market_record perpetual_markets%ROWTYPE;
BEGIN
perpetual_market_record."id" = (event_data->'id')::bigint;
Expand All @@ -28,6 +30,16 @@ BEGIN
perpetual_market_record."liquidityTierId" = (event_data->'liquidityTier')::integer;
perpetual_market_record."marketType" = dydx_protocol_market_type_to_perpetual_market_type(event_data->'marketType');
perpetual_market_record."baseOpenInterest" = 0;
perpetual_market_record."defaultFundingRate1H" =
/* For backwards compatibility, handle the case where defaultFundingPpm is not present */
CASE
/* Convert defaultFundingPpm from parts-per-million to a rate */
WHEN event_data ? 'defaultFunding8hrPpm' THEN dydx_trim_scale(
power(10, PPM_EXPONENT) /
FUNDING_RATE_FROM_PROTOCOL_IN_HOURS *
(event_data->'defaultFunding8hrPpm')::numeric);
ELSE 0.0
END;

INSERT INTO perpetual_markets VALUES (perpetual_market_record.*) RETURNING * INTO perpetual_market_record;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ CREATE OR REPLACE FUNCTION dydx_update_perpetual_v2_handler(event_data jsonb) RE
(Note that no text should exist before the function declaration to ensure that exception line numbers are correct.)
*/
DECLARE
PPM_EXPONENT constant numeric = -6;
FUNDING_RATE_FROM_PROTOCOL_IN_HOURS constant numeric = 8;
perpetual_market_id bigint;
perpetual_market_record perpetual_markets%ROWTYPE;
BEGIN
Expand All @@ -18,14 +20,24 @@ BEGIN
perpetual_market_record."atomicResolution" = (event_data->'atomicResolution')::integer;
perpetual_market_record."liquidityTierId" = (event_data->'liquidityTier')::integer;
perpetual_market_record."marketType" = dydx_protocol_market_type_to_perpetual_market_type(event_data->'marketType');

perpetual_market_record."defaultFundingRate1H" =
/* For backwards compatibility, handle the case where defaultFundingPpm is not present */
CASE
/* Convert defaultFundingPpm from parts-per-million to a rate */
WHEN event_data ? 'defaultFunding8hrPpm' THEN dydx_trim_scale(
power(10, PPM_EXPONENT) /
FUNDING_RATE_FROM_PROTOCOL_IN_HOURS *
(event_data->'defaultFunding8hrPpm')::numeric);
ELSE 0.0
END;
UPDATE perpetual_markets
SET
"ticker" = perpetual_market_record."ticker",
"marketId" = perpetual_market_record."marketId",
"atomicResolution" = perpetual_market_record."atomicResolution",
"liquidityTierId" = perpetual_market_record."liquidityTierId",
"marketType" = perpetual_market_record."marketType"
"marketType" = perpetual_market_record."marketType",
"defaultFundingRate1H" = perpetual_market_record."defaultFundingRate1H"
WHERE "id" = perpetual_market_id
RETURNING * INTO perpetual_market_record;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ describe('pnl-ticks-helper', () => {
liquidityTierId: 0,
marketType: PerpetualMarketType.ISOLATED,
baseOpenInterest: '100000',
defaultFundingRate1H: '0.0001',
});
await perpetualMarketRefresher.updatePerpetualMarkets();
const positions2: PerpetualPositionFromDatabase[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ const RAW_TABLE_COLUMNS: string = `
\`stepBaseQuantums\` int,
\`liquidityTierId\` int,
\`marketType\` string,
\`baseOpenInterest\` string
\`baseOpenInterest\` string,
\`defaultFundingRate1H\` string
`;
const TABLE_COLUMNS: string = `
"id",
Expand All @@ -41,7 +42,8 @@ const TABLE_COLUMNS: string = `
"stepBaseQuantums",
"liquidityTierId",
"marketType",
${castToDouble('baseOpenInterest')}
${castToDouble('baseOpenInterest')},
${castToDouble('defaultFundingRate1H')}
`;

export function generateRawTable(tablePrefix: string, rdsExportIdentifier: string): string {
Expand Down
8 changes: 8 additions & 0 deletions proto/dydxprotocol/indexer/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,10 @@ message PerpetualMarketCreateEventV2 {

// Market type of the perpetual.
dydxprotocol.indexer.protocol.v1.PerpetualMarketType market_type = 11;

// Since: v8.x
// Default 8hr funding rate in parts-per-million.
int32 default_funding8hr_ppm = 12;
}

// LiquidityTierUpsertEventV1 message contains all the information to
Expand Down Expand Up @@ -542,6 +546,10 @@ message UpdatePerpetualEventV2 {

// Market type of the perpetual.
dydxprotocol.indexer.protocol.v1.PerpetualMarketType market_type = 6;

// Since: v8.x
// Default 8hr funding rate in parts-per-million.
int32 default_funding8hr_ppm = 7;
}

// TradingRewardsEventV1 is communicates all trading rewards for all accounts
Expand Down
Loading

0 comments on commit 76278b2

Please sign in to comment.