Skip to content

Commit

Permalink
refactor vault deposit into megavault deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed Aug 19, 2024
1 parent ba3492b commit 8023eed
Show file tree
Hide file tree
Showing 39 changed files with 1,444 additions and 1,309 deletions.
2 changes: 1 addition & 1 deletion protocol/app/module/interface_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewInterfaceRegistry(addrPrefix string, valAddrPrefix string) (types.Interf
"dydxprotocol.sending.MsgWithdrawFromSubaccount": getLegacyMsgSignerFn(
[]string{"sender", "owner"},
),
"dydxprotocol.vault.MsgDepositToVault": getLegacyMsgSignerFn(
"dydxprotocol.vault.MsgDepositToMegavault": getLegacyMsgSignerFn(
[]string{"subaccount_id", "owner"},
),
"dydxprotocol.listing.MsgCreateMarketPermissionless": getLegacyMsgSignerFn(
Expand Down
4 changes: 2 additions & 2 deletions protocol/app/msgs/all_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ var (
"/dydxprotocol.stats.MsgUpdateParamsResponse": {},

// vault
"/dydxprotocol.vault.MsgDepositToVault": {},
"/dydxprotocol.vault.MsgDepositToVaultResponse": {},
"/dydxprotocol.vault.MsgDepositToMegavault": {},
"/dydxprotocol.vault.MsgDepositToMegavaultResponse": {},
"/dydxprotocol.vault.MsgSetVaultParams": {},
"/dydxprotocol.vault.MsgSetVaultParamsResponse": {},
"/dydxprotocol.vault.MsgUpdateDefaultQuotingParams": {},
Expand Down
4 changes: 2 additions & 2 deletions protocol/app/msgs/normal_msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ var (
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse": nil,

// vault
"/dydxprotocol.vault.MsgDepositToVault": &vault.MsgDepositToVault{},
"/dydxprotocol.vault.MsgDepositToVaultResponse": nil,
"/dydxprotocol.vault.MsgDepositToMegavault": &vault.MsgDepositToMegavault{},
"/dydxprotocol.vault.MsgDepositToMegavaultResponse": nil,
}

NormalMsgsSlinky = map[string]sdk.Msg{
Expand Down
4 changes: 2 additions & 2 deletions protocol/app/msgs/normal_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func TestNormalMsgs_Key(t *testing.T) {
"/dydxprotocol.sending.MsgWithdrawFromSubaccountResponse",

// vault
"/dydxprotocol.vault.MsgDepositToVault",
"/dydxprotocol.vault.MsgDepositToVaultResponse",
"/dydxprotocol.vault.MsgDepositToMegavault",
"/dydxprotocol.vault.MsgDepositToMegavaultResponse",

// ibc application module: ICA
"/ibc.applications.interchain_accounts.v1.InterchainAccount",
Expand Down
4 changes: 4 additions & 0 deletions protocol/app/testdata/default_genesis_state.json
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,10 @@
"order_expiration_seconds": 60,
"activation_threshold_quote_quantums": "1000000000"
},
"owner_shares": [],
"total_shares": {
"num_shares": "0"
},
"vaults": []
},
"vest": {
Expand Down
2 changes: 1 addition & 1 deletion protocol/app/upgrades/v5.2.0/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func addAllVaultsToVaultAddressStore(
) {
allVaults := vaultKeeper.GetAllVaults(ctx)
for _, vault := range allVaults {
vaultKeeper.AddVaultToAddressStore(ctx, *vault.VaultId)
vaultKeeper.AddVaultToAddressStore(ctx, vault.VaultId)
}
}

Expand Down
36 changes: 18 additions & 18 deletions protocol/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ services:
ports:
- "8080:8080"
- "8002:8002" # metrics
prometheus:
image: prom/prometheus:latest
volumes:
- ./contrib/prometheus:/etc/prometheus/
ports:
- "9091:9090"
datadog-agent:
image: public.ecr.aws/datadog/agent:7
environment:
# See https://docs.datadoghq.com/containers/docker/?tab=standard#environment-variables for agent configuration
- DD_API_KEY=${DD_API_KEY}
- DD_APM_ENABLED=true
- DD_APM_NON_LOCAL_TRAFFIC=true
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
ports:
- "8125:8215"
- "8126:8126"
# prometheus:
# image: prom/prometheus:latest
# volumes:
# - ./contrib/prometheus:/etc/prometheus/
# ports:
# - "9091:9090"
# datadog-agent:
# image: public.ecr.aws/datadog/agent:7
# environment:
# # See https://docs.datadoghq.com/containers/docker/?tab=standard#environment-variables for agent configuration
# - DD_API_KEY=${DD_API_KEY}
# - DD_APM_ENABLED=true
# - DD_APM_NON_LOCAL_TRAFFIC=true
# volumes:
# - /var/run/docker.sock:/var/run/docker.sock:ro
# ports:
# - "8125:8215"
# - "8126:8126"
18 changes: 9 additions & 9 deletions protocol/testing/genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2227,25 +2227,25 @@ function edit_genesis() {
update_ica_controller_params

# Vaults
# Set vault params.
# Set default quoting params.
dasel put -t int -f "$GENESIS" ".app_state.vault.default_quoting_params.spread_min_ppm" -v '3000'
# Set total shares and owner shares of each vault.
vault_idx=0
# Set total shares and owner shares.
if [ -z "${INPUT_TEST_ACCOUNTS[0]}" ]; then
vault_owner_address='dydx199tqg4wdlnu4qjlxchpd7seg454937hjrknju4' # alice as default vault owner
else
vault_owner_address="${INPUT_TEST_ACCOUNTS[0]}"
fi
total_deposit=$((DEFAULT_SUBACCOUNT_QUOTE_BALANCE_VAULT * ${#INPUT_VAULT_NUMBERS[@]}))
dasel put -t string -f "$GENESIS" ".app_state.vault.total_shares.num_shares" -v "${total_deposit}"
dasel put -t json -f "$GENESIS" ".app_state.vault.owner_shares.[]" -v '{}'
dasel put -t string -f "$GENESIS" ".app_state.vault.owner_shares.[0].owner" -v "${vault_owner_address}"
dasel put -t string -f "$GENESIS" ".app_state.vault.owner_shares.[0].shares.num_shares" -v "${total_deposit}"
# Set vaults.
vault_idx=0
for number in "${INPUT_VAULT_NUMBERS[@]}"; do
dasel put -t json -f "$GENESIS" '.app_state.vault.vaults.[]' -v '{}'
dasel put -t string -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].vault_id.type" -v 'VAULT_TYPE_CLOB'
dasel put -t int -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].vault_id.number" -v "${number}"
dasel put -t string -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].total_shares.num_shares" -v "${DEFAULT_SUBACCOUNT_QUOTE_BALANCE_VAULT}"

dasel put -t json -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].owner_shares.[]" -v '{}'
dasel put -t string -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].owner_shares.[0].owner" -v "${vault_owner_address}"
dasel put -t string -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].owner_shares.[0].shares.num_shares" -v "${DEFAULT_SUBACCOUNT_QUOTE_BALANCE_VAULT}"

dasel put -t string -f "$GENESIS" ".app_state.vault.vaults.[${vault_idx}].vault_params.status" -v 'VAULT_STATUS_QUOTING'
vault_idx=$(($vault_idx + 1))
done
Expand Down
4 changes: 4 additions & 0 deletions protocol/testutil/constants/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4543,6 +4543,10 @@ const GenesisState = `{
"order_expiration_seconds": 60,
"activation_threshold_quote_quantums": "1000000000"
},
"owner_shares": [],
"total_shares": {
"num_shares": "0"
},
"vaults": []
},
"vest": {
Expand Down
3 changes: 1 addition & 2 deletions protocol/testutil/constants/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ var (
Number: 7,
}

MsgDepositToVault_Clob0_Alice0_100 = &types.MsgDepositToVault{
VaultId: &Vault_Clob0,
MsgDepositToMegavault_Alice0_100 = &types.MsgDepositToMegavault{
SubaccountId: &Alice_Num0,
QuoteQuantums: dtypes.NewInt(100),
}
Expand Down
2 changes: 1 addition & 1 deletion protocol/testutil/encoding/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func GetTestEncodingCfg() testutil.TestEncodingConfig {
&sendingtypes.MsgWithdrawFromSubaccount{},

// Vault.
&vaulttypes.MsgDepositToVault{},
&vaulttypes.MsgDepositToMegavault{},
}

for _, msg := range msgInterfacesToRegister {
Expand Down
20 changes: 2 additions & 18 deletions protocol/x/vault/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ func CmdQueryListVault() *cobra.Command {

func CmdQueryListOwnerShares() *cobra.Command {
cmd := &cobra.Command{
Use: "list-owner-shares [type] [number]",
Short: "list owner shares of a vault by its type and number",
Long: "list owner shares of a vault by its type and number. Current support types are: clob.",
Args: cobra.ExactArgs(2),
Use: "list-owner-shares",
Short: "list owner shares",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)

Expand All @@ -144,21 +142,7 @@ func CmdQueryListOwnerShares() *cobra.Command {

queryClient := types.NewQueryClient(clientCtx)

// Parse vault type.
vaultType, err := GetVaultTypeFromString(args[0])
if err != nil {
return err
}

// Parse vault number.
vaultNumber, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return err
}

request := &types.QueryOwnerSharesRequest{
Type: vaultType,
Number: uint32(vaultNumber),
Pagination: pageReq,
}

Expand Down
36 changes: 10 additions & 26 deletions protocol/x/vault/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,25 @@ func GetTxCmd() *cobra.Command {
RunE: client.ValidateCmd,
}

cmd.AddCommand(CmdDepositToVault())
cmd.AddCommand(CmdDepositToMegavault())

return cmd
}

func CmdDepositToVault() *cobra.Command {
func CmdDepositToMegavault() *cobra.Command {
cmd := &cobra.Command{
Use: "deposit-to-vault [vault_type] [vault_number] [depositor_owner] [depositor_number] [quantums]",
Short: "Broadcast message DepositToVault",
Args: cobra.ExactArgs(5),
Use: "deposit-to-megavault [depositor_owner] [depositor_number] [quantums]",
Short: "Broadcast message DepositToMegavault",
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) (err error) {
// Parse vault type.
vaultType, err := GetVaultTypeFromString(args[0])
if err != nil {
return err
}

// Parse vault number.
vaultNumber, err := cast.ToUint32E(args[1])
if err != nil {
return err
}

// Parse depositor number.
depositorNumber, err := cast.ToUint32E(args[3])
depositorNumber, err := cast.ToUint32E(args[1])
if err != nil {
return err
}

// Parse quantums.
quantums, err := cast.ToUint64E(args[4])
quantums, err := cast.ToUint64E(args[2])
if err != nil {
return err
}
Expand All @@ -64,14 +52,10 @@ func CmdDepositToVault() *cobra.Command {
return err
}

// Create MsgDepositToVault.
msg := &types.MsgDepositToVault{
VaultId: &types.VaultId{
Type: vaultType,
Number: vaultNumber,
},
// Create MsgDepositToMegavault.
msg := &types.MsgDepositToMegavault{
SubaccountId: &satypes.SubaccountId{
Owner: args[2],
Owner: args[0],
Number: depositorNumber,
},
QuoteQuantums: dtypes.NewIntFromUint64(quantums),
Expand Down
36 changes: 20 additions & 16 deletions protocol/x/vault/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,37 @@ func InitGenesis(ctx sdk.Context, k keeper.Keeper, genState types.GenesisState)
if err := k.SetDefaultQuotingParams(ctx, genState.DefaultQuotingParams); err != nil {
panic(err)
}
// For each vault:
// 1. Set total shares
// 2. Set owner shares
// 3. Set vault params
// 4. Set most recent client ids
// 5. Add to address store
for _, vault := range genState.Vaults {
if err := k.SetTotalShares(ctx, *vault.VaultId, *vault.TotalShares); err != nil {
// Set total shares and owner shares.
if err := k.SetTotalShares(ctx, genState.TotalShares); err != nil {
panic(err)
}
for _, ownerShares := range genState.OwnerShares {
if err := k.SetOwnerShares(ctx, ownerShares.Owner, ownerShares.Shares); err != nil {
panic(err)
}
for _, ownerShares := range vault.OwnerShares {
if err := k.SetOwnerShares(ctx, *vault.VaultId, ownerShares.Owner, *ownerShares.Shares); err != nil {
panic(err)
}
}
if err := k.SetVaultParams(ctx, *vault.VaultId, vault.VaultParams); err != nil {
}

// For each vault:
// 1. Set vault params
// 2. Set most recent client ids
// 3. Add to address store
for _, vault := range genState.Vaults {
if err := k.SetVaultParams(ctx, vault.VaultId, vault.VaultParams); err != nil {
panic(err)
}
k.SetMostRecentClientIds(ctx, *vault.VaultId, vault.MostRecentClientIds)
k.AddVaultToAddressStore(ctx, *vault.VaultId)
k.SetMostRecentClientIds(ctx, vault.VaultId, vault.MostRecentClientIds)
k.AddVaultToAddressStore(ctx, vault.VaultId)
}
}

// ExportGenesis returns the module's exported genesis.
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
genesis := types.DefaultGenesis()

// Export total shares and owner shares.
genesis.TotalShares = k.GetTotalShares(ctx)
genesis.OwnerShares = k.GetAllOwnerShares(ctx)

// Export params.
genesis.DefaultQuotingParams = k.GetDefaultQuotingParams(ctx)

Expand Down
27 changes: 10 additions & 17 deletions protocol/x/vault/keeper/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ import (
"github.com/dydxprotocol/v4-chain/protocol/x/vault/types"
)

// MintShares mints shares of a vault for `owner` based on `quantumsToDeposit` by:
// 1. Increasing total shares of the vault.
// 2. Increasing owner shares of the vault for given `owner`.
// MintShares mints shares for `owner` based on `quantumsToDeposit` by:
// 1. Increasing total shares.
// 2. Increasing owner shares for given `owner`.
func (k Keeper) MintShares(
ctx sdk.Context,
vaultId types.VaultId,
owner string,
quantumsToDeposit *big.Int,
) error {
Expand All @@ -21,18 +20,15 @@ func (k Keeper) MintShares(
return types.ErrInvalidDepositAmount
}
// Get existing TotalShares of the vault.
totalShares, exists := k.GetTotalShares(ctx, vaultId)
existingTotalShares := totalShares.NumShares.BigInt()
existingTotalShares := k.GetTotalShares(ctx).NumShares.BigInt()
// Calculate shares to mint.
var sharesToMint *big.Int
if !exists || existingTotalShares.Sign() <= 0 {
if existingTotalShares.Sign() <= 0 {
// Mint `quoteQuantums` number of shares.
sharesToMint = new(big.Int).Set(quantumsToDeposit)
// Initialize existingTotalShares as 0.
existingTotalShares = big.NewInt(0)
} else {
// Get vault equity.
equity, err := k.GetVaultEquity(ctx, vaultId)
// Get megavault equity.
equity, err := k.GetMegavaultEquity(ctx)
if err != nil {
return err
}
Expand All @@ -56,10 +52,9 @@ func (k Keeper) MintShares(
}
}

// Increase TotalShares of the vault.
// Increase total shares.
err := k.SetTotalShares(
ctx,
vaultId,
types.BigIntToNumShares(
existingTotalShares.Add(existingTotalShares, sharesToMint),
),
Expand All @@ -68,13 +63,12 @@ func (k Keeper) MintShares(
return err
}

// Increase owner shares in the vault.
ownerShares, exists := k.GetOwnerShares(ctx, vaultId, owner)
// Increase owner shares.
ownerShares, exists := k.GetOwnerShares(ctx, owner)
if !exists {
// Set owner shares to be sharesToMint.
err := k.SetOwnerShares(
ctx,
vaultId,
owner,
types.BigIntToNumShares(sharesToMint),
)
Expand All @@ -86,7 +80,6 @@ func (k Keeper) MintShares(
existingOwnerShares := ownerShares.NumShares.BigInt()
err = k.SetOwnerShares(
ctx,
vaultId,
owner,
types.BigIntToNumShares(
existingOwnerShares.Add(existingOwnerShares, sharesToMint),
Expand Down
Loading

0 comments on commit 8023eed

Please sign in to comment.