Skip to content

Commit

Permalink
validate that vault deposit does not exceed max uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
tqin7 committed May 23, 2024
1 parent 5f1c22b commit 7fbc368
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
16 changes: 15 additions & 1 deletion protocol/x/vault/keeper/msg_server_deposit_to_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"bytes"
"math"
"math/big"
"testing"

Expand Down Expand Up @@ -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{
{
Expand Down Expand Up @@ -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),
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions protocol/x/vault/types/msg_deposit_to_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func (msg *MsgDepositToVault) ValidateBasic() error {
return err
}

// Validate that quote quantums is positive.
if msg.QuoteQuantums.Cmp(dtypes.NewInt(0)) <= 0 {
// Validate that quote quantums is positive and an uint64.
if msg.QuoteQuantums.Cmp(dtypes.NewInt(0)) <= 0 || !msg.QuoteQuantums.BigInt().IsUint64() {
return ErrInvalidDepositAmount
}

Expand Down
22 changes: 22 additions & 0 deletions protocol/x/vault/types/msg_deposit_to_vault_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types_test

import (
"math"
"math/big"
"testing"

"github.com/dydxprotocol/v4-chain/protocol/dtypes"
Expand All @@ -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: > max uint64 quote quantums": {
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,
Expand Down

0 comments on commit 7fbc368

Please sign in to comment.