-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
290 lines (257 loc) · 7.74 KB
/
types.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package tokens
import (
"fmt"
"math/big"
"github.com/deltaswapio/swaprouter/v3/common"
"github.com/deltaswapio/swaprouter/v3/common/hexutil"
)
// SwapType type
type SwapType uint32
// SwapType constants
const (
UnknownSwapType SwapType = iota
ERC20SwapType
NFTSwapType
AnyCallSwapType
// special flags, do not use in register
ERC20SwapTypeMixPool
SapphireRPCType
MaxValidSwapType
)
// SwapSubType constants
const (
CurveAnycallSubType = "curve"
AnycallSubTypeV5 = "v5" // for curve
AnycallSubTypeV6 = "v6" // for hundred
AnycallSubTypeV7 = "v7" // add callback
)
// IsValidAnycallSubType is valid anycall subType
func IsValidAnycallSubType(subType string) bool {
switch subType {
case CurveAnycallSubType, AnycallSubTypeV5, AnycallSubTypeV6, AnycallSubTypeV7:
return true
default:
return false
}
}
func (s SwapType) String() string {
switch s {
case ERC20SwapType:
return "erc20swap"
case NFTSwapType:
return "nftswap"
case AnyCallSwapType:
return "anycallswap"
case ERC20SwapTypeMixPool:
return "mixpool"
case SapphireRPCType:
return "sapphireRPCType"
default:
return "unknownswap"
}
}
// IsValidType is valid swap type
func (s SwapType) IsValidType() bool {
return s > UnknownSwapType && s < MaxValidSwapType
}
// ERC20SwapInfo struct
type ERC20SwapInfo struct {
Token string `json:"token"`
TokenID string `json:"tokenID"`
SwapoutID string `json:"swapoutID,omitempty"`
CallProxy string `json:"callProxy,omitempty"`
CallData hexutil.Bytes `json:"callData,omitempty"`
}
// NFTSwapInfo struct
type NFTSwapInfo struct {
Token string `json:"token"`
TokenID string `json:"tokenID"`
IDs []*big.Int `json:"ids"`
Amounts []*big.Int `json:"amounts"`
Batch bool `json:"batch"`
Data hexutil.Bytes `json:"data,omitempty"`
}
// AnyCallSwapInfo struct
type AnyCallSwapInfo struct {
CallFrom string `json:"callFrom"`
CallTo string `json:"callTo"`
CallData hexutil.Bytes `json:"callData"`
Fallback string `json:"fallback,omitempty"`
Flags string `json:"flags,omitempty"`
AppID string `json:"appid,omitempty"`
Nonce string `json:"nonce,omitempty"`
ExtData hexutil.Bytes `json:"extdata,omitempty"`
Message hexutil.Bytes `json:"message,omitempty"`
Attestation hexutil.Bytes `json:"attestation,omitempty"`
}
// SwapInfo struct
type SwapInfo struct {
ERC20SwapInfo *ERC20SwapInfo `json:"routerSwapInfo,omitempty"`
NFTSwapInfo *NFTSwapInfo `json:"nftSwapInfo,omitempty"`
AnyCallSwapInfo *AnyCallSwapInfo `json:"anycallSwapInfo2,omitempty"`
}
// GetTokenID get tokenID
func (s *SwapInfo) GetTokenID() string {
if s.ERC20SwapInfo != nil {
return s.ERC20SwapInfo.TokenID
}
if s.NFTSwapInfo != nil {
return s.NFTSwapInfo.TokenID
}
return ""
}
// GetToken get token
func (s *SwapInfo) GetToken() string {
if s.ERC20SwapInfo != nil {
return s.ERC20SwapInfo.Token
}
if s.NFTSwapInfo != nil {
return s.NFTSwapInfo.Token
}
return ""
}
// SwapTxInfo struct
type SwapTxInfo struct {
SwapInfo `json:"swapinfo"`
SwapType SwapType `json:"swaptype"`
Hash string `json:"hash"`
Height uint64 `json:"height"`
Timestamp uint64 `json:"timestamp"`
From string `json:"from"`
TxTo string `json:"txto"`
To string `json:"to"`
Bind string `json:"bind"`
Value *big.Int `json:"value"`
LogIndex int `json:"logIndex"`
FromChainID *big.Int `json:"fromChainID"`
ToChainID *big.Int `json:"toChainID"`
}
// TxStatus struct
type TxStatus struct {
Receipt interface{} `json:"receipt,omitempty"`
Confirmations uint64 `json:"confirmations"`
BlockHeight uint64 `json:"block_height"`
BlockHash string `json:"block_hash"`
BlockTime uint64 `json:"block_time,omitempty"`
}
// StatusInterface interface
type StatusInterface interface {
IsStatusOk() bool
}
// IsSwapTxOnChain is tx onchain
func (s *TxStatus) IsSwapTxOnChain() bool {
return s != nil && s.BlockHeight > 0
}
// IsSwapTxOnChainAndFailed to make failed of swaptx
func (s *TxStatus) IsSwapTxOnChainAndFailed() bool {
if !s.IsSwapTxOnChain() {
return false // not on chain
}
if status, ok := s.Receipt.(StatusInterface); ok {
if !status.IsStatusOk() {
return true
}
}
return false
}
// VerifyArgs struct
type VerifyArgs struct {
SwapType SwapType `json:"swaptype,omitempty"`
LogIndex int `json:"logIndex,omitempty"`
AllowUnstable bool `json:"allowUnstable,omitempty"`
}
// RegisterArgs struct
type RegisterArgs struct {
SwapType SwapType `json:"swaptype,omitempty"`
LogIndex int `json:"logIndex,omitempty"`
}
// SwapArgs struct
type SwapArgs struct {
SwapInfo `json:"swapinfo"`
Identifier string `json:"identifier,omitempty"`
SwapID string `json:"swapid,omitempty"`
SwapType SwapType `json:"swaptype,omitempty"`
Bind string `json:"bind,omitempty"`
LogIndex int `json:"logIndex"`
FromChainID *big.Int `json:"fromChainID"`
ToChainID *big.Int `json:"toChainID"`
Reswapping bool `json:"reswapping,omitempty"`
}
// BuildTxArgs struct
type BuildTxArgs struct {
SwapArgs `json:"swapArgs,omitempty"`
From string `json:"from,omitempty"`
To string `json:"to,omitempty"`
OriginFrom string `json:"originFrom,omitempty"`
OriginTxTo string `json:"originTxTo,omitempty"`
OriginValue *big.Int `json:"originValue,omitempty"`
SwapValue *big.Int `json:"swapValue,omitempty"`
Value *big.Int `json:"value,omitempty"`
Memo string `json:"memo,omitempty"`
Selector string `json:"selector,omitempty"`
Input *hexutil.Bytes `json:"input,omitempty"`
Extra *AllExtras `json:"extra,omitempty"`
}
// AllExtras struct
type AllExtras struct {
Gas *uint64 `json:"gas,omitempty"`
GasPrice *big.Int `json:"gasPrice,omitempty"`
GasTipCap *big.Int `json:"gasTipCap,omitempty"`
GasFeeCap *big.Int `json:"gasFeeCap,omitempty"`
Sequence *uint64 `json:"sequence,omitempty"`
ReplaceNum uint64 `json:"replaceNum,omitempty"`
Fee *string `json:"fee,omitempty"`
RawTx hexutil.Bytes `json:"rawTx,omitempty"`
BlockHash *string `json:"blockHash,omitempty"`
BlockID *string `json:"blockID,omitempty"`
BlockNumber *uint64 `json:"blockNumber,omitempty"`
TTL *uint64 `json:"ttl,omitempty"`
BridgeFee *big.Int `json:"bridgeFee,omitempty"`
}
// GetReplaceNum get rplace swap count
func (args *BuildTxArgs) GetReplaceNum() uint64 {
if args.Extra != nil {
return args.Extra.ReplaceNum
}
return 0
}
// GetExtraArgs get extra args
func (args *BuildTxArgs) GetExtraArgs() *BuildTxArgs {
swapArgs := args.SwapArgs
if swapArgs.SwapInfo.AnyCallSwapInfo != nil {
// message should be retrieved from receipt logs
anycallInfo := *swapArgs.SwapInfo.AnyCallSwapInfo
anycallInfo.Message = nil
swapArgs.SwapInfo.AnyCallSwapInfo = &anycallInfo
}
return &BuildTxArgs{
From: args.From,
SwapArgs: swapArgs,
Extra: args.Extra,
}
}
// GetTxNonce get tx nonce
func (args *BuildTxArgs) GetTxNonce() uint64 {
if args.Extra != nil && args.Extra.Sequence != nil {
return *args.Extra.Sequence
}
return 0
}
// SetTxNonce set tx nonce
func (args *BuildTxArgs) SetTxNonce(nonce uint64) {
if args.Extra != nil {
args.Extra.Sequence = &nonce
} else {
args.Extra = &AllExtras{Sequence: &nonce}
}
}
// GetUniqueSwapIdentifier get unique swap identifier
func (args *BuildTxArgs) GetUniqueSwapIdentifier() string {
fromChainID := args.FromChainID
swapID := args.SwapID
logIndex := args.LogIndex
if common.IsHexHash(swapID) {
swapID = common.HexToHash(swapID).Hex()
}
return fmt.Sprintf("%v:%v:%v", fromChainID, swapID, logIndex)
}