Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
notJoon committed Jan 24, 2025
1 parent 349349a commit 8d33d0b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
40 changes: 35 additions & 5 deletions contract/p/gnoswap/launchpad/deposit.gno
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 }
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions contract/r/gnoswap/launchpad/reward.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 8d33d0b

Please sign in to comment.