diff --git a/contract/p/gnoswap/launchpad/deposit.gno b/contract/p/gnoswap/launchpad/deposit.gno index c181e7cc..91794817 100644 --- a/contract/p/gnoswap/launchpad/deposit.gno +++ b/contract/p/gnoswap/launchpad/deposit.gno @@ -1,12 +1,17 @@ package launchpad import ( + "errors" "std" "strings" "gno.land/p/demo/ufmt" ) +var ( + errNotYetClaimable = errors.New("not yet claimable") +) + // region: types // TODO: Deprecate this types @@ -66,6 +71,23 @@ func (d *DepositState2) AddDeposit(deposit Deposit) { d.indices.byID[deposit.id] = idx } +// Index returns the index of the deposit in the deposits array. +func (d *DepositState2) Index(id string) (int, error) { + idx, ok := d.indices.byID[id] + if !ok { + return 0, ufmt.Errorf("deposit not found: %s", id) + } + return idx, nil +} + +func (d *DepositState2) GetDeposit(id string) (Deposit, bool) { + idx, err := d.Index(id) + if err != nil { + return Deposit{}, false + } + return d.deposits[idx], true +} + // getters and setters func (d *DepositState) Deposits() Deposits { return d.deposits } @@ -178,6 +200,16 @@ func NewDeposit( } } +// RewardClaimable checks if the reward is claimable +func (d *Deposit) RewardClaimable(currentHeight uint64) error { + if d.rewardCollectTime == 0 { + if currentHeight < d.claimableHeight { + return errNotYetClaimable + } + } + return nil +} + // region: utils // splitProjectIdAndTier extracts the project ID and tier from a given tier ID. @@ -193,12 +225,10 @@ func NewDeposit( // - string: The tier derived from the third part of the `tierId`. // - error: If the tier ID is invalid, an error is returned. func SplitProjectIdAndTier(tierId string) (string, string, error) { - res := strings.Split(tierId, ":") - if len(res) != 3 { - return "", "", ufmt.Errorf("invalid tierId: %s", tierId) + if parts := strings.SplitN(tierId, ":", 3); len(parts) == 3 { + return createId(parts[0], parts[1]), parts[2], nil } - - return createId(res[0], res[2]), res[2], nil + return "", "", ufmt.Errorf("invalid tierId: %s", tierId) } // ParseTierDuration converts a tier type string to its corresponding uint64 value. diff --git a/contract/r/gnoswap/launchpad/reward.gno b/contract/r/gnoswap/launchpad/reward.gno index ce6dcdad..57859384 100644 --- a/contract/r/gnoswap/launchpad/reward.gno +++ b/contract/r/gnoswap/launchpad/reward.gno @@ -226,6 +226,8 @@ func validateRewardOwner(depositId string, caller std.Address) error { // validateRewardClaimable checks if reward can be claimed // e.g. deposit.claimableHeight, deposit.claimableTime +// +// TODO: deprecate this and use `Deposit.RewardClaimable` (p//launchpad) instead func validateRewardClaimable(deposit Deposit, currentHeight uint64) error { if deposit.rewardCollectTime == 0 { if currentHeight < deposit.claimableHeight {