github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/txs/fee/calculator.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package fee
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/MetalBlockchain/metalgo/utils/constants"
    10  	"github.com/MetalBlockchain/metalgo/vms/platformvm/txs"
    11  	"github.com/MetalBlockchain/metalgo/vms/platformvm/upgrade"
    12  )
    13  
    14  var _ txs.Visitor = (*calculator)(nil)
    15  
    16  func NewStaticCalculator(config StaticConfig, upgradeTimes upgrade.Config) *Calculator {
    17  	return &Calculator{
    18  		config:       config,
    19  		upgradeTimes: upgradeTimes,
    20  	}
    21  }
    22  
    23  type Calculator struct {
    24  	config       StaticConfig
    25  	upgradeTimes upgrade.Config
    26  }
    27  
    28  // [CalculateFee] returns the minimal fee needed to accept [tx], at chain time [time]
    29  func (c *Calculator) CalculateFee(tx txs.UnsignedTx, time time.Time) uint64 {
    30  	tmp := &calculator{
    31  		upgrades:  c.upgradeTimes,
    32  		staticCfg: c.config,
    33  		time:      time,
    34  	}
    35  
    36  	// this is guaranteed to never return an error
    37  	_ = tx.Visit(tmp)
    38  	return tmp.fee
    39  }
    40  
    41  // calculator is intentionally unexported and used through Calculator to provide
    42  // a more convenient API
    43  type calculator struct {
    44  	// Pre E-fork inputs
    45  	upgrades  upgrade.Config
    46  	staticCfg StaticConfig
    47  	time      time.Time
    48  
    49  	// outputs of visitor execution
    50  	fee uint64
    51  }
    52  
    53  func (c *calculator) AddValidatorTx(*txs.AddValidatorTx) error {
    54  	c.fee = c.staticCfg.AddPrimaryNetworkValidatorFee
    55  	return nil
    56  }
    57  
    58  func (c *calculator) AddSubnetValidatorTx(*txs.AddSubnetValidatorTx) error {
    59  	c.fee = c.staticCfg.AddSubnetValidatorFee
    60  	return nil
    61  }
    62  
    63  func (c *calculator) AddDelegatorTx(*txs.AddDelegatorTx) error {
    64  	c.fee = c.staticCfg.AddPrimaryNetworkDelegatorFee
    65  	return nil
    66  }
    67  
    68  func (c *calculator) CreateChainTx(*txs.CreateChainTx) error {
    69  	if c.upgrades.IsApricotPhase3Activated(c.time) {
    70  		c.fee = c.staticCfg.CreateBlockchainTxFee
    71  	} else {
    72  		c.fee = c.staticCfg.CreateAssetTxFee
    73  	}
    74  	return nil
    75  }
    76  
    77  func (c *calculator) CreateSubnetTx(*txs.CreateSubnetTx) error {
    78  	if c.upgrades.IsApricotPhase3Activated(c.time) {
    79  		c.fee = c.staticCfg.CreateSubnetTxFee
    80  	} else {
    81  		c.fee = c.staticCfg.CreateAssetTxFee
    82  	}
    83  	return nil
    84  }
    85  
    86  func (c *calculator) AdvanceTimeTx(*txs.AdvanceTimeTx) error {
    87  	c.fee = 0 // no fees
    88  	return nil
    89  }
    90  
    91  func (c *calculator) RewardValidatorTx(*txs.RewardValidatorTx) error {
    92  	c.fee = 0 // no fees
    93  	return nil
    94  }
    95  
    96  func (c *calculator) RemoveSubnetValidatorTx(*txs.RemoveSubnetValidatorTx) error {
    97  	c.fee = c.staticCfg.TxFee
    98  	return nil
    99  }
   100  
   101  func (c *calculator) TransformSubnetTx(*txs.TransformSubnetTx) error {
   102  	c.fee = c.staticCfg.TransformSubnetTxFee
   103  	return nil
   104  }
   105  
   106  func (c *calculator) TransferSubnetOwnershipTx(*txs.TransferSubnetOwnershipTx) error {
   107  	c.fee = c.staticCfg.TxFee
   108  	return nil
   109  }
   110  
   111  func (c *calculator) AddPermissionlessValidatorTx(tx *txs.AddPermissionlessValidatorTx) error {
   112  	if tx.Subnet != constants.PrimaryNetworkID {
   113  		c.fee = c.staticCfg.AddSubnetValidatorFee
   114  	} else {
   115  		c.fee = c.staticCfg.AddPrimaryNetworkValidatorFee
   116  	}
   117  	return nil
   118  }
   119  
   120  func (c *calculator) AddPermissionlessDelegatorTx(tx *txs.AddPermissionlessDelegatorTx) error {
   121  	if tx.Subnet != constants.PrimaryNetworkID {
   122  		c.fee = c.staticCfg.AddSubnetDelegatorFee
   123  	} else {
   124  		c.fee = c.staticCfg.AddPrimaryNetworkDelegatorFee
   125  	}
   126  	return nil
   127  }
   128  
   129  func (c *calculator) BaseTx(*txs.BaseTx) error {
   130  	c.fee = c.staticCfg.TxFee
   131  	return nil
   132  }
   133  
   134  func (c *calculator) ImportTx(*txs.ImportTx) error {
   135  	c.fee = c.staticCfg.TxFee
   136  	return nil
   137  }
   138  
   139  func (c *calculator) ExportTx(*txs.ExportTx) error {
   140  	c.fee = c.staticCfg.TxFee
   141  	return nil
   142  }