project_tier.gno
2.26 Kb · 72 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
25func depositToTier(t *launchpad.ProjectTier, deposit launchpad.Deposit) {
26 totalDepositAmount := gnsmath.SafeAddInt64(t.TotalDepositAmount(), deposit.DepositAmount())
27 totalDepositCount := gnsmath.SafeAddInt64(t.TotalDepositCount(), 1)
28
29 t.SetTotalDepositAmount(totalDepositAmount)
30 t.SetTotalDepositCount(totalDepositCount)
31}
32
33func withdrawToTier(t *launchpad.ProjectTier, deposit launchpad.Deposit) {
34 totalWithdrawAmount := gnsmath.SafeAddInt64(t.TotalWithdrawAmount(), deposit.DepositAmount())
35 totalWithdrawCount := gnsmath.SafeAddInt64(t.TotalWithdrawCount(), 1)
36
37 t.SetTotalWithdrawAmount(totalWithdrawAmount)
38 t.SetTotalWithdrawCount(totalWithdrawCount)
39}
40
41func updateTierDistributeAmountPerSecond(t *launchpad.ProjectTier) {
42 // Use time duration instead of block count
43 distributeTimeDuration := t.EndTime() - t.StartTime()
44 if distributeTimeDuration <= 0 {
45 return
46 }
47
48 totalDistributeAmountX128, overflow := u256.Zero().MulOverflow(u256.NewUintFromInt64(t.TotalDistributeAmount()), consts.Q128())
49 if overflow {
50 panic(errors.New(errOverflow))
51 }
52
53 // Divide by time duration in seconds
54 distributeAmountPerSecondX128 := u256.Zero().Div(totalDistributeAmountX128, u256.NewUintFromInt64(distributeTimeDuration))
55
56 t.SetDistributeAmountPerSecondX128(distributeAmountPerSecondX128)
57}
58
59// newProjectTier returns a pointer to a new ProjectTier with the given values.
60func newProjectTier(
61 projectID string,
62 tierDuration int64,
63 totalDistributeAmount int64,
64 startTime int64,
65 endTime int64,
66) *launchpad.ProjectTier {
67 tier := launchpad.NewProjectTier(projectID, tierDuration, totalDistributeAmount, startTime, endTime)
68
69 updateTierDistributeAmountPerSecond(tier)
70
71 return tier
72}