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

[TRA-572] Add upsert vault event. #2250

Merged
merged 7 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions proto/dydxprotocol/indexer/events/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "dydxprotocol/indexer/shared/removal_reason.proto";
import "dydxprotocol/indexer/protocol/v1/clob.proto";
import "dydxprotocol/indexer/protocol/v1/perpetual.proto";
import "dydxprotocol/indexer/protocol/v1/subaccount.proto";
import "dydxprotocol/indexer/protocol/v1/vault.proto";

option go_package = "github.com/dydxprotocol/v4-chain/protocol/indexer/events";

Expand Down Expand Up @@ -590,3 +591,15 @@ message RegisterAffiliateEventV1 {
// Address of the affiliate associated with the referee.
string affiliate = 2 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
}

// Event emitted when a vault is created / updated.
message UpsertVaultEventV1 {
// Address of the vault.
string address = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];

// Clob pair Id associated with the vault.
uint32 clob_pair_id = 2;

// Status of the vault.
dydxprotocol.indexer.protocol.v1.VaultStatus status = 3;
}
27 changes: 27 additions & 0 deletions proto/dydxprotocol/indexer/protocol/v1/vault.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
syntax = "proto3";
package dydxprotocol.indexer.protocol.v1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the package directory to match the package name.

The static analysis tool buf has flagged an issue with the package directory not matching the package name. To fix this, move the file to the directory dydxprotocol/indexer/protocol/v1 relative to the root of the repository.

Apply this fix to resolve the buf issue:

  1. Create the directory dydxprotocol/indexer/protocol/v1 relative to the root of the repository if it doesn't exist.
  2. Move the file proto/dydxprotocol/indexer/protocol/v1/vault.proto to the new directory dydxprotocol/indexer/protocol/v1/vault.proto.
  3. Update any import statements in other files that reference this file to use the new path.
Tools
buf

2-2: Files with package "dydxprotocol.indexer.protocol.v1" must be within a directory "dydxprotocol/indexer/protocol/v1" relative to root but were in directory "proto/dydxprotocol/indexer/protocol/v1".

(PACKAGE_DIRECTORY_MATCH)


option go_package = "github.com/dydxprotocol/v4-chain/protocol/indexer/protocol/v1/types";

// Initial copy of protos from dYdX chain application state protos for the
// vault module for use to send Indexer specific messages. Do not make any
// breaking changes to these protos, a new version should be created if a
// breaking change is needed.

// VaultStatus represents the status of a vault.
enum VaultStatus {
// Default value, invalid and unused.
VAULT_STATUS_UNSPECIFIED = 0;

// Don’t place orders. Does not count toward global vault balances.
VAULT_STATUS_DEACTIVATED = 1;

// Don’t place orders. Does count towards global vault balances.
VAULT_STATUS_STAND_BY = 2;

// Places orders on both sides of the book.
VAULT_STATUS_QUOTING = 3;

// Only place orders that close the position.
VAULT_STATUS_CLOSE_ONLY = 4;
}
543 changes: 395 additions & 148 deletions protocol/indexer/events/events.pb.go

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions protocol/indexer/events/upsert_vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package events

import (
v1 "github.com/dydxprotocol/v4-chain/protocol/indexer/protocol/v1"
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

// NewUpsertVaultEvent creates a UpsertVaultEventV1
// representing an create / update of a vault.
func NewUpsertVaultEvent(
vaultAddress string,
clobPairId clobtypes.ClobPairId,
status types.VaultStatus,
) *UpsertVaultEventV1 {
return &UpsertVaultEventV1{
Address: vaultAddress,
ClobPairId: uint32(clobPairId),
Status: v1.VaultStatusToIndexerVaultStatus(status),
}
}
26 changes: 26 additions & 0 deletions protocol/indexer/events/upsert_vault_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package events_test

import (
"testing"

"github.com/dydxprotocol/v4-chain/protocol/indexer/events"
v1types "github.com/dydxprotocol/v4-chain/protocol/indexer/protocol/v1/types"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"

"github.com/stretchr/testify/require"
)

func TestNewUpsertVaultEvent_Success(t *testing.T) {
upsertVaultEvent := events.NewUpsertVaultEvent(
constants.Alice_Num0.Owner,
0,
vaulttypes.VaultStatus_VAULT_STATUS_QUOTING,
)
expectedUpsertVaultEventProto := &events.UpsertVaultEventV1{
Address: constants.Alice_Num0.Owner,
ClobPairId: 0,
Status: v1types.VaultStatus_VAULT_STATUS_QUOTING,
}
require.Equal(t, expectedUpsertVaultEventProto, upsertVaultEvent)
}
89 changes: 89 additions & 0 deletions protocol/indexer/protocol/v1/types/vault.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions protocol/indexer/protocol/v1/v1_mappers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

func SubaccountIdToIndexerSubaccountId(
Expand Down Expand Up @@ -205,3 +206,7 @@ func ConvertToPerpetualMarketType(marketType perptypes.PerpetualMarketType) v1ty
)
}
}

func VaultStatusToIndexerVaultStatus(vaultStatus vaulttypes.VaultStatus) v1types.VaultStatus {
return v1types.VaultStatus(vaultStatus)
}
31 changes: 31 additions & 0 deletions protocol/indexer/protocol/v1/v1_mappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
clobtypes "github.com/dydxprotocol/v4-chain/protocol/x/clob/types"
perptypes "github.com/dydxprotocol/v4-chain/protocol/x/perpetuals/types"
satypes "github.com/dydxprotocol/v4-chain/protocol/x/subaccounts/types"
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -473,3 +474,33 @@ func TestConvertToPerpetualMarketType(t *testing.T) {
})
}
}

func TestVaultStatusToVaultStatus(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit

Suggested change
func TestVaultStatusToVaultStatus(t *testing.T) {
func TestVaultStatusToIndexerVaultStatus(t *testing.T) {

tests := map[string]struct {
// Input
vaultStatus vaulttypes.VaultStatus

// Expectations
expectedVaultStatus v1types.VaultStatus
}{}
// Iterate through all the values for VaultStatus to create test cases.
for name, value := range vaulttypes.VaultStatus_value {
testName := fmt.Sprintf("Converts VaultStatus %s to IndexerVaultStatus", name)
tests[testName] = struct {
vaultStatus vaulttypes.VaultStatus
expectedVaultStatus v1types.VaultStatus
}{
vaultStatus: vaulttypes.VaultStatus(value),
expectedVaultStatus: v1types.VaultStatus(v1types.VaultStatus_value[name]),
}
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
require.Equal(
t,
tc.expectedVaultStatus,
v1.VaultStatusToIndexerVaultStatus(tc.vaultStatus),
)
})
}
}
Loading