-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_pairs.go
240 lines (184 loc) · 8.41 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package keeper
import (
"strconv"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/UptickNetwork/wasm-nft-convert/types"
)
// GetTokenPairs - get all registered token tokenPairs
func (k Keeper) GetTokenPairs(ctx sdk.Context) []types.TokenPair {
tokenPairs := []types.TokenPair{}
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)
tokenPairs = append(tokenPairs, tokenPair)
}
return tokenPairs
}
// GetTokenPair - get 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)
}
// SetWasmCode stores a WasmCode
func (k Keeper) SetWasmCode(ctx sdk.Context, account string, codeId uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWasmCode)
store.Set([]byte(account), []byte(strconv.FormatUint(codeId, 10)))
}
// GetWasmCode stores a WasmCode
func (k Keeper) GetWasmCode(ctx sdk.Context, account string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixWasmCode)
return store.Get([]byte(account))
}
// DeleteTokenPair removes a token pair.
func (k Keeper) DeleteTokenPair(ctx sdk.Context, tokenPair types.TokenPair) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPair)
key := tokenPair.GetID()
store.Delete(key)
}
// SetCW721Map sets the token pair id for the given address
func (k Keeper) SetCW721Map(ctx sdk.Context, cw721 string, id []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByCW721)
store.Set([]byte(cw721), id)
}
// GetCW721Map returns the token pair id for the given address
func (k Keeper) GetCW721Map(ctx sdk.Context, cw721 string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByCW721)
return store.Get([]byte(cw721))
}
// GetClassMap returns the token pair id for the given class
func (k Keeper) GetClassMap(ctx sdk.Context, classID string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByClass)
return store.Get([]byte(classID))
}
// DeleteCW721Map deletes the token pair id for the given address
func (k Keeper) DeleteCW721Map(ctx sdk.Context, cw721 common.Address) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByCW721)
store.Delete(cw721.Bytes())
}
// SetClassMap sets the token pair id for the classID
func (k Keeper) SetClassMap(ctx sdk.Context, classID string, id []byte) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByClass)
store.Set([]byte(classID), id)
}
// 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)
}
// IsCW721Registered check if registered CW721 token is registered
func (k Keeper) IsCW721Registered(ctx sdk.Context, cw721 string) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByCW721)
return store.Has([]byte(cw721))
}
// IsClassRegistered check if registered nft class is registered
func (k Keeper) IsClassRegistered(ctx sdk.Context, classID string) bool {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixTokenPairByClass)
return store.Has([]byte(classID))
}
func (k Keeper) SetNFTPairs(ctx sdk.Context, contractAddress string, tokenID string, classID string, nftID string) {
// save nft pair
if len(k.GetNFTPairByContractTokenID(ctx, contractAddress, tokenID)) == 0 {
k.SetNFTPairByContractTokenID(ctx, contractAddress, tokenID, classID, nftID)
}
if len(k.GetNFTPairByClassNFTID(ctx, classID, nftID)) == 0 {
k.SetNFTPairByClassNFTID(ctx, classID, nftID, contractAddress, tokenID)
}
}
// GetTokenPairID returns the pair id from either of the registered tokens.
func (k Keeper) GetTokenPairID(ctx sdk.Context, token string) []byte {
//if common.IsHexAddress(token) {
// return k.GetCW721Map(ctx, token)
//}
if len(k.GetCW721Map(ctx, token)) == 0 {
return k.GetClassMap(ctx, token)
} else {
return k.GetCW721Map(ctx, token)
}
}
func (k Keeper) SetNFTPairByContractTokenID(ctx sdk.Context, contractAddress string, tokenID string, classID string, nftID string) {
tokenUID := types.CreateTokenUID(contractAddress, tokenID)
nftUID := types.CreateNFTUID(classID, nftID)
k.SetNFTUIDPairByTokenUID(ctx, tokenUID, nftUID)
}
func (k Keeper) SetNFTUIDPairByTokenUID(ctx sdk.Context, tokenUID string, nftUID string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNFTUIDPairByTokenUID)
store.Set([]byte(tokenUID), []byte(nftUID))
}
func (k Keeper) GetNFTPairByContractTokenID(ctx sdk.Context, contractAddress string, tokenID string) []byte {
tokenUID := types.CreateTokenUID(contractAddress, tokenID)
return k.GetNFTUIDPairByTokenUID(ctx, tokenUID)
}
func (k Keeper) GetNFTUIDPairByTokenUID(ctx sdk.Context, tokenUID string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNFTUIDPairByTokenUID)
return store.Get([]byte(tokenUID))
}
func (k Keeper) DeleteNFTPairByTokenID(ctx sdk.Context, contractAddress string, tokenID string) {
tokenUID := types.CreateTokenUID(contractAddress, tokenID)
k.DeleteNFTUIDPairByTokenUID(ctx, tokenUID)
}
func (k Keeper) DeleteNFTUIDPairByTokenUID(ctx sdk.Context, tokenUID string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNFTUIDPairByTokenUID)
store.Delete([]byte(tokenUID))
}
func (k Keeper) SetNFTPairByClassNFTID(ctx sdk.Context, classID string, nftID string, contractAddress string, tokenID string) {
nftUID := types.CreateNFTUID(classID, nftID)
tokenUID := types.CreateTokenUID(contractAddress, tokenID)
k.SetNFTUIDPairByNFTUID(ctx, nftUID, tokenUID)
}
func (k Keeper) SetNFTUIDPairByNFTUID(ctx sdk.Context, nftUID string, tokenUID string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNFTUIDPairByNFTUID)
store.Set([]byte(nftUID), []byte(tokenUID))
}
func (k Keeper) GetNFTPairByClassNFTID(ctx sdk.Context, classID string, nftID string) []byte {
nftUID := types.CreateNFTUID(classID, nftID)
return k.GetTokenUIDPairByNFTUID(ctx, nftUID)
}
func (k Keeper) GetTokenUIDPairByNFTUID(ctx sdk.Context, nftUID string) []byte {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNFTUIDPairByNFTUID)
return store.Get([]byte(nftUID))
}
func (k Keeper) DeleteNFTPairByNFTID(ctx sdk.Context, classID string, nftID string) {
nftUID := types.CreateNFTUID(classID, nftID)
k.DeleteNFTUIDPairByNFTUID(ctx, nftUID)
}
func (k Keeper) DeleteNFTUIDPairByNFTUID(ctx sdk.Context, nftUID string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixNFTUIDPairByNFTUID)
store.Delete([]byte(nftUID))
}
// SetCwAddressByContractTokenId
func (k Keeper) SetCwAddressByContractTokenId(ctx sdk.Context, evmContractAddress string, evmTokenId string, evmAddress string) {
contractAndTokenId := evmContractAddress + evmTokenId
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCwAddressByContractTokenId)
store.Set([]byte(contractAndTokenId), []byte(evmAddress))
}
func (k Keeper) GetCwAddressByContractTokenId(ctx sdk.Context, evmContractAddress string, evmTokenId string) []byte {
contractAndTokenId := evmContractAddress + evmTokenId
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCwAddressByContractTokenId)
return store.Get([]byte(contractAndTokenId))
}
func (k Keeper) DeleteCwAddressByContractTokenId(ctx sdk.Context, evmContractAddress string, evmTokenId string) {
contractAndTokenId := evmContractAddress + evmTokenId
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefixCwAddressByContractTokenId)
store.Delete([]byte(contractAndTokenId))
}