Skip to content

Commit

Permalink
[TRA-466] add individual parameters of vaults (dydxprotocol#1828)
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Jul 2, 2024
1 parent 4074ccf commit 178779c
Show file tree
Hide file tree
Showing 12 changed files with 461 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Params, ParamsSDKType } from "./params";
import { VaultId, VaultIdSDKType, NumShares, NumSharesSDKType, OwnerShare, OwnerShareSDKType } from "./vault";
import { VaultId, VaultIdSDKType, NumShares, NumSharesSDKType, OwnerShare, OwnerShareSDKType, VaultParams, VaultParamsSDKType } from "./vault";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** GenesisState defines `x/vault`'s genesis state. */
Expand Down Expand Up @@ -31,6 +31,9 @@ export interface Vault {
/** The shares of each owner in the vault. */

ownerShares: OwnerShare[];
/** The individual parameters of the vault. */

vaultParams?: VaultParams;
}
/** Vault defines the total shares and owner shares of a vault. */

Expand All @@ -43,6 +46,9 @@ export interface VaultSDKType {
/** The shares of each owner in the vault. */

owner_shares: OwnerShareSDKType[];
/** The individual parameters of the vault. */

vault_params?: VaultParamsSDKType;
}

function createBaseGenesisState(): GenesisState {
Expand Down Expand Up @@ -104,7 +110,8 @@ function createBaseVault(): Vault {
return {
vaultId: undefined,
totalShares: undefined,
ownerShares: []
ownerShares: [],
vaultParams: undefined
};
}

Expand All @@ -122,6 +129,10 @@ export const Vault = {
OwnerShare.encode(v!, writer.uint32(26).fork()).ldelim();
}

if (message.vaultParams !== undefined) {
VaultParams.encode(message.vaultParams, writer.uint32(34).fork()).ldelim();
}

return writer;
},

Expand All @@ -146,6 +157,10 @@ export const Vault = {
message.ownerShares.push(OwnerShare.decode(reader, reader.uint32()));
break;

case 4:
message.vaultParams = VaultParams.decode(reader, reader.uint32());
break;

default:
reader.skipType(tag & 7);
break;
Expand All @@ -160,6 +175,7 @@ export const Vault = {
message.vaultId = object.vaultId !== undefined && object.vaultId !== null ? VaultId.fromPartial(object.vaultId) : undefined;
message.totalShares = object.totalShares !== undefined && object.totalShares !== null ? NumShares.fromPartial(object.totalShares) : undefined;
message.ownerShares = object.ownerShares?.map(e => OwnerShare.fromPartial(e)) || [];
message.vaultParams = object.vaultParams !== undefined && object.vaultParams !== null ? VaultParams.fromPartial(object.vaultParams) : undefined;
return message;
}

Expand Down
58 changes: 58 additions & 0 deletions indexer/packages/v4-protos/src/codegen/dydxprotocol/vault/vault.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MarketPrice, MarketPriceSDKType } from "../prices/market_price";
import * as _m0 from "protobufjs/minimal";
import { DeepPartial } from "../../helpers";
/** VaultType represents different types of vaults. */
Expand Down Expand Up @@ -91,6 +92,18 @@ export interface OwnerShareSDKType {
owner: string;
shares?: NumSharesSDKType;
}
/** VaultParams is the individual parameters of a vault. */

export interface VaultParams {
/** Lagged price that the vault quotes at. */
laggedPrice?: MarketPrice;
}
/** VaultParams is the individual parameters of a vault. */

export interface VaultParamsSDKType {
/** Lagged price that the vault quotes at. */
lagged_price?: MarketPriceSDKType;
}

function createBaseVaultId(): VaultId {
return {
Expand Down Expand Up @@ -245,4 +258,49 @@ export const OwnerShare = {
return message;
}

};

function createBaseVaultParams(): VaultParams {
return {
laggedPrice: undefined
};
}

export const VaultParams = {
encode(message: VaultParams, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
if (message.laggedPrice !== undefined) {
MarketPrice.encode(message.laggedPrice, writer.uint32(10).fork()).ldelim();
}

return writer;
},

decode(input: _m0.Reader | Uint8Array, length?: number): VaultParams {
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseVaultParams();

while (reader.pos < end) {
const tag = reader.uint32();

switch (tag >>> 3) {
case 1:
message.laggedPrice = MarketPrice.decode(reader, reader.uint32());
break;

default:
reader.skipType(tag & 7);
break;
}
}

return message;
},

fromPartial(object: DeepPartial<VaultParams>): VaultParams {
const message = createBaseVaultParams();
message.laggedPrice = object.laggedPrice !== undefined && object.laggedPrice !== null ? MarketPrice.fromPartial(object.laggedPrice) : undefined;
return message;
}

};
2 changes: 2 additions & 0 deletions proto/dydxprotocol/vault/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ message Vault {
NumShares total_shares = 2;
// The shares of each owner in the vault.
repeated OwnerShare owner_shares = 3;
// The individual parameters of the vault.
VaultParams vault_params = 4;
}
7 changes: 7 additions & 0 deletions proto/dydxprotocol/vault/vault.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
syntax = "proto3";
package dydxprotocol.vault;

import "dydxprotocol/prices/market_price.proto";
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";

Expand Down Expand Up @@ -39,3 +40,9 @@ message OwnerShare {
string owner = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
NumShares shares = 2;
}

// VaultParams is the individual parameters of a vault.
message VaultParams {
// Lagged price that the vault quotes at.
dydxprotocol.prices.MarketPrice lagged_price = 1;
}
7 changes: 6 additions & 1 deletion protocol/x/vault/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
if err := k.SetParams(ctx, genState.Params); err != nil {
panic(err)
}
// Set total shares and owner shares of each vault.
// Set total shares, owner shares, and vault params of each vault.
for _, vault := range genState.Vaults {
if err := k.SetTotalShares(ctx, *vault.VaultId, *vault.TotalShares); err != nil {
panic(err)
Expand All @@ -24,6 +24,11 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
panic(err)
}
}
if vault.VaultParams != nil {
if err := k.SetVaultParams(ctx, *vault.VaultId, *vault.VaultParams); err != nil {
panic(err)
}
}
}
}

