Skip to content

Commit

Permalink
feat: vault account
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 committed Oct 21, 2024
1 parent 260cc2e commit 6470285
Show file tree
Hide file tree
Showing 82 changed files with 11,270 additions and 5,811 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- [11726](https://github.com/vegaprotocol/vega/issues/11726) - Combined `AMM` uncrossing orders for better performance when uncrossing the book.
- [11711](https://github.com/vegaprotocol/vega/issues/11711) - Manage closed team membership by updating the allow list.
- [11722](https://github.com/vegaprotocol/vega/issues/11722) - Expose active protocol automated purchase identifier in market data API.
- [11745](https://github.com/vegaprotocol/vega/issues/11745) - Implement vault accounts

### 🐛 Fixes

Expand Down
4 changes: 4 additions & 0 deletions commands/amend_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func checkAmendAMM(cmd *commandspb.AmendAMM) Errors {
errs.AddForProperty("amend_amm.slippage_tolerance", ErrMustBeBetween01)
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("amend_amm.vault_id", ErrInvalidVaultID)
}

var hasUpdate bool

if cmd.CommitmentAmount != nil {
Expand Down
9 changes: 9 additions & 0 deletions commands/amend_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

func TestCheckAmendAMM(t *testing.T) {
banana := "banana"
cases := []struct {
submission commandspb.AmendAMM
errStr string
Expand Down Expand Up @@ -270,6 +271,14 @@ func TestCheckAmendAMM(t *testing.T) {
},
errStr: "* (no updates provided)",
},
{
submission: commandspb.AmendAMM{
MarketId: "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
SlippageTolerance: "0.09",
VaultId: &banana,
},
errStr: "amend_amm.vault_id (is not a valid vault identifier)",
},
{
submission: commandspb.AmendAMM{
MarketId: "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
Expand Down
4 changes: 4 additions & 0 deletions commands/cancel_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func checkCancelAMM(cmd *commandspb.CancelAMM) Errors {
errs.AddForProperty("cancel_amm.market_id", ErrShouldBeAValidVegaID)
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("cancel_amm.vault_id", ErrInvalidVaultID)
}

if cmd.Method == commandspb.CancelAMM_METHOD_UNSPECIFIED {
errs.AddForProperty("cancel_amm.method", ErrIsRequired)
}
Expand Down
9 changes: 9 additions & 0 deletions commands/cancel_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

func TestCheckCancelAMM(t *testing.T) {
banana := "banana"
cases := []struct {
submission commandspb.CancelAMM
errStr string
Expand Down Expand Up @@ -61,6 +62,14 @@ func TestCheckCancelAMM(t *testing.T) {
},
errStr: "cancel_amm.method (is not a valid value)",
},
{
submission: commandspb.CancelAMM{
MarketId: "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
Method: commandspb.CancelAMM_Method(999),
VaultId: &banana,
},
errStr: "cancel_amm.vault_id (is not a valid vault identifier)",
},
}

for n, c := range cases {
Expand Down
1 change: 1 addition & 0 deletions commands/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ var (
ErrMustBeAtMost250 = errors.New("must be at most 250")
ErrNoUpdatesProvided = errors.New("no updates provided")
ErrMaxPriceMustRespectTickSize = errors.New("must respect tick size")
ErrInvalidVaultID = errors.New("is not a valid vault identifier")
)

type Errors map[string][]error
Expand Down
4 changes: 4 additions & 0 deletions commands/liquidity_provision_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ func checkLiquidityProvisionSubmission(cmd *commandspb.LiquidityProvisionSubmiss
errs.AddForProperty("liquidity_provision_submission.reference", ErrReferenceTooLong)
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("liquidity_provision_submission.vault_id", ErrInvalidVaultID)
}

// if the commitment amount is 0, then the command should be interpreted as
// a cancellation of the liquidity provision. As a result, the validation
// shouldn't be made on the rest of the field.
Expand Down
11 changes: 10 additions & 1 deletion commands/liquidity_provision_submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestNilLiquidityProvisionSubmissionFails(t *testing.T) {
}

func TestLiquidityProvisionSubmission(t *testing.T) {
banana := "banana"
cases := []struct {
lp commandspb.LiquidityProvisionSubmission
errString string
Expand Down Expand Up @@ -67,7 +68,15 @@ func TestLiquidityProvisionSubmission(t *testing.T) {
},
errString: "liquidity_provision_submission.market_id (is required)",
},

{
lp: commandspb.LiquidityProvisionSubmission{
CommitmentAmount: "100",
MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
Fee: "0.1",
VaultId: &banana,
},
errString: "liquidity_provision_submission.vault_id (is not a valid vault identifier)",
},
{
lp: commandspb.LiquidityProvisionSubmission{
CommitmentAmount: "100",
Expand Down
4 changes: 4 additions & 0 deletions commands/order_amendment.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func checkOrderAmendment(cmd *commandspb.OrderAmendment) Errors {
}
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("order_amendment.vault_id", ErrInvalidVaultID)
}

if !isAmending {
errs.Add(errors.New("order_amendment does not amend anything"))
}
Expand Down
13 changes: 13 additions & 0 deletions commands/order_amendment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestCheckOrderAmendment(t *testing.T) {
t.Run("amend order tif to GFA - fail", testAmendOrderToGFA)
t.Run("amend order tif to GFN - fail", testAmendOrderToGFN)
t.Run("amend order pegged_offset", testAmendOrderPeggedOffset)
t.Run("amend order on bahalf of a vault with invalid id", testAmendInvalidVaultID)
}

func testNilOrderAmendmentFails(t *testing.T) {
Expand Down Expand Up @@ -214,6 +215,18 @@ func testAmendOrderToGFN(t *testing.T) {
assert.Error(t, err)
}

func testAmendInvalidVaultID(t *testing.T) {
banana := "banana"
arg := &commandspb.OrderAmendment{
OrderId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
SizeDelta: 100,
VaultId: &banana,
}
err := checkOrderAmendment(arg)
assert.Equal(t, "order_amendment.vault_id (is not a valid vault identifier)", err.Error())
}

func testAmendOrderToGFA(t *testing.T) {
arg := &commandspb.OrderAmendment{
OrderId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
Expand Down
3 changes: 3 additions & 0 deletions commands/order_cancellation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ func checkOrderCancellation(cmd *commandspb.OrderCancellation) Errors {
errs.AddForProperty("order_cancellation.order_id", ErrShouldBeAValidVegaID)
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("order_cancellation.vault_id", ErrInvalidVaultID)
}
return errs
}
4 changes: 4 additions & 0 deletions commands/order_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ func checkOrderSubmission(cmd *commandspb.OrderSubmission) Errors {
}
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("order_submission.vault_id", ErrInvalidVaultID)
}

if cmd.PeggedOrder != nil {
if cmd.PeggedOrder.Reference == types.PeggedReference_PEGGED_REFERENCE_UNSPECIFIED {
errs.AddForProperty("order_submission.pegged_order.reference", ErrIsRequired)
Expand Down
16 changes: 16 additions & 0 deletions commands/order_submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"

"code.vegaprotocol.io/vega/commands"
"code.vegaprotocol.io/vega/libs/crypto"
"code.vegaprotocol.io/vega/libs/test"
types "code.vegaprotocol.io/vega/protos/vega"
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"
Expand Down Expand Up @@ -775,6 +776,21 @@ func testPeggedOrderSubmissionWithSideSellAndMidReferenceAndPositiveOffsetSuccee
assert.NotContains(t, err.Get("order_submission.pegged_order.offset"), errors.New("must be positive"))
}

func TestOrderSubmissionWithInvalidVaultIDFails(t *testing.T) {
banana := "banana"
err := checkOrderSubmission(&commandspb.OrderSubmission{
MarketId: crypto.RandomHash(),
Price: "100",
Size: 100,
TimeInForce: types.Order_TIME_IN_FORCE_FOK,
Side: types.Side_SIDE_SELL,
Type: types.Order_TYPE_LIMIT,
VaultId: &banana,
})

assert.Equal(t, "order_submission.vault_id (is not a valid vault identifier)", err.Error())
}

func checkOrderSubmission(cmd *commandspb.OrderSubmission) commands.Errors {
err := commands.CheckOrderSubmission(cmd)

Expand Down
4 changes: 4 additions & 0 deletions commands/stop_orders_cancellation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ func checkStopOrdersCancellation(cmd *commandspb.StopOrdersCancellation) Errors
errs.AddForProperty("stop_orders_cancellation.stop_order_id", ErrShouldBeAValidVegaID)
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("stop_orders_cancellation.vault_id", ErrInvalidVaultID)
}

return errs
}
6 changes: 6 additions & 0 deletions commands/stop_orders_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ func checkStopOrdersSubmission(cmd *commandspb.StopOrdersSubmission) Errors {
"stop_orders_submission.falls_below", errs, cmd.FallsBelow, cmd.RisesAbove != nil)
if cmd.FallsBelow.OrderSubmission != nil {
market1 = cmd.FallsBelow.OrderSubmission.MarketId
if cmd.FallsBelow.OrderSubmission.VaultId != nil && !IsVegaID(*cmd.FallsBelow.OrderSubmission.VaultId) {
errs.AddForProperty("stop_orders_submission.falls_below.vault_id", ErrInvalidVaultID)
}
if cmd.FallsBelow.SizeOverrideSetting != nil {
if *cmd.FallsBelow.SizeOverrideSetting == types.StopOrder_SIZE_OVERRIDE_SETTING_POSITION {
if cmd.FallsBelow.SizeOverrideValue != nil {
Expand All @@ -66,6 +69,9 @@ func checkStopOrdersSubmission(cmd *commandspb.StopOrdersSubmission) Errors {
checkStopOrderSetup(
"stop_orders_submission.rises_below", errs, cmd.RisesAbove, cmd.FallsBelow != nil)
if cmd.RisesAbove.OrderSubmission != nil {
if cmd.RisesAbove.OrderSubmission.VaultId != nil && !IsVegaID(*cmd.RisesAbove.OrderSubmission.VaultId) {
errs.AddForProperty("stop_orders_submission.rises_above.vault_id", ErrInvalidVaultID)
}
market2 = cmd.RisesAbove.OrderSubmission.MarketId
if cmd.RisesAbove.SizeOverrideSetting != nil {
if *cmd.RisesAbove.SizeOverrideSetting == types.StopOrder_SIZE_OVERRIDE_SETTING_POSITION {
Expand Down
4 changes: 4 additions & 0 deletions commands/submit_amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,9 @@ func checkSubmitAMM(cmd *commandspb.SubmitAMM) Errors {
}
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("submit_amm.vault_id", ErrInvalidVaultID)
}

return errs
}
18 changes: 18 additions & 0 deletions commands/submit_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

func TestCheckSubmitAMM(t *testing.T) {
banana := "banana"
cases := []struct {
submission commandspb.SubmitAMM
errStr string
Expand Down Expand Up @@ -300,6 +301,23 @@ func TestCheckSubmitAMM(t *testing.T) {
},
errStr: "submit_amm.concentrated_liquidity_parameters.lower_bound (lower_bound and upper_bound cannot both be empty)",
},
{
submission: commandspb.SubmitAMM{
MarketId: "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
SlippageTolerance: "0.09",
CommitmentAmount: "10000",
ProposedFee: "0.03",
ConcentratedLiquidityParameters: &commandspb.SubmitAMM_ConcentratedLiquidityParameters{
Base: "20000",
UpperBound: ptr.From("30000"),
LowerBound: ptr.From("10000"),
LeverageAtUpperBound: ptr.From("0.1"),
LeverageAtLowerBound: ptr.From("0.1"),
},
VaultId: &banana,
},
errStr: "submit_amm.vault_id (is not a valid vault identifier)",
},
{
submission: commandspb.SubmitAMM{
MarketId: "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
Expand Down
10 changes: 10 additions & 0 deletions commands/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,16 @@ func CheckInputData(rawInputData []byte) (*commandspb.InputData, Errors) {
errs.Merge(checkAmendAMM(cmd.AmendAmm))
case *commandspb.InputData_CancelAmm:
errs.Merge(checkCancelAMM(cmd.CancelAmm))
case *commandspb.InputData_CreateVault:
errs.Merge(checkCreateVault(cmd.CreateVault))
case *commandspb.InputData_UpdateVault:
errs.Merge(checkUpdateVault(cmd.UpdateVault))
case *commandspb.InputData_DepositToVault:
errs.Merge(checkDepositToVault(cmd.DepositToVault))
case *commandspb.InputData_WithdrawFromVault:
errs.Merge(checkWithdrawFromVault(cmd.WithdrawFromVault))
case *commandspb.InputData_ChangeVaultOwnership:
errs.Merge(checkChangeVaultOwnership(cmd.ChangeVaultOwnership))
case *commandspb.InputData_DelayedTransactionsWrapper:
break
default:
Expand Down
4 changes: 4 additions & 0 deletions commands/update_margin_mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,9 @@ func checkUpdateMarginMode(cmd *commandspb.UpdateMarginMode) Errors {
errs.AddForProperty("update_margin_mode.market_id", ErrIsRequired)
}

if cmd.VaultId != nil && !IsVegaID(*cmd.VaultId) {
errs.AddForProperty("update_margin_mode.vault_id", ErrInvalidVaultID)
}

return errs
}
15 changes: 13 additions & 2 deletions commands/update_margin_mode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (

func TestUpdateMarginMode(t *testing.T) {
positiveMarginFactor := "123"
invalidNumderMarginFactor := "banana"
banana := "banana"
invalidDecimalMarginFactor := "1.2.3"
negativeMarginFactor := "-0.2"
zeroMarginFactor := "0"
Expand Down Expand Up @@ -77,7 +77,7 @@ func TestUpdateMarginMode(t *testing.T) {
"cross margin mode with invalid number as margin factor 1",
&commandspb.UpdateMarginMode{
Mode: commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
MarginFactor: &invalidNumderMarginFactor,
MarginFactor: &banana,
},
"update_margin_mode.margin_factor",
commands.ErrIsNotValidNumber,
Expand Down Expand Up @@ -128,6 +128,17 @@ func TestUpdateMarginMode(t *testing.T) {
"update_margin_mode.market_id",
commands.ErrIsRequired,
},
{
"invalid vault ID",
&commandspb.UpdateMarginMode{
Mode: commandspb.UpdateMarginMode_MODE_ISOLATED_MARGIN,
MarginFactor: &validMarginFactor1,
MarketId: "123",
VaultId: &banana,
},
"update_margin_mode.vault_id",
commands.ErrInvalidVaultID,
},
{
"valid cross margin update",
&commandspb.UpdateMarginMode{
Expand Down
Loading

0 comments on commit 6470285

Please sign in to comment.