-
Notifications
You must be signed in to change notification settings - Fork 805
/
pointer.go
228 lines (202 loc) · 8.33 KB
/
pointer.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
package keeper
import (
"encoding/binary"
"fmt"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/ethereum/go-ethereum/common"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/cw721"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/erc20"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/erc721"
"github.com/sei-protocol/sei-chain/x/evm/artifacts/native"
artifactsutils "github.com/sei-protocol/sei-chain/x/evm/artifacts/utils"
"github.com/sei-protocol/sei-chain/x/evm/types"
)
var ErrorPointerToPointerNotAllowed = sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "cannot create a pointer to a pointer")
// ERC20 -> Native Token
func (k *Keeper) SetERC20NativePointer(ctx sdk.Context, token string, addr common.Address) error {
return k.SetERC20NativePointerWithVersion(ctx, token, addr, native.CurrentVersion)
}
// ERC20 -> Native Token
func (k *Keeper) SetERC20NativePointerWithVersion(ctx sdk.Context, token string, addr common.Address, version uint16) error {
if k.cwAddressIsPointer(ctx, token) {
return ErrorPointerToPointerNotAllowed
}
err := k.setPointerInfo(ctx, types.PointerERC20NativeKey(token), addr[:], version)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(token), version)
}
// ERC20 -> Native Token
func (k *Keeper) GetERC20NativePointer(ctx sdk.Context, token string) (addr common.Address, version uint16, exists bool) {
addrBz, version, exists := k.GetPointerInfo(ctx, types.PointerERC20NativeKey(token))
if exists {
addr = common.BytesToAddress(addrBz)
}
return
}
// ERC20 -> Native Token
func (k *Keeper) DeleteERC20NativePointer(ctx sdk.Context, token string, version uint16) {
addr, _, exists := k.GetERC20NativePointer(ctx, token)
if exists {
k.deletePointerInfo(ctx, types.PointerERC20NativeKey(token), version)
k.deletePointerInfo(ctx, types.PointerReverseRegistryKey(addr), version)
}
}
// ERC20 -> CW20
func (k *Keeper) SetERC20CW20Pointer(ctx sdk.Context, cw20Address string, addr common.Address) error {
return k.SetERC20CW20PointerWithVersion(ctx, cw20Address, addr, cw20.CurrentVersion(ctx))
}
// ERC20 -> CW20
func (k *Keeper) SetERC20CW20PointerWithVersion(ctx sdk.Context, cw20Address string, addr common.Address, version uint16) error {
if k.cwAddressIsPointer(ctx, cw20Address) {
return ErrorPointerToPointerNotAllowed
}
err := k.setPointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), addr[:], version)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(cw20Address), version)
}
// ERC20 -> CW20
func (k *Keeper) GetERC20CW20Pointer(ctx sdk.Context, cw20Address string) (addr common.Address, version uint16, exists bool) {
addrBz, version, exists := k.GetPointerInfo(ctx, types.PointerERC20CW20Key(cw20Address))
if exists {
addr = common.BytesToAddress(addrBz)
}
return
}
// ERC20 -> CW20
func (k *Keeper) DeleteERC20CW20Pointer(ctx sdk.Context, cw20Address string, version uint16) {
addr, _, exists := k.GetERC20CW20Pointer(ctx, cw20Address)
if exists {
k.deletePointerInfo(ctx, types.PointerERC20CW20Key(cw20Address), version)
k.deletePointerInfo(ctx, types.PointerReverseRegistryKey(addr), version)
}
}
// ERC721 -> CW721
func (k *Keeper) SetERC721CW721Pointer(ctx sdk.Context, cw721Address string, addr common.Address) error {
return k.SetERC721CW721PointerWithVersion(ctx, cw721Address, addr, cw721.CurrentVersion)
}
// ERC721 -> CW721
func (k *Keeper) SetERC721CW721PointerWithVersion(ctx sdk.Context, cw721Address string, addr common.Address, version uint16) error {
if k.cwAddressIsPointer(ctx, cw721Address) {
return ErrorPointerToPointerNotAllowed
}
err := k.setPointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), addr[:], version)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(addr), []byte(cw721Address), version)
}
// ERC721 -> CW721
func (k *Keeper) GetERC721CW721Pointer(ctx sdk.Context, cw721Address string) (addr common.Address, version uint16, exists bool) {
addrBz, version, exists := k.GetPointerInfo(ctx, types.PointerERC721CW721Key(cw721Address))
if exists {
addr = common.BytesToAddress(addrBz)
}
return
}
// ERC721 -> CW721
func (k *Keeper) DeleteERC721CW721Pointer(ctx sdk.Context, cw721Address string, version uint16) {
addr, _, exists := k.GetERC721CW721Pointer(ctx, cw721Address)
if exists {
k.deletePointerInfo(ctx, types.PointerERC721CW721Key(cw721Address), version)
k.deletePointerInfo(ctx, types.PointerReverseRegistryKey(addr), version)
}
}
// CW20 -> ERC20
func (k *Keeper) SetCW20ERC20Pointer(ctx sdk.Context, erc20Address common.Address, addr string) error {
if k.evmAddressIsPointer(ctx, erc20Address) {
return ErrorPointerToPointerNotAllowed
}
err := k.setPointerInfo(ctx, types.PointerCW20ERC20Key(erc20Address), []byte(addr), erc20.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(common.BytesToAddress([]byte(addr))), erc20Address[:], erc20.CurrentVersion)
}
// CW20 -> ERC20
func (k *Keeper) GetCW20ERC20Pointer(ctx sdk.Context, erc20Address common.Address) (addr sdk.AccAddress, version uint16, exists bool) {
addrBz, version, exists := k.GetPointerInfo(ctx, types.PointerCW20ERC20Key(erc20Address))
if exists {
addr = sdk.MustAccAddressFromBech32(string(addrBz))
}
return
}
func (k *Keeper) evmAddressIsPointer(ctx sdk.Context, addr common.Address) bool {
_, _, exists := k.GetPointerInfo(ctx, types.PointerReverseRegistryKey(addr))
return exists
}
func (k *Keeper) cwAddressIsPointer(ctx sdk.Context, addr string) bool {
_, _, exists := k.GetPointerInfo(ctx, types.PointerReverseRegistryKey(common.BytesToAddress([]byte(addr))))
return exists
}
func (k *Keeper) SetCW721ERC721Pointer(ctx sdk.Context, erc721Address common.Address, addr string) error {
if k.evmAddressIsPointer(ctx, erc721Address) {
return ErrorPointerToPointerNotAllowed
}
err := k.setPointerInfo(ctx, types.PointerCW721ERC721Key(erc721Address), []byte(addr), erc721.CurrentVersion)
if err != nil {
return err
}
return k.setPointerInfo(ctx, types.PointerReverseRegistryKey(common.BytesToAddress([]byte(addr))), erc721Address[:], erc721.CurrentVersion)
}
func (k *Keeper) GetCW721ERC721Pointer(ctx sdk.Context, erc721Address common.Address) (addr sdk.AccAddress, version uint16, exists bool) {
addrBz, version, exists := k.GetPointerInfo(ctx, types.PointerCW721ERC721Key(erc721Address))
if exists {
addr = sdk.MustAccAddressFromBech32(string(addrBz))
}
return
}
func (k *Keeper) GetPointerInfo(ctx sdk.Context, pref []byte) (addr []byte, version uint16, exists bool) {
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), pref)
iter := store.ReverseIterator(nil, nil)
defer iter.Close()
exists = iter.Valid()
if !exists {
return
}
version = binary.BigEndian.Uint16(iter.Key())
addr = iter.Value()
return
}
func (k *Keeper) setPointerInfo(ctx sdk.Context, pref []byte, addr []byte, version uint16) error {
existingAddr, existingVersion, exists := k.GetPointerInfo(ctx, pref)
if exists && existingVersion >= version {
return fmt.Errorf("pointer at %X with version %d exists when trying to set pointer for version %d", string(existingAddr), existingVersion, version)
}
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), pref)
versionBz := make([]byte, 2)
binary.BigEndian.PutUint16(versionBz, version)
store.Set(versionBz, addr)
return nil
}
func (k *Keeper) deletePointerInfo(ctx sdk.Context, pref []byte, version uint16) {
store := prefix.NewStore(ctx.KVStore(k.GetStoreKey()), pref)
versionBz := make([]byte, 2)
binary.BigEndian.PutUint16(versionBz, version)
store.Delete(versionBz)
}
func (k *Keeper) GetStoredPointerCodeID(ctx sdk.Context, pointerType types.PointerType) uint64 {
store := k.PrefixStore(ctx, types.PointerCWCodePrefix)
var versionBz []byte
switch pointerType {
case types.PointerType_ERC20:
store = prefix.NewStore(store, types.PointerCW20ERC20Prefix)
versionBz = artifactsutils.GetVersionBz(erc20.CurrentVersion)
case types.PointerType_ERC721:
store = prefix.NewStore(store, types.PointerCW721ERC721Prefix)
versionBz = artifactsutils.GetVersionBz(erc721.CurrentVersion)
default:
return 0
}
bz := store.Get(versionBz)
if bz == nil {
return 0
}
return binary.BigEndian.Uint64(bz)
}