Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

project_tier.gno

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