-
Notifications
You must be signed in to change notification settings - Fork 226
/
types.go
62 lines (52 loc) · 1.81 KB
/
types.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
package types
import (
"fmt"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/common"
)
var (
Ten = big.NewInt(10)
TenPowTen = Ten.Exp(Ten, Ten, nil)
)
const (
ibcDenomPrefix = "ibc/"
ibcDenomLen = len(ibcDenomPrefix) + 64
gravityDenomPrefix = "gravity0x"
gravityDenomLen = len(gravityDenomPrefix) + 40
cronosDenomPrefix = "cronos0x"
cronosDenomLen = len(cronosDenomPrefix) + 40
)
// IsValidIBCDenom returns true if denom is a valid ibc denom
func IsValidIBCDenom(denom string) bool {
return len(denom) == ibcDenomLen && strings.HasPrefix(denom, ibcDenomPrefix)
}
// IsValidGravityDenom returns true if denom is a valid gravity denom
func IsValidGravityDenom(denom string) bool {
return len(denom) == gravityDenomLen && strings.HasPrefix(denom, gravityDenomPrefix)
}
// IsValidCronosDenom returns true if denom is a valid cronos denom
func IsValidCronosDenom(denom string) bool {
return len(denom) == cronosDenomLen && strings.HasPrefix(denom, cronosDenomPrefix)
}
// IsSourceCoin returns true if denom is a coin originated from cronos
func IsSourceCoin(denom string) bool {
return IsValidCronosDenom(denom)
}
// IsValidCoinDenom returns true if it's ok it is a valid coin denom
func IsValidCoinDenom(denom string) bool {
return IsValidIBCDenom(denom) || IsValidGravityDenom(denom) || IsValidCronosDenom(denom)
}
// GetContractAddressFromDenom get the contract address from the coin denom
func GetContractAddressFromDenom(denom string) (string, error) {
contractAddress := ""
if strings.HasPrefix(denom, gravityDenomPrefix) {
contractAddress = denom[7:]
} else if strings.HasPrefix(denom, cronosDenomPrefix) {
contractAddress = denom[6:]
}
if !common.IsHexAddress(contractAddress) {
return "", fmt.Errorf("invalid contract address (%s)", contractAddress)
}
return contractAddress, nil
}