Expand Down
40 changes: 39 additions & 1 deletion protocol/x/vault/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"cosmossdk.io/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)
Expand All @@ -18,7 +19,7 @@ func (k Keeper) GetParams(
}

// SetParams updates `Params` in state.
// Returns an error iff validation fails.
// Returns an error if validation fails.
func (k Keeper) SetParams(
ctx sdk.Context,
params types.Params,
Expand All @@ -33,3 +34,40 @@ func (k Keeper) SetParams(

return nil
}

// GetVaultParams returns `VaultParams` in state for a given vault.
func (k Keeper) GetVaultParams(
ctx sdk.Context,
vaultId types.VaultId,
) (
vaultParams types.VaultParams,
exists bool,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.VaultParamsKeyPrefix))

b := store.Get(vaultId.ToStateKey())
if b == nil {
return vaultParams, false
}

k.cdc.MustUnmarshal(b, &vaultParams)
return vaultParams, true
}

// SetVaultParams sets `VaultParams` in state for a given vault.
// Returns an error if validation fails.
func (k Keeper) SetVaultParams(
ctx sdk.Context,
vaultId types.VaultId,
vaultParams types.VaultParams,
) error {
if err := vaultParams.Validate(); err != nil {
return err
}

b := k.cdc.MustMarshal(&vaultParams)
store := prefix.NewStore(ctx.KVStore(k.storeKey), []byte(types.VaultParamsKeyPrefix))
store.Set(vaultId.ToStateKey(), b)

return nil
}
32 changes: 32 additions & 0 deletions protocol/x/vault/keeper/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
testapp "github.com/dydxprotocol/v4-chain/protocol/testutil/app"
"github.com/dydxprotocol/v4-chain/protocol/testutil/constants"
pricestypes "github.com/dydxprotocol/v4-chain/protocol/x/prices/types"
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -46,3 +48,33 @@ func TestGetSetParams(t *testing.T) {
require.Error(t, err)
require.Equal(t, newParams, k.GetParams(ctx))
}

func TestGetSetVaultParams(t *testing.T) {
tApp := testapp.NewTestAppBuilder(t).Build()
ctx := tApp.InitChain()
k := tApp.App.VaultKeeper

// Get non-existent vault params.
_, exists := k.GetVaultParams(ctx, constants.Vault_Clob_0)
require.False(t, exists)

// Set vault params of vault clob 0.
vaultClob0Params := types.VaultParams{
LaggedPrice: &pricestypes.MarketPrice{
Id: uint32(0),
Exponent: -5,
Price: 123_456_789,
},
}
err := k.SetVaultParams(ctx, constants.Vault_Clob_0, vaultClob0Params)
require.NoError(t, err)

// Get vault params of vault clob 0.
params, exists := k.GetVaultParams(ctx, constants.Vault_Clob_0)
require.True(t, exists)
require.Equal(t, vaultClob0Params, params)

// Get vault params of vault clob 1.
_, exists = k.GetVaultParams(ctx, constants.Vault_Clob_1)
require.False(t, exists)
}
6 changes: 5 additions & 1 deletion protocol/x/vault/keeper/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ func (k Keeper) DecommissionVault(
}
}

// GetAllVaults returns all vaults with their total shares and owner shares.
// GetAllVaults returns all vaults with their total shares, owner shares, and individual params.
// Note: This function is only used for exporting module state.
func (k Keeper) GetAllVaults(ctx sdk.Context) []*types.Vault {
vaults := []*types.Vault{}
totalSharesIterator := k.getTotalSharesIterator(ctx)
Expand All @@ -116,10 +117,13 @@ func (k Keeper) GetAllVaults(ctx sdk.Context) []*types.Vault {

allOwnerShares := k.GetAllOwnerShares(ctx, *vaultId)

vaultParams, _ := k.GetVaultParams(ctx, *vaultId)

vaults = append(vaults, &types.Vault{
VaultId: vaultId,
TotalShares: &totalShares,
OwnerShares: allOwnerShares,
VaultParams: &vaultParams,
})
}
return vaults
Expand Down
Loading

0 comments on commit 178779c

Please sign in to comment.