/
token_pairs.go
156 lines (131 loc) · 5.48 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
150
151
152
153
154
155
156
// Copyright 2022 Evmos Foundation
// This file is part of the Evmos Network packages.
//
// Evmos is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Evmos packages are distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/evmos/v11/x/erc20/types"
)
// GetTokenPairs - get all registered token tokenPairs
func (k Keeper) GetTokenPairs(ctx sdk.Context) []types.TokenPair {
tokenPairs := []types.TokenPair{}
k.IterateTokenPairs(ctx, func(tokenPair types.TokenPair) (stop bool) {
tokenPairs = append(tokenPairs, tokenPair)
return false
})
return tokenPairs
}
// IterateTokenPairs iterates over all the stored token pairs
func (k Keeper) IterateTokenPairs(ctx sdk.Context, cb func(tokenPair types.TokenPair) (stop bool)) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, types.KeyPrefixTokenPair)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var tokenPair types.TokenPair
k.cdc.MustUnmarshal(iterator.Value(), &tokenPair)
if cb(tokenPair) {
break
}
}
}
// GetTokenPairID returns the pair id from either of the registered tokens.
// Hex address or Denom can be used as token argument.
func (k Keeper) GetTokenPairID(ctx sdk.Context, token string) []byte {
if common.IsHexAddress(token) {
addr := common.HexToAddress(token)
return k.GetERC20Map(ctx, addr)
}
return k.GetDenomMap(ctx, token)
}
// GetTokenPair gets a registered token pair from the identifier.
func (k Keeper) GetTokenPair(ctx sdk.Context, id []byte) (types.TokenPair, bool) {
if id == nil {
return types.TokenPair{}, false
}
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPair)
var tokenPair types.TokenPair
bz := store.Get(id)
if len(bz) == 0 {
return types.TokenPair{}, false
}
k.cdc.MustUnmarshal(bz, &tokenPair)
return tokenPair, true
}
// SetTokenPair stores a token pair
func (k Keeper) SetTokenPair(ctx sdk.Context, tokenPair types.TokenPair) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPair)
key := tokenPair.GetID()
bz := k.cdc.MustMarshal(&tokenPair)
store.Set(key, bz)
}
// DeleteTokenPair removes a token pair.
func (k Keeper) DeleteTokenPair(ctx sdk.Context, tokenPair types.TokenPair) {
id := tokenPair.GetID()
k.deleteTokenPair(ctx, id)
k.deleteERC20Map(ctx, tokenPair.GetERC20Contract())
k.deleteDenomMap(ctx, tokenPair.Denom)
}
// deleteTokenPair deletes the token pair for the given id
func (k Keeper) deleteTokenPair(ctx sdk.Context, id []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPair)
store.Delete(id)
}
// GetERC20Map returns the token pair id for the given address
func (k Keeper) GetERC20Map(ctx sdk.Context, erc20 common.Address) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByERC20)
return store.Get(erc20.Bytes())
}
// GetDenomMap returns the token pair id for the given denomination
func (k Keeper) GetDenomMap(ctx sdk.Context, denom string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByDenom)
return store.Get([]byte(denom))
}
// SetERC20Map sets the token pair id for the given address
func (k Keeper) SetERC20Map(ctx sdk.Context, erc20 common.Address, id []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByERC20)
store.Set(erc20.Bytes(), id)
}
// deleteERC20Map deletes the token pair id for the given address
func (k Keeper) deleteERC20Map(ctx sdk.Context, erc20 common.Address) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByERC20)
store.Delete(erc20.Bytes())
}
// SetDenomMap sets the token pair id for the denomination
func (k Keeper) SetDenomMap(ctx sdk.Context, denom string, id []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByDenom)
store.Set([]byte(denom), id)
}
// deleteDenomMap deletes the token pair id for the given denom
func (k Keeper) deleteDenomMap(ctx sdk.Context, denom string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByDenom)
store.Delete([]byte(denom))
}
// IsTokenPairRegistered - check if registered token tokenPair is registered
func (k Keeper) IsTokenPairRegistered(ctx sdk.Context, id []byte) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPair)
return store.Has(id)
}
// IsERC20Registered check if registered ERC20 token is registered
func (k Keeper) IsERC20Registered(ctx sdk.Context, erc20 common.Address) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByERC20)
return store.Has(erc20.Bytes())
}
// IsDenomRegistered check if registered coin denom is registered
func (k Keeper) IsDenomRegistered(ctx sdk.Context, denom string) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByDenom)
return store.Has([]byte(denom))
}