Search Apps Documentation Source Content File Folder Download Copy Actions Download

reward_state.gno

1.42 Kb · 52 lines
 1package v1
 2
 3import (
 4	"gno.land/r/gnoswap/launchpad"
 5
 6	u256 "gno.land/p/gnoswap/uint256"
 7)
 8
 9// Helper functions for RewardState
10
11func isRewardStateClaimable(r *launchpad.RewardState, currentTime int64) bool {
12	return currentTime >= r.ClaimableTime()
13}
14
15// calculateReward calculates the total reward amount based on
16// the accumulated reward per deposit.
17// Returns the total reward amount.
18func calculateReward(r *launchpad.RewardState, accumRewardPerDepositX128 *u256.Uint) int64 {
19	if accumRewardPerDepositX128 == nil || r.PriceDebtX128() == nil {
20		return 0
21	}
22
23	actualRewardPerDepositX128 := u256.Zero().Sub(accumRewardPerDepositX128, r.PriceDebtX128())
24	if actualRewardPerDepositX128.IsZero() {
25		return 0
26	}
27
28	reward, overflow := u256.Zero().MulOverflow(actualRewardPerDepositX128, u256.NewUintFromInt64(r.DepositAmount()))
29	if overflow {
30		panic(errOverflow)
31	}
32	reward = reward.Rsh(reward, 128)
33	return safeConvertToInt64(reward)
34}
35
36// calculateClaimableReward calculates the amount of reward that can be claimed
37// based on the current accumulated reward per deposit.
38// Returns the amount of reward that can be claimed.
39func calculateClaimableReward(r *launchpad.RewardState, accumRewardPerDepositX128 *u256.Uint) int64 {
40	if accumRewardPerDepositX128 == nil {
41		return 0
42	}
43
44	reward := calculateReward(r, accumRewardPerDepositX128)
45	claimedAmount := r.ClaimedAmount()
46
47	if reward <= claimedAmount {
48		return 0
49	}
50
51	return reward - claimedAmount
52}