Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

project_condition.gno

4.61 Kb · 133 lines
  1package launchpad
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	ufmt "gno.land/p/nt/ufmt/v0"
  8)
  9
 10const stringSplitterPad = "*PAD*"
 11
 12// ProjectCondition represents a condition for a project.
 13//
 14// This struct contains the necessary data and methods to manage and distribute
 15// rewards for a specific project.
 16//
 17// Fields:
 18// - tokenPath (string): The path of the token associated with the project.
 19// - minimumAmount (int64): The minimum amount of the token required for the project.
 20type ProjectCondition struct {
 21	tokenPath     string
 22	minimumAmount int64
 23}
 24
 25// TokenPath returns the token contract path required by this condition.
 26//
 27// Returns:
 28//   - tokenPath: configured condition token path; an empty string means no token is configured.
 29func (p *ProjectCondition) TokenPath() string {
 30	return p.tokenPath
 31}
 32
 33// MinimumAmount returns the minimum balance amount required by this condition.
 34//
 35// Returns:
 36//   - minimumAmount: minimum amount in the token's base units; values at or below zero make the condition unavailable.
 37func (p *ProjectCondition) MinimumAmount() int64 {
 38	return p.minimumAmount
 39}
 40
 41// IsAvailable reports whether this condition has a non-empty token path and a positive minimum amount.
 42//
 43// Returns:
 44//   - available: true when both the token path and minimum amount are valid; false otherwise.
 45func (p *ProjectCondition) IsAvailable() bool {
 46	return p.tokenPath != "" && p.minimumAmount > 0
 47}
 48
 49// CheckBalanceCondition validates an input token balance against this condition.
 50//
 51// Parameters:
 52//   - inputTokenPath: token path whose balance is being checked; it must equal the condition token path.
 53//   - inputAmount: input token balance in base units; it must be at least the configured minimum.
 54//
 55// Returns:
 56//   - err: nil when the token path and amount satisfy the condition; non-nil describing a mismatch otherwise.
 57func (p *ProjectCondition) CheckBalanceCondition(inputTokenPath string, inputAmount int64) error {
 58	if p.tokenPath != inputTokenPath {
 59		return ufmt.Errorf("token path(%s) is not matched", inputTokenPath)
 60	}
 61
 62	if inputAmount < p.minimumAmount {
 63		return ufmt.Errorf("input amount(%d) is less than minimum amount(%d)", inputAmount, p.minimumAmount)
 64	}
 65
 66	return nil
 67}
 68
 69// Clone returns an independent copy of this project condition.
 70//
 71// Returns:
 72//   - condition: copied token path and minimum amount in a new ProjectCondition value.
 73func (p ProjectCondition) Clone() *ProjectCondition {
 74	return &ProjectCondition{
 75		tokenPath:     p.tokenPath,
 76		minimumAmount: p.minimumAmount,
 77	}
 78}
 79
 80// NewProjectCondition creates a project condition for a token path and minimum amount.
 81//
 82// Parameters:
 83//   - tokenPath: token contract path whose balance is required.
 84//   - minimumAmount: minimum balance in the token's base units.
 85//
 86// Returns:
 87//   - condition: newly allocated project condition containing the supplied values.
 88func NewProjectCondition(tokenPath string, minimumAmount int64) *ProjectCondition {
 89	return &ProjectCondition{
 90		tokenPath:     tokenPath,
 91		minimumAmount: minimumAmount,
 92	}
 93}
 94
 95// NewProjectConditionsWithError parses parallel *PAD*-separated token paths and minimum amounts.
 96//
 97// Parameters:
 98//   - conditionTokens: *PAD*-separated token contract paths, in condition order.
 99//   - conditionAmounts: *PAD*-separated decimal int64 minimum amounts paired by index with conditionTokens; entries beyond the token list are not consumed.
100//
101// Returns:
102//   - conditions: parsed available conditions in the same order as their token paths; empty when both inputs are empty.
103//   - err: nil on successful parsing; non-nil when a token lacks a matching amount, an amount is not a valid int64, or a condition is unavailable.
104func NewProjectConditionsWithError(conditionTokens string, conditionAmounts string) ([]*ProjectCondition, error) {
105	if conditionTokens == "" && conditionAmounts == "" {
106		return []*ProjectCondition{}, nil
107	}
108
109	conditions := []*ProjectCondition{}
110
111	tokenPaths := strings.Split(conditionTokens, stringSplitterPad)
112	minimumAmounts := strings.Split(conditionAmounts, stringSplitterPad)
113
114	for index, tokenPath := range tokenPaths {
115		if index >= len(minimumAmounts) {
116			return nil, ufmt.Errorf("condition amount(%s) is not matched with condition token(%s)", conditionAmounts, conditionTokens)
117		}
118
119		minimumAmount, err := strconv.ParseInt(minimumAmounts[index], 10, 64)
120		if err != nil {
121			return nil, ufmt.Errorf("condition amount(%s) is not a valid integer", minimumAmounts[index])
122		}
123
124		condition := NewProjectCondition(tokenPath, minimumAmount)
125		if !condition.IsAvailable() {
126			return nil, ufmt.Errorf("condition(%s) is not available", condition.TokenPath())
127		}
128
129		conditions = append(conditions, condition)
130	}
131
132	return conditions, nil
133}