project_tier.gno
2.45 Kb · 77 lines
1package launchpad
2
3import (
4 "errors"
5
6 "gno.land/r/gnoswap/launchpad"
7
8 "gno.land/p/gnoswap/consts/v1"
9 gnsmath "gno.land/p/gnoswap/gnsmath/v1"
10 u256 "gno.land/p/gnoswap/uint256/v1"
11)
12
13func getTierCurrentDepositCount(t *launchpad.ProjectTier) int64 {
14 return gnsmath.SafeSubInt64(t.TotalDepositCount(), t.TotalWithdrawCount())
15}
16
17func getTierCurrentDepositAmount(t *launchpad.ProjectTier) int64 {
18 return gnsmath.SafeSubInt64(t.TotalDepositAmount(), t.TotalWithdrawAmount())
19}
20
21func getCalculatedLeftReward(t *launchpad.ProjectTier) int64 {
22 return gnsmath.SafeSubInt64(t.TotalDistributeAmount(), t.TotalCollectedAmount())
23}
24
25// isTierDepositable allows deposits from project creation until the tier ends.
26func isTierDepositable(t *launchpad.ProjectTier, currentTime int64) bool {
27 return currentTime < t.EndTime()
28}
29
30func depositToTier(t *launchpad.ProjectTier, deposit launchpad.Deposit) {
31 totalDepositAmount := gnsmath.SafeAddInt64(t.TotalDepositAmount(), deposit.DepositAmount())
32 totalDepositCount := gnsmath.SafeAddInt64(t.TotalDepositCount(), 1)
33
34 t.SetTotalDepositAmount(totalDepositAmount)
35 t.SetTotalDepositCount(totalDepositCount)
36}
37
38func withdrawToTier(t *launchpad.ProjectTier, deposit launchpad.Deposit) {
39 totalWithdrawAmount := gnsmath.SafeAddInt64(t.TotalWithdrawAmount(), deposit.DepositAmount())
40 totalWithdrawCount := gnsmath.SafeAddInt64(t.TotalWithdrawCount(), 1)
41
42 t.SetTotalWithdrawAmount(totalWithdrawAmount)
43 t.SetTotalWithdrawCount(totalWithdrawCount)
44}
45
46func updateTierDistributeAmountPerSecond(t *launchpad.ProjectTier) {
47 // Use time duration instead of block count
48 distributeTimeDuration := t.EndTime() - t.StartTime()
49 if distributeTimeDuration <= 0 {
50 return
51 }
52
53 totalDistributeAmountX128, overflow := u256.Zero().MulOverflow(u256.NewUintFromInt64(t.TotalDistributeAmount()), consts.Q128())
54 if overflow {
55 panic(errors.New(errOverflow))
56 }
57
58 // Divide by time duration in seconds
59 distributeAmountPerSecondX128 := u256.Zero().Div(totalDistributeAmountX128, u256.NewUintFromInt64(distributeTimeDuration))
60
61 t.SetDistributeAmountPerSecondX128(distributeAmountPerSecondX128)
62}
63
64// newProjectTier returns a pointer to a new ProjectTier with the given values.
65func newProjectTier(
66 projectID string,
67 tierDuration int64,
68 totalDistributeAmount int64,
69 startTime int64,
70 endTime int64,
71) *launchpad.ProjectTier {
72 tier := launchpad.NewProjectTier(projectID, tierDuration, totalDistributeAmount, startTime, endTime)
73
74 updateTierDistributeAmountPerSecond(tier)
75
76 return tier
77}