arkfee

package
v0.8.1-0...-3b18725 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2026 License: MIT Imports: 6 Imported by: 0

README

arkfee

The arkfee package provides fee estimation for Ark intents using CEL (Common Expression Language) expressions. It allows you to define custom fee calculation logic for both input and output operations.

CEL language definition: https://github.com/google/cel-spec/blob/master/doc/langdef.md#list-of-standard-definitions

Overview

The package provides an Estimator that can evaluate CEL expressions to calculate fees based on input and output characteristics. Each estimator can have four separate programs:

  • Intent Offchain Input Program: Evaluated for each offchain input (vtxo) in an intent
  • Intent Onchain Input Program: Evaluated for each onchain input (boarding) in an intent
  • Intent Offchain Output Program: Evaluated for each offchain output (vtxo) in an intent
  • Intent Onchain Output Program: Evaluated for each onchain output (collaborative exit) in an intent

The total fee is the sum of all input fees plus all output fees.

CEL Environments

The package provides three CEL environments, each with their own set of available variables and functions.

IntentOffchainInputEnv

Used for evaluating offchain input (vtxo) fee calculations. Available variables:

Variable Type Description
amount double Amount in satoshis
expiry double Expiry date in Unix timestamp seconds (only available if input has an expiry)
birth double Birth date in Unix timestamp seconds (only available if input has a birth time)
weight double Weighted liquidity lockup ratio of a vtxo
inputType string Type of the input: 'vtxo', 'recoverable', or 'note'
IntentOnchainInputEnv

Used for evaluating onchain input (boarding) fee calculations. Available variables:

Variable Type Description
amount double Amount in satoshis
IntentOutputEnv

Used for evaluating output fee calculations. Available variables:

Variable Type Description
amount double Amount in satoshis
script string Hex encoded pkscript

Available Functions

All environments provide the following functions:

now() -> double

Returns the current Unix timestamp in seconds.

Example:

expiry - now() < double(duration('5m').getSeconds()) ? 0.0 : amount / 2.0

Usage

Creating an Estimator
intentFees := arkfee.IntentFees{
    IntentOffchainInputProgram:  "inputProgram",
    IntentOnchainInputProgram:   "onchainInputProgram",
    IntentOffchainOutputProgram: "offchainOutputProgram",
    IntentOnchainOutputProgram:  "onchainOutputProgram",
}
estimator, err := arkfee.New(intentFees)
if err != nil {
    // handle error
}

All programs are optional. If a program is empty, the corresponding fee evaluation will return 0.

Evaluating Fees
// Evaluate fee for a single offchain input
offchainInputFee, err := estimator.EvalOffchainInput(arkfee.OffchainInput{
    Amount: 10000,
    Expiry: time.Now().Add(time.Hour),
    Birth:  time.Now().Add(-10 * time.Minute),
    Type:   arkfee.VtxoTypeVtxo,
    Weight: 1.0,
})

// Evaluate fee for a single onchain input
onchainInputFee, err := estimator.EvalOnchainInput(arkfee.OnchainInput{
    Amount: 5000,
})

// Evaluate fee for a single offchain output
offchainOutputFee, err := estimator.EvalOffchainOutput(arkfee.Output{
    Amount: 3000,
    Script: "0014...",
})

// Evaluate fee for a single onchain output
onchainOutputFee, err := estimator.EvalOnchainOutput(arkfee.Output{
    Amount: 2000,
    Script: "0014...",
})

// Evaluate total fee for multiple inputs and outputs
totalFee, err := estimator.Eval(
    offchainInputs, onchainInputs,
    offchainOutputs, onchainOutputs,
)

Example Programs

Offchain Input Program Examples

Free for recoverable inputs:

inputType == 'recoverable' ? 0.0 : 200.0

Weighted fee (1% of amount):

weight * 0.01 * amount

Time-based fee (free if expires in less than 5 minutes):

expiry - now() < double(duration('5m').getSeconds()) ? 0.0 : amount / 2.0
Onchain Input Program Examples

Fixed fee per boarding input:

200.0

Percentage fee (0.1% of amount):

amount * 0.001
Output Program Examples

Fixed fee per output:

100.0

Percentage fee:

amount * 0.002

Fee based on script type (example using script length):

size(script) > 50 ? amount * 0.01 : amount * 0.005

Return Type

All CEL programs must return a double (floating-point number) representing the fee amount in satoshis. The result is converted to a FeeAmount type, which can be converted to satoshis using ToSatoshis().

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(txt string, env *cel.Env) (*program, error)

Types

type Config

type Config struct {
	IntentOffchainInputProgram  string
	IntentOnchainInputProgram   string
	IntentOffchainOutputProgram string
	IntentOnchainOutputProgram  string
}

type Estimator

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

func New

func New(config Config) (estimator *Estimator, err error)

New parses the intent input and output programs if not empty and returns a new Estimator

func (Estimator) Eval

func (e Estimator) Eval(
	offchainInputs []OffchainInput, onchainInputs []OnchainInput,
	offchainOutputs, onchainOutputs []Output,
) (FeeAmount, error)

Eval evaluates the fee for a given set of inputs and outputs

func (Estimator) EvalOffchainInput

func (e Estimator) EvalOffchainInput(input OffchainInput) (FeeAmount, error)

EvalOffchainInput evalutes the fee for a given vtxo input

func (Estimator) EvalOffchainOutput

func (e Estimator) EvalOffchainOutput(output Output) (FeeAmount, error)

EvalOffchainOutput evalutes the fee for a given vtxo output

func (Estimator) EvalOnchainInput

func (e Estimator) EvalOnchainInput(input OnchainInput) (FeeAmount, error)

EvalOnchainInput evalutes the fee for a given boarding input

func (Estimator) EvalOnchainOutput

func (e Estimator) EvalOnchainOutput(output Output) (FeeAmount, error)

EvalOnchainOutput evalutes the fee for a given collaborative exit output

type FeeAmount

type FeeAmount float64

func (FeeAmount) ToSatoshis

func (f FeeAmount) ToSatoshis() int64

type OffchainInput

type OffchainInput struct {
	Amount uint64
	Expiry time.Time
	Birth  time.Time
	Type   VtxoType
	Weight float64
}

type OnchainInput

type OnchainInput struct {
	Amount uint64
}

type Output

type Output struct {
	Amount uint64
	Script string
}

type VtxoType

type VtxoType string
const (
	VtxoTypeRecoverable VtxoType = "recoverable"
	VtxoTypeVtxo        VtxoType = "vtxo"
	VtxoTypeNote        VtxoType = "note"
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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