-
Notifications
You must be signed in to change notification settings - Fork 75
/
pool_denom.go
57 lines (47 loc) · 1.28 KB
/
pool_denom.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
package types
import (
"fmt"
"regexp"
"strconv"
sdk "github.com/cosmos/cosmos-sdk/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)
const (
PoolDenomPrefix = "neutron/pool/"
PoolDenomRegexpStr = "^" + PoolDenomPrefix + `(\d+)` + "$"
)
var PoolDenomRegexp = regexp.MustCompile(PoolDenomRegexpStr)
func NewPoolDenom(poolID uint64) string {
return fmt.Sprintf("%s%d", PoolDenomPrefix, poolID)
}
func ValidatePoolDenom(denom string) error {
if _, err := ParsePoolIDFromDenom(denom); err != nil {
return err
}
return nil
}
func ParsePoolIDFromDenom(denom string) (uint64, error) {
res := PoolDenomRegexp.FindStringSubmatch(denom)
if len(res) != 2 {
return 0, ErrInvalidPoolDenom
}
idStr := res[1]
idInt, err := strconv.ParseUint(idStr, 10, 0)
if err != nil {
return 0, ErrInvalidPoolDenom
}
return idInt, nil
}
// NewDexMintCoinsRestriction creates and returns a BankMintingRestrictionFn that only allows minting of
// valid pool denoms
func NewDexDenomMintCoinsRestriction() bankkeeper.MintingRestrictionFn {
return func(_ sdk.Context, coinsToMint sdk.Coins) error {
for _, coin := range coinsToMint {
err := ValidatePoolDenom(coin.Denom)
if err != nil {
return fmt.Errorf("does not have permission to mint %s", coin.Denom)
}
}
return nil
}
}