Skip to content

Commit

Permalink
Merge pull request #11282 from vegaprotocol/feat-11263-new
Browse files Browse the repository at this point in the history
  • Loading branch information
karlem authored May 20, 2024
2 parents 51793e7 + 312612f commit f4f398c
Show file tree
Hide file tree
Showing 43 changed files with 4,064 additions and 3,363 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
- [11196](https://github.com/vegaprotocol/vega/issues/11196) - Add an active field in the price monitoring bounds payload.
- [11211](https://github.com/vegaprotocol/vega/issues/11211) - Liquidation engine includes `vAMM` shapes as available volume.
- [11217](https://github.com/vegaprotocol/vega/issues/11217) - Allow market proposals to override risk factors.
- [11282](https://github.com/vegaprotocol/vega/issues/11282) - Allow a party to withdraw rewards from an AMM vested account.

### 🐛 Fixes

- [11066](https://github.com/vegaprotocol/vega/issues/11066) - Ensure vesting statistics match vesting accounts numbers.
- [11279](https://github.com/vegaprotocol/vega/issues/11279) - Handle properly the case of multiple transfers for the same game id.
- [11279](https://github.com/vegaprotocol/vega/issues/11279) - Handle properly the case of multiple transfers for the same game id.
- [11293](https://github.com/vegaprotocol/vega/issues/11293) - Panic in data node with position estimate endpoint.
- [11279](https://github.com/vegaprotocol/vega/issues/11279) - Handle properly the case of multiple transfers for the same game id.

## 0.76.1

Expand All @@ -32,8 +33,7 @@
- [11250](https://github.com/vegaprotocol/vega/issues/11250) - Add missing dispatch strategy in transfer proposal.
- [11252](https://github.com/vegaprotocol/vega/issues/11252) - Treat no position as zero position when calculating stop order overrides.
- [11249](https://github.com/vegaprotocol/vega/issues/11249) - Composite mark price oracles are now deactivated when a market is rejected.
- [11262](https://github.com/vegaprotocol/vega/pull/11262) - Fix auction extensions after risk parameter update.

- [11262](https://github.com/vegaprotocol/vega/pull/11262) - Fix auction extensions after risk parameter update.

## 0.76.0

Expand Down Expand Up @@ -117,7 +117,6 @@
- [10374](https://github.com/vegaprotocol/vega/issues/10374) - Add transfer ID to recurring governance transfer ledger entries.
- [11221](https://github.com/vegaprotocol/vega/issues/11221) - Fix for `totalRewardsEarned` being twice the `rewardEarned`.


## 0.75.0

### 🚨 Breaking changes
Expand Down
15 changes: 15 additions & 0 deletions commands/transfer_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ func checkTransfer(cmd *commandspb.Transfer) (e Errors) {
errs.AddForProperty("transfer.reference", ErrMustBeLessThan100Chars)
}

// derived key check
if cmd.From != nil {
if !IsVegaPublicKey(*cmd.From) {
errs.AddForProperty("transfer.from", ErrShouldBeAValidVegaPublicKey)
}

if cmd.FromAccountType != vega.AccountType_ACCOUNT_TYPE_VESTED_REWARDS {
errs.AddForProperty("transfer.from", errors.New("from can only be set for vested rewards"))
}

if cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GENERAL {
errs.AddForProperty("transfer.from", errors.New("from can only be set when transferring to general account"))
}
}

if cmd.Kind == nil {
errs.AddForProperty("transfer.kind", ErrIsRequired)
} else {
Expand Down
64 changes: 64 additions & 0 deletions commands/transfer_funds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1381,6 +1381,66 @@ func TestTransferFunds(t *testing.T) {
},
errString: "transfer.kind.dispatch_strategy.transfer_interval (must be between 1 and 100)",
},
// sub account type tests
{
transfer: commandspb.Transfer{
FromAccountType: vega.AccountType_ACCOUNT_TYPE_VESTED_REWARDS,
ToAccountType: vega.AccountType_ACCOUNT_TYPE_GENERAL,
From: toPointer("derived_key"),
Kind: &commandspb.Transfer_OneOff{
OneOff: &commandspb.OneOffTransfer{},
},
To: "0000000000000000000000000000000000000000000000000000000000000000",
Asset: "080538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff",
Amount: "1",
Reference: "testing",
},
errString: "transfer.from (should be a valid vega public key)",
},
{
transfer: commandspb.Transfer{
FromAccountType: vega.AccountType_ACCOUNT_TYPE_GENERAL,
ToAccountType: vega.AccountType_ACCOUNT_TYPE_GENERAL,
From: toPointer("171538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff"),
Kind: &commandspb.Transfer_OneOff{
OneOff: &commandspb.OneOffTransfer{},
},
To: "0000000000000000000000000000000000000000000000000000000000000000",
Asset: "080538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff",
Amount: "1",
Reference: "testing",
},
errString: "transfer.from (from can only be set for vested rewards)",
},
{
transfer: commandspb.Transfer{
FromAccountType: vega.AccountType_ACCOUNT_TYPE_VESTED_REWARDS,
ToAccountType: vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD,
From: toPointer("171538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff"),
Kind: &commandspb.Transfer_OneOff{
OneOff: &commandspb.OneOffTransfer{},
},
To: "0000000000000000000000000000000000000000000000000000000000000000",
Asset: "080538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff",
Amount: "1",
Reference: "testing",
},
errString: "transfer.from (from can only be set when transferring to general account)",
},
{
transfer: commandspb.Transfer{
FromAccountType: vega.AccountType_ACCOUNT_TYPE_VESTED_REWARDS,
ToAccountType: vega.AccountType_ACCOUNT_TYPE_GENERAL,
From: toPointer("171538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff"),
Kind: &commandspb.Transfer_OneOff{
OneOff: &commandspb.OneOffTransfer{},
},
To: "0000000000000000000000000000000000000000000000000000000000000000",
Asset: "080538b7cc2249de568cb4272a17f4d5e0b0a69a1a240acbf5119d816178daff",
Amount: "1",
Reference: "testing",
},
},
}

invalidAccountTypesForOneOff := []vega.AccountType{
Expand Down Expand Up @@ -1445,3 +1505,7 @@ func checkTransfer(cmd *commandspb.Transfer) commands.Errors {

return e
}

func toPointer[T any](val T) *T {
return &val
}
9 changes: 8 additions & 1 deletion core/banking/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"golang.org/x/exp/maps"
)

//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/banking Assets,Notary,Collateral,Witness,TimeService,EpochService,Topology,MarketActivityTracker,ERC20BridgeView,EthereumEventSource
//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/banking Assets,Notary,Collateral,Witness,TimeService,EpochService,Topology,MarketActivityTracker,ERC20BridgeView,EthereumEventSource,Parties

var (
ErrWrongAssetTypeUsedInBuiltinAssetChainEvent = errors.New("non builtin asset used for builtin asset chain event")
Expand Down Expand Up @@ -120,6 +120,10 @@ type EthereumEventSource interface {
UpdateCollateralStartingBlock(uint64)
}

type Parties interface {
CheckDerivedKeyOwnership(party types.PartyID, derivedKey string) bool
}

const (
pendingState uint32 = iota
okState
Expand All @@ -143,6 +147,7 @@ type Engine struct {
notary Notary
assets Assets
top Topology
parties Parties

// assetActions tracks all the asset actions the engine must process on network
// tick.
Expand Down Expand Up @@ -234,6 +239,7 @@ func New(log *logging.Logger,
secondaryBridgeView ERC20BridgeView,
primaryEthEventSource EthereumEventSource,
secondaryEthEventSource EthereumEventSource,
parties Parties,
) (e *Engine) {
log = log.Named(namedLogger)
log.SetLevel(cfg.Level.Get())
Expand All @@ -250,6 +256,7 @@ func New(log *logging.Logger,
top: top,
primaryEthEventSource: primaryEthEventSource,
secondaryEthEventSource: secondaryEthEventSource,
parties: parties,
assetActions: map[string]*assetAction{},
seenAssetActions: treeset.NewWithStringComparator(),
withdrawals: map[string]withdrawalRef{},
Expand Down
5 changes: 4 additions & 1 deletion core/banking/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type testEngine struct {
marketActivityTracker *mocks.MockMarketActivityTracker
ethSource *mocks.MockEthereumEventSource
secondaryBridgeView *mocks.MockERC20BridgeView
parties *mocks.MockParties
}

func getTestEngine(t *testing.T) *testEngine {
Expand All @@ -79,7 +80,8 @@ func getTestEngine(t *testing.T) *testEngine {

notary.EXPECT().OfferSignatures(gomock.Any(), gomock.Any()).AnyTimes()
epoch.EXPECT().NotifyOnEpoch(gomock.Any(), gomock.Any()).AnyTimes()
eng := banking.New(logging.NewTestLogger(), banking.NewDefaultConfig(), col, witness, tsvc, assets, notary, broker, top, marketActivityTracker, primaryBridgeView, secondaryBridgeView, ethSource, nil)
parties := mocks.NewMockParties(ctrl)
eng := banking.New(logging.NewTestLogger(), banking.NewDefaultConfig(), col, witness, tsvc, assets, notary, broker, top, marketActivityTracker, primaryBridgeView, secondaryBridgeView, ethSource, nil, parties)

require.NoError(t, eng.OnMaxQuantumAmountUpdate(context.Background(), num.DecimalOne()))
eng.OnPrimaryEthChainIDUpdated("1")
Expand All @@ -98,6 +100,7 @@ func getTestEngine(t *testing.T) *testEngine {
secondaryBridgeView: secondaryBridgeView,
marketActivityTracker: marketActivityTracker,
ethSource: ethSource,
parties: parties,
}
}

Expand Down
8 changes: 6 additions & 2 deletions core/banking/fee_estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ func EstimateFee(
accumulatedDiscount *num.Uint,
from string,
fromAccountType types.AccountType,
fromDerivedKey *string,
to string,
) (fee *num.Uint, discount *num.Uint) {
tFee := calculateFeeForTransfer(assetQuantum, maxQuantumAmount, transferFeeFactor, amount, from, fromAccountType, to)
tFee := calculateFeeForTransfer(assetQuantum, maxQuantumAmount, transferFeeFactor, amount, from,
fromAccountType, fromDerivedKey, to)
return calculateDiscount(accumulatedDiscount, tFee)
}

Expand All @@ -42,12 +44,14 @@ func calculateFeeForTransfer(
amount *num.Uint,
from string,
fromAccountType types.AccountType,
fromDerivedKey *string,
to string,
) *num.Uint {
feeAmount := num.UintZero()

// no fee for Vested account
if fromAccountType == types.AccountTypeVestedRewards && from == to {
// either from owner's vested to their general account or from derived key vested to owner's general account
if fromAccountType == types.AccountTypeVestedRewards && (from == to || fromDerivedKey != nil) {
return feeAmount
}

Expand Down
39 changes: 38 additions & 1 deletion core/banking/mocks/mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions core/banking/oneoff_transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,22 @@ func (e *Engine) oneOffTransfer(
return err
}

if err := e.ensureMinimalTransferAmount(a, transfer.Amount, transfer.FromAccountType, transfer.From); err != nil {
if transfer.FromDerivedKey != nil {
if ownsDerivedKey := e.parties.CheckDerivedKeyOwnership(types.PartyID(transfer.From), *transfer.FromDerivedKey); !ownsDerivedKey {
transfer.Status = types.TransferStatusRejected
return fmt.Errorf("party %s does not own derived key %s", transfer.From, *transfer.FromDerivedKey)
}
}

if err := e.ensureMinimalTransferAmount(a, transfer.Amount, transfer.FromAccountType, transfer.From, transfer.FromDerivedKey); err != nil {
transfer.Status = types.TransferStatusRejected
return err
}

tresps, err := e.processTransfer(
ctx, a, transfer.From, transfer.To, "", transfer.FromAccountType,
transfer.ToAccountType, transfer.Amount, transfer.Reference, transfer.ID, e.currentEpoch, transfer,
transfer.ToAccountType, transfer.Amount, transfer.Reference, transfer.ID, e.currentEpoch, transfer.FromDerivedKey,
transfer,
)
if err != nil {
transfer.Status = types.TransferStatusRejected
Expand Down
Loading

0 comments on commit f4f398c

Please sign in to comment.