-
Notifications
You must be signed in to change notification settings - Fork 114
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-116] add x/vault total shares store #1179
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package constants | ||
|
||
import ( | ||
vaulttypes "github.com/dydxprotocol/v4-chain/protocol/x/vault/types" | ||
) | ||
|
||
var ( | ||
Vault_Clob_0 = vaulttypes.VaultId{ | ||
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB, | ||
Number: 0, | ||
} | ||
Vault_Clob_1 = vaulttypes.VaultId{ | ||
Type: vaulttypes.VaultType_VAULT_TYPE_CLOB, | ||
Number: 1, | ||
} | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package keeper | ||
|
||
import ( | ||
"cosmossdk.io/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/dydxprotocol/v4-chain/protocol/dtypes" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types" | ||
) | ||
|
||
// GetTotalShares gets TotalShares for a vault. | ||
func (k Keeper) GetTotalShares( | ||
ctx sdk.Context, | ||
vaultId types.VaultId, | ||
) (val types.NumShares, exists bool) { | ||
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.TotalSharesKeyPrefix)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is finding the existing keyvalue store that is associated with this keeper? Why do we use this instead of just storing this KV store directly in the keeper in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We usually create a separate store (within |
||
|
||
b := store.Get(vaultId.ToStateKey()) | ||
if b == nil { | ||
return val, false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's correct. See here for more details. |
||
} | ||
|
||
k.cdc.MustUnmarshal(b, &val) | ||
return val, true | ||
} | ||
|
||
// SetTotalShares sets TotalShares for a vault. Returns error if `totalShares` is negative. | ||
func (k Keeper) SetTotalShares( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if instead of having nit: Should we panic on There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah we can build these increment decrement on top of As for verifying whether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. chatted with Vincent offline about this check on negative. will change this to return error if negative |
||
ctx sdk.Context, | ||
vaultId types.VaultId, | ||
totalShares types.NumShares, | ||
) error { | ||
if totalShares.NumShares.Cmp(dtypes.NewInt(0)) == -1 { | ||
return types.ErrNegativeShares | ||
} | ||
|
||
b := k.cdc.MustMarshal(&totalShares) | ||
totalSharesStore := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.TotalSharesKeyPrefix)) | ||
totalSharesStore.Set(vaultId.ToStateKey(), b) | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/dtypes" | ||
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app" | ||
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants" | ||
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetSetTotalShares(t *testing.T) { | ||
tApp := testapp.NewTestAppBuilder(t).Build() | ||
ctx := tApp.InitChain() | ||
k := tApp.App.VaultKeeper | ||
|
||
// Get total shares for a non-existing vault. | ||
_, exists := k.GetTotalShares(ctx, constants.Vault_Clob_0) | ||
require.Equal(t, false, exists) | ||
|
||
// Set total shares for a vault and then get. | ||
err := k.SetTotalShares(ctx, constants.Vault_Clob_0, types.NumShares{ | ||
NumShares: dtypes.NewInt(7), | ||
}) | ||
require.NoError(t, err) | ||
numShares, exists := k.GetTotalShares(ctx, constants.Vault_Clob_0) | ||
require.Equal(t, true, exists) | ||
require.Equal(t, dtypes.NewInt(7), numShares.NumShares) | ||
|
||
// Set total shares for another vault and then get. | ||
err = k.SetTotalShares(ctx, constants.Vault_Clob_1, types.NumShares{ | ||
NumShares: dtypes.NewInt(456), | ||
}) | ||
require.NoError(t, err) | ||
numShares, exists = k.GetTotalShares(ctx, constants.Vault_Clob_1) | ||
require.Equal(t, true, exists) | ||
require.Equal(t, dtypes.NewInt(456), numShares.NumShares) | ||
|
||
// Set total shares for second vault to 0. | ||
err = k.SetTotalShares(ctx, constants.Vault_Clob_1, types.NumShares{ | ||
NumShares: dtypes.NewInt(0), | ||
}) | ||
require.NoError(t, err) | ||
numShares, exists = k.GetTotalShares(ctx, constants.Vault_Clob_1) | ||
require.Equal(t, true, exists) | ||
require.Equal(t, dtypes.NewInt(0), numShares.NumShares) | ||
|
||
// Set total shares for the first vault again and then get. | ||
err = k.SetTotalShares(ctx, constants.Vault_Clob_0, types.NumShares{ | ||
NumShares: dtypes.NewInt(123), | ||
}) | ||
require.NoError(t, err) | ||
numShares, exists = k.GetTotalShares(ctx, constants.Vault_Clob_0) | ||
require.Equal(t, true, exists) | ||
require.Equal(t, dtypes.NewInt(123), numShares.NumShares) | ||
|
||
// Set total shares for the first vault to a negative value. | ||
// Should get error and total shares should remain unchanged. | ||
err = k.SetTotalShares(ctx, constants.Vault_Clob_0, types.NumShares{ | ||
NumShares: dtypes.NewInt(-1), | ||
}) | ||
require.Equal(t, types.ErrNegativeShares, err) | ||
numShares, exists = k.GetTotalShares(ctx, constants.Vault_Clob_0) | ||
require.Equal(t, true, exists) | ||
require.Equal(t, dtypes.NewInt(123), numShares.NumShares) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package types | ||
|
||
// DONTCOVER | ||
|
||
import errorsmod "cosmossdk.io/errors" | ||
|
||
var ( | ||
ErrNegativeShares = errorsmod.Register( | ||
ModuleName, | ||
1, | ||
"Shares are negative", | ||
) | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
package types | ||
|
||
// Module name and store keys | ||
// Module name and store keys. | ||
const ( | ||
// ModuleName defines the module name | ||
// ModuleName defines the module name. | ||
ModuleName = "vault" | ||
|
||
// StoreKey defines the primary module store key | ||
// StoreKey defines the primary module store key. | ||
StoreKey = ModuleName | ||
) | ||
|
||
// State. | ||
const ( | ||
// TotalSharesKeyPrefix is the prefix to retrieve all TotalShares. | ||
TotalSharesKeyPrefix = "TotalShares:" | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestModuleKeys(t *testing.T) { | ||
require.Equal(t, "vault", types.ModuleName) | ||
require.Equal(t, "vault", types.StoreKey) | ||
} | ||
|
||
func TestStateKeys(t *testing.T) { | ||
require.Equal(t, "TotalShares:", types.TotalSharesKeyPrefix) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package types | ||
|
||
func (id *VaultId) ToStateKey() []byte { | ||
b, err := id.Marshal() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
tqin7 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package types_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestToStateKey(t *testing.T) { | ||
b, _ := constants.Vault_Clob_0.Marshal() | ||
require.Equal(t, b, constants.Vault_Clob_0.ToStateKey()) | ||
|
||
b, _ = constants.Vault_Clob_1.Marshal() | ||
require.Equal(t, b, constants.Vault_Clob_1.ToStateKey()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is this field named
Number
instead of likeId
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
Id
is a bit confusing asVaultId
already hasId
in the name. For example, see subaccountId