Skip to content

Commit

Permalink
[OTE-877] deprecate OI indexer update event (#2499)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored and adamfraser committed Nov 20, 2024
1 parent 3533030 commit e085891
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 360 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1372,31 +1372,47 @@ export interface AddressTradingRewardSDKType {

denom_amount: Uint8Array;
}
/** OpenInterestUpdateEventV1 is used for open interest update events */
/**
* OpenInterestUpdateEventV1 is used for open interest update events
* Deprecated.
*/

/** @deprecated */

export interface OpenInterestUpdateEventV1 {
/** The list of all open interest updates in the block. */
openInterestUpdates: OpenInterestUpdate[];
}
/** OpenInterestUpdateEventV1 is used for open interest update events */
/**
* OpenInterestUpdateEventV1 is used for open interest update events
* Deprecated.
*/

/** @deprecated */

export interface OpenInterestUpdateEventV1SDKType {
/** The list of all open interest updates in the block. */
open_interest_updates: OpenInterestUpdateSDKType[];
}
/** OpenInterestUpdate contains a single open interest update for a perpetual */
/**
* OpenInterestUpdate contains a single open interest update for a perpetual
* Deprecated.
*/

/** @deprecated */

export interface OpenInterestUpdate {
/** The ID of the perpetual market. */
perpetualId: number;
/** The new open interest value for the perpetual market. */

openInterest: Uint8Array;
}
/** OpenInterestUpdate contains a single open interest update for a perpetual */
/**
* OpenInterestUpdate contains a single open interest update for a perpetual
* Deprecated.
*/

/** @deprecated */

export interface OpenInterestUpdateSDKType {
/** The ID of the perpetual market. */
perpetual_id: number;
/** The new open interest value for the perpetual market. */

Expand Down
4 changes: 4 additions & 0 deletions proto/dydxprotocol/indexer/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -534,14 +534,18 @@ message AddressTradingReward {
}

// OpenInterestUpdateEventV1 is used for open interest update events
// Deprecated.
message OpenInterestUpdateEventV1 {
// The list of all open interest updates in the block.
option deprecated = true;
repeated OpenInterestUpdate open_interest_updates = 1;
}

// OpenInterestUpdate contains a single open interest update for a perpetual
// Deprecated.
message OpenInterestUpdate {
// The ID of the perpetual market.
option deprecated = true;
uint32 perpetual_id = 1;

// The new open interest value for the perpetual market.
Expand Down
316 changes: 160 additions & 156 deletions protocol/indexer/events/events.pb.go

Large diffs are not rendered by default.

64 changes: 0 additions & 64 deletions protocol/x/clob/e2e/long_term_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,22 +825,6 @@ func TestPlaceLongTermOrder(t *testing.T) {
EventIndex: 2,
Version: indexerevents.OrderFillEventVersion,
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewInt(10_000_000_000),
},
},
}),
},
},
TxHashes: []string{
string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
Expand Down Expand Up @@ -1169,22 +1153,6 @@ func TestPlaceLongTermOrder(t *testing.T) {
EventIndex: 2,
Version: indexerevents.OrderFillEventVersion,
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewInt(10_000_000_000),
},
},
}),
},
},
TxHashes: []string{
string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
Expand Down Expand Up @@ -1340,22 +1308,6 @@ func TestPlaceLongTermOrder(t *testing.T) {
EventIndex: 2,
Version: indexerevents.OrderFillEventVersion,
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewInt(20_000_000_000),
},
},
}),
},
},
TxHashes: []string{
string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
Expand Down Expand Up @@ -1739,22 +1691,6 @@ func TestRegression_InvalidTimeInForce(t *testing.T) {
EventIndex: 2,
Version: indexerevents.OrderFillEventVersion,
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewInt(10_000_000_000),
},
},
}),
},
},
TxHashes: []string{
string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
Expand Down
55 changes: 0 additions & 55 deletions protocol/x/clob/e2e/short_term_orders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/stretchr/testify/require"
"golang.org/x/exp/slices"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
"github.com/dydxprotocol/v4-chain/protocol/indexer"
indexerevents "github.com/dydxprotocol/v4-chain/protocol/indexer/events"
"github.com/dydxprotocol/v4-chain/protocol/indexer/indexer_manager"
Expand Down Expand Up @@ -219,24 +218,6 @@ func TestPlaceOrder(t *testing.T) {
),
),
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewIntFromUint64(
PlaceOrder_Alice_Num0_Id0_Clob0_Buy5_Price10_GTB20.Order.GetBigQuantums().Uint64(),
),
},
},
}),
},
},
TxHashes: []string{string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
OperationsQueue: []clobtypes.OperationRaw{
Expand Down Expand Up @@ -399,24 +380,6 @@ func TestPlaceOrder(t *testing.T) {
),
),
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewIntFromUint64(
PlaceOrder_Bob_Num0_Id0_Clob0_Sell5_Price10_GTB20.Order.GetBigQuantums().Uint64(),
),
},
},
}),
},
},
TxHashes: []string{string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
OperationsQueue: []clobtypes.OperationRaw{
Expand Down Expand Up @@ -579,24 +542,6 @@ func TestPlaceOrder(t *testing.T) {
),
),
},
{
Subtype: indexerevents.SubtypeOpenInterestUpdate,
OrderingWithinBlock: &indexer_manager.IndexerTendermintEvent_BlockEvent_{
BlockEvent: indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
},
Version: indexerevents.OpenInterestUpdateVersion,
DataBytes: indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: []*indexerevents.OpenInterestUpdate{
{
PerpetualId: Clob_0.MustGetPerpetualId(),
OpenInterest: dtypes.NewIntFromUint64(
PlaceOrder_Bob_Num0_Id0_Clob0_Sell5_Price10_GTB20.Order.GetBigQuantums().Uint64(),
),
},
},
}),
},
},
TxHashes: []string{string(lib.GetTxHash(testtx.MustGetTxBytes(&clobtypes.MsgProposedOperations{
OperationsQueue: []clobtypes.OperationRaw{
Expand Down
1 change: 0 additions & 1 deletion protocol/x/perpetuals/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ func EndBlocker(ctx sdk.Context, k types.PerpetualsKeeper) {
// first so that new samples are processed in `MaybeProcessNewFundingTickEpoch`.
k.MaybeProcessNewFundingSampleEpoch(ctx)
k.MaybeProcessNewFundingTickEpoch(ctx)
k.SendOIUpdatesToIndexer(ctx)
}
4 changes: 0 additions & 4 deletions protocol/x/perpetuals/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func TestEndBlocker(t *testing.T) {
"MaybeProcessNewFundingSampleEpoch",
ctx,
).Return(nil)
mck.On(
"SendOIUpdatesToIndexer",
ctx,
)
},
expectedErr: nil,
},
Expand Down
45 changes: 0 additions & 45 deletions protocol/x/perpetuals/keeper/perpetual.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper

import (
"encoding/binary"
"fmt"
"math/big"
"math/rand"
Expand Down Expand Up @@ -1041,14 +1040,6 @@ func (k Keeper) ModifyOpenInterest(
perpetual.OpenInterest = dtypes.NewIntFromBigInt(bigOpenInterest)
k.setPerpetual(ctx, perpetual)

if ctx.ExecMode() == sdk.ExecModeFinalize {
updatedOIStore := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), []byte(types.UpdatedOIKeyPrefix))
openInterestInBytes, err := perpetual.OpenInterest.Marshal()
if err != nil {
return err
}
updatedOIStore.Set(lib.Uint32ToKey(perpetualId), openInterestInBytes)
}
return nil
}

Expand Down Expand Up @@ -1481,42 +1472,6 @@ func (k Keeper) IsPositionUpdatable(
return true, nil
}

func (k Keeper) SendOIUpdatesToIndexer(ctx sdk.Context) {
updatedOIStore := prefix.NewStore(ctx.TransientStore(k.transientStoreKey), []byte(types.UpdatedOIKeyPrefix))
iterator := updatedOIStore.Iterator(nil, nil)
defer iterator.Close()

OIMessageArray := make([]*indexerevents.OpenInterestUpdate, 0)

for ; iterator.Valid(); iterator.Next() {
openInterestSerializableInt := dtypes.SerializableInt{}
if err := openInterestSerializableInt.Unmarshal(iterator.Value()); err != nil {
panic(errorsmod.Wrap(err, "failed to unmarshal open interest"))
}
OIMessage := indexerevents.OpenInterestUpdate{
PerpetualId: binary.BigEndian.Uint32(iterator.Key()),
OpenInterest: openInterestSerializableInt,
}
OIMessageArray = append(OIMessageArray, &OIMessage)
}

if len(OIMessageArray) == 0 {
return
}

k.GetIndexerEventManager().AddBlockEvent(
ctx,
indexerevents.SubtypeOpenInterestUpdate,
indexer_manager.IndexerTendermintEvent_BLOCK_EVENT_END_BLOCK,
indexerevents.OpenInterestUpdateVersion,
indexer_manager.GetBytes(
&indexerevents.OpenInterestUpdateEventV1{
OpenInterestUpdates: OIMessageArray,
},
),
)
}

// GetNextPerpetualID returns the next perpetual id to be used from the module store
func (k Keeper) GetNextPerpetualID(ctx sdk.Context) uint32 {
store := ctx.KVStore(k.storeKey)
Expand Down
26 changes: 0 additions & 26 deletions protocol/x/perpetuals/keeper/perpetual_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3013,32 +3013,6 @@ func TestIsPositionUpdatable(t *testing.T) {
}
}

func TestModifyOpenInterest_store(t *testing.T) {
pc := keepertest.PerpetualsKeepers(t)
perps := keepertest.CreateLiquidityTiersAndNPerpetuals(t, pc.Ctx, pc.PerpetualsKeeper, pc.PricesKeeper, 100)
pc.Ctx = pc.Ctx.WithExecMode(sdk.ExecModeFinalize)
for _, perp := range perps {
openInterestDeltaBaseQuantums := big.NewInt(2_000_000 * (int64(perp.Params.Id)))

err := pc.PerpetualsKeeper.ModifyOpenInterest(
pc.Ctx,
perp.Params.Id,
openInterestDeltaBaseQuantums,
)
require.NoError(t, err)
}

transientStore := prefix.NewStore(pc.Ctx.TransientStore(pc.TransientStoreKey), []byte(types.UpdatedOIKeyPrefix))
for _, perp := range perps {
perpetualObject, err := pc.PerpetualsKeeper.GetPerpetual(pc.Ctx, perp.Params.Id)
require.NoError(t, err)
serializedOpenInterest := dtypes.SerializableInt{}
err = serializedOpenInterest.Unmarshal(transientStore.Get(lib.Uint32ToKey(perpetualObject.Params.Id)))
require.NoError(t, err)
require.Equal(t, perpetualObject.OpenInterest, serializedOpenInterest)
}
}

func TestIsIsolatedPerpetual(t *testing.T) {
testCases := map[string]struct {
perp types.Perpetual
Expand Down
1 change: 0 additions & 1 deletion protocol/x/perpetuals/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ type PerpetualsKeeper interface {
ctx sdk.Context,
) []Perpetual
GetAllLiquidityTiers(ctx sdk.Context) (list []LiquidityTier)
SendOIUpdatesToIndexer(ctx sdk.Context)
ValidateAndSetPerpetual(
ctx sdk.Context,
perpetual Perpetual,
Expand Down

0 comments on commit e085891

Please sign in to comment.