Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: vault account #11746

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@
- [11685](https://github.com/vegaprotocol/vega/issues/11685) - Automated purchase support added.
- [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.
- [11722](https://github.com/vegaprotocol/vega/issues/11722) - Expose active protocol automated purchase identifier in market data API.
- [11744](https://github.com/vegaprotocol/vega/issues/11744) - Staking from collateral bridged assets.
- [11745](https://github.com/vegaprotocol/vega/issues/11745) - Implement vault accounts
- [11750](https://github.com/vegaprotocol/vega/issues/11745) - Fix division by zero when vault is empty.

### 🐛 Fixes

Expand All @@ -45,6 +47,7 @@
- [11699](https://github.com/vegaprotocol/vega/issues/11699) - Update factors of programs when they are updated.
- [11724](https://github.com/vegaprotocol/vega/issues/11724) - Allow nil initial time in time trigger.
- [11733](https://github.com/vegaprotocol/vega/issues/11733) - Fix division by zero.
- [11753](https://github.com/vegaprotocol/vega/issues/11753) - Update the vesting engine correctly on which party is getting the reward in the case of a vault.

## 0.78.2

Expand Down
2 changes: 2 additions & 0 deletions cmd/data-node/commands/start/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ func (l *NodeCommand) createGRPCServer(config api.Config) *api.GRPCServer {
l.ammPoolsService,
l.volumeRebateStatsService,
l.volumeRebateProgramService,
l.vaultService,
l.vaultRedemptionService,
)
return grpcServer
}
15 changes: 14 additions & 1 deletion cmd/data-node/commands/start/sqlsubscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ type SQLSubscribers struct {
ammPoolsStore *sqlstore.AMMPools
volumeRebateStatsStore *sqlstore.VolumeRebateStats
volumeRebateProgramsStore *sqlstore.VolumeRebatePrograms
vaultStore *sqlstore.Vault
vaultRedemptionStore *sqlstore.VaultRedemptions

// Services
candleService *candlesv2.Svc
Expand Down Expand Up @@ -146,6 +148,8 @@ type SQLSubscribers struct {
ammPoolsService *service.AMMPools
volumeRebateStatsService *service.VolumeRebateStats
volumeRebateProgramService *service.VolumeRebatePrograms
vaultService *service.Vault
vaultRedemptionService *service.VaultRedemptions

// Subscribers
accountSub *sqlsubscribers.Account
Expand Down Expand Up @@ -202,6 +206,8 @@ type SQLSubscribers struct {
ammPoolsSub *sqlsubscribers.AMMPools
volumeRebateStatsSub *sqlsubscribers.VolumeRebateStatsUpdated
volumeRebateProgramSub *sqlsubscribers.VolumeRebateProgram
vaultSub *sqlsubscribers.Vault
vaultRedemptionSub *sqlsubscribers.VaultRedemptions
}

func (s *SQLSubscribers) GetSQLSubscribers() []broker.SQLBrokerSubscriber {
Expand Down Expand Up @@ -262,6 +268,8 @@ func (s *SQLSubscribers) GetSQLSubscribers() []broker.SQLBrokerSubscriber {
s.ammPoolsSub,
s.volumeRebateProgramSub,
s.volumeRebateStatsSub,
s.vaultSub,
s.vaultRedemptionSub,
}
}

Expand Down Expand Up @@ -327,6 +335,8 @@ func (s *SQLSubscribers) CreateAllStores(ctx context.Context, Log *logging.Logge
s.ammPoolsStore = sqlstore.NewAMMPools(transactionalConnectionSource)
s.volumeRebateStatsStore = sqlstore.NewVolumeRebateStats(transactionalConnectionSource)
s.volumeRebateProgramsStore = sqlstore.NewVolumeRebatePrograms(transactionalConnectionSource)
s.vaultStore = sqlstore.NewVault(transactionalConnectionSource)
s.vaultRedemptionStore = sqlstore.NewVaultRedemptions(transactionalConnectionSource)
}

func (s *SQLSubscribers) SetupServices(ctx context.Context, log *logging.Logger, cfg service.Config, candlesConfig candlesv2.Config) error {
Expand Down Expand Up @@ -386,7 +396,8 @@ func (s *SQLSubscribers) SetupServices(ctx context.Context, log *logging.Logger,
s.ammPoolsService = service.NewAMMPools(s.ammPoolsStore)
s.volumeRebateStatsService = service.NewVolumeRebateStats(s.volumeRebateStatsStore)
s.volumeRebateProgramService = service.NewVolumeRebatePrograms(s.volumeRebateProgramsStore)

s.vaultService = service.NewVault(s.vaultStore, log)
s.vaultRedemptionService = service.NewVaultRedemptions(s.vaultRedemptionStore, log)
s.marketDepthService = service.NewMarketDepth(
cfg.MarketDepth,
s.orderStore,
Expand Down Expand Up @@ -470,4 +481,6 @@ func (s *SQLSubscribers) SetupSQLSubscribers() {
s.volumeRebateStatsSub = sqlsubscribers.NewVolumeRebateStatsUpdated(s.volumeRebateStatsService)
s.volumeRebateProgramSub = sqlsubscribers.NewVolumeRebateProgram(s.volumeRebateProgramService)
s.ammPoolsSub = sqlsubscribers.NewAMMPools(s.ammPoolsService, s.marketDepthService)
s.vaultSub = sqlsubscribers.NewVault(s.vaultStore)
s.vaultRedemptionSub = sqlsubscribers.NewVaultRedemptions(s.vaultRedemptionStore)
}
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
2 changes: 2 additions & 0 deletions commands/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ var (
ErrMustHaveAtLeastOneOfRisesAboveOrFallsBelow = errors.New("must have at least one of rises above or falls below")
ErrMustHaveAStopOrderTrigger = errors.New("must have a stop order trigger")
ErrFallsBelowAndRiseAboveMarketIDMustBeTheSame = errors.New("market ID for falls below and rises above must be the same")
ErrFallsBelowAndRiseAboveVaultIDMustBeTheSame = errors.New("vault ID for falls below and rises above must be the same")
ErrTrailingPercentOffsetMinimalIncrementNotReached = errors.New("trailing percent offset minimal increment must be >= 0.001")
ErrMustBeEmpty = errors.New("must be empty")
ErrMustBeGTEClampLowerBound = errors.New("must be greater than or equal to clamp lower bound")
Expand All @@ -97,6 +98,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
39 changes: 39 additions & 0 deletions commands/stop_order_submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
)

func TestCheckStopOrdersStubmission(t *testing.T) {
vault1ID := "e9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca"
vault2ID := "f9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca"
cases := []struct {
submission commandspb.StopOrdersSubmission
errStr string
Expand Down Expand Up @@ -238,6 +240,43 @@ func TestCheckStopOrdersStubmission(t *testing.T) {
},
errStr: "* (market ID for falls below and rises above must be the same)",
},
{
submission: commandspb.StopOrdersSubmission{
RisesAbove: &commandspb.StopOrderSetup{
OrderSubmission: &commandspb.OrderSubmission{
MarketId: "f9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
VaultId: &vault1ID,
Side: vega.Side_SIDE_BUY,
Size: 100,
TimeInForce: vega.Order_TIME_IN_FORCE_IOC,
Type: vega.Order_TYPE_MARKET,
ReduceOnly: true,
},
ExpiresAt: ptr.From(int64(1000)),
ExpiryStrategy: ptr.From(vega.StopOrder_EXPIRY_STRATEGY_CANCELS),
Trigger: &commandspb.StopOrderSetup_TrailingPercentOffset{
TrailingPercentOffset: "0.1",
},
},
FallsBelow: &commandspb.StopOrderSetup{
OrderSubmission: &commandspb.OrderSubmission{
MarketId: "f9982447fb4128f9968f9981612c5ea85d19b62058ec2636efc812dcbbc745ca",
VaultId: &vault2ID,
Side: vega.Side_SIDE_BUY,
Size: 100,
TimeInForce: vega.Order_TIME_IN_FORCE_IOC,
Type: vega.Order_TYPE_MARKET,
ReduceOnly: true,
},
ExpiresAt: ptr.From(int64(1000)),
ExpiryStrategy: ptr.From(vega.StopOrder_EXPIRY_STRATEGY_CANCELS),
Trigger: &commandspb.StopOrderSetup_TrailingPercentOffset{
TrailingPercentOffset: "0.1",
},
},
},
errStr: "* (vault ID for falls below and rises above must be the same)",
},
{
submission: commandspb.StopOrdersSubmission{
RisesAbove: &commandspb.StopOrderSetup{
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
}
Loading
Loading