This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
/
entity.go
328 lines (271 loc) · 7.83 KB
/
entity.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
Copyright 2017 Loopring Project Ltd (Loopring Foundation).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package types
import (
"encoding/json"
"fmt"
util "github.com/Loopring/relay-lib/marketutil"
"github.com/Loopring/relay-lib/types"
"github.com/ethereum/go-ethereum/common"
"math/big"
)
type TransactionEntity struct {
From common.Address `json:"from"`
To common.Address `json:"to"`
Protocol common.Address `json:"protocol"`
BlockNumber int64 `json:"block_number"`
Hash common.Hash `json:"hash"`
LogIndex int64 `json:"log_index"`
Value *big.Int `json:"value"`
Content string `json:"content"`
Status types.TxStatus `json:"status"`
GasLimit *big.Int `json:"gas_limit"`
GasUsed *big.Int `json:"gas_used"`
GasPrice *big.Int `json:"gas_price"`
Nonce *big.Int `json:"nonce"`
BlockTime int64 `json:"block_time"`
}
type ApproveContent struct {
Owner string `json:"owner"`
Spender string `json:"spender"`
Amount string `json:"amount"`
}
type CancelContent struct {
OrderHash string `json:"order_hash"`
Amount string `json:"amount"`
}
type CutoffContent struct {
Owner string `json:"owner"`
CutoffTimeStamp int64 `json:"cutoff"`
}
type CutoffPairContent struct {
Owner string `json:"owner"`
Token1 string `json:"token1"`
Token2 string `json:"token2"`
CutoffTimeStamp int64 `json:"cutoff"`
}
type WethWithdrawalContent struct {
Src string `json:"src"`
Amount string `json:"amount"`
}
type WethDepositContent struct {
Dst string `json:"dst"`
Amount string `json:"amount"`
}
type TransferContent struct {
Sender string `json:"sender"`
Receiver string `json:"receiver"`
Amount string `json:"amount"`
}
type OrderFilledContent struct {
RingHash string `json:"ring_hash"`
OrderHash string `json:"order_hash"`
Owner string `json:"owner"`
TokenS string `json:"token_s"`
TokenB string `json:"token_b"`
RingIndex string `json:"ring_index"`
AmountS string `json:"amount_s"`
AmountB string `json:"amount_b"`
LrcReward string `json:"lrc_reward"`
LrcFee string `json:"lrc_fee"`
SplitS string `json:"split_s"`
SplitB string `json:"split_b"`
Market string `json:"market"`
FillIndex string `json:"fill_index"`
}
func (tx *TransactionEntity) FromApproveEvent(src *types.ApprovalEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content ApproveContent
content.Owner = src.Owner.Hex()
content.Spender = src.Spender.Hex()
content.Amount = src.Amount.String()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) FromCancelEvent(src *types.OrderCancelledEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content CancelContent
content.OrderHash = src.OrderHash.Hex()
content.Amount = src.AmountCancelled.String()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) FromCutoffEvent(src *types.CutoffEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content CutoffContent
content.Owner = src.Owner.Hex()
content.CutoffTimeStamp = src.Cutoff.Int64()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) FromCutoffPairEvent(src *types.CutoffPairEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content CutoffPairContent
content.Owner = src.Owner.Hex()
content.Token1 = src.Token1.Hex()
content.Token2 = src.Token2.Hex()
content.CutoffTimeStamp = src.Cutoff.Int64()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
// 充值和提现from和to都是用户钱包自己的地址,因为合约限制了发送方msg.sender
func (tx *TransactionEntity) FromWethDepositEvent(src *types.WethDepositEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content WethDepositContent
content.Dst = src.Dst.Hex()
content.Amount = src.Amount.String()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) FromWethWithdrawalEvent(src *types.WethWithdrawalEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content WethWithdrawalContent
content.Src = src.Src.Hex()
content.Amount = src.Amount.String()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) FromTransferEvent(src *types.TransferEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content TransferContent
content.Sender = src.Sender.Hex()
content.Receiver = src.Receiver.Hex()
content.Amount = src.Amount.String()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) FromEthTransferEvent(src *types.EthTransferEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
tx.Content = ""
return nil
}
func (tx *TransactionEntity) FromUnsupportedContractEvent(src *types.UnsupportedContractEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
tx.Content = ""
return nil
}
func (tx *TransactionEntity) FromOrderFilledEvent(src *types.OrderFilledEvent) error {
if err := tx.fullFilled(src.TxInfo); err != nil {
return err
}
var content OrderFilledContent
content.RingHash = src.Ringhash.Hex()
content.OrderHash = src.OrderHash.Hex()
content.TokenS = src.TokenS.Hex()
content.TokenB = src.TokenB.Hex()
content.RingIndex = src.RingIndex.String()
content.AmountS = src.AmountS.String()
content.AmountB = src.AmountB.String()
content.LrcReward = src.LrcReward.String()
content.LrcFee = src.LrcFee.String()
content.SplitS = src.SplitS.String()
content.SplitB = src.SplitB.String()
content.Market, _ = util.WrapMarketByAddress(src.TokenB.Hex(), src.TokenS.Hex())
content.FillIndex = src.FillIndex.String()
bs, err := json.Marshal(&content)
if err != nil {
return err
}
tx.Content = string(bs)
return nil
}
func (tx *TransactionEntity) fullFilled(src types.TxInfo) error {
if src.Nonce == nil || src.GasPrice == nil || src.GasLimit == nil || src.Value == nil {
return fmt.Errorf("transaction manager, full fill tx entity error: nonce/gas/gasPrice/value cann't be nill")
}
tx.Hash = src.TxHash
tx.Protocol = src.Protocol
tx.From = src.From
tx.To = src.To
tx.LogIndex = src.TxLogIndex
tx.Value = src.Value
tx.Status = src.Status
tx.GasLimit = src.GasLimit
tx.GasUsed = src.GasUsed
tx.GasPrice = src.GasPrice
tx.Nonce = src.Nonce
tx.BlockTime = src.BlockTime
if src.BlockNumber != nil {
tx.BlockNumber = src.BlockNumber.Int64()
} else {
tx.BlockNumber = 0
}
if src.GasUsed != nil {
tx.GasUsed = src.GasUsed
} else {
tx.GasUsed = big.NewInt(0)
}
return nil
}
// Compare return true: is the same
func (tx *TransactionEntity) Compare(src *TransactionEntity) bool {
if tx.Hash != src.Hash {
return false
}
if tx.LogIndex != src.LogIndex {
return false
}
if tx.Nonce != src.Nonce {
return false
}
if tx.Status != src.Status {
return false
}
return true
}