diff --git a/emission/distribution.gno b/emission/distribution.gno index 5d2a0a9ef..cba3e7592 100644 --- a/emission/distribution.gno +++ b/emission/distribution.gno @@ -4,7 +4,6 @@ import ( "std" "strconv" - "gno.land/r/gnoswap/v1/common" "gno.land/r/gnoswap/v1/consts" "gno.land/r/gnoswap/v1/gns" @@ -46,7 +45,7 @@ func init() { distributionBpsPct.Set(strconv.Itoa(LIQUIDITY_STAKER), uint64(7500)) distributionBpsPct.Set(strconv.Itoa(DEVOPS), uint64(2000)) distributionBpsPct.Set(strconv.Itoa(COMMUNITY_POOL), uint64(500)) - distributionBpsPct.Set(strconv.Itoa(GOV_STAKER), uint64(0)) + distributionBpsPct.Set(strconv.Itoa(GOV_STAKER), uint64(0)) } // ChangeDistributionPctByAdmin changes the distribution percentage for the given targets. @@ -112,7 +111,7 @@ func changeDistributionPcts( target04 int, pct04 uint64, ) { // First, cache the percentage of the staker just before it changes Callback if needed - // (check if the LIQUIDITY_STAKER was located between target01 and 04) + // (check if the LIQUIDITY_STAKER was located between target01 and 04) setDistributionBpsPct(target01, pct01) setDistributionBpsPct(target02, pct02) setDistributionBpsPct(target03, pct03) @@ -148,8 +147,8 @@ func distributeToTarget(amount uint64) uint64 { )) } - pct := iPct.(uint64) - distAmount := calculateAmount(amount, pct) + pct := iPct.(int) + distAmount := calculateAmount(amount, uint64(pct)) totalSent += distAmount transferToTarget(targetInt, distAmount) @@ -262,12 +261,20 @@ func ClearDistributedToGovStaker() { func setDistributionBpsPct(target int, pct uint64) { if target == LIQUIDITY_STAKER { - oldPct, exist := distributionBpsPct.Get(strconv.Itoa(target)) + oldPctI, exist := distributionBpsPct.Get(strconv.Itoa(target)) if !exist { panic("should not happen") } - - if oldPct.(uint64) != pct { + + oldPct := oldPctI.(int) + if uint64(oldPct) != pct { + if callbackStakerEmissionChange == nil { + panic(addDetailToError( + errCallbackIsNil, + "callbackStakerEmissionChange is not set", + )) + } + callbackStakerEmissionChange(calculateAmount(gns.GetEmission(), pct)) } } diff --git a/emission/distribution_test.gno b/emission/distribution_test.gno index a2deb2bfa..5ee48d513 100644 --- a/emission/distribution_test.gno +++ b/emission/distribution_test.gno @@ -13,6 +13,9 @@ import ( func TestChangeDistributionPctByAdmin(t *testing.T) { resetObject(t) + originCallback := callbackStakerEmissionChange + callbackStakerEmissionChange = func(amount uint64) {} + tests := []struct { name string shouldPanic bool @@ -53,10 +56,10 @@ func TestChangeDistributionPctByAdmin(t *testing.T) { targets: []int{1, 2, 3, 4}, pcts: []uint64{1000, 2000, 3000, 4000}, verify: func() { - uassert.Equal(t, uint64(1000), GetDistributionBpsPct(1)) - uassert.Equal(t, uint64(2000), GetDistributionBpsPct(2)) - uassert.Equal(t, uint64(3000), GetDistributionBpsPct(3)) - uassert.Equal(t, uint64(4000), GetDistributionBpsPct(4)) + uassert.Equal(t, uint64(1000), GetDistributionBpsPct(int(1))) + uassert.Equal(t, uint64(2000), GetDistributionBpsPct(int(2))) + uassert.Equal(t, uint64(3000), GetDistributionBpsPct(int(3))) + uassert.Equal(t, uint64(4000), GetDistributionBpsPct(int(4))) }, }, } @@ -96,11 +99,15 @@ func TestChangeDistributionPctByAdmin(t *testing.T) { } }) } + callbackStakerEmissionChange = originCallback } func TestChangeDistributionPct(t *testing.T) { resetObject(t) + originCallback := callbackStakerEmissionChange + callbackStakerEmissionChange = func(amount uint64) {} + tests := []struct { name string shouldPanic bool @@ -184,11 +191,15 @@ func TestChangeDistributionPct(t *testing.T) { } }) } + callbackStakerEmissionChange = originCallback } func TestChangeDistributionPcts(t *testing.T) { resetObject(t) + originCallback := callbackStakerEmissionChange + callbackStakerEmissionChange = func(amount uint64) {} + changeDistributionPcts( 1, 1000, 2, 2000, @@ -199,6 +210,8 @@ func TestChangeDistributionPcts(t *testing.T) { uassert.Equal(t, uint64(2000), GetDistributionBpsPct(2)) uassert.Equal(t, uint64(3000), GetDistributionBpsPct(3)) uassert.Equal(t, uint64(4000), GetDistributionBpsPct(4)) + + callbackStakerEmissionChange = originCallback } func TestCalculateAmount(t *testing.T) { diff --git a/emission/errors.gno b/emission/errors.gno index f97851de5..c657d0916 100644 --- a/emission/errors.gno +++ b/emission/errors.gno @@ -7,7 +7,7 @@ import ( ) var ( - errNoPermission = errors.New("[GNOSWAP-EMISSION-001] caller has no permission") + errCallbackIsNil = errors.New("[GNOSWAP-EMISSION-001] callback func is nil") errInvalidEmissionTarget = errors.New("[GNOSWAP-EMISSION-002] invalid emission target") errInvalidEmissionPct = errors.New("[GNOSWAP-EMISSION-003] invalid emission percentage") )