github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/txs/fee/static_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 "github.com/ava-labs/avalanchego/utils/constants" 8 "github.com/ava-labs/avalanchego/vms/platformvm/txs" 9 ) 10 11 var ( 12 _ Calculator = (*staticCalculator)(nil) 13 _ txs.Visitor = (*staticVisitor)(nil) 14 ) 15 16 func NewStaticCalculator(config StaticConfig) Calculator { 17 return &staticCalculator{ 18 config: config, 19 } 20 } 21 22 type staticCalculator struct { 23 config StaticConfig 24 } 25 26 func (c *staticCalculator) CalculateFee(tx txs.UnsignedTx) (uint64, error) { 27 v := staticVisitor{ 28 config: c.config, 29 } 30 err := tx.Visit(&v) 31 return v.fee, err 32 } 33 34 type staticVisitor struct { 35 // inputs 36 config StaticConfig 37 38 // outputs 39 fee uint64 40 } 41 42 func (*staticVisitor) AdvanceTimeTx(*txs.AdvanceTimeTx) error { 43 return ErrUnsupportedTx 44 } 45 46 func (*staticVisitor) RewardValidatorTx(*txs.RewardValidatorTx) error { 47 return ErrUnsupportedTx 48 } 49 50 func (c *staticVisitor) AddValidatorTx(*txs.AddValidatorTx) error { 51 c.fee = c.config.AddPrimaryNetworkValidatorFee 52 return nil 53 } 54 55 func (c *staticVisitor) AddSubnetValidatorTx(*txs.AddSubnetValidatorTx) error { 56 c.fee = c.config.AddSubnetValidatorFee 57 return nil 58 } 59 60 func (c *staticVisitor) AddDelegatorTx(*txs.AddDelegatorTx) error { 61 c.fee = c.config.AddPrimaryNetworkDelegatorFee 62 return nil 63 } 64 65 func (c *staticVisitor) CreateChainTx(*txs.CreateChainTx) error { 66 c.fee = c.config.CreateBlockchainTxFee 67 return nil 68 } 69 70 func (c *staticVisitor) CreateSubnetTx(*txs.CreateSubnetTx) error { 71 c.fee = c.config.CreateSubnetTxFee 72 return nil 73 } 74 75 func (c *staticVisitor) RemoveSubnetValidatorTx(*txs.RemoveSubnetValidatorTx) error { 76 c.fee = c.config.TxFee 77 return nil 78 } 79 80 func (c *staticVisitor) TransformSubnetTx(*txs.TransformSubnetTx) error { 81 c.fee = c.config.TransformSubnetTxFee 82 return nil 83 } 84 85 func (c *staticVisitor) TransferSubnetOwnershipTx(*txs.TransferSubnetOwnershipTx) error { 86 c.fee = c.config.TxFee 87 return nil 88 } 89 90 func (c *staticVisitor) AddPermissionlessValidatorTx(tx *txs.AddPermissionlessValidatorTx) error { 91 if tx.Subnet != constants.PrimaryNetworkID { 92 c.fee = c.config.AddSubnetValidatorFee 93 } else { 94 c.fee = c.config.AddPrimaryNetworkValidatorFee 95 } 96 return nil 97 } 98 99 func (c *staticVisitor) AddPermissionlessDelegatorTx(tx *txs.AddPermissionlessDelegatorTx) error { 100 if tx.Subnet != constants.PrimaryNetworkID { 101 c.fee = c.config.AddSubnetDelegatorFee 102 } else { 103 c.fee = c.config.AddPrimaryNetworkDelegatorFee 104 } 105 return nil 106 } 107 108 func (c *staticVisitor) BaseTx(*txs.BaseTx) error { 109 c.fee = c.config.TxFee 110 return nil 111 } 112 113 func (c *staticVisitor) ImportTx(*txs.ImportTx) error { 114 c.fee = c.config.TxFee 115 return nil 116 } 117 118 func (c *staticVisitor) ExportTx(*txs.ExportTx) error { 119 c.fee = c.config.TxFee 120 return nil 121 }