diff --git a/protocol/x/vault/keeper/msg_server_deposit_to_vault.go b/protocol/x/vault/keeper/msg_server_deposit_to_vault.go index 6be3d80de8..aa91177d13 100644 --- a/protocol/x/vault/keeper/msg_server_deposit_to_vault.go +++ b/protocol/x/vault/keeper/msg_server_deposit_to_vault.go @@ -16,13 +16,14 @@ func (k msgServer) DepositToVault( msg *types.MsgDepositToVault, ) (*types.MsgDepositToVaultResponse, error) { ctx := lib.UnwrapSDKContext(goCtx, types.ModuleName) + quoteQuantums := msg.QuoteQuantums.BigInt() // Mint shares for the vault. err := k.MintShares( ctx, *msg.VaultId, msg.SubaccountId.Owner, - msg.QuoteQuantums.BigInt(), + quoteQuantums, ) if err != nil { return nil, err @@ -33,10 +34,19 @@ func (k msgServer) DepositToVault( // shares calculation to be correct. err = k.subaccountsKeeper.TransferFundsFromSubaccountToSubaccount( ctx, +<<<<<<< HEAD *msg.SubaccountId, *msg.VaultId.ToSubaccountId(), assettypes.AssetUsdc.Id, msg.QuoteQuantums.BigInt(), +======= + &sendingtypes.Transfer{ + Sender: *msg.SubaccountId, + Recipient: *msg.VaultId.ToSubaccountId(), + AssetId: assettypes.AssetUsdc.Id, + Amount: quoteQuantums.Uint64(), + }, +>>>>>>> 76c548c2 (validate that vault deposit does not exceed max uint64 (#1576)) ) if err != nil { return nil, err diff --git a/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go b/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go index b803769a86..af5c72f70f 100644 --- a/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go +++ b/protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go @@ -2,6 +2,7 @@ package keeper_test import ( "bytes" + "math" "math/big" "testing" @@ -198,7 +199,7 @@ func TestMsgDepositToVault(t *testing.T) { big.NewInt(1_000), }, }, - "Two failed deposits due to non-positive amounts": { + "Three failed deposits due to invalid deposit amount": { vaultId: constants.Vault_Clob_1, depositorSetups: []DepositorSetup{ { @@ -227,14 +228,27 @@ func TestMsgDepositToVault(t *testing.T) { checkTxResponseContains: "Deposit amount is invalid", expectedOwnerShares: nil, }, + { + depositor: constants.Bob_Num0, + depositAmount: new(big.Int).Add( + new(big.Int).SetUint64(math.MaxUint64), + big.NewInt(1), + ), + msgSigner: constants.Bob_Num0.Owner, + checkTxFails: true, + checkTxResponseContains: "Deposit amount is invalid", + expectedOwnerShares: nil, + }, }, totalSharesHistory: []*big.Int{ big.NewInt(0), big.NewInt(0), + big.NewInt(0), }, vaultEquityHistory: []*big.Int{ big.NewInt(0), big.NewInt(0), + big.NewInt(0), }, }, } diff --git a/protocol/x/vault/types/msg_deposit_to_vault.go b/protocol/x/vault/types/msg_deposit_to_vault.go index eb3d091402..12bb006b3d 100644 --- a/protocol/x/vault/types/msg_deposit_to_vault.go +++ b/protocol/x/vault/types/msg_deposit_to_vault.go @@ -1,8 +1,8 @@ package types import ( + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/dydxprotocol/v4-chain/protocol/dtypes" ) var _ sdk.Msg = &MsgDepositToVault{} @@ -14,9 +14,10 @@ func (msg *MsgDepositToVault) ValidateBasic() error { return err } - // Validate that quote quantums is positive. - if msg.QuoteQuantums.Cmp(dtypes.NewInt(0)) <= 0 { - return ErrInvalidDepositAmount + // Validate that quote quantums is positive and an uint64. + quoteQuantums := msg.QuoteQuantums.BigInt() + if quoteQuantums.Sign() <= 0 || !quoteQuantums.IsUint64() { + return errorsmod.Wrap(ErrInvalidDepositAmount, "quote quantums must be strictly positive and less than 2^64") } return nil diff --git a/protocol/x/vault/types/msg_deposit_to_vault_test.go b/protocol/x/vault/types/msg_deposit_to_vault_test.go index ab9a25c648..a369a69af7 100644 --- a/protocol/x/vault/types/msg_deposit_to_vault_test.go +++ b/protocol/x/vault/types/msg_deposit_to_vault_test.go @@ -1,6 +1,8 @@ package types_test import ( + "math" + "math/big" "testing" "github.com/dydxprotocol/v4-chain/protocol/dtypes" @@ -22,6 +24,26 @@ func TestMsgDepositToVault_ValidateBasic(t *testing.T) { QuoteQuantums: dtypes.NewInt(1), }, }, + "Success: max uint64 quote quantums": { + msg: types.MsgDepositToVault{ + VaultId: &constants.Vault_Clob_0, + SubaccountId: &constants.Alice_Num0, + QuoteQuantums: dtypes.NewIntFromUint64(math.MaxUint64), + }, + }, + "Failure: quote quantums greater than max uint64": { + msg: types.MsgDepositToVault{ + VaultId: &constants.Vault_Clob_0, + SubaccountId: &constants.Alice_Num0, + QuoteQuantums: dtypes.NewIntFromBigInt( + new(big.Int).Add( + new(big.Int).SetUint64(math.MaxUint64), + new(big.Int).SetUint64(1), + ), + ), + }, + expectedErr: "Deposit amount is invalid", + }, "Failure: zero quote quantums": { msg: types.MsgDepositToVault{ VaultId: &constants.Vault_Clob_0,