-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfee_parameters.go
More file actions
64 lines (51 loc) · 1.69 KB
/
fee_parameters.go
File metadata and controls
64 lines (51 loc) · 1.69 KB
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
package models
import (
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
)
const feeParamsPrecision = 100_000_000
var surgeFactorScale = big.NewInt(feeParamsPrecision)
func DefaultFeeParameters() *FeeParameters {
return &FeeParameters{
SurgeFactor: cadence.UFix64(feeParamsPrecision),
InclusionEffortCost: cadence.UFix64(feeParamsPrecision),
ExecutionEffortCost: cadence.UFix64(feeParamsPrecision),
}
}
type FeeParameters struct {
SurgeFactor cadence.UFix64 `cadence:"surgeFactor"`
InclusionEffortCost cadence.UFix64 `cadence:"inclusionEffortCost"`
ExecutionEffortCost cadence.UFix64 `cadence:"executionEffortCost"`
}
func (f *FeeParameters) ToBytes() ([]byte, error) {
return rlp.EncodeToBytes(f)
}
func (f *FeeParameters) CalculateGasPrice(currentGasPrice *big.Int) *big.Int {
if currentGasPrice == nil {
return new(big.Int) // zero
}
// gasPrice = (currentGasPrice * surgeFactor) / feeParamsPrecision
surgeFactor := new(big.Int).SetUint64(uint64(f.SurgeFactor))
gasPrice := new(big.Int).Mul(currentGasPrice, surgeFactor)
return new(big.Int).Quo(gasPrice, surgeFactorScale)
}
func NewFeeParametersFromBytes(data []byte) (*FeeParameters, error) {
feeParameters := &FeeParameters{}
if err := rlp.DecodeBytes(data, feeParameters); err != nil {
return nil, err
}
return feeParameters, nil
}
func decodeFeeParametersChangedEvent(event cadence.Event) (*FeeParameters, error) {
feeParameters := &FeeParameters{}
if err := cadence.DecodeFields(event, feeParameters); err != nil {
return nil, fmt.Errorf(
"failed to Cadence-decode FlowFees.FeeParametersChanged event [%s]: %w",
event.String(),
err,
)
}
return feeParameters, nil
}