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