/
serialize.go
216 lines (185 loc) · 5.46 KB
/
serialize.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
package types
import (
"bytes"
"math/big"
"github.com/cosmos/cosmos-sdk/bsc/rlp"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type BindPackageType uint8
const (
BindTypeBind BindPackageType = 0
BindTypeUnbind BindPackageType = 1
)
type BindSynPackage struct {
PackageType BindPackageType
TokenSymbol [32]byte
ContractAddr sdk.SmartChainAddress
TotalSupply *big.Int
PeggyAmount *big.Int
Decimals uint8
ExpireTime uint64
}
func DeserializeBindSynPackage(serializedPackage []byte) (*BindSynPackage, sdk.Error) {
var pack BindSynPackage
err := rlp.DecodeBytes(serializedPackage, &pack)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize bind syn package failed")
}
return &pack, nil
}
type BindAckPackage struct {
TokenSymbol [32]byte
}
type BindStatus uint32
const (
BindStatusSuccess BindStatus = 0
BindStatusRejected BindStatus = 1
BindStatusTimeout BindStatus = 2
BindStatusInvalidParameter BindStatus = 3
)
type ApproveBindSynPackage struct {
Status BindStatus
TokenSymbol [32]byte
}
func DeserializeApproveBindSynPackage(serializedPackage []byte) (*ApproveBindSynPackage, sdk.Error) {
var pack ApproveBindSynPackage
err := rlp.DecodeBytes(serializedPackage, &pack)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize approve bind package failed")
}
return &pack, nil
}
type ApproveBindAckPackage struct {
TokenSymbol [32]byte
}
type TransferInSynPackage struct {
TokenSymbol [32]byte
ContractAddress sdk.SmartChainAddress
Amounts []*big.Int
ReceiverAddresses []sdk.AccAddress
RefundAddresses []sdk.SmartChainAddress
ExpireTime uint64
}
func DeserializeTransferInSynPackage(serializedPackage []byte) (*TransferInSynPackage, sdk.Error) {
var tp TransferInSynPackage
err := rlp.DecodeBytes(serializedPackage, &tp)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize transfer in package failed")
}
return &tp, nil
}
type TransferInRefundPackage struct {
ContractAddr sdk.SmartChainAddress
RefundAmounts []*big.Int
RefundAddresses []sdk.SmartChainAddress
RefundReason RefundReason
}
type TransferOutSynPackage struct {
TokenSymbol [32]byte
ContractAddress sdk.SmartChainAddress
Amount *big.Int
Recipient sdk.SmartChainAddress
RefundAddress sdk.AccAddress
ExpireTime uint64
}
func DeserializeTransferOutSynPackage(serializedPackage []byte) (*TransferOutSynPackage, sdk.Error) {
var tp TransferOutSynPackage
err := rlp.DecodeBytes(serializedPackage, &tp)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize transfer out package failed")
}
return &tp, nil
}
type RefundReason uint32
const (
UnboundToken RefundReason = 1
Timeout RefundReason = 2
InsufficientBalance RefundReason = 3
Unknown RefundReason = 4
ForbidTransferToBPE12Addr RefundReason = 5
)
type TransferOutRefundPackage struct {
TokenSymbol [32]byte
RefundAmount *big.Int
RefundAddr sdk.AccAddress
RefundReason RefundReason
}
func DeserializeTransferOutRefundPackage(serializedPackage []byte) (*TransferOutRefundPackage, sdk.Error) {
var tp TransferOutRefundPackage
err := rlp.DecodeBytes(serializedPackage, &tp)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize transfer out refund package failed")
}
return &tp, nil
}
type MirrorSynPackage struct {
MirrorSender sdk.SmartChainAddress
ContractAddr sdk.SmartChainAddress
BEP20Name [32]byte
BEP20Symbol [32]byte
BEP20TotalSupply *big.Int
BEP20Decimals uint8
MirrorFee *big.Int
ExpireTime uint64
}
func DeserializeMirrorSynPackage(serializedPackage []byte) (*MirrorSynPackage, sdk.Error) {
var ms MirrorSynPackage
err := rlp.DecodeBytes(serializedPackage, &ms)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize mirror package failed")
}
return &ms, nil
}
const (
MirrorErrCodeExpired uint8 = 1
MirrorErrCodeBEP2SymbolExists uint8 = 2
MirrorErrCodeDecimalOverflow uint8 = 3
MirrorErrCodeInvalidSymbol uint8 = 4
MirrorErrCodeInvalidSupply uint8 = 5
)
type MirrorAckPackage struct {
MirrorSender sdk.SmartChainAddress
ContractAddr sdk.SmartChainAddress
Decimals uint8
BEP2Symbol [32]byte
MirrorFee *big.Int
ErrorCode uint8
}
const (
MirrorSyncErrCodeExpired uint8 = 1
MirrorSyncErrNotBoundByMirror uint8 = 2
MirrorSyncErrInvalidSupply uint8 = 3
)
type MirrorSyncSynPackage struct {
SyncSender sdk.SmartChainAddress
ContractAddr sdk.SmartChainAddress
BEP2Symbol [32]byte
BEP20TotalSupply *big.Int
SyncFee *big.Int
ExpireTime uint64
}
func DeserializeMirrorSyncSynPackage(serializedPackage []byte) (*MirrorSyncSynPackage, sdk.Error) {
var ms MirrorSyncSynPackage
err := rlp.DecodeBytes(serializedPackage, &ms)
if err != nil {
return nil, ErrDeserializePackageFailed("deserialize mirror sync package failed")
}
return &ms, nil
}
type MirrorSyncAckPackage struct {
SyncSender sdk.SmartChainAddress
ContractAddr sdk.SmartChainAddress
SyncFee *big.Int
ErrorCode uint8
}
func SymbolToBytes(symbol string) [32]byte {
// length of bound token symbol length should not be larger than 32
serializedBytes := [32]byte{}
copy(serializedBytes[:], symbol)
return serializedBytes
}
func BytesToSymbol(symbolBytes [32]byte) string {
tokenSymbolBytes := make([]byte, 32)
copy(tokenSymbolBytes[:], symbolBytes[:])
return string(bytes.Trim(tokenSymbolBytes, "\x00"))
}