Skip to content

Commit

Permalink
Merge pull request #9929 from vegaprotocol/feature/expose-quantum-fee…
Browse files Browse the repository at this point in the history
…-stats

feat: expose quanum balance in fee stats event
  • Loading branch information
jeremyletang authored Oct 30, 2023
2 parents 71bdf36 + bcc4399 commit e0c750f
Show file tree
Hide file tree
Showing 15 changed files with 1,719 additions and 1,670 deletions.
3 changes: 3 additions & 0 deletions core/execution/engine_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func createEngine(t *testing.T) (*execution.Engine, *gomock.Controller) {
collateralService.EXPECT().GetInsurancePoolBalance(gomock.Any(), gomock.Any()).AnyTimes().Return(num.UintZero(), true)
collateralService.EXPECT().CreateSpotMarketAccounts(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
collateralService.EXPECT().GetAssetQuantum("ETH").AnyTimes().Return(num.DecimalFromInt64(1), nil)
collateralService.EXPECT().GetAssetQuantum("Ethereum/Ether").AnyTimes().Return(num.DecimalFromInt64(1), nil)
collateralService.EXPECT().GetOrCreatePartyBondAccount(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Account{Balance: num.UintZero()}, nil)
collateralService.EXPECT().GetOrCreatePartyLiquidityFeeAccount(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
collateralService.EXPECT().BondSpotUpdate(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(&types.LedgerMovement{}, nil)
Expand Down Expand Up @@ -564,6 +565,7 @@ func TestValidSpotMarketSnapshot(t *testing.T) {
func TestValidSettledMarketSnapshot(t *testing.T) {
ctx := vgcontext.WithTraceID(context.Background(), hex.EncodeToString([]byte("0deadbeef")))
engine := getMockedEngine(t)
engine.collateral.EXPECT().GetAssetQuantum("Ethereum/Ether").AnyTimes().Return(num.DecimalFromInt64(1), nil)
engine.collateral.EXPECT().AssetExists(gomock.Any()).AnyTimes().Return(true)
engine.collateral.EXPECT().CreateMarketAccounts(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
engine.collateral.EXPECT().GetMarketLiquidityFeeAccount(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Account{Balance: num.UintZero()}, nil)
Expand Down Expand Up @@ -692,6 +694,7 @@ func TestValidSettledMarketSnapshot(t *testing.T) {
func TestSuccessorMapSnapshot(t *testing.T) {
ctx := vgcontext.WithTraceID(context.Background(), hex.EncodeToString([]byte("0deadbeef")))
engine := getMockedEngine(t)
engine.collateral.EXPECT().GetAssetQuantum("Ethereum/Ether").AnyTimes().Return(num.DecimalFromInt64(1), nil)
engine.collateral.EXPECT().AssetExists(gomock.Any()).AnyTimes().Return(true)
engine.collateral.EXPECT().CreateMarketAccounts(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
engine.collateral.EXPECT().GetMarketLiquidityFeeAccount(gomock.Any(), gomock.Any()).AnyTimes().Return(&types.Account{Balance: num.UintZero()}, nil)
Expand Down
1 change: 1 addition & 0 deletions core/execution/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func TestMarketSuccession(t *testing.T) {
}
})
exec.broker.EXPECT().SendBatch(gomock.Any()).AnyTimes()
exec.collateral.EXPECT().GetAssetQuantum("Ethereum/Ether").AnyTimes().Return(num.DecimalFromInt64(1), nil)
exec.collateral.EXPECT().AssetExists(gomock.Any()).AnyTimes().Return(true)
exec.collateral.EXPECT().CreateMarketAccounts(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
exec.oracle.EXPECT().Subscribe(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(spec.SubscriptionID(0), func(_ context.Context, _ spec.SubscriptionID) {}, nil)
Expand Down
3 changes: 2 additions & 1 deletion core/execution/future/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,8 @@ func (m *Market) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
if !m.finalFeesDistributed {
m.liquidity.OnEpochEnd(ctx, m.timeService.GetTimeNow(), epoch)
}
feesStats := m.fee.GetFeesStatsOnEpochEnd()
assetQuantum, _ := m.collateral.GetAssetQuantum(m.settlementAsset)
feesStats := m.fee.GetFeesStatsOnEpochEnd(assetQuantum)
feesStats.Market = m.GetID()
feesStats.EpochSeq = epoch.Seq
m.broker.Send(events.NewFeesStatsEvent(ctx, feesStats))
Expand Down
3 changes: 2 additions & 1 deletion core/execution/future/market_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func (m *Market) GetState() *types.ExecMarket {

parties := maps.Keys(m.parties)
sort.Strings(parties)
assetQuantum, _ := m.collateral.GetAssetQuantum(m.settlementAsset)

em := &types.ExecMarket{
Market: m.mkt.DeepClone(),
Expand Down Expand Up @@ -301,7 +302,7 @@ func (m *Market) GetState() *types.ExecMarket {
StopOrders: m.stopOrders.ToProto(),
ExpiringStopOrders: m.expiringStopOrders.GetState(),
Product: m.tradableInstrument.Instrument.Product.Serialize(),
FeesStats: m.fee.GetState(),
FeesStats: m.fee.GetState(assetQuantum),
}

return em
Expand Down
10 changes: 6 additions & 4 deletions core/execution/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,9 @@ func getEngine(t *testing.T, vegaPath paths.Paths, now time.Time) *snapshotTestD
ethAsset := types.Asset{
ID: "Ethereum/Ether",
Details: &types.AssetDetails{
Name: "Ethereum/Ether",
Symbol: "Ethereum/Ether",
Name: "Ethereum/Ether",
Symbol: "Ethereum/Ether",
Quantum: num.DecimalFromInt64(1),
},
}
require.NoError(t, collateralEngine.EnableAsset(context.Background(), ethAsset))
Expand Down Expand Up @@ -657,8 +658,9 @@ func getEngineWithParties(t *testing.T, now time.Time, balance *num.Uint, partie
ethAsset := types.Asset{
ID: "Ethereum/Ether",
Details: &types.AssetDetails{
Name: "Ethereum/Ether",
Symbol: "Ethereum/Ether",
Name: "Ethereum/Ether",
Symbol: "Ethereum/Ether",
Quantum: num.DecimalFromInt64(1),
},
}
collateralEngine.EnableAsset(context.Background(), ethAsset)
Expand Down
3 changes: 2 additions & 1 deletion core/execution/spot/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -2878,7 +2878,8 @@ func (m *Market) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
} else if epoch.Action == vega.EpochAction_EPOCH_ACTION_END {
m.liquidity.OnEpochEnd(ctx, m.timeService.GetTimeNow(), epoch)
m.updateLiquidityFee(ctx)
feesStats := m.fee.GetFeesStatsOnEpochEnd()
quoteAssetQuantum, _ := m.collateral.GetAssetQuantum(m.quoteAsset)
feesStats := m.fee.GetFeesStatsOnEpochEnd(quoteAssetQuantum)
feesStats.EpochSeq = epoch.Seq
feesStats.Market = m.GetID()
m.broker.Send(events.NewFeesStatsEvent(ctx, feesStats))
Expand Down
3 changes: 2 additions & 1 deletion core/execution/spot/market_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func NewMarketFromSnapshot(
func (m *Market) GetState() *types.ExecSpotMarket {
parties := maps.Keys(m.parties)
sort.Strings(parties)
quoteAssetQuantum, _ := m.collateral.GetAssetQuantum(m.quoteAsset)

em := &types.ExecSpotMarket{
Market: m.mkt.DeepClone(),
Expand All @@ -212,7 +213,7 @@ func (m *Market) GetState() *types.ExecSpotMarket {
Parties: parties,
Closed: m.closed,
HasTraded: m.hasTraded,
FeesStats: m.fee.GetState(),
FeesStats: m.fee.GetState(quoteAssetQuantum),
}

return em
Expand Down
16 changes: 11 additions & 5 deletions core/fee/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ type factors struct {
liquidityFee num.Decimal
}

func New(log *logging.Logger, cfg Config, feeCfg types.Fees, asset string, positionFactor num.Decimal) (*Engine, error) {
func New(
log *logging.Logger,
cfg Config,
feeCfg types.Fees,
asset string,
positionFactor num.Decimal,
) (*Engine, error) {
log = log.Named(namedLogger)
log.SetLevel(cfg.Level.Get())

Expand Down Expand Up @@ -93,12 +99,12 @@ func NewFromState(
return e, nil
}

func (e *Engine) GetState() *eventspb.FeesStats {
return e.feesStats.ToProto(e.asset)
func (e *Engine) GetState(assetQuantum num.Decimal) *eventspb.FeesStats {
return e.feesStats.ToProto(e.asset, assetQuantum)
}

func (e *Engine) GetFeesStatsOnEpochEnd() (FeesStats *eventspb.FeesStats) {
FeesStats, e.feesStats = e.feesStats.ToProto(e.asset), NewFeesStats()
func (e *Engine) GetFeesStatsOnEpochEnd(assetQuantum num.Decimal) (FeesStats *eventspb.FeesStats) {
FeesStats, e.feesStats = e.feesStats.ToProto(e.asset, assetQuantum), NewFeesStats()
return
}

Expand Down
54 changes: 32 additions & 22 deletions core/fee/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,34 +199,38 @@ func testCalcContinuousTradingAndCheckAmounts(t *testing.T) {
ReferrerRewardsGenerated: []*eventspb.ReferrerRewardsGenerated{},
RefereesDiscountApplied: []*eventspb.PartyAmount{
{
Party: "party1",
Amount: "0",
Party: "party1",
Amount: "0",
QuantumAmount: "0",
},
},
VolumeDiscountApplied: []*eventspb.PartyAmount{
{
Party: "party1",
Amount: "0",
Party: "party1",
Amount: "0",
QuantumAmount: "0",
},
},
TotalMakerFeesReceived: []*eventspb.PartyAmount{
{
Party: "party2",
Amount: "125",
Party: "party2",
Amount: "125",
QuantumAmount: "125",
},
},
MakerFeesGenerated: []*eventspb.MakerFeesGenerated{
{
Taker: "party1",
MakerFeesPaid: []*eventspb.PartyAmount{
{
Party: "party2",
Amount: "125",
Party: "party2",
Amount: "125",
QuantumAmount: "125",
},
},
},
},
}, eng.GetFeesStatsOnEpochEnd())
}, eng.GetFeesStatsOnEpochEnd(num.DecimalFromInt64(1)))
}

func testCalcContinuousTradingAndCheckAmountsWithDiscount(t *testing.T) {
Expand Down Expand Up @@ -287,51 +291,57 @@ func testCalcContinuousTradingAndCheckAmountsWithDiscount(t *testing.T) {
Asset: testAsset,
TotalRewardsReceived: []*eventspb.PartyAmount{
{
Party: "party3",
Amount: "110",
Party: "party3",
Amount: "110",
QuantumAmount: "110",
},
},
ReferrerRewardsGenerated: []*eventspb.ReferrerRewardsGenerated{
{
Referrer: "party3",
GeneratedReward: []*eventspb.PartyAmount{
{
Party: "party1",
Amount: "110",
Party: "party1",
Amount: "110",
QuantumAmount: "110",
},
},
},
},
RefereesDiscountApplied: []*eventspb.PartyAmount{
{
Party: "party1",
Amount: "262",
Party: "party1",
Amount: "262",
QuantumAmount: "262",
},
},
VolumeDiscountApplied: []*eventspb.PartyAmount{
{
Party: "party1",
Amount: "60",
Party: "party1",
Amount: "60",
QuantumAmount: "60",
},
},
TotalMakerFeesReceived: []*eventspb.PartyAmount{
{
Party: "party2",
Amount: "64",
Party: "party2",
Amount: "64",
QuantumAmount: "64",
},
},
MakerFeesGenerated: []*eventspb.MakerFeesGenerated{
{
Taker: "party1",
MakerFeesPaid: []*eventspb.PartyAmount{
{
Party: "party2",
Amount: "64",
Party: "party2",
Amount: "64",
QuantumAmount: "64",
},
},
},
},
}, eng.GetFeesStatsOnEpochEnd())
}, eng.GetFeesStatsOnEpochEnd(num.DecimalFromInt64(1)))
}

func testCalcContinuousTradingAndCheckAmountsWithDiscountsAndRewardsBySide(t *testing.T, aggressorSide types.Side) {
Expand Down
32 changes: 19 additions & 13 deletions core/fee/rebate_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (f *FeesStats) RegisterVolumeDiscount(party string, amount *num.Uint) {
total.Add(total, amount)
}

func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
func (f *FeesStats) ToProto(asset string, assetQuantum num.Decimal) *eventspb.FeesStats {
fs := &eventspb.FeesStats{
Asset: asset,
TotalRewardsReceived: make([]*eventspb.PartyAmount, 0, len(f.TotalRewardsReceived)),
Expand All @@ -177,8 +177,9 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
for _, party := range totalRewardsReceivedParties {
amount := f.TotalRewardsReceived[party]
fs.TotalRewardsReceived = append(fs.TotalRewardsReceived, &eventspb.PartyAmount{
Party: party,
Amount: amount.String(),
Party: party,
Amount: amount.String(),
QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(2).String(),
})
}

Expand All @@ -187,8 +188,9 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
for _, party := range refereesDiscountAppliedParties {
amount := f.RefereeDiscountApplied[party]
fs.RefereesDiscountApplied = append(fs.RefereesDiscountApplied, &eventspb.PartyAmount{
Party: party,
Amount: amount.String(),
Party: party,
Amount: amount.String(),
QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(2).String(),
})
}

Expand All @@ -197,8 +199,9 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
for _, party := range volumeDiscountAppliedParties {
amount := f.VolumeDiscountApplied[party]
fs.VolumeDiscountApplied = append(fs.VolumeDiscountApplied, &eventspb.PartyAmount{
Party: party,
Amount: amount.String(),
Party: party,
Amount: amount.String(),
QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(2).String(),
})
}

Expand All @@ -219,8 +222,9 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
rewardsGenerated.GeneratedReward = append(
rewardsGenerated.GeneratedReward,
&eventspb.PartyAmount{
Party: party,
Amount: amount.String(),
Party: party,
Amount: amount.String(),
QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(2).String(),
},
)
}
Expand All @@ -233,8 +237,9 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
for _, maker := range totalMakerFeesReceivedParties {
amount := f.TotalMakerFeesReceived[maker]
fs.TotalMakerFeesReceived = append(fs.TotalMakerFeesReceived, &eventspb.PartyAmount{
Party: maker,
Amount: amount.String(),
Party: maker,
Amount: amount.String(),
QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(2).String(),
})
}

Expand All @@ -255,8 +260,9 @@ func (f *FeesStats) ToProto(asset string) *eventspb.FeesStats {
rewardsGenerated.MakerFeesPaid = append(
rewardsGenerated.MakerFeesPaid,
&eventspb.PartyAmount{
Party: maker,
Amount: amount.String(),
Party: maker,
Amount: amount.String(),
QuantumAmount: amount.ToDecimal().Div(assetQuantum).Truncate(2).String(),
},
)
}
Expand Down
2 changes: 1 addition & 1 deletion core/integration/steps/the_spot_markets.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func enableSpotMarketAssets(markets []types.Market, collateralEngine *collateral
err := collateralEngine.EnableAsset(context.Background(), types.Asset{
ID: assetToEnable,
Details: &types.AssetDetails{
Quantum: num.DecimalZero(),
Quantum: num.DecimalOne(),
Symbol: assetToEnable,
},
})
Expand Down
6 changes: 5 additions & 1 deletion core/integration/stubs/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ func NewIsAssetStub(id string, dp uint64, quantum *num.Decimal) *assets.Asset {
func (a isAssetStub) Type() *types.Asset {
quantum := num.DecimalFromFloat(5000)
if a.Quantum != nil {
quantum = *a.Quantum
if a.Quantum.IsZero() {
quantum = num.DecimalOne()
} else {
quantum = *a.Quantum
}
}
return &types.Asset{
ID: a.ID,
Expand Down
2 changes: 1 addition & 1 deletion datanode/sqlstore/referral_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ with epoch_range as (select coalesce(max(id) - %d, 0) as start_epoch, coalesce(m
and jsonb_typeof(referees_stats) != 'null'
group by ref_stats->>'party_id'
), ref_period_rewards (party, period_rewards) as (
select decode(gen_rewards->>'party', 'hex'), sum((gen_rewards ->> 'amount')::numeric) as period_rewards
select decode(gen_rewards->>'party', 'hex'), sum((gen_rewards ->> 'quantum_amount')::numeric) as period_rewards
from fees_stats,
jsonb_array_elements(referrer_rewards_generated) as ref_rewards,
jsonb_array_elements(ref_rewards->'generated_reward') as gen_rewards,
Expand Down
2 changes: 2 additions & 0 deletions protos/sources/vega/events/v1/events.proto
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ message PartyAmount {
string party = 1;
// Amount received.
string amount = 2;
// Amount value in quantum.
string quantum_amount = 3;
}

// The updated activity streak of a party at end of epoch
Expand Down
Loading

0 comments on commit e0c750f

Please sign in to comment.