Skip to content

Commit

Permalink
fix fee protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
mconcat committed Jan 9, 2025
1 parent 2dc6624 commit 2bac374
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 130 deletions.
24 changes: 12 additions & 12 deletions gov/staker/api_staker.gno
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,31 @@ func GetLockedInfoByAddress(addr std.Address) string {
func GetClaimableRewardByAddress(addr std.Address) string {
en.MintAndDistributeGns()

emissionReward, exist := userEmissionReward.Get(addr.String())
if !exist {
rewardState.finalize(getCurrentBalance(), getCurrentProtocolFeeBalance())

emissionReward, protocolFeeRewards := rewardState.CalculateReward(addr)

if emissionReward == 0 && len(protocolFeeRewards) == 0 {
return ""
}

data := json.Builder().
WriteString("height", formatInt(std.GetHeight())).
WriteString("now", formatInt(time.Now().Unix())).
WriteString("emissionReward", formatUint(emissionReward.(uint64))).
WriteString("emissionReward", formatUint(emissionReward)).
Node()

protocolFees, exist := userProtocolFeeReward.Get(addr.String())
if exist {
if len(protocolFeeRewards) > 0 {
pfArr := json.ArrayNode("", nil)
protocolFees.(*avl.Tree).Iterate("", "", func(key string, value interface{}) bool {
amount := value.(uint64)
if amount > 0 {
for tokenPath, protocolFeeReward := range protocolFeeRewards {
if protocolFeeReward > 0 {
pfObj := json.Builder().
WriteString("tokenPath", key).
WriteString("amount", formatUint(amount)).
WriteString("tokenPath", tokenPath).
WriteString("amount", formatUint(protocolFeeReward)).
Node()
pfArr.AppendArray(pfObj)
}
return false
})
}

data.AppendObject("protocolFees", pfArr)
}
Expand Down
24 changes: 19 additions & 5 deletions gov/staker/api_staker_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,40 @@ func TestGetLockedInfoByAddress_EmptyLocks(t *testing.T) {

func TestGetClaimableRewardByAddress(t *testing.T) {
addr := testutils.TestAddress("claimable_test")
userEmissionReward.Set(addr.String(), uint64(1000))

pfTree := avl.NewTree()
pfTree.Set("token1:token2", uint64(500))
pfTree.Set("token2:token3", uint64(300))
userProtocolFeeReward.Set(addr.String(), pfTree)
rewardState.AddStake(uint64(std.GetHeight()), addr, 100, 0, nil)

currentGNSBalance = 1000
// userEmissionReward.Set(addr.String(), uint64(1000))

currentProtocolFeeBalance["token1:token2"] = 500
currentProtocolFeeBalance["token2:token3"] = 300
//pfTree := avl.NewTree()
//pfTree.Set("token1:token2", uint64(500))
//pfTree.Set("token2:token3", uint64(300))
//userProtocolFeeReward.Set(addr.String(), pfTree)

result := GetClaimableRewardByAddress(addr)

node := json.Must(json.Unmarshal([]byte(result)))


println("4444")
uassert.True(t, node.HasKey("height"))
uassert.True(t, node.HasKey("now"))

println("3333")
emissionReward, err := node.MustKey("emissionReward").GetString()
uassert.NoError(t, err)
uassert.Equal(t, emissionReward, "1000")

println("2222")

protocolFees := node.MustKey("protocolFees")
uassert.True(t, protocolFees.IsArray())

println("1111")

protocolFees.ArrayEach(func(i int, fee *json.Node) {
tokenPath, err := fee.MustKey("tokenPath").GetString()
uassert.NoError(t, err)
Expand All @@ -116,5 +129,6 @@ func TestGetClaimableRewardByAddress_NoRewards(t *testing.T) {
addr := testutils.TestAddress("no_reward_test")

result := GetClaimableRewardByAddress(addr)
println("result", result)
uassert.Equal(t, result, "")
}
Loading

0 comments on commit 2bac374

Please sign in to comment.