Skip to content

Commit

Permalink
Showing 7 changed files with 55 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -256,6 +256,7 @@ describe('DeleveragingHandler', () => {
clientMetadata: null,
hasOrderId: false,
fee: '0',
affiliateRevShare: '0',
}),
expectFillInDatabase({
subaccountId: SubaccountTable.subaccountIdToUuid(defaultDeleveragingEvent.liquidated!),
@@ -275,6 +276,7 @@ describe('DeleveragingHandler', () => {
clientMetadata: null,
hasOrderId: false,
fee: '0',
affiliateRevShare: '0',
}),
expectPerpetualPosition(
PerpetualPositionTable.uuid(
Original file line number Diff line number Diff line change
@@ -76,6 +76,7 @@ import { expectStateFilledQuantums } from '../../helpers/redis-helpers';
const defaultClobPairId: string = testConstants.defaultPerpetualMarket.clobPairId;
const defaultMakerFeeQuantum: number = 1_000_000;
const defaultTakerFeeQuantum: number = 2_000_000;
const defaultAffiliateRevShareQuantum: number = 3_000_000;
const defaultMakerFee: string = protocolTranslations.quantumsToHumanFixedString(
defaultMakerFeeQuantum.toString(),
testConstants.defaultAsset.atomicResolution,
@@ -84,6 +85,10 @@ const defaultTakerFee: string = protocolTranslations.quantumsToHumanFixedString(
defaultTakerFeeQuantum.toString(),
testConstants.defaultAsset.atomicResolution,
);
const defaultAffiliateRevShare: string = protocolTranslations.quantumsToHumanFixedString(
defaultAffiliateRevShareQuantum.toString(),
testConstants.defaultAsset.atomicResolution,
);

describe('LiquidationHandler', () => {
beforeAll(async () => {
@@ -336,6 +341,7 @@ describe('LiquidationHandler', () => {
orderFlags: makerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -354,6 +360,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
clientMetadata: null,
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
hasOrderId: false,
});

@@ -567,6 +574,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_LONG_TERM.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -585,6 +593,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
clientMetadata: null,
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
hasOrderId: false,
});

@@ -785,6 +794,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_LONG_TERM.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -803,6 +813,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
clientMetadata: null,
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
hasOrderId: false,
});

@@ -943,6 +954,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -961,6 +973,7 @@ describe('LiquidationHandler', () => {
orderFlags: ORDER_FLAG_SHORT_TERM.toString(),
clientMetadata: null,
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
hasOrderId: false,
});

@@ -1179,7 +1192,6 @@ function createLiquidationOrderFillEvent(
liquidationOrderProto: LiquidationOrderV1,
fillAmount: number,
totalFilledMaker: number,
affiliateRevShare: number = 0,
): OrderFillEventV1 {
return {
makerOrder: makerOrderProto,
@@ -1189,7 +1201,7 @@ function createLiquidationOrderFillEvent(
takerFee: Long.fromValue(defaultTakerFeeQuantum, false),
totalFilledMaker: Long.fromValue(totalFilledMaker, true),
totalFilledTaker: Long.fromValue(fillAmount, true),
affiliateRevShare: Long.fromValue(affiliateRevShare, false),
affiliateRevShare: Long.fromValue(defaultAffiliateRevShareQuantum, false),
} as OrderFillEventV1;
}

Original file line number Diff line number Diff line change
@@ -79,6 +79,7 @@ import { expectStateFilledQuantums } from '../../helpers/redis-helpers';
const defaultClobPairId: string = testConstants.defaultPerpetualMarket.clobPairId;
const defaultMakerFeeQuantum: number = 1_000_000;
const defaultTakerFeeQuantum: number = 2_000_000;
const defaultAffiliateRevShareQuantum: number = 3_000_000;
const defaultMakerFee: string = protocolTranslations.quantumsToHumanFixedString(
defaultMakerFeeQuantum.toString(),
testConstants.defaultAsset.atomicResolution,
@@ -87,6 +88,10 @@ const defaultTakerFee: string = protocolTranslations.quantumsToHumanFixedString(
defaultTakerFeeQuantum.toString(),
testConstants.defaultAsset.atomicResolution,
);
const defaultAffiliateRevShare: string = protocolTranslations.quantumsToHumanFixedString(
defaultAffiliateRevShareQuantum.toString(),
testConstants.defaultAsset.atomicResolution,
);

describe('OrderHandler', () => {
beforeAll(async () => {
@@ -360,6 +365,7 @@ describe('OrderHandler', () => {
orderFlags: makerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -378,6 +384,7 @@ describe('OrderHandler', () => {
orderFlags: takerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: takerOrderProto.clientMetadata.toString(),
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});

const expectedMakerOffchainUpdate: OffChainUpdateV1 = {
@@ -699,6 +706,7 @@ describe('OrderHandler', () => {
orderFlags: makerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -717,6 +725,7 @@ describe('OrderHandler', () => {
orderFlags: takerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: takerOrderProto.clientMetadata.toString(),
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});

const expectedMakerUpdateOffchainUpdate: OffChainUpdateV1 = {
@@ -983,6 +992,7 @@ describe('OrderHandler', () => {
orderFlags: makerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -1001,6 +1011,7 @@ describe('OrderHandler', () => {
orderFlags: takerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: takerOrderProto.clientMetadata.toString(),
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});

const expectedMakerUpdateOffchainUpdate: OffChainUpdateV1 = {
@@ -1204,6 +1215,7 @@ describe('OrderHandler', () => {
orderFlags: makerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -1222,6 +1234,7 @@ describe('OrderHandler', () => {
orderFlags: takerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: takerOrderProto.clientMetadata.toString(),
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});

await Promise.all([
@@ -1406,6 +1419,7 @@ describe('OrderHandler', () => {
orderFlags: makerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: makerOrderProto.clientMetadata.toString(),
fee: defaultMakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});
await expectFillInDatabase({
subaccountId: testConstants.defaultSubaccountId2,
@@ -1424,6 +1438,7 @@ describe('OrderHandler', () => {
orderFlags: takerOrderProto.orderId!.orderFlags.toString(),
clientMetadata: takerOrderProto.clientMetadata.toString(),
fee: defaultTakerFee,
affiliateRevShare: defaultAffiliateRevShare,
});

await Promise.all([
@@ -1822,7 +1837,6 @@ function createOrderFillEvent(
fillAmount: number,
totalFilledMaker: number,
totalFilledTaker: number,
affiliateRevShare: number = 0,
): OrderFillEventV1 {
return {
makerOrder: makerOrderProto,
@@ -1832,7 +1846,7 @@ function createOrderFillEvent(
takerFee: Long.fromValue(defaultTakerFeeQuantum, false),
totalFilledMaker: Long.fromValue(totalFilledMaker, true),
totalFilledTaker: Long.fromValue(totalFilledTaker, true),
affiliateRevShare: Long.fromValue(affiliateRevShare, false),
affiliateRevShare: Long.fromValue(defaultAffiliateRevShareQuantum, false),
} as OrderFillEventV1;
}

Original file line number Diff line number Diff line change
@@ -542,6 +542,7 @@ export async function expectFillInDatabase({
orderFlags,
clientMetadata,
fee,
affiliateRevShare,
hasOrderId = true,
}: {
subaccountId: string,
@@ -560,6 +561,7 @@ export async function expectFillInDatabase({
orderFlags: string,
clientMetadata: string | null,
fee: string,
affiliateRevShare: string,
hasOrderId?: boolean,
}): Promise<void> {
const fillId: string = FillTable.uuid(eventId, liquidity);
@@ -582,6 +584,7 @@ export async function expectFillInDatabase({
createdAtHeight,
clientMetadata,
fee,
affiliateRevShare,
}));
}

Original file line number Diff line number Diff line change
@@ -23,6 +23,7 @@ CREATE OR REPLACE FUNCTION dydx_deleveraging_handler(
DECLARE
QUOTE_CURRENCY_ATOMIC_RESOLUTION constant numeric = -6;
FEE constant numeric = 0;
AFFILIATE_REV_SHARE constant numeric = 0;
perpetual_id bigint;
clob_pair_id bigint;
liquidated_subaccount_uuid uuid;
@@ -81,7 +82,7 @@ BEGIN
block_height, transaction_index, event_index);
INSERT INTO fills
("id", "subaccountId", "side", "liquidity", "type", "clobPairId", "size", "price", "quoteAmount",
"eventId", "transactionHash", "createdAt", "createdAtHeight", "fee")
"eventId", "transactionHash", "createdAt", "createdAtHeight", "fee", "affiliateRevShare")
VALUES (dydx_uuid_from_fill_event_parts(event_id, 'TAKER'),
liquidated_subaccount_uuid,
liquidated_side,
@@ -95,12 +96,13 @@ BEGIN
transaction_hash,
block_time,
block_height,
FEE)
FEE,
AFFILIATE_REV_SHARE)
RETURNING * INTO liquidated_fill_record;

INSERT INTO fills
("id", "subaccountId", "side", "liquidity", "type", "clobPairId", "size", "price", "quoteAmount",
"eventId", "transactionHash", "createdAt", "createdAtHeight", "fee")
"eventId", "transactionHash", "createdAt", "createdAtHeight", "fee", "affiliateRevShare")
VALUES (dydx_uuid_from_fill_event_parts(event_id, 'MAKER'),
offsetting_subaccount_uuid,
offsetting_side,
@@ -114,7 +116,8 @@ BEGIN
transaction_hash,
block_time,
block_height,
FEE)
FEE,
AFFILIATE_REV_SHARE)
RETURNING * INTO offsetting_fill_record;

/* Upsert the perpetual_position records for this deleveraging event. */
Original file line number Diff line number Diff line change
@@ -18,6 +18,7 @@ DECLARE
order_price numeric;
order_client_metadata bigint;
fee numeric;
affiliate_rev_share numeric;
fill_amount numeric;
total_filled numeric;
maker_price numeric;
@@ -80,6 +81,8 @@ BEGIN
power(10, perpetual_market_record."atomicResolution")::numeric);
fee = dydx_trim_scale(dydx_get_fee(fill_liquidity, event_data) *
power(10, asset_record."atomicResolution")::numeric);
affiliate_rev_share = dydx_trim_scale(dydx_from_jsonlib_long(event_data->'affiliateRevShare') *
power(10, asset_record."atomicResolution")::numeric);
order_price = dydx_trim_scale(dydx_from_jsonlib_long(order_->'subticks') *
power(10, perpetual_market_record."quantumConversionExponent" +
asset_record."atomicResolution" -
@@ -160,7 +163,7 @@ BEGIN
block_height, transaction_index, event_index);
INSERT INTO fills
("id", "subaccountId", "side", "liquidity", "type", "clobPairId", "orderId", "size", "price", "quoteAmount",
"eventId", "transactionHash", "createdAt", "createdAtHeight", "clientMetadata", "fee")
"eventId", "transactionHash", "createdAt", "createdAtHeight", "clientMetadata", "fee", "affiliateRevShare")
VALUES (dydx_uuid_from_fill_event_parts(event_id, fill_liquidity),
subaccount_uuid,
order_side,
@@ -176,7 +179,8 @@ BEGIN
block_time,
block_height,
order_client_metadata,
fee)
fee,
affiliate_rev_share)
RETURNING * INTO fill_record;

/* Upsert the perpetual_position record for this order_fill event. */
Original file line number Diff line number Diff line change
@@ -41,6 +41,8 @@ DECLARE
order_price numeric;
order_client_metadata bigint;
fee numeric;
affiliate_rev_share numeric;
affiliate_rev numeric;
fill_amount numeric;
total_filled numeric;
maker_price numeric;
@@ -80,6 +82,8 @@ BEGIN
power(10, perpetual_market_record."atomicResolution")::numeric);
fee = dydx_trim_scale(dydx_get_fee(fill_liquidity, event_data) *
power(10, asset_record."atomicResolution")::numeric);
affiliate_rev_share = dydx_trim_scale(dydx_from_jsonlib_long(event_data->'affiliateRevShare') *
power(10, asset_record."atomicResolution")::numeric);

order_uuid = dydx_uuid_from_order_id(order_->'orderId');
subaccount_uuid = dydx_uuid_from_subaccount_id(jsonb_extract_path(order_, 'orderId', 'subaccountId'));
@@ -143,7 +147,7 @@ BEGIN
block_height, transaction_index, event_index);
INSERT INTO fills
("id", "subaccountId", "side", "liquidity", "type", "clobPairId", "orderId", "size", "price", "quoteAmount",
"eventId", "transactionHash", "createdAt", "createdAtHeight", "clientMetadata", "fee")
"eventId", "transactionHash", "createdAt", "createdAtHeight", "clientMetadata", "fee", "affiliateRevShare")
VALUES (dydx_uuid_from_fill_event_parts(event_id, fill_liquidity),
subaccount_uuid,
order_side,
@@ -159,7 +163,8 @@ BEGIN
block_time,
block_height,
order_client_metadata,
fee)
fee,
affiliate_rev_share)
RETURNING * INTO fill_record;

/* Upsert the perpetual_position record for this order_fill event. */

0 comments on commit 0ce92b3

Please sign in to comment.