Skip to content

Commit

Permalink
Merge branch 'develop' into fix_ti_schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ze97286 authored May 13, 2024
2 parents 8286d1c + e5c5078 commit 014fbde
Show file tree
Hide file tree
Showing 13 changed files with 1,374 additions and 795 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

### 🐛 Fixes

- [](https://github.com/vegaprotocol/vega/issues/xxx)
- [11066](https://github.com/vegaprotocol/vega/issues/11066) - Ensure vesting statistics match vesting accounts numbers.


## 0.76.1
Expand Down
22 changes: 10 additions & 12 deletions core/collateral/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,8 @@ func (e *Engine) GetPartyBalance(party string) *num.Uint {
return num.UintZero()
}

func (e *Engine) GetAllVestingQuantumBalance(party string) *num.Uint {
balance := num.UintZero()
func (e *Engine) GetAllVestingQuantumBalance(party string) num.Decimal {
balance := num.DecimalZero()

for asset, details := range e.enabledAssets {
// vesting balance
Expand All @@ -229,14 +229,14 @@ func (e *Engine) GetAllVestingQuantumBalance(party string) *num.Uint {
quantum = details.Details.Quantum
}
if acc, ok := e.accs[e.accountID(noMarket, party, asset, types.AccountTypeVestingRewards)]; ok {
quantumBalance, _ := num.UintFromDecimal(acc.Balance.ToDecimal().Div(quantum))
balance.AddSum(quantumBalance)
quantumBalance := acc.Balance.ToDecimal().Div(quantum)
balance = balance.Add(quantumBalance)
}

// vested balance
if acc, ok := e.accs[e.accountID(noMarket, party, asset, types.AccountTypeVestedRewards)]; ok {
quantumBalance, _ := num.UintFromDecimal(acc.Balance.ToDecimal().Div(quantum))
balance.AddSum(quantumBalance)
quantumBalance := acc.Balance.ToDecimal().Div(quantum)
balance = balance.Add(quantumBalance)
}
}

Expand Down Expand Up @@ -863,9 +863,7 @@ func (e *Engine) TransferRewards(ctx context.Context, rewardAccountID string, tr
return responses, nil
}

func (e *Engine) TransferVestedRewards(
ctx context.Context, transfers []*types.Transfer,
) ([]*types.LedgerMovement, error) {
func (e *Engine) TransferVestedRewards(ctx context.Context, transfers []*types.Transfer) ([]*types.LedgerMovement, error) {
if len(transfers) == 0 {
return nil, nil
}
Expand Down Expand Up @@ -3640,10 +3638,10 @@ func (e *Engine) CreatePartyGeneralAccount(ctx context.Context, partyID, asset s
return generalID, nil
}

// GetOrCreatePartyVestingAccount create the general account for a party.
// GetOrCreatePartyVestingRewardAccount create the general account for a party.
func (e *Engine) GetOrCreatePartyVestingRewardAccount(ctx context.Context, partyID, asset string) *types.Account {
if !e.AssetExists(asset) {
e.log.Panic("trying to use a nonexisting asset for reward accounts, something went very wrong somewhere",
e.log.Panic("trying to use a non-existent asset for reward accounts, something went very wrong somewhere",
logging.String("asset-id", asset))
}

Expand Down Expand Up @@ -3673,7 +3671,7 @@ func (e *Engine) GetPartyVestedRewardAccount(partyID, asset string) (*types.Acco
return e.GetAccountByID(vested)
}

// GetOrCreatePartyVestedAccount create the general account for a party.
// GetOrCreatePartyVestedRewardAccount create the general account for a party.
func (e *Engine) GetOrCreatePartyVestedRewardAccount(ctx context.Context, partyID, asset string) *types.Account {
if !e.AssetExists(asset) {
e.log.Panic("trying to use a nonexisting asset for reward accounts, something went very wrong somewhere",
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 @@ -149,7 +149,7 @@ func TestGetAllVestingQuantumBalance(t *testing.T) {
party := "party1"

balance := eng.GetAllVestingQuantumBalance(party)
assert.Equal(t, int(balance.Uint64()), 0)
assert.Equal(t, balance.String(), "0")

assetT := types.Asset{
ID: "USDC",
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestGetAllVestingQuantumBalance(t *testing.T) {
)

balance = eng.GetAllVestingQuantumBalance(party)
assert.Equal(t, int(balance.Uint64()), 100)
assert.Equal(t, balance.String(), "100")

// add some more of the other account
// now add some balance to an asset
Expand All @@ -190,7 +190,7 @@ func TestGetAllVestingQuantumBalance(t *testing.T) {
)

balance = eng.GetAllVestingQuantumBalance(party)
assert.Equal(t, int(balance.Uint64()), 103)
assert.Equal(t, balance.String(), "103.1666666666666667")
}

func testClearFeeAccounts(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion core/rewards/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ type Teams interface {

type Vesting interface {
AddReward(party, asset string, amount *num.Uint, lockedForEpochs uint64)
GetRewardBonusMultiplier(party string) (*num.Uint, num.Decimal)
GetRewardBonusMultiplier(party string) (num.Decimal, num.Decimal)
}

type ActivityStreak interface {
Expand Down
4 changes: 2 additions & 2 deletions core/rewards/mocks/mocks.go

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

66 changes: 25 additions & 41 deletions core/vesting/vesting.go → core/vesting/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ import (
"golang.org/x/exp/slices"
)

//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/vesting Collateral,ActivityStreakVestingMultiplier,Broker,Assets
//go:generate go run github.com/golang/mock/mockgen -destination mocks/mocks.go -package mocks code.vegaprotocol.io/vega/core/vesting ActivityStreakVestingMultiplier,Assets

type Collateral interface {
TransferVestedRewards(
ctx context.Context, transfers []*types.Transfer,
) ([]*types.LedgerMovement, error)
TransferVestedRewards(ctx context.Context, transfers []*types.Transfer) ([]*types.LedgerMovement, error)
GetVestingRecovery() map[string]map[string]*num.Uint
GetAllVestingQuantumBalance(party string) *num.Uint
GetVestingAccounts() []*types.Account
GetAllVestingQuantumBalance(party string) num.Decimal
}

type ActivityStreakVestingMultiplier interface {
Expand Down Expand Up @@ -111,9 +108,7 @@ func (e *Engine) OnCheckpointLoaded() {
}
}

func (e *Engine) OnBenefitTiersUpdate(
_ context.Context, v interface{},
) error {
func (e *Engine) OnBenefitTiersUpdate(_ context.Context, v interface{}) error {
tiers, err := types.VestingBenefitTiersFromUntypedProto(v)
if err != nil {
return err
Expand All @@ -126,31 +121,27 @@ func (e *Engine) OnBenefitTiersUpdate(
return nil
}

func (e *Engine) OnRewardVestingBaseRateUpdate(
_ context.Context, baseRate num.Decimal,
) error {
func (e *Engine) OnRewardVestingBaseRateUpdate(_ context.Context, baseRate num.Decimal) error {
e.baseRate = baseRate
return nil
}

func (e *Engine) OnRewardVestingMinimumTransferUpdate(
_ context.Context, minimumTransfer num.Decimal,
) error {
func (e *Engine) OnRewardVestingMinimumTransferUpdate(_ context.Context, minimumTransfer num.Decimal) error {
e.minTransfer = minimumTransfer
return nil
}

func (e *Engine) OnEpochEvent(ctx context.Context, epoch types.Epoch) {
if epoch.Action == proto.EpochAction_EPOCH_ACTION_END {
e.broadcastRewardBonusMultipliers(ctx, epoch.Seq)
e.moveLocked()
e.distributeVested(ctx)
e.clearup()
e.broadcastVestingStatsUpdate(ctx, epoch.Seq)
e.broadcastSummary(ctx, epoch.Seq)
e.clearState()
}
}

func (e *Engine) OnEpochRestore(ctx context.Context, epoch types.Epoch) {
func (e *Engine) OnEpochRestore(_ context.Context, epoch types.Epoch) {
e.epochSeq = epoch.Seq
}

Expand All @@ -161,24 +152,20 @@ func (e *Engine) AddReward(
) {
// no locktime, just increase the amount in vesting
if lockedForEpochs == 0 {
e.increaseVestingBalance(
party, asset, amount,
)
e.increaseVestingBalance(party, asset, amount)
return
}

e.increaseLockedForAsset(
party, asset, amount, lockedForEpochs,
)
e.increaseLockedForAsset(party, asset, amount, lockedForEpochs)
}

func (e *Engine) GetRewardBonusMultiplier(party string) (*num.Uint, num.Decimal) {
func (e *Engine) GetRewardBonusMultiplier(party string) (num.Decimal, num.Decimal) {
quantumBalance := e.c.GetAllVestingQuantumBalance(party)

multiplier := num.DecimalOne()

for _, b := range e.benefitTiers {
if quantumBalance.LT(b.MinimumQuantumBalance) {
if quantumBalance.LessThan(num.DecimalFromUint(b.MinimumQuantumBalance)) {
break
}

Expand Down Expand Up @@ -234,7 +221,7 @@ func (e *Engine) increaseVestingBalance(
partyRewards.Vesting[asset] = vesting
}

// checkLocked will move around locked funds.
// moveLocked will move around locked funds.
// if the lock for epoch reach 0, the full amount
// is added to the vesting amount for the asset.
func (e *Engine) moveLocked() {
Expand Down Expand Up @@ -272,9 +259,7 @@ func (e *Engine) distributeVested(ctx context.Context) {
sort.Strings(assets)
for _, asset := range assets {
balance := rewards.Vesting[asset]
transfer := e.makeTransfer(
party, asset, balance.Clone(),
)
transfer := e.makeTransfer(party, asset, balance.Clone())

// we are clearing the account,
// we can delete it.
Expand All @@ -289,7 +274,7 @@ func (e *Engine) distributeVested(ctx context.Context) {
}

// nothing to be done
if len(transfers) <= 0 {
if len(transfers) == 0 {
return
}

Expand Down Expand Up @@ -343,10 +328,9 @@ func (e *Engine) makeTransfer(
return transfer
}

// just remove party entries once they are not needed anymore.
func (e *Engine) clearup() {
func (e *Engine) clearState() {
for party, v := range e.state {
if len(v.Locked) <= 0 && len(v.Vesting) <= 0 {
if len(v.Locked) == 0 && len(v.Vesting) == 0 {
delete(e.state, party)
}
}
Expand All @@ -358,13 +342,11 @@ func (e *Engine) broadcastSummary(ctx context.Context, seq uint64) {
PartiesVestingSummary: []*eventspb.PartyVestingSummary{},
}

parties := make([]string, 0, len(e.state))
for k := range e.state {
parties = append(parties, k)
}
sort.Strings(parties)

for p, pRewards := range e.state {
if len(pRewards.Vesting) == 0 && len(pRewards.Locked) == 0 {
continue
}

pSummary := &eventspb.PartyVestingSummary{
Party: p,
PartyLockedBalances: []*eventspb.PartyLockedBalance{},
Expand Down Expand Up @@ -416,7 +398,7 @@ func (e *Engine) broadcastSummary(ctx context.Context, seq uint64) {
e.broker.Send(events.NewVestingBalancesSummaryEvent(ctx, evt))
}

func (e *Engine) broadcastRewardBonusMultipliers(ctx context.Context, seq uint64) {
func (e *Engine) broadcastVestingStatsUpdate(ctx context.Context, seq uint64) {
evt := &eventspb.VestingStatsUpdated{
AtEpoch: seq,
Stats: make([]*eventspb.PartyVestingStats, 0, len(e.state)),
Expand All @@ -427,6 +409,8 @@ func (e *Engine) broadcastRewardBonusMultipliers(ctx context.Context, seq uint64

for _, party := range parties {
quantumBalance, multiplier := e.GetRewardBonusMultiplier(party)
// To avoid excessively large decimals.
quantumBalance.Round(2)
evt.Stats = append(evt.Stats, &eventspb.PartyVestingStats{
PartyId: party,
RewardBonusMultiplier: multiplier.String(),
Expand Down
Loading

0 comments on commit 014fbde

Please sign in to comment.