Skip to content

Commit 9ce175b

Browse files
committed
reward states
1 parent 515289b commit 9ce175b

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

contract/p/gnoswap/launchpad/reward_calculation.gno

+57
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55

66
"gno.land/p/demo/avl"
7+
"gno.land/p/demo/ufmt"
78

89
u256 "gno.land/p/gnoswap/uint256"
910
)
@@ -249,6 +250,62 @@ func (rs *RewardState) RemoveStake(depositId string, amount uint64, currentHeigh
249250
return reward, nil
250251
}
251252

253+
// region: RewardStates
254+
255+
type RewardStates struct {
256+
states *avl.Tree // projectId:tier string -> RewardState
257+
}
258+
259+
func NewRewardStates() RewardStates {
260+
return RewardStates{
261+
states: avl.NewTree(),
262+
}
263+
}
264+
265+
func (rs *RewardStates) Get(pid, tier string) (*RewardState, error) {
266+
key := createId(pid, tier)
267+
statesI, exists := rs.states.Get(key)
268+
if !exists {
269+
return nil, ufmt.Errorf("reward state not found for projectId %s and tierStr %s", pid, tier)
270+
}
271+
return statesI.(*RewardState), nil
272+
}
273+
274+
func (rs *RewardStates) Set(pid, tier string, state *RewardState) error {
275+
key := createId(pid, tier)
276+
ok := rs.states.Set(key, state)
277+
if !ok {
278+
return ufmt.Errorf("failed to set reward state for projectId %s and tierStr %s", pid, tier)
279+
}
280+
return nil
281+
}
282+
283+
func (rs *RewardStates) DeleteProject(pid string) uint64 {
284+
totalLeftOver := uint64(0)
285+
286+
keys := make([]string, 0)
287+
rs.states.Iterate(pid+":", pid+";", func(key string, value interface{}) bool {
288+
state := value.(*RewardState)
289+
totalEmptyBlock := state.TotalEmptyBlock()
290+
if state.TotalStake() == 0 {
291+
totalEmptyBlock += state.EndHeight() - state.LastHeight()
292+
}
293+
294+
totalEmptyRewards := u256.Zero().Mul(u256.NewUint(totalEmptyBlock), state.RewardPerBlock())
295+
totalLeftOver += totalEmptyRewards.Uint64()
296+
keys = append(keys, key)
297+
return false
298+
})
299+
300+
for _, key := range keys {
301+
rs.states.Remove(key)
302+
}
303+
304+
return totalLeftOver
305+
}
306+
307+
// region: depositInfo
308+
252309
// Helper struct for deposit info
253310
type depositInfo struct {
254311
id string

0 commit comments

Comments
 (0)