Skip to content

Commit

Permalink
fix: callbackStakerEmissionChange is nil
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyhyde committed Jan 16, 2025
1 parent 1b55315 commit b89a6b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
23 changes: 15 additions & 8 deletions emission/distribution.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
}
Expand Down
21 changes: 17 additions & 4 deletions emission/distribution_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
func TestChangeDistributionPctByAdmin(t *testing.T) {
resetObject(t)

originCallback := callbackStakerEmissionChange
callbackStakerEmissionChange = func(amount uint64) {}

tests := []struct {
name string
shouldPanic bool
Expand Down Expand Up @@ -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)))
},
},
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion emission/errors.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)
Expand Down

0 comments on commit b89a6b3

Please sign in to comment.