Skip to content

Commit

Permalink
Merge pull request #9978 from vegaprotocol/fix-infrafee
Browse files Browse the repository at this point in the history
Transfer infra fees directly to general account without going through…
  • Loading branch information
ze97286 authored Nov 3, 2023
2 parents b21a871 + 29c87b7 commit f4e925f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 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 @@
- [9940](https://github.com/vegaprotocol/vega/issues/9940) - Truncate fee stats in quantum down to the 6 decimal.
- [9956](https://github.com/vegaprotocol/vega/issues/9956) - Prevent validator node from starting if they do not have a Ethereum `RPCAddress` set.
- [9952](https://github.com/vegaprotocol/vega/issues/9952) - `PnL` flickering fix.
- [9977](https://github.com/vegaprotocol/vega/issues/9977) - Transfer infra fees directly to general account without going through vesting.


## 0.73.0
Expand Down
20 changes: 15 additions & 5 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,35 +804,45 @@ func (e *Engine) TransferFees(ctx context.Context, marketID string, assetID stri
// returns the corresponding transfer request for the slice of transfers
// if the reward accound doesn't exist return error
// if the party account doesn't exist log the error and continue.
func (e *Engine) getRewardTransferRequests(ctx context.Context, rewardAccountID string, transfers []*types.Transfer) ([]*types.TransferRequest, error) {
func (e *Engine) getRewardTransferRequests(ctx context.Context, rewardAccountID string, transfers []*types.Transfer, rewardType types.AccountType) ([]*types.TransferRequest, error) {
rewardAccount, err := e.GetAccountByID(rewardAccountID)
if err != nil {
return nil, err
}

rewardTRs := make([]*types.TransferRequest, 0, len(transfers))
for _, t := range transfers {
vesting := e.GetOrCreatePartyVestingRewardAccount(ctx, t.Owner, t.Amount.Asset)
var destination *types.Account
if rewardType == types.AccountTypeFeesInfrastructure {
destination, err = e.GetPartyGeneralAccount(t.Owner, t.Amount.Asset)
if err != nil {
e.CreatePartyGeneralAccount(ctx, t.Owner, t.Amount.Asset)
destination, _ = e.GetPartyGeneralAccount(t.Owner, t.Amount.Asset)
}
} else {
destination = e.GetOrCreatePartyVestingRewardAccount(ctx, t.Owner, t.Amount.Asset)
}

rewardTRs = append(rewardTRs, &types.TransferRequest{
Amount: t.Amount.Amount.Clone(),
MinAmount: t.Amount.Amount.Clone(),
Asset: t.Amount.Asset,
Type: types.TransferTypeRewardPayout,
FromAccount: []*types.Account{rewardAccount},
ToAccount: []*types.Account{vesting},
ToAccount: []*types.Account{destination},
})
}
return rewardTRs, nil
}

// TransferRewards takes a slice of transfers and serves them to transfer rewards from the reward account to parties general account.
func (e *Engine) TransferRewards(ctx context.Context, rewardAccountID string, transfers []*types.Transfer) ([]*types.LedgerMovement, error) {
func (e *Engine) TransferRewards(ctx context.Context, rewardAccountID string, transfers []*types.Transfer, rewardType types.AccountType) ([]*types.LedgerMovement, error) {
responses := make([]*types.LedgerMovement, 0, len(transfers))

if len(transfers) == 0 {
return responses, nil
}
transferReqs, err := e.getRewardTransferRequests(ctx, rewardAccountID, transfers)
transferReqs, err := e.getRewardTransferRequests(ctx, rewardAccountID, transfers, rewardType)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions core/collateral/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func testTransferRewardsEmptySlice(t *testing.T) {
eng := getTestEngine(t)
defer eng.Finish()
eng.broker.EXPECT().Send(gomock.Any()).AnyTimes()
res, err := eng.TransferRewards(context.Background(), "reward", []*types.Transfer{})
res, err := eng.TransferRewards(context.Background(), "reward", []*types.Transfer{}, types.AccountTypeGlobalReward)
assert.Nil(t, err)
assert.Equal(t, 0, len(res))
}
Expand All @@ -272,7 +272,7 @@ func testTransferRewardsNoRewardsAccount(t *testing.T) {
},
}

res, err := eng.TransferRewards(context.Background(), "rewardAccID", transfers)
res, err := eng.TransferRewards(context.Background(), "rewardAccID", transfers, types.AccountTypeGlobalReward)
require.Error(t, errors.New("account does not exists"), err)
require.Nil(t, res)
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func testTransferRewardsSuccess(t *testing.T) {
},
}

lm, err := eng.TransferRewards(context.Background(), rewardAcc.ID, transfers)
lm, err := eng.TransferRewards(context.Background(), rewardAcc.ID, transfers, types.AccountTypeGlobalReward)
require.Nil(t, err)
partyAccount, _ := eng.GetAccountByID(partyAccountID)
require.Equal(t, num.NewUint(1000), partyAccount.Balance)
Expand Down
4 changes: 2 additions & 2 deletions core/rewards/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Delegation interface {
// Collateral engine provides access to account data and transferring rewards.
type Collateral interface {
GetAccountByID(id string) (*types.Account, error)
TransferRewards(ctx context.Context, rewardAccountID string, transfers []*types.Transfer) ([]*types.LedgerMovement, error)
TransferRewards(ctx context.Context, rewardAccountID string, transfers []*types.Transfer, rewardType types.AccountType) ([]*types.LedgerMovement, error)
GetRewardAccountsByType(rewardAcccountType types.AccountType) []*types.Account
}

Expand Down Expand Up @@ -441,7 +441,7 @@ func (e *Engine) distributePayout(ctx context.Context, po *payout) {
})
}

responses, err := e.collateral.TransferRewards(ctx, po.fromAccount, transfers)
responses, err := e.collateral.TransferRewards(ctx, po.fromAccount, transfers, po.rewardType)
if err != nil {
e.log.Error("error in transfer rewards", logging.Error(err))
return
Expand Down
13 changes: 6 additions & 7 deletions core/rewards/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,12 @@ func TestErsatzTendermintRewardSplit(t *testing.T) {
// node 4 gets 25000

// get party account balances
ctx := context.Background()
party1Acc := testEngine.collateral.GetOrCreatePartyVestingRewardAccount(ctx, "party1", "VEGA")
party2Acc := testEngine.collateral.GetOrCreatePartyVestingRewardAccount(ctx, "party2", "VEGA")
node1Acc := testEngine.collateral.GetOrCreatePartyVestingRewardAccount(ctx, "node1", "VEGA")
node2Acc := testEngine.collateral.GetOrCreatePartyVestingRewardAccount(ctx, "node2", "VEGA")
node3Acc := testEngine.collateral.GetOrCreatePartyVestingRewardAccount(ctx, "node3", "VEGA")
node4Acc := testEngine.collateral.GetOrCreatePartyVestingRewardAccount(ctx, "node4", "VEGA")
party1Acc, _ := testEngine.collateral.GetPartyGeneralAccount("party1", "VEGA")
party2Acc, _ := testEngine.collateral.GetPartyGeneralAccount("party2", "VEGA")
node1Acc, _ := testEngine.collateral.GetPartyGeneralAccount("node1", "VEGA")
node2Acc, _ := testEngine.collateral.GetPartyGeneralAccount("node2", "VEGA")
node3Acc, _ := testEngine.collateral.GetPartyGeneralAccount("node3", "VEGA")
node4Acc, _ := testEngine.collateral.GetPartyGeneralAccount("node4", "VEGA")

require.Equal(t, num.NewUint(172500), party1Acc.Balance)
require.Equal(t, num.NewUint(15000), party2Acc.Balance)
Expand Down

0 comments on commit f4e925f

Please sign in to comment.