diff --git a/x/streamer/types/distr_info.go b/x/streamer/types/distr_info.go index 8429b062a..76d43b503 100644 --- a/x/streamer/types/distr_info.go +++ b/x/streamer/types/distr_info.go @@ -1,6 +1,7 @@ package types import ( + "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" sponsorshiptypes "github.com/dymensionxyz/dymension/v3/x/sponsorship/types" @@ -35,19 +36,38 @@ func (r DistrRecord) ValidateBasic() error { return nil } -// DistrInfoFromDistribution converts sponsorship distribution to the DistrInfo type. -// Returning an empty DistrInfo (with zero DistrInfo.TotalWeight) is a valid scenario. +// DistrInfoFromDistribution converts sponsorship distribution to the DistrInfo type. Returning an empty +// DistrInfo (with zero DistrInfo.TotalWeight) is a valid scenario. Note that some part of the distribution +// might be abstained. In that case, the sum of gauge powers would be less than the total distribution weight. +// +// Example! Let's say we have the following distribution in the sponsorship: +// +// Total: 100 +// Gauge1: 30% +// Gauge2: 50% +// Abstained: 20% +// +// We want to distribute 100 DYM according to this distribution. Since 20% is abstained, we will normalize the figures +// for Gauge1 and Gauge2. The total "active" voting power is 80%, which comes from 30% (Gauge1) + 50% (Gauge2). +// +// Gauge1: 30% / 80% = 37.5% (in the new distribution) +// Gauge2: 50% / 80% = 62.5% (in the new distribution) +// +// So, Gauge1 gets 37.5 DYM, and Gauge2 gets 62.5 DYM. func DistrInfoFromDistribution(d sponsorshiptypes.Distribution) *DistrInfo { + totalWeight := math.ZeroInt() + records := make([]DistrRecord, 0, len(d.Gauges)) for _, g := range d.Gauges { records = append(records, DistrRecord{ GaugeId: g.GaugeId, Weight: g.Power, }) + totalWeight = totalWeight.Add(g.Power) } return &DistrInfo{ - TotalWeight: d.VotingPower, + TotalWeight: totalWeight, Records: records, } } diff --git a/x/streamer/types/distr_info_test.go b/x/streamer/types/distr_info_test.go index 3bb7405fa..28b805186 100644 --- a/x/streamer/types/distr_info_test.go +++ b/x/streamer/types/distr_info_test.go @@ -74,6 +74,17 @@ func TestDistrInfoFromDistribution(t *testing.T) { }, }, }, + { + name: "Distribution with empty gauges", + distr: sponsorshiptypes.Distribution{ + VotingPower: sdk.NewInt(30), + Gauges: []sponsorshiptypes.Gauge{}, + }, + expDistr: &types.DistrInfo{ + TotalWeight: sdk.ZeroInt(), + Records: []types.DistrRecord{}, + }, + }, { name: "Distribution with abstained gauge", distr: sponsorshiptypes.Distribution{ @@ -91,7 +102,7 @@ func TestDistrInfoFromDistribution(t *testing.T) { }, }, expDistr: &types.DistrInfo{ - TotalWeight: sdk.NewInt(100), + TotalWeight: sdk.NewInt(70), Records: []types.DistrRecord{ // 30 is abstained {