Skip to content

Commit

Permalink
Merge branch 'develop' into 9773
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 authored Oct 16, 2023
2 parents 6e01529 + e067171 commit 4d0dd0b
Show file tree
Hide file tree
Showing 35 changed files with 4,190 additions and 3,297 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@
- [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add filtering for party to the referral fees API.
- [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add X day aggregate totals for referral set referees.
- [2985](https://github.com/vegaprotocol/system-tests/issues/2985) - Coverage for insurance pool transfers, fix deadlock when terminating pending market through governance.
- [9770](https://github.com/vegaprotocol/vega/issues/9770) - Fix `PnL` flickering bug.
- [9785](https://github.com/vegaprotocol/vega/issues/9785) - Support transfers to network treasury.
- [9743](https://github.com/vegaprotocol/system-tests/issues/9743) - Expose maker fees in fees stats API.

### 🐛 Fixes

Expand Down Expand Up @@ -312,6 +315,7 @@
- [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add filtering for party to the referral fees API.
- [9751](https://github.com/vegaprotocol/vega/issues/9751) - Make sure that LP fee party accounts exists.
- [9762](https://github.com/vegaprotocol/vega/issues/9762) - Referral fees API not filtering by party correctly.
- [9775](https://github.com/vegaprotocol/vega/issues/9775) - Do not pay discount if set is not eligible

## 0.72.1

Expand Down
2 changes: 1 addition & 1 deletion commands/transfer_funds.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func checkTransfer(cmd *commandspb.Transfer) (e Errors) {
} else {
switch k := cmd.Kind.(type) {
case *commandspb.Transfer_OneOff:
if cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GENERAL && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_UNSPECIFIED {
if cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GLOBAL_REWARD && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_GENERAL && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_UNSPECIFIED && cmd.ToAccountType != vega.AccountType_ACCOUNT_TYPE_NETWORK_TREASURY {
errs.AddForProperty("transfer.to_account_type", errors.New("account type is not valid for one off transfer"))
}
if k.OneOff.GetDeliverOn() < 0 {
Expand Down
3 changes: 1 addition & 2 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2538,7 +2538,7 @@ func (e *Engine) getTransferFundsTransferRequest(ctx context.Context, t *types.T
}

// this could not exists as well, let's just create in this case
case types.AccountTypeGlobalReward, types.AccountTypeLPFeeReward, types.AccountTypeMakerReceivedFeeReward,
case types.AccountTypeGlobalReward, types.AccountTypeLPFeeReward, types.AccountTypeMakerReceivedFeeReward, types.AccountTypeNetworkTreasury,
types.AccountTypeMakerPaidFeeReward, types.AccountTypeMarketProposerReward, types.AccountTypeAveragePositionReward,
types.AccountTypeRelativeReturnReward, types.AccountTypeReturnVolatilityReward, types.AccountTypeValidatorRankingReward:
market := noMarket
Expand All @@ -2550,7 +2550,6 @@ func (e *Engine) getTransferFundsTransferRequest(ctx context.Context, t *types.T
// shouldn't happen, we just created it...
return nil, err
}

default:
return nil, fmt.Errorf("unsupported to account for TransferFunds: %v", accountType.String())
}
Expand Down
4 changes: 2 additions & 2 deletions core/execution/common/liquidity_provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func (m *MarketLiquidity) updateSharesWithLiquidityScores(shares, scores map[str

func (m *MarketLiquidity) canSubmitCommitment(marketState types.MarketState) bool {
switch marketState {
case types.MarketStateActive, types.MarketStatePending, types.MarketStateSuspended, types.MarketStateProposed:
case types.MarketStateActive, types.MarketStatePending, types.MarketStateSuspended, types.MarketStateProposed, types.MarketStateSuspendedViaGovernance:
return true
}

Expand Down Expand Up @@ -679,7 +679,7 @@ func (m *MarketLiquidity) ensureAndTransferCollateral(
// first check if there's enough funds in the gen + bond
// account to cover the new commitment
if !m.collateral.CanCoverBond(m.marketID, party, m.asset, commitmentAmount.Clone()) {
return nil, ErrCommitmentSubmissionNotAllowed
return nil, ErrNotEnoughStake
}

// build our transfer to be sent to collateral
Expand Down
11 changes: 5 additions & 6 deletions core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"code.vegaprotocol.io/vega/libs/ptr"
"code.vegaprotocol.io/vega/logging"
vegapb "code.vegaprotocol.io/vega/protos/vega"

"golang.org/x/exp/maps"
)

Expand Down Expand Up @@ -362,12 +363,10 @@ func (m *Market) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
if !m.finalFeesDistributed {
m.liquidity.OnEpochEnd(ctx, m.timeService.GetTimeNow())
}
FeesStats := m.fee.GetFeesStatsOnEpochEnd()
FeesStats.Market = m.GetID()
FeesStats.EpochSeq = epoch.Seq
m.broker.Send(
events.NewFeesStatsEvent(ctx, FeesStats),
)
feesStats := m.fee.GetFeesStatsOnEpochEnd()
feesStats.Market = m.GetID()
feesStats.EpochSeq = epoch.Seq
m.broker.Send(events.NewFeesStatsEvent(ctx, feesStats))
}

m.updateLiquidityFee(ctx)
Expand Down
43 changes: 24 additions & 19 deletions core/fee/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (e *Engine) CalculateForContinuousMode(
}

var (
aggressor, maker string
taker, maker string
totalFeeAmount = num.UintZero()
totalInfrastructureFeeAmount = num.UintZero()
totalLiquidityFeeAmount = num.UintZero()
Expand All @@ -162,29 +162,34 @@ func (e *Engine) CalculateForContinuousMode(
transfersRecv = make([]*types.Transfer, 0, len(trades)+2)
)

for _, v := range trades {
aggressor = v.Buyer
if v.Aggressor == types.SideSell {
aggressor = v.Seller
for _, trade := range trades {
taker = trade.Buyer
maker = trade.Seller
if trade.Aggressor == types.SideSell {
taker = trade.Seller
maker = trade.Buyer
}
fee, reward := e.applyDiscountsAndRewards(aggressor, e.calculateContinuousModeFees(v), referral, volumeDiscountService)
switch v.Aggressor {
fee, reward := e.applyDiscountsAndRewards(taker, e.calculateContinuousModeFees(trade), referral, volumeDiscountService)

e.feesStats.RegisterMakerFee(maker, taker, fee.MakerFee)

switch trade.Aggressor {
case types.SideBuy:
v.BuyerFee = fee
v.SellerFee = types.NewFee()
maker = v.Seller
trade.BuyerFee = fee
trade.SellerFee = types.NewFee()
maker = trade.Seller
case types.SideSell:
v.SellerFee = fee
v.BuyerFee = types.NewFee()
maker = v.Buyer
trade.SellerFee = fee
trade.BuyerFee = types.NewFee()
maker = trade.Buyer
}

totalFeeAmount.AddSum(fee.InfrastructureFee, fee.LiquidityFee, fee.MakerFee)
totalInfrastructureFeeAmount.AddSum(fee.InfrastructureFee)
totalLiquidityFeeAmount.AddSum(fee.LiquidityFee)
// create a transfer for the aggressor
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Owner: taker,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: fee.MakerFee.Clone(),
Expand All @@ -211,7 +216,7 @@ func (e *Engine) CalculateForContinuousMode(

// now create transfer for the infrastructure
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Owner: taker,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: totalInfrastructureFeeAmount,
Expand All @@ -220,7 +225,7 @@ func (e *Engine) CalculateForContinuousMode(
})
// now create transfer for the liquidity
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Owner: taker,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: totalLiquidityFeeAmount,
Expand All @@ -230,9 +235,9 @@ func (e *Engine) CalculateForContinuousMode(

// if there's a referral reward - add transfers for it
if !totalRewardAmount.IsZero() {
referrer, _ := referral.GetReferrer(types.PartyID(aggressor))
referrer, _ := referral.GetReferrer(types.PartyID(taker))
transfers = append(transfers, &types.Transfer{
Owner: aggressor,
Owner: taker,
Amount: &types.FinancialAmount{
Asset: e.asset,
Amount: totalRewardAmount.Clone(),
Expand All @@ -250,7 +255,7 @@ func (e *Engine) CalculateForContinuousMode(
}

return &feesTransfer{
totalFeesAmountsPerParty: map[string]*num.Uint{aggressor: totalFeeAmount, maker: num.UintZero()},
totalFeesAmountsPerParty: map[string]*num.Uint{taker: totalFeeAmount, maker: num.UintZero()},
transfers: append(transfers, transfersRecv...),
}, nil
}
Expand Down
83 changes: 83 additions & 0 deletions core/fee/rebate_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import (
)

type FeesStats struct {
// TotalMakerFeesReceived is the total maker fees received by the maker side.
// maker -> amount
TotalMakerFeesReceived map[string]*num.Uint
// MakerFeesGenerated tracks maker fees paid by taker (aggressor) to the maker.
// taker -> maker -> amount
MakerFeesGenerated map[string]map[string]*num.Uint
TotalRewardsPaid map[string]*num.Uint
ReferrerRewardsGenerated map[string]map[string]*num.Uint
RefereeDiscountApplied map[string]*num.Uint
Expand All @@ -33,6 +39,8 @@ type FeesStats struct {

func NewFeesStats() *FeesStats {
return &FeesStats{
TotalMakerFeesReceived: map[string]*num.Uint{},
MakerFeesGenerated: map[string]map[string]*num.Uint{},
TotalRewardsPaid: map[string]*num.Uint{},
ReferrerRewardsGenerated: map[string]map[string]*num.Uint{},
RefereeDiscountApplied: map[string]*num.Uint{},
Expand Down Expand Up @@ -64,9 +72,46 @@ func NewFeesStatsFromProto(fsp *eventspb.FeesStats) *FeesStats {
fs.ReferrerRewardsGenerated[v.Referrer] = rg
}

for _, v := range fsp.TotalMakerFeesReceived {
fs.TotalMakerFeesReceived[v.Party] = num.MustUintFromString(v.Amount, 10)
}

for _, f := range fsp.MakerFeesGenerated {
rg := map[string]*num.Uint{}
for _, pa := range f.MakerFeesPaid {
rg[pa.Party] = num.MustUintFromString(pa.Amount, 10)
}

fs.MakerFeesGenerated[f.Taker] = rg
}

return fs
}

func (f *FeesStats) RegisterMakerFee(makerID, takerID string, amount *num.Uint) {
total, ok := f.TotalMakerFeesReceived[makerID]
if !ok {
total = num.NewUint(0)
f.TotalMakerFeesReceived[makerID] = total
}

total.Add(total, amount)

makerFeesGenerated, ok := f.MakerFeesGenerated[takerID]
if !ok {
makerFeesGenerated = map[string]*num.Uint{}
f.MakerFeesGenerated[takerID] = makerFeesGenerated
}

makerTally, ok := makerFeesGenerated[makerID]
if !ok {
makerTally = num.NewUint(0)
makerFeesGenerated[makerID] = makerTally
}

makerTally.Add(makerTally, amount)
}

func (f *FeesStats) RegisterReferrerReward(
referrer, referee string,
amount *num.Uint,
Expand Down Expand Up @@ -121,6 +166,8 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
ReferrerRewardsGenerated: make([]*eventspb.ReferrerRewardsGenerated, 0, len(f.ReferrerRewardsGenerated)),
RefereesDiscountApplied: make([]*eventspb.PartyAmount, 0, len(f.RefereeDiscountApplied)),
VolumeDiscountApplied: make([]*eventspb.PartyAmount, 0, len(f.VolumeDiscountApplied)),
TotalMakerFeesReceived: make([]*eventspb.PartyAmount, 0, len(f.TotalMakerFeesReceived)),
MakerFeesGenerated: make([]*eventspb.MakerFeesGenerated, 0, len(f.MakerFeesGenerated)),
}

totalRewardsPaidParties := maps.Keys(f.TotalRewardsPaid)
Expand Down Expand Up @@ -179,5 +226,41 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
fs.ReferrerRewardsGenerated = append(fs.ReferrerRewardsGenerated, rewardsGenerated)
}

totalMakerFeesReceivedParties := maps.Keys(f.TotalMakerFeesReceived)
sort.Strings(totalMakerFeesReceivedParties)
for _, maker := range totalMakerFeesReceivedParties {
amount := f.TotalMakerFeesReceived[maker]
fs.TotalMakerFeesReceived = append(fs.TotalMakerFeesReceived, &eventspb.PartyAmount{
Party: maker,
Amount: amount.String(),
})
}

makerFeesGeneratedParties := maps.Keys(f.MakerFeesGenerated)
sort.Strings(makerFeesGeneratedParties)
for _, taker := range makerFeesGeneratedParties {
makersAmounts := f.MakerFeesGenerated[taker]

rewardsGenerated := &eventspb.MakerFeesGenerated{
Taker: taker,
MakerFeesPaid: make([]*eventspb.PartyAmount, 0, len(makersAmounts)),
}

makersAmountsParties := maps.Keys(makersAmounts)
sort.Strings(makersAmountsParties)
for _, maker := range makersAmountsParties {
amount := makersAmounts[maker]
rewardsGenerated.MakerFeesPaid = append(
rewardsGenerated.MakerFeesPaid,
&eventspb.PartyAmount{
Party: maker,
Amount: amount.String(),
},
)
}

fs.MakerFeesGenerated = append(fs.MakerFeesGenerated, rewardsGenerated)
}

return fs
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Feature: Test LP mechanics when there are multiple liquidity providers;
| 0.000001 | 0.1 | 0 | 0 | 1.0 |
And the liquidity monitoring parameters:
| name | triggering ratio | time window | scaling factor |
| lqm-params | 1.0 | 20s | 1 |
| lqm-params | 1.0 | 20s | 1 |

And the following network parameters are set:
| name | value |
| market.value.windowLength | 60s |
Expand Down Expand Up @@ -209,7 +209,7 @@ Feature: Test LP mechanics when there are multiple liquidity providers;
#AC: 0044-LIME-030, lp increases commitment and they do not have sufficient collateral in the settlement asset
And the parties submit the following liquidity provision:
| id | party | market id | commitment amount | fee | lp type | error |
| lp_1 | lp1 | ETH/MAR22 | 600000 | 0.02 | amendment | commitment submission not allowed |
| lp_1 | lp1 | ETH/MAR22 | 600000 | 0.02 | amendment | commitment submission rejected, not enough stake |
Then the network moves ahead "1" blocks
And the supplied stake should be "60000" for the market "ETH/MAR22"

Expand Down Expand Up @@ -261,5 +261,3 @@ Feature: Test LP mechanics when there are multiple liquidity providers;
| from | to | from account | to account | market id | amount | asset |
| market | lp1 | ACCOUNT_TYPE_FEES_LIQUIDITY | ACCOUNT_TYPE_LP_LIQUIDITY_FEES | ETH/MAR22 | 33 | USD |
| market | lp2 | ACCOUNT_TYPE_FEES_LIQUIDITY | ACCOUNT_TYPE_LP_LIQUIDITY_FEES | ETH/MAR22 | 6 | USD |


Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Feature: Test LP mechanics when there are multiple liquidity providers, and LPs
| market.fee.factors.makerFee | 0.004 |
And the liquidity monitoring parameters:
| name | triggering ratio | time window | scaling factor |
| lqm-params | 1.0 | 20s | 1 |
| lqm-params | 1.0 | 20s | 1 |
#risk factor short:3.5569036
#risk factor long:0.801225765
And the following assets are registered:
Expand Down Expand Up @@ -114,7 +114,7 @@ Feature: Test LP mechanics when there are multiple liquidity providers, and LPs
#AC 0044-LIME-063: When the LP increases its commitment and the increment is higher than its general account balance, the increments are rejected
And the parties submit the following liquidity provision:
| id | party | market id | commitment amount | fee | lp type | error |
| lp_1 | lp1 | ETH/MAR22 | 600000 | 0.02 | amendment | commitment submission not allowed |
| lp_1 | lp1 | ETH/MAR22 | 600000 | 0.02 | amendment | commitment submission rejected, not enough stake |

#AC 0044-LIME-101: During continuous trading an LP can submit a transaction to decrease commitment but it will only happen at the end of current epoch.
And the parties submit the following liquidity provision:
Expand Down Expand Up @@ -189,7 +189,3 @@ Feature: Test LP mechanics when there are multiple liquidity providers, and LPs
| lp1 | market | ACCOUNT_TYPE_BOND | ACCOUNT_TYPE_INSURANCE | ETH/MAR22 | 206 | USD |
| lp2 | market | ACCOUNT_TYPE_BOND | ACCOUNT_TYPE_INSURANCE | ETH/MAR22 | 310 | USD |
And the insurance pool balance should be "1546" for the market "ETH/MAR22"




Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ Feature: Test LP mechanics when there are multiple liquidity providers;
| 0.5 | 0.5 | 1 | 1.0 |
And the liquidity monitoring parameters:
| name | triggering ratio | time window | scaling factor |
| lqm-params | 1.0 | 20s | 10 |
| lqm-params | 1.0 | 20s | 10 |

And the markets:
| id | quote name | asset | liquidity monitoring | risk model | margin calculator | auction duration | fees | price monitoring | data source config | linear slippage factor | quadratic slippage factor | sla params |
| ETH/MAR22 | USD | USD | lqm-params | log-normal-risk-model | margin-calculator-1 | 2 | fees-config-1 | price-monitoring | default-eth-for-future | 1e0 | 0 | SLA |
Expand Down Expand Up @@ -192,7 +192,7 @@ Feature: Test LP mechanics when there are multiple liquidity providers;
#AC: 0044-LIME-030, lp increases commitment and they do not have sufficient collateral in the settlement asset
And the parties submit the following liquidity provision:
| id | party | market id | commitment amount | fee | lp type | error |
| lp_1 | lp1 | ETH/MAR22 | 600000 | 0.02 | amendment | commitment submission not allowed |
| lp_1 | lp1 | ETH/MAR22 | 600000 | 0.02 | amendment | commitment submission rejected, not enough stake |
Then the network moves ahead "1" blocks
And the supplied stake should be "60000" for the market "ETH/MAR22"

Expand All @@ -201,6 +201,3 @@ Feature: Test LP mechanics when there are multiple liquidity providers;
| id | party | market id | commitment amount | fee | lp type |
| lp_1 | lp1 | ETH/MAR22 | 60000 | 0.02 | amendment |
And the supplied stake should be "70000" for the market "ETH/MAR22"



Loading

0 comments on commit 4d0dd0b

Please sign in to comment.