Search Apps Documentation Source Content File Folder Download Copy Actions Download

delegation_withdraw.gno

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