-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethereum.go
105 lines (89 loc) · 3.15 KB
/
ethereum.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
package types
import (
"bytes"
"fmt"
"regexp"
"strings"
sdk "github.com/ci123chain/ci123chain/pkg/abci/types"
sdkerrors "github.com/ci123chain/ci123chain/pkg/abci/types/errors"
)
const (
// GravityDenomPrefix indicates the prefix for all assests minted by this module
GravityDenomPrefix = ModuleName
// GravityDenomSeparator is the separator for gravity denoms
GravityDenomSeparator = ""
// ETHContractAddressLen is the length of contract address strings
ETHContractAddressLen = 42
// GravityDenomLen is the length of the denoms generated by the gravity module
GravityDenomLen = len(GravityDenomPrefix) + len(GravityDenomSeparator) + ETHContractAddressLen
)
// EthAddrLessThan migrates the Ethereum address less than function
func EthAddrLessThan(e, o string) bool {
return bytes.Compare([]byte(e)[:], []byte(o)[:]) == -1
}
// ValidateEthAddress validates the ethereum address strings
func ValidateEthAddress(a string) error {
if a == "" {
return fmt.Errorf("empty")
}
if !regexp.MustCompile("^0x[0-9a-fA-F]{40}$").MatchString(a) {
return fmt.Errorf("address(%s) doesn't pass regex", a)
}
if len(a) != ETHContractAddressLen {
return fmt.Errorf("address(%s) of the wrong length exp(%d) actual(%d)", a, len(a), ETHContractAddressLen)
}
return nil
}
/////////////////////////
// ERC20Token //
/////////////////////////
// NewERC20Token returns a new instance of an ERC20
func NewERC20Token(amount uint64, contract string) *ERC20Token {
return &ERC20Token{Amount: sdk.NewIntFromUint64(amount), Contract: contract}
}
func NewSDKIntERC20Token(amount sdk.Int, contract string) *ERC20Token {
return &ERC20Token{Amount: amount, Contract: contract}
}
// GravityCoin returns the gravity representation of the ERC20
func (e *ERC20Token) GravityCoin() sdk.Coin {
return sdk.NewCoin(GravityDenom(e.Contract), e.Amount)
}
func GravityDenom(tokenContract string) string {
return fmt.Sprintf("%s%s%s", GravityDenomPrefix, GravityDenomSeparator, tokenContract)
}
// ValidateBasic permforms stateless validation
func (e *ERC20Token) ValidateBasic() error {
if err := ValidateEthAddress(e.Contract); err != nil {
return sdkerrors.Wrap(err, "ethereum address")
}
// TODO: Validate all the things
return nil
}
// Add adds one ERC20 to another
// TODO: make this return errors instead
func (e *ERC20Token) Add(o *ERC20Token) *ERC20Token {
if string(e.Contract) != string(o.Contract) {
panic("invalid contract address")
}
sum := e.Amount.Add(o.Amount)
if !sum.IsUint64() {
panic("invalid amount")
}
return NewERC20Token(sum.Uint64(), e.Contract)
}
func GravityDenomToERC20(denom string) (string, error) {
fullPrefix := GravityDenomPrefix + GravityDenomSeparator
if !strings.HasPrefix(denom, fullPrefix) {
return "", fmt.Errorf("denom prefix(%s) not equal to expected(%s)", denom, fullPrefix)
}
contract := strings.TrimPrefix(denom, fullPrefix)
err := ValidateEthAddress(contract)
switch {
case err != nil:
return "", fmt.Errorf("error(%s) validating ethereum contract address", err)
case len(denom) != GravityDenomLen:
return "", fmt.Errorf("len(denom)(%d) not equal to GravityDenomLen(%d)", len(denom), GravityDenomLen)
default:
return contract, nil
}
}