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

Support amending the order size by defining the target size #9979

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- [9955](https://github.com/vegaprotocol/vega/issues/9955) - Add data node subscription for transaction results.
- [10004](https://github.com/vegaprotocol/vega/issues/10004) Track average entry price in position engine
- [9825](https://github.com/vegaprotocol/vega/issues/9825) - Remove quadratic slippage.
- [9943](https://github.com/vegaprotocol/vega/issues/9943) - Support amending the order size by defining the target size.

### 🐛 Fixes

Expand Down
1 change: 1 addition & 0 deletions commands/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var (
ErrMustBeGreaterThanEnactmentTimestamp = errors.New("must be greater than proposal_submission.terms.enactment_timestamp")
ErrMustBeLessThen366 = errors.New("must be less then 366")
ErrMustBeAtMost500 = errors.New("must be at most 500")
ErrMustBeSetTo0IfSizeSet = errors.New("must be set to 0 if the property \"order_amendment.size\" is set")
ErrMustBeWithinRangeGT0LT20 = errors.New("price range must be strictly greater than 0 and less than or equal to 20")
)

Expand Down
7 changes: 7 additions & 0 deletions commands/order_amendment.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,14 @@ func checkOrderAmendment(cmd *commandspb.OrderAmendment) Errors {
}
}

if cmd.Size != nil {
isAmending = true
}

if cmd.SizeDelta != 0 {
if cmd.Size != nil {
errs.AddForProperty("order_amendment.size_delta", ErrMustBeSetTo0IfSizeSet)
}
isAmending = true
}

Expand Down
34 changes: 30 additions & 4 deletions commands/order_amendment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ import (
commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCheckOrderAmendment(t *testing.T) {
t.Run("Submitting a nil command fails", testNilOrderAmendmentFails)
t.Run("amend order price - success", testAmendOrderJustPriceSuccess)
t.Run("amend order reduce - success", testAmendOrderJustReduceSuccess)
t.Run("amend order increase - success", testAmendOrderJustIncreaseSuccess)
t.Run("amend order reduce size delta - success", testAmendOrderJustReduceSizeDeltaSuccess)
t.Run("amend order increase size delta - success", testAmendOrderJustIncreaseSizeDeltaSuccess)
t.Run("amend order setting size delta and size - fails", testAmendOrderSettingSizeDeltaAndSizeFails)
t.Run("amend order update size - success", testAmendOrderJustUpdateSizeSuccess)
t.Run("amend order expiry - success", testAmendOrderJustExpirySuccess)
t.Run("amend order tif - success", testAmendOrderJustTIFSuccess)
t.Run("amend order expiry before creation time - success", testAmendOrderPastExpiry)
Expand Down Expand Up @@ -61,7 +64,7 @@ func testAmendOrderJustPriceSuccess(t *testing.T) {
assert.NoError(t, err.ErrorOrNil())
}

func testAmendOrderJustReduceSuccess(t *testing.T) {
func testAmendOrderJustReduceSizeDeltaSuccess(t *testing.T) {
arg := &commandspb.OrderAmendment{
OrderId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
Expand All @@ -71,7 +74,7 @@ func testAmendOrderJustReduceSuccess(t *testing.T) {
assert.NoError(t, err.ErrorOrNil())
}

func testAmendOrderJustIncreaseSuccess(t *testing.T) {
func testAmendOrderJustIncreaseSizeDeltaSuccess(t *testing.T) {
arg := &commandspb.OrderAmendment{
OrderId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
Expand All @@ -81,6 +84,29 @@ func testAmendOrderJustIncreaseSuccess(t *testing.T) {
assert.NoError(t, err.ErrorOrNil())
}

func testAmendOrderSettingSizeDeltaAndSizeFails(t *testing.T) {
arg := &commandspb.OrderAmendment{
OrderId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
SizeDelta: 10,
Size: ptr.From(uint64(10)),
}
err := checkOrderAmendment(arg)
foundErrors := err.Get("order_amendment.size_delta")
require.Len(t, foundErrors, 1, "expected 1 error on size_delta")
assert.ErrorIs(t, foundErrors[0], commands.ErrMustBeSetTo0IfSizeSet)
}

func testAmendOrderJustUpdateSizeSuccess(t *testing.T) {
arg := &commandspb.OrderAmendment{
OrderId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
MarketId: "08dce6ebf50e34fedee32860b6f459824e4b834762ea66a96504fdc57a9c4741",
Size: ptr.From(uint64(10)),
}
err := checkOrderAmendment(arg)
assert.NoError(t, err.ErrorOrNil())
}

func testAmendOrderJustExpirySuccess(t *testing.T) {
now := time.Now()
expires := now.Add(-2 * time.Hour)
Expand Down
6 changes: 0 additions & 6 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,6 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
// see https://github.com/vegaprotocol/vega/pull/2745 for more information
e.addAccountToHashableSlice(externalAcc)
e.broker.Send(events.NewAccountEvent(ctx, *externalAcc))
e.log.Info("event emitted for new account", logging.String("accID", externalID))
}

// when an asset is enabled a staking reward account is created for it
Expand All @@ -438,7 +437,6 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[rewardID] = rewardAcc
e.addAccountToHashableSlice(rewardAcc)
e.broker.Send(events.NewAccountEvent(ctx, *rewardAcc))
e.log.Info("event emitted for new account", logging.String("accID", rewardID))
}
}

Expand All @@ -456,7 +454,6 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[netTreasury] = ntAcc
e.addAccountToHashableSlice(ntAcc)
e.broker.Send(events.NewAccountEvent(ctx, *ntAcc))
e.log.Info("event emitted for new account", logging.String("accID", netTreasury))
}

// global insurance for the asset
Expand All @@ -473,7 +470,6 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[globalInsurance] = giAcc
e.addAccountToHashableSlice(giAcc)
e.broker.Send(events.NewAccountEvent(ctx, *giAcc))
e.log.Info("event emitted for new account", logging.String("accID", globalInsurance))
}

// pending transfers account
Expand All @@ -491,7 +487,6 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[pendingTransfersID] = pendingTransfersAcc
e.addAccountToHashableSlice(pendingTransfersAcc)
e.broker.Send(events.NewAccountEvent(ctx, *pendingTransfersAcc))
e.log.Info("event emitted for new account", logging.String("accID", pendingTransfersID))
}

pendingFeeReferrerRewardID := e.accountID(noMarket, systemOwner, asset.ID, types.AccountTypePendingFeeReferralReward)
Expand All @@ -508,7 +503,6 @@ func (e *Engine) ensureAllAssetAccounts(ctx context.Context, asset types.Asset)
e.accs[pendingFeeReferrerRewardID] = pendingFeeReferrerRewardAcc
e.addAccountToHashableSlice(pendingFeeReferrerRewardAcc)
e.broker.Send(events.NewAccountEvent(ctx, *pendingFeeReferrerRewardAcc))
e.log.Info("event emitted for new account", logging.String("accID", pendingFeeReferrerRewardID))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Feature: Distressed parties should not have general balance left
| id | quote name | asset | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params |
| ETH/DEC20 | ETH | ETH | default-simple-risk-model-3 | default-margin-calculator | 1 | default-none | default-none | default-eth-for-future | 1e6 | 1e6 | default-futures |
And the following network parameters are set:
| name | value |
| market.auction.minimumDuration | 1 |
| network.markPriceUpdateMaximumFrequency | 0s |
| limits.markets.maxPeggedOrders | 4 |
| name | value |
| market.auction.minimumDuration | 1 |
| network.markPriceUpdateMaximumFrequency | 0s |
| limits.markets.maxPeggedOrders | 4 |
| market.liquidity.providersFeeCalculationTimeStep | 1s |

Scenario: Upper bound breached
Expand Down Expand Up @@ -71,11 +71,11 @@ Feature: Distressed parties should not have general balance left
Then the parties submit the following liquidity provision:
| id | party | market id | commitment amount | fee | lp type |
| lp2 | party3 | ETH/DEC20 | 20000 | 0.1 | submission |
And the parties place the following pegged iceberg orders:
And the parties place the following pegged iceberg orders:
| party | market id | peak size | minimum visible size | side | pegged reference | volume | offset | reference |
| party3 | ETH/DEC20 | 189 | 1 | buy | BID | 189 | 10 | lp2-ice-buy |
| party3 | ETH/DEC20 | 117 | 1 | sell | ASK | 117 | 10 | lp2-ice-sell |

Then the liquidity provisions should have the following states:
| id | party | market | commitment amount | status |
| lp2 | party3 | ETH/DEC20 | 20000 | STATUS_PENDING |
Expand All @@ -84,11 +84,11 @@ Feature: Distressed parties should not have general balance left
Then the liquidity provisions should have the following states:
| id | party | market | commitment amount | status |
| lp2 | party3 | ETH/DEC20 | 20000 | STATUS_ACTIVE |

Then the orders should have the following states:
| party | market id | side | volume | price | status |
| party3 | ETH/DEC20 | buy | 189 | 100 | STATUS_ACTIVE |
| party3 | ETH/DEC20 | sell | 117 | 130 | STATUS_ACTIVE |
| party | market id | side | volume | remaining | price | status |
| party3 | ETH/DEC20 | buy | 189 | 189 | 100 | STATUS_ACTIVE |
| party3 | ETH/DEC20 | sell | 117 | 117 | 130 | STATUS_ACTIVE |
## The sum of the margin + general account == 24000 - 10000 (commitment amount)
Then the parties should have the following account balances:
| party | asset | market id | margin | general |
Expand All @@ -101,28 +101,28 @@ Feature: Distressed parties should not have general balance left
| party1 | ETH/DEC20 | sell | 20 | 1850 | 0 | TYPE_LIMIT | TIF_GTC | ref-3 |
Then the mark price should be "120" for the market "ETH/DEC20"
Then the liquidity provider fee shares for the market "ETH/DEC20" should be:
| party | equity like share | average entry valuation |
| lpprov | 0.6428571428571429 | 10000 |
| party3 | 0.3571428571428571 | 60000.0000000000000556 |
| party | equity like share | average entry valuation |
| lpprov | 0.6428571428571429 | 10000 |
| party3 | 0.3571428571428571 | 60000.0000000000000556 |

And the following trades should be executed:
| buyer | price | size | seller |
| party5 | 120 | 20 | party1 |
| party5 | 120 | 20 | party3 |
Then the parties should have the following account balances:
| party5 | 120 | 20 | party1 |
| party5 | 120 | 20 | party3 |
Then the parties should have the following account balances:
| party | asset | market id | margin | general |
| party3 | ETH | ETH/DEC20 | 3152 | 1040 |
Then the parties cancel the following orders:
| party | reference |
| party3 | lp2-ice-sell |
| party3 | lp2-ice-buy |
| party | reference |
| party3 | lp2-ice-sell |
| party3 | lp2-ice-buy |

And the parties place the following pegged iceberg orders:
| party | market id | peak size | minimum visible size | side | pegged reference | volume | offset |
| party3 | ETH/DEC20 | 189 | 1 | buy | BID | 189 | 10 |
| party3 | ETH/DEC20 | 136 | 1 | sell | ASK | 136 | 10 |
Then the network moves ahead "10" blocks

Then the network moves ahead "10" blocks

And the parties should have the following account balances:
| party | asset | market id | margin | general |
Expand Down
Loading