-
Notifications
You must be signed in to change notification settings - Fork 206
/
state.go
108 lines (89 loc) · 2.62 KB
/
state.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
package types
import (
"fmt"
"strings"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
// ----------------------------------------------------
// ModuleParams functions
// ----------------------------------------------------
func DefaultModuleParams() ModuleParams {
return ModuleParams{
DenomCreationGasConsume: 4_000_000,
}
}
func (params ModuleParams) Validate() error {
if params.DenomCreationGasConsume < 1 {
return ErrInvalidModuleParams.Wrap("cannot set gas creation cost to zero")
}
return nil
}
// ----------------------------------------------------
// TFDenom functions
// ----------------------------------------------------
func (tfd TFDenom) Denom() DenomStr {
return DenomStr(tfd.String())
}
// String: returns the standard string representation.
func (tfd TFDenom) String() string {
return fmt.Sprintf("tf/%s/%s", tfd.Creator, tfd.Subdenom)
}
func (tfd TFDenom) Validate() error {
return tfd.Denom().Validate()
}
func (tfd TFDenom) DefaultBankMetadata() banktypes.Metadata {
denom := tfd.String()
return banktypes.Metadata{
DenomUnits: []*banktypes.DenomUnit{{
Denom: denom,
Exponent: 0,
}},
Base: denom,
// The following is necessary for x/bank denom validation
Display: denom,
Name: denom,
Symbol: denom,
}
}
// ----------------------------------------------------
// DenomStr functions
// ----------------------------------------------------
// DenomStr: string identifier for a token factory denom (TFDenom)
type DenomStr string
func DenomFormatError(got string, msg ...string) error {
errStr := fmt.Sprintf(`denom format error: expected "tf/{creator-bech32}/{subdenom}", got %v`, got)
if len(msg) > 0 {
errStr += fmt.Sprintf(": %v", msg)
}
return fmt.Errorf(errStr)
}
func (denomStr DenomStr) Validate() error {
_, err := denomStr.ToStruct()
return err
}
func (denomStr DenomStr) String() string { return string(denomStr) }
func (genDenom GenesisDenom) Validate() error {
return DenomStr(genDenom.Denom).Validate()
}
func (denomStr DenomStr) ToStruct() (res TFDenom, err error) {
str := string(denomStr)
parts := strings.Split(str, "/")
switch {
case len(parts) != 3:
return res, DenomFormatError("denom has invalid number of sections separated by '/'")
case parts[0] != "tf":
return res, DenomFormatError(str, `missing denom prefix "tf"`)
case len(parts[1]) < 1:
return res, DenomFormatError(str, "empty creator address")
case len(parts[2]) < 1:
return res, DenomFormatError(str, "empty subdenom")
}
return TFDenom{
Creator: parts[1],
Subdenom: parts[2],
}, nil
}
func (denomStr DenomStr) MustToStruct() TFDenom {
out, _ := denomStr.ToStruct()
return out
}