Skip to content

Commit

Permalink
[CT-641] Update deleveraging handler for DeleveragingEventV1 (#1121)
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill authored Feb 29, 2024
1 parent b8038e7 commit 564608e
Show file tree
Hide file tree
Showing 11 changed files with 210 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ export interface DeleveragingEventV1 {
*/

fillAmount: Long;
/** Fill price of deleveraging event, in USDC quote quantums. */
/** Total quote quantums filled. */

price: Long;
totalQuoteQuantums: Long;
/** `true` if liquidating a short position, `false` otherwise. */

isBuy: boolean;
Expand Down Expand Up @@ -501,9 +501,9 @@ export interface DeleveragingEventV1SDKType {
*/

fill_amount: Long;
/** Fill price of deleveraging event, in USDC quote quantums. */
/** Total quote quantums filled. */

price: Long;
total_quote_quantums: Long;
/** `true` if liquidating a short position, `false` otherwise. */

is_buy: boolean;
Expand Down Expand Up @@ -1853,7 +1853,7 @@ function createBaseDeleveragingEventV1(): DeleveragingEventV1 {
offsetting: undefined,
perpetualId: 0,
fillAmount: Long.UZERO,
price: Long.UZERO,
totalQuoteQuantums: Long.UZERO,
isBuy: false,
isFinalSettlement: false
};
Expand All @@ -1877,8 +1877,8 @@ export const DeleveragingEventV1 = {
writer.uint32(32).uint64(message.fillAmount);
}

if (!message.price.isZero()) {
writer.uint32(40).uint64(message.price);
if (!message.totalQuoteQuantums.isZero()) {
writer.uint32(40).uint64(message.totalQuoteQuantums);
}

if (message.isBuy === true) {
Expand Down Expand Up @@ -1918,7 +1918,7 @@ export const DeleveragingEventV1 = {
break;

case 5:
message.price = (reader.uint64() as Long);
message.totalQuoteQuantums = (reader.uint64() as Long);
break;

case 6:
Expand All @@ -1944,7 +1944,7 @@ export const DeleveragingEventV1 = {
message.offsetting = object.offsetting !== undefined && object.offsetting !== null ? IndexerSubaccountId.fromPartial(object.offsetting) : undefined;
message.perpetualId = object.perpetualId ?? 0;
message.fillAmount = object.fillAmount !== undefined && object.fillAmount !== null ? Long.fromValue(object.fillAmount) : Long.UZERO;
message.price = object.price !== undefined && object.price !== null ? Long.fromValue(object.price) : Long.UZERO;
message.totalQuoteQuantums = object.totalQuoteQuantums !== undefined && object.totalQuoteQuantums !== null ? Long.fromValue(object.totalQuoteQuantums) : Long.UZERO;
message.isBuy = object.isBuy ?? false;
message.isFinalSettlement = object.isFinalSettlement ?? false;
return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,9 @@ describe('DeleveragingHandler', () => {
);

// This size should be in fixed-point notation rather than exponential notation.
const quoteAmount: string = '0.00000000001'; // quote amount is price * fillAmount = 1e3 * 1e-14 = 1e-11
const quoteAmount: string = '1000'; // quote amount is event->price * QUOTE_CURRENCY_ATOMIC_RESOLUTION = 1e9*1e-6=1e3
const totalFilled: string = '0.00000000000001'; // fillAmount in human = 10^4*10^-18=10^-14
const price: string = '1000'; // 10^9*10^-6=10^3
const price: string = '100000000000000000'; // 1e3/1e-14=1e17
const perpetualMarket: PerpetualMarketFromDatabase | undefined = perpetualMarketRefresher
.getPerpetualMarketFromId(
defaultDeleveragingEvent.perpetualId.toString(),
Expand Down
2 changes: 1 addition & 1 deletion indexer/services/ender/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ export const defaultDeleveragingEvent: DeleveragingEventV1 = {
offsetting: defaultRecipientSubaccountId,
perpetualId: 1,
fillAmount: Long.fromValue(10_000, true),
price: Long.fromValue(1_000_000_000, true),
totalQuoteQuantums: Long.fromValue(1_000_000_000, true),
isBuy: true,
isFinalSettlement: false,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../helpers/constants';
import { createIndexerTendermintBlock, createIndexerTendermintEvent } from '../helpers/indexer-proto-helpers';
import { expectDidntLogError, expectLoggedParseMessageError } from '../helpers/validator-helpers';
import Long from 'long';

describe('deleveraging-validator', () => {
beforeEach(() => {
Expand Down Expand Up @@ -46,6 +47,22 @@ describe('deleveraging-validator', () => {
},
'DeleveragingEvent must have an offsetting subaccount id',
],
[
'has fillAmount of 0',
{
...defaultDeleveragingEvent,
fillAmount: new Long(0),
},
'DeleveragingEvent fillAmount cannot equal 0',
],
[
'has totalQuoteQuantums of 0',
{
...defaultDeleveragingEvent,
totalQuoteQuantums: new Long(0),
},
'DeleveragingEvent totalQuoteQuantums cannot equal 0',
],
])('throws error if event %s', (_message: string, event: DeleveragingEventV1, message: string) => {
const validator: DeleveragingValidator = new DeleveragingValidator(
event,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ DECLARE
liquidated_side text;
offsetting_side text;
size numeric;
quote_amount numeric;
price numeric;
event_id bytea;
BEGIN
Expand All @@ -55,8 +56,9 @@ BEGIN
*/
size = dydx_trim_scale(dydx_from_jsonlib_long(event_data->'fillAmount') *
power(10, perpetual_market_record."atomicResolution")::numeric);
price = dydx_trim_scale(dydx_from_jsonlib_long(event_data->'price') *
quote_amount = dydx_trim_scale(dydx_from_jsonlib_long(event_data->'totalQuoteQuantums') *
power(10, QUOTE_CURRENCY_ATOMIC_RESOLUTION)::numeric);
price = dydx_trim_scale(quote_amount / size);

liquidated_subaccount_uuid = dydx_uuid_from_subaccount_id(event_data->'liquidated');
offsetting_subaccount_uuid = dydx_uuid_from_subaccount_id(event_data->'offsetting');
Expand All @@ -78,7 +80,7 @@ BEGIN
clob_pair_id,
size,
price,
dydx_trim_scale(size * price),
quote_amount,
event_id,
transaction_hash,
block_time,
Expand All @@ -97,7 +99,7 @@ BEGIN
clob_pair_id,
size,
price,
dydx_trim_scale(size * price),
quote_amount,
event_id,
transaction_hash,
block_time,
Expand Down
13 changes: 13 additions & 0 deletions indexer/services/ender/src/validators/deleveraging-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,19 @@ export class DeleveragingValidator extends Validator<DeleveragingEventV1> {
);
}

if (this.event.fillAmount.eq(0)) {
return this.logAndThrowParseMessageError(
'DeleveragingEvent fillAmount cannot equal 0',
{ event: this.event },
);
}

if (this.event.totalQuoteQuantums.eq(0)) {
return this.logAndThrowParseMessageError(
'DeleveragingEvent totalQuoteQuantums cannot equal 0',
{ event: this.event },
);
}
}

public createHandlers(
Expand Down
4 changes: 2 additions & 2 deletions proto/dydxprotocol/indexer/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ message DeleveragingEventV1 {
// The amount filled between the liquidated and offsetting position, in
// base quantums.
uint64 fill_amount = 4;
// Fill price of deleveraging event, in USDC quote quantums.
uint64 price = 5;
// Total quote quantums filled.
uint64 total_quote_quantums = 5;
// `true` if liquidating a short position, `false` otherwise.
bool is_buy = 6;
// `true` if the deleveraging event is for final settlement, indicating
Expand Down
17 changes: 9 additions & 8 deletions protocol/indexer/events/deleveraging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ import (
// liquidatedSubaccountId is not actually an account that is liquidatable. More
// specifically, it may be a well-collateralized subaccount with an open position
// in a market with the final settlement status.
// TODO(CT-641): Use the actual unit price rather than the total quote quantums.
func NewDeleveragingEvent(
liquidatedSubaccountId satypes.SubaccountId,
offsettingSubaccountId satypes.SubaccountId,
perpetualId uint32,
fillAmount satypes.BaseQuantums,
price satypes.BaseQuantums,
totalQuoteQuantums satypes.BaseQuantums,
isBuy bool,
isFinalSettlement bool,
) *DeleveragingEventV1 {
indexerLiquidatedSubaccountId := v1.SubaccountIdToIndexerSubaccountId(liquidatedSubaccountId)
indexerOffsettingSubaccountId := v1.SubaccountIdToIndexerSubaccountId(offsettingSubaccountId)
return &DeleveragingEventV1{
Liquidated: indexerLiquidatedSubaccountId,
Offsetting: indexerOffsettingSubaccountId,
PerpetualId: perpetualId,
FillAmount: fillAmount.ToUint64(),
Price: price.ToUint64(),
IsBuy: isBuy,
IsFinalSettlement: isFinalSettlement,
Liquidated: indexerLiquidatedSubaccountId,
Offsetting: indexerOffsettingSubaccountId,
PerpetualId: perpetualId,
FillAmount: fillAmount.ToUint64(),
TotalQuoteQuantums: totalQuoteQuantums.ToUint64(),
IsBuy: isBuy,
IsFinalSettlement: isFinalSettlement,
}
}
16 changes: 8 additions & 8 deletions protocol/indexer/events/deleveraging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var (
liquidatedSubaccountId = constants.Alice_Num0
offsettingSubaccountId = constants.Bob_Num0
perpetualId = uint32(1)
price = satypes.BaseQuantums(1000)
totalQuoteQuantums = satypes.BaseQuantums(1000)
isBuy = true
)

Expand All @@ -25,19 +25,19 @@ func TestNewDeleveragingEvent_Success(t *testing.T) {
offsettingSubaccountId,
perpetualId,
fillAmount,
price,
totalQuoteQuantums,
isBuy,
false,
)
indexerLiquidatedSubaccountId := v1.SubaccountIdToIndexerSubaccountId(liquidatedSubaccountId)
indexerOffsettingSubaccountId := v1.SubaccountIdToIndexerSubaccountId(offsettingSubaccountId)
expectedDeleveragingEventProto := &events.DeleveragingEventV1{
Liquidated: indexerLiquidatedSubaccountId,
Offsetting: indexerOffsettingSubaccountId,
PerpetualId: perpetualId,
FillAmount: fillAmount.ToUint64(),
Price: price.ToUint64(),
IsBuy: isBuy,
Liquidated: indexerLiquidatedSubaccountId,
Offsetting: indexerOffsettingSubaccountId,
PerpetualId: perpetualId,
FillAmount: fillAmount.ToUint64(),
TotalQuoteQuantums: totalQuoteQuantums.ToUint64(),
IsBuy: isBuy,
}
require.Equal(t, expectedDeleveragingEventProto, deleveragingEvent)
}
Loading

0 comments on commit 564608e

Please sign in to comment.