pricing

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultQuoteExpiry = 5 * time.Minute

DefaultQuoteExpiry is how long a price quote remains valid.

View Source
const USDCDecimals = 6

USDCDecimals is the number of decimal places for USDC.

Variables

This section is empty.

Functions

This section is empty.

Types

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine computes dynamic prices using rule-based evaluation.

func New

New creates a pricing engine from config.

func (*Engine) AdaptToPricingFunc

func (e *Engine) AdaptToPricingFunc() func(toolName string) (string, bool)

AdaptToPricingFunc returns a function compatible with paygate.PricingFunc. Signature: func(toolName string) (price string, isFree bool) Uses a background context and empty peerDID for anonymous pricing lookups.

func (*Engine) AdaptToPricingFuncWithPeer

func (e *Engine) AdaptToPricingFuncWithPeer(peerDID string) func(toolName string) (string, bool)

AdaptToPricingFuncWithPeer returns a paygate-compatible PricingFunc that includes peer identity for trust-based pricing.

func (*Engine) AddRule

func (e *Engine) AddRule(rule PricingRule)

AddRule adds a pricing rule to the engine.

func (*Engine) Quote

func (e *Engine) Quote(ctx context.Context, toolName, peerDID string) (*Quote, error)

Quote computes a price quote for a tool invocation.

func (*Engine) RemoveRule

func (e *Engine) RemoveRule(name string)

RemoveRule removes a pricing rule by name.

func (*Engine) Rules

func (e *Engine) Rules() []PricingRule

Rules returns a snapshot of the current pricing rules.

func (*Engine) SetBasePrice

func (e *Engine) SetBasePrice(toolName string, price *big.Int)

SetBasePrice sets the base price for a tool in smallest USDC units.

func (*Engine) SetBasePriceFromString

func (e *Engine) SetBasePriceFromString(toolName, price string) error

SetBasePriceFromString parses a USDC decimal string and sets the base price.

func (*Engine) SetReputation

func (e *Engine) SetReputation(fn ReputationQuerier)

SetReputation sets the reputation querier for trust-based discounts.

type MapToolPricer

type MapToolPricer struct {
	// contains filtered or unexported fields
}

MapToolPricer provides a simple way to set base prices from a map during engine construction. Call SetBasePrice on the engine directly for runtime updates.

func NewMapToolPricer

func NewMapToolPricer(prices map[string]*big.Int, defaultPrice *big.Int) *MapToolPricer

NewMapToolPricer creates a MapToolPricer backed by a map. Tools not in the map use the default price. If defaultPrice is nil, unlisted tools have no price.

func (*MapToolPricer) LoadInto

func (m *MapToolPricer) LoadInto(e *Engine)

LoadInto sets all prices from this pricer into the engine.

type PriceModifier

type PriceModifier struct {
	Type        PriceModifierType `json:"type"`
	Description string            `json:"description"`
	Factor      float64           `json:"factor"` // multiplier: 0.9 = 10% discount, 1.2 = 20% surge
}

PriceModifier represents an adjustment to a base price.

type PriceModifierType

type PriceModifierType string

PriceModifierType identifies the type of price modification.

const (
	ModifierTrustDiscount  PriceModifierType = "trust_discount"
	ModifierVolumeDiscount PriceModifierType = "volume_discount"
	ModifierSurge          PriceModifierType = "surge"
	ModifierCustom         PriceModifierType = "custom"
)

type PricingRule

type PricingRule struct {
	Name      string        `json:"name"`
	Priority  int           `json:"priority"` // lower = higher priority
	Condition RuleCondition `json:"condition"`
	Modifier  PriceModifier `json:"modifier"`
	Enabled   bool          `json:"enabled"`
}

PricingRule defines a pricing rule with condition and modifier.

type Quote

type Quote struct {
	ToolName   string          `json:"toolName"`
	BasePrice  *big.Int        `json:"basePrice"` // in smallest USDC units
	FinalPrice *big.Int        `json:"finalPrice"`
	Currency   string          `json:"currency"` // "USDC"
	Modifiers  []PriceModifier `json:"modifiers"`
	IsFree     bool            `json:"isFree"`
	ValidUntil time.Time       `json:"validUntil"`
	PeerDID    string          `json:"peerDid,omitempty"`
}

Quote represents a computed price for a tool invocation.

type ReputationQuerier

type ReputationQuerier func(ctx context.Context, peerDID string) (float64, error)

ReputationQuerier queries peer trust scores. Defined locally to avoid import cycles.

type RuleCondition

type RuleCondition struct {
	ToolPattern   string  `json:"toolPattern,omitempty"` // glob pattern for tool name
	MinTrustScore float64 `json:"minTrustScore,omitempty"`
	MaxTrustScore float64 `json:"maxTrustScore,omitempty"`
	PeerDID       string  `json:"peerDid,omitempty"` // specific peer
}

RuleCondition defines when a pricing rule applies.

type RuleSet

type RuleSet struct {
	// contains filtered or unexported fields
}

RuleSet holds an ordered collection of pricing rules.

func NewRuleSet

func NewRuleSet() *RuleSet

NewRuleSet creates a new empty RuleSet.

func (*RuleSet) Add

func (rs *RuleSet) Add(rule PricingRule)

Add inserts a rule and keeps the list sorted by priority.

func (*RuleSet) Evaluate

func (rs *RuleSet) Evaluate(toolName string, trustScore float64, peerDID string, basePrice *big.Int) (*big.Int, []PriceModifier)

Evaluate walks rules in priority order, applies matching modifiers, and returns the final price and the list of applied modifiers.

func (*RuleSet) Remove

func (rs *RuleSet) Remove(name string)

Remove deletes a rule by name.

func (*RuleSet) Rules

func (rs *RuleSet) Rules() []PricingRule

Rules returns a copy of all rules sorted by priority.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL