delegation_withdraw.gno
5.48 Kb · 187 lines
1package staker
2
3import (
4 gnsmath "gno.land/p/gnoswap/gnsmath"
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 will be collectable after the lockup period expires.
119//
120// Parameters:
121// - delegationID: unique identifier of the associated delegation
122// - unDelegateAmount: amount being withdrawn
123// - createdAt: timestamp when the withdrawal was created
124// - unDelegationLockupPeriod: duration of the lockup period in seconds
125//
126// Returns:
127// - *DelegationWithdraw: new withdrawal instance with lockup
128func NewDelegationWithdraw(
129 delegationID,
130 unDelegateAmount,
131 createdHeight,
132 createdAt,
133 unDelegationLockupPeriod int64,
134) DelegationWithdraw {
135 return DelegationWithdraw{
136 delegationID: delegationID,
137 unDelegateAmount: unDelegateAmount,
138 unDelegatedHeight: createdHeight,
139 unDelegatedAt: createdAt,
140 collectableTime: gnsmath.SafeAddInt64(createdAt, unDelegationLockupPeriod),
141 collectedAmount: 0,
142 collectedAt: 0,
143 collected: false,
144 }
145}
146
147// NewDelegationWithdrawWithoutLockup creates a new delegation withdrawal that is immediately collectable.
148// This is used for special cases like redelegation where no lockup period is required.
149//
150// Parameters:
151// - delegationID: unique identifier of the associated delegation
152// - unDelegateAmount: amount being withdrawn
153// - createdAt: timestamp when the withdrawal was created
154//
155// Returns:
156// - *DelegationWithdraw: new withdrawal instance that is immediately collected
157func NewDelegationWithdrawWithoutLockup(
158 delegationID,
159 unDelegateAmount,
160 createdHeight,
161 createdAt int64,
162) DelegationWithdraw {
163 return DelegationWithdraw{
164 delegationID: delegationID,
165 unDelegateAmount: unDelegateAmount,
166 unDelegatedHeight: createdHeight,
167 unDelegatedAt: createdAt,
168 collectableTime: createdAt,
169 collectedAmount: unDelegateAmount, // Immediately collected
170 collectedAt: createdAt,
171 collected: true,
172 }
173}
174
175// Clone creates a deep copy of the delegation withdraw.
176func (d *DelegationWithdraw) Clone() DelegationWithdraw {
177 return DelegationWithdraw{
178 delegationID: d.delegationID,
179 unDelegateAmount: d.unDelegateAmount,
180 unDelegatedHeight: d.unDelegatedHeight,
181 unDelegatedAt: d.unDelegatedAt,
182 collectedAmount: d.collectedAmount,
183 collectableTime: d.collectableTime,
184 collectedAt: d.collectedAt,
185 collected: d.collected,
186 }
187}