delegation_withdraw.gno
5.77 Kb · 192 lines
1package staker
2
3import (
4 gnsmath "gno.land/p/gnoswap/gnsmath/v1"
5)
6
7// DelegationWithdraw represents a pending withdrawal from a delegation.
8// This struct tracks undelegated amounts that are subject to lockup periods
9// and manages the collection process once the lockup period expires.
10type DelegationWithdraw struct {
11 // delegationID is the unique identifier of the associated delegation
12 delegationID int64
13 // unDelegateAmount is the total amount that was undelegated
14 unDelegateAmount int64
15 // unDelegatedHeight is the height when the undelegation occurred
16 unDelegatedHeight int64
17 // unDelegatedAt is the timestamp when the undelegation occurred
18 unDelegatedAt int64
19 // collectedAmount is the amount that has already been collected
20 collectedAmount int64
21 // collectableTime is the timestamp when collection becomes available
22 collectableTime int64
23 // collectedAt is the timestamp when collection occurred
24 collectedAt int64
25 // collected indicates whether the withdrawal has been fully collected
26 collected bool
27}
28
29// DelegationID returns the unique identifier of the associated delegation.
30//
31// Returns:
32// - int64: delegation ID
33func (d *DelegationWithdraw) DelegationID() int64 {
34 return d.delegationID
35}
36
37// UnDelegateAmount returns the total amount that was undelegated.
38//
39// Returns:
40// - int64: undelegated amount
41func (d *DelegationWithdraw) UnDelegateAmount() int64 {
42 return d.unDelegateAmount
43}
44
45// UnDelegatedAt returns the timestamp when the undelegation occurred.
46//
47// Returns:
48// - int64: undelegation timestamp
49func (d *DelegationWithdraw) UnDelegatedAt() int64 {
50 return d.unDelegatedAt
51}
52
53// UnDelegatedHeight returns the height when the undelegation occurred.
54//
55// Returns:
56// - int64: undelegation height
57func (d *DelegationWithdraw) UnDelegatedHeight() int64 {
58 return d.unDelegatedHeight
59}
60
61// CollectedAmount returns the amount that has already been collected.
62//
63// Returns:
64// - int64: collected amount
65func (d *DelegationWithdraw) CollectedAmount() int64 {
66 return d.collectedAmount
67}
68
69// CollectableTime returns the timestamp when collection becomes available.
70//
71// Returns:
72// - int64: collectable time
73func (d *DelegationWithdraw) CollectableTime() int64 {
74 return d.collectableTime
75}
76
77// CollectedAt returns the timestamp when collection occurred.
78//
79// Returns:
80// - int64: collection timestamp
81func (d *DelegationWithdraw) CollectedAt() int64 {
82 return d.collectedAt
83}
84
85// IsCollected returns whether the withdrawal has been fully collected.
86//
87// Returns:
88// - bool: true if fully collected, false otherwise
89func (d *DelegationWithdraw) IsCollected() bool {
90 return d.collected
91}
92
93// SetCollected sets the collected status.
94//
95// Parameters:
96// - collected: new collected status
97func (d *DelegationWithdraw) SetCollected(collected bool) {
98 d.collected = collected
99}
100
101// SetCollectedAt sets the collection timestamp.
102//
103// Parameters:
104// - collectedAt: collection timestamp
105func (d *DelegationWithdraw) SetCollectedAt(collectedAt int64) {
106 d.collectedAt = collectedAt
107}
108
109// SetCollectedAmount sets the collected amount.
110//
111// Parameters:
112// - amount: collected amount
113func (d *DelegationWithdraw) SetCollectedAmount(amount int64) {
114 d.collectedAmount = amount
115}
116
117// NewDelegationWithdraw creates a new delegation withdrawal with lockup period.
118// The withdrawal becomes collectable after the lockup period expires.
119//
120// Parameters:
121// - delegationID: Unique identifier of the associated delegation.
122// - unDelegateAmount: Amount being withdrawn.
123// - createdHeight: Block height at which the undelegation was created.
124// - createdAt: Unix timestamp at which the withdrawal was created.
125// - unDelegationLockupPeriod: Lockup duration in seconds.
126//
127// Returns:
128// - DelegationWithdraw: new withdrawal instance with its collectable time set after the lockup.
129func NewDelegationWithdraw(
130 delegationID,
131 unDelegateAmount,
132 createdHeight,
133 createdAt,
134 unDelegationLockupPeriod int64,
135) DelegationWithdraw {
136 return DelegationWithdraw{
137 delegationID: delegationID,
138 unDelegateAmount: unDelegateAmount,
139 unDelegatedHeight: createdHeight,
140 unDelegatedAt: createdAt,
141 collectableTime: gnsmath.SafeAddInt64(createdAt, unDelegationLockupPeriod),
142 collectedAmount: 0,
143 collectedAt: 0,
144 collected: false,
145 }
146}
147
148// NewDelegationWithdrawWithoutLockup creates a withdrawal that is immediately collectable.
149// This is used for special cases such as redelegation where no lockup is required.
150//
151// Parameters:
152// - delegationID: Unique identifier of the associated delegation.
153// - unDelegateAmount: Amount being withdrawn.
154// - createdHeight: Block height at which the undelegation was created.
155// - createdAt: Unix timestamp at which the withdrawal was created.
156//
157// Returns:
158// - DelegationWithdraw: new withdrawal instance marked collected with collectable time equal to createdAt.
159func NewDelegationWithdrawWithoutLockup(
160 delegationID,
161 unDelegateAmount,
162 createdHeight,
163 createdAt int64,
164) DelegationWithdraw {
165 return DelegationWithdraw{
166 delegationID: delegationID,
167 unDelegateAmount: unDelegateAmount,
168 unDelegatedHeight: createdHeight,
169 unDelegatedAt: createdAt,
170 collectableTime: createdAt,
171 collectedAmount: unDelegateAmount, // Immediately collected
172 collectedAt: createdAt,
173 collected: true,
174 }
175}
176
177// Clone creates a deep copy of the delegation withdraw.
178//
179// Returns:
180// - DelegationWithdraw: independent copy of the withdrawal state.
181func (d *DelegationWithdraw) Clone() DelegationWithdraw {
182 return DelegationWithdraw{
183 delegationID: d.delegationID,
184 unDelegateAmount: d.unDelegateAmount,
185 unDelegatedHeight: d.unDelegatedHeight,
186 unDelegatedAt: d.unDelegatedAt,
187 collectedAmount: d.collectedAmount,
188 collectableTime: d.collectableTime,
189 collectedAt: d.collectedAt,
190 collected: d.collected,
191 }
192}