-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgas_priority.go
63 lines (54 loc) · 1.34 KB
/
gas_priority.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
package crosschain
import (
"errors"
"fmt"
"github.com/shopspring/decimal"
)
type GasFeePriority string
var Low GasFeePriority = "low"
var Market GasFeePriority = "market"
var Aggressive GasFeePriority = "aggressive"
var VeryAggressive GasFeePriority = "very-aggressive"
func NewPriority(input string) (GasFeePriority, error) {
p := GasFeePriority(input)
if p.IsEnum() {
return p, nil
}
_, err := p.AsCustom()
return p, err
}
func (p GasFeePriority) IsEnum() bool {
switch p {
case Low, Market, Aggressive, VeryAggressive:
return true
}
return false
}
var max = decimal.NewFromInt(10)
func (p GasFeePriority) AsCustom() (decimal.Decimal, error) {
if p.IsEnum() {
return decimal.Decimal{}, errors.New("not a custom enum")
}
dec, err := decimal.NewFromString(string(p))
if err != nil {
return dec, fmt.Errorf("invalid decimal: %v", err)
}
if dec.GreaterThan(max) {
return dec, fmt.Errorf("exceeds custom multiplier: %s > %s", dec.String(), max.String())
}
return dec, nil
}
func (p GasFeePriority) GetDefault() (decimal.Decimal, error) {
switch p {
case Low:
return decimal.NewFromFloat(0.7), nil
case Market:
// use int for market to be exact 1
return decimal.NewFromInt(1), nil
case Aggressive:
return decimal.NewFromFloat(1.5), nil
case VeryAggressive:
return decimal.NewFromInt(2), nil
}
return p.AsCustom()
}