Search Apps Documentation Source Content File Folder Download Copy Actions Download

delegation_withdraw.gno

4.22 Kb ยท 146 lines
  1package v1
  2
  3import (
  4	"gno.land/r/gnoswap/gov/staker"
  5)
  6
  7type DelegationWithdrawResolver struct {
  8	withdraw *staker.DelegationWithdraw
  9}
 10
 11func NewDelegationWithdrawResolver(withdraw *staker.DelegationWithdraw) *DelegationWithdrawResolver {
 12	return &DelegationWithdrawResolver{withdraw}
 13}
 14
 15func (r *DelegationWithdrawResolver) Get() *staker.DelegationWithdraw {
 16	return r.withdraw
 17}
 18
 19// CollectableAmount calculates the amount available for collection at the given time.
 20// Returns zero if the withdrawal is not yet collectable or has been fully collected.
 21//
 22// Parameters:
 23//   - currentTime: current timestamp to check collectability against
 24//
 25// Returns:
 26//   - int64: amount available for collection
 27func (r *DelegationWithdrawResolver) CollectableAmount(currentTime int64) int64 {
 28	if r.IsCollectable(currentTime) {
 29		return safeSubInt64(r.withdraw.UnDelegateAmount(), r.withdraw.CollectedAmount())
 30	}
 31
 32	return 0
 33}
 34
 35// IsCollectable determines whether the withdrawal can be collected at the given time.
 36// A withdrawal is collectable if:
 37// - The undelegated amount is positive
 38// - There is remaining uncollected amount
 39// - The current time is at or after the collectable time
 40//
 41// Parameters:
 42//   - currentTime: current timestamp to check against
 43//
 44// Returns:
 45//   - bool: true if the withdrawal can be collected, false otherwise
 46func (r *DelegationWithdrawResolver) IsCollectable(currentTime int64) bool {
 47	if r.withdraw.UnDelegateAmount() <= 0 {
 48		return false
 49	}
 50
 51	remaining := safeSubInt64(r.withdraw.UnDelegateAmount(), r.withdraw.CollectedAmount())
 52	if remaining <= 0 {
 53		return false
 54	}
 55
 56	if currentTime < r.withdraw.CollectableTime() {
 57		return false
 58	}
 59
 60	return true
 61}
 62
 63// IsCollected returns whether the withdrawal has been fully collected.
 64//
 65// Returns:
 66//   - bool: true if fully collected, false otherwise
 67func (r *DelegationWithdrawResolver) IsCollected() bool {
 68	return r.withdraw.IsCollected()
 69}
 70
 71// collect processes the collection of the specified amount from this withdrawal.
 72// This method validates collectability and updates the collection state.
 73//
 74// Parameters:
 75//   - amount: amount to collect
 76//   - currentTime: current timestamp
 77//
 78// Returns:
 79//   - error: nil on success, error if collection is not allowed
 80func (r *DelegationWithdrawResolver) Collect(amount int64, currentTime int64) error {
 81	if !r.IsCollectable(currentTime) {
 82		return errWithdrawNotCollectable
 83	}
 84
 85	r.withdraw.SetCollectedAmount(safeAddInt64(r.withdraw.CollectedAmount(), amount))
 86
 87	if r.withdraw.UnDelegateAmount() == r.withdraw.CollectedAmount() {
 88		r.withdraw.SetCollected(true)
 89		r.withdraw.SetCollectedAt(currentTime)
 90	}
 91
 92	return nil
 93}
 94
 95// NewDelegationWithdraw creates a new delegation withdrawal with lockup period.
 96// This is a convenience wrapper around staker.NewDelegationWithdraw.
 97//
 98// Parameters:
 99//   - delegationID: unique identifier of the associated delegation
100//   - unDelegateAmount: amount being withdrawn
101//   - createdHeight: height when the withdrawal was created
102//   - createdAt: timestamp when the withdrawal was created
103//   - unDelegationLockupPeriod: duration of the lockup period in seconds
104//
105// Returns:
106//   - *staker.DelegationWithdraw: new withdrawal instance with lockup
107func NewDelegationWithdraw(
108	delegationID,
109	unDelegateAmount,
110	createdHeight,
111	createdAt,
112	unDelegationLockupPeriod int64,
113) *staker.DelegationWithdraw {
114	return staker.NewDelegationWithdraw(
115		delegationID,
116		unDelegateAmount,
117		createdHeight,
118		createdAt,
119		unDelegationLockupPeriod,
120	)
121}
122
123// NewDelegationWithdrawWithoutLockup creates a new delegation withdrawal that is immediately collectable.
124// This is a convenience wrapper around staker.NewDelegationWithdrawWithoutLockup.
125//
126// Parameters:
127//   - delegationID: unique identifier of the associated delegation
128//   - unDelegateAmount: amount being withdrawn
129//   - createdHeight: height when the withdrawal was created
130//   - createdAt: timestamp when the withdrawal was created
131//
132// Returns:
133//   - *staker.DelegationWithdraw: new withdrawal instance that is immediately collected
134func NewDelegationWithdrawWithoutLockup(
135	delegationID,
136	unDelegateAmount,
137	createdHeight,
138	createdAt int64,
139) *staker.DelegationWithdraw {
140	return staker.NewDelegationWithdrawWithoutLockup(
141		delegationID,
142		unDelegateAmount,
143		createdHeight,
144		createdAt,
145	)
146}