-
Notifications
You must be signed in to change notification settings - Fork 13
/
token_pairs.go
149 lines (126 loc) · 4.97 KB
/
token_pairs.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/functionx/fx-core/v3/x/erc20/types"
)
// GetAllTokenPairs - get all registered token tokenPairs
func (k Keeper) GetAllTokenPairs(ctx sdk.Context) []types.TokenPair {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixTokenPair)
defer iterator.Close()
var tokenPairs []types.TokenPair
for ; iterator.Valid(); iterator.Next() {
var tokenPair types.TokenPair
k.cdc.MustUnmarshal(iterator.Value(), &tokenPair)
tokenPairs = append(tokenPairs, tokenPair)
}
return tokenPairs
}
// GetTokenPair - get registered token pair from the token or denom
func (k Keeper) GetTokenPair(ctx sdk.Context, tokenOrDenom string) (types.TokenPair, bool) {
var id []byte
if common.IsHexAddress(tokenOrDenom) {
id = k.getERC20Map(ctx, common.HexToAddress(tokenOrDenom))
} else {
id = k.getDenomMap(ctx, tokenOrDenom)
}
if id == nil {
return types.TokenPair{}, false
}
return k.getTokenPair(ctx, id)
}
func (k Keeper) GetTokenPairByAddress(ctx sdk.Context, address common.Address) (types.TokenPair, bool) {
pairID := k.getERC20Map(ctx, address)
if len(pairID) == 0 {
return types.TokenPair{}, false
}
return k.getTokenPair(ctx, pairID)
}
func (k Keeper) getTokenPair(ctx sdk.Context, id []byte) (types.TokenPair, bool) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(append(types.KeyPrefixTokenPair, id...))
if len(bz) == 0 {
return types.TokenPair{}, false
}
var tokenPair types.TokenPair
k.cdc.MustUnmarshal(bz, &tokenPair)
return tokenPair, true
}
func (k Keeper) AddTokenPair(ctx sdk.Context, tokenPair types.TokenPair) {
store := ctx.KVStore(k.storeKey)
id := tokenPair.GetID()
// set pair
store.Set(append(types.KeyPrefixTokenPair, id...), k.cdc.MustMarshal(&tokenPair))
// set denom map
store.Set(append(types.KeyPrefixTokenPairByDenom, []byte(tokenPair.Denom)...), id)
// set erc20 map
store.Set(append(types.KeyPrefixTokenPairByERC20, tokenPair.GetERC20Contract().Bytes()...), id)
}
func (k Keeper) SetTokenPair(ctx sdk.Context, tokenPair types.TokenPair) {
store := ctx.KVStore(k.storeKey)
store.Set(append(types.KeyPrefixTokenPair, tokenPair.GetID()...), k.cdc.MustMarshal(&tokenPair))
}
// RemoveTokenPair removes a token pair.
func (k Keeper) RemoveTokenPair(ctx sdk.Context, tokenPair types.TokenPair) {
store := ctx.KVStore(k.storeKey)
id := tokenPair.GetID()
// delete token pair
store.Delete(append(types.KeyPrefixTokenPair, id...))
// delete denom map
store.Delete(append(types.KeyPrefixTokenPairByDenom, []byte(tokenPair.Denom)...))
// delete erc20 map
store.Delete(append(types.KeyPrefixTokenPairByERC20, tokenPair.GetERC20Contract().Bytes()...))
// delete denom alias
if md, found := k.HasDenomAlias(ctx, tokenPair.Denom); found {
k.DeleteAliasesDenom(ctx, md.DenomUnits[0].Aliases...)
}
}
// getERC20Map returns the token pair id for the given address
func (k Keeper) getERC20Map(ctx sdk.Context, erc20 common.Address) []byte {
store := ctx.KVStore(k.storeKey)
return store.Get(append(types.KeyPrefixTokenPairByERC20, erc20.Bytes()...))
}
// IsERC20Registered check if registered ERC20 token is registered
func (k Keeper) IsERC20Registered(ctx sdk.Context, erc20 common.Address) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(append(types.KeyPrefixTokenPairByERC20, erc20.Bytes()...))
}
// getDenomMap returns the token pair id for the given denomination
func (k Keeper) getDenomMap(ctx sdk.Context, denom string) []byte {
store := ctx.KVStore(k.storeKey)
return store.Get(append(types.KeyPrefixTokenPairByDenom, []byte(denom)...))
}
// IsDenomRegistered check if registered coin denom is registered
func (k Keeper) IsDenomRegistered(ctx sdk.Context, denom string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(append(types.KeyPrefixTokenPairByDenom, []byte(denom)...))
}
// SetAliasesDenom sets the aliases for the denomination
func (k Keeper) SetAliasesDenom(ctx sdk.Context, denom string, aliases ...string) {
store := ctx.KVStore(k.storeKey)
for _, alias := range aliases {
store.Set(append(types.KeyPrefixAliasDenom, []byte(alias)...), []byte(denom))
}
}
// GetAliasDenom returns the denom for the given alias
func (k Keeper) GetAliasDenom(ctx sdk.Context, alias string) (string, bool) {
store := ctx.KVStore(k.storeKey)
value := store.Get(append(types.KeyPrefixAliasDenom, []byte(alias)...))
if value == nil {
return "", false
}
return string(value), true
}
// DeleteAliasesDenom deletes the denom-alias for the given alias
func (k Keeper) DeleteAliasesDenom(ctx sdk.Context, aliases ...string) {
store := ctx.KVStore(k.storeKey)
for _, alias := range aliases {
store.Delete(append(types.KeyPrefixAliasDenom, []byte(alias)...))
}
}
// IsAliasDenomRegistered check if registered coin alias is registered
func (k Keeper) IsAliasDenomRegistered(ctx sdk.Context, alias string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(append(types.KeyPrefixAliasDenom, []byte(alias)...))
}