This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
/
order_tx.go
91 lines (72 loc) · 2.91 KB
/
order_tx.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
/*
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 dao
import (
omtyp "github.com/Loopring/relay-cluster/ordermanager/types"
"github.com/Loopring/relay-lib/types"
"github.com/ethereum/go-ethereum/common"
)
type OrderPendingTransaction struct {
ID int `gorm:"column:id;primary_key;"`
Owner string `gorm:"column:owner;type:varchar(42)"`
OrderHash string `gorm:"column:order_hash;type:varchar(82)"`
OrderStatus uint8 `gorm:"column:order_status;type:tinyint(4)"`
TxHash string `gorm:"column:tx_hash;type:varchar(82)"`
Nonce int64 `gorm:"column:nonce;type:bigint"`
}
// convert types/orderTxRecord to dao/ordertx
func (tx *OrderPendingTransaction) ConvertDown(src *omtyp.OrderTx) error {
tx.OrderHash = src.OrderHash.Hex()
tx.OrderStatus = uint8(src.OrderStatus)
tx.TxHash = src.TxHash.Hex()
tx.Owner = src.Owner.Hex()
tx.Nonce = src.Nonce
return nil
}
func (tx *OrderPendingTransaction) ConvertUp(dst *omtyp.OrderTx) error {
dst.OrderHash = common.HexToHash(tx.OrderHash)
dst.TxHash = common.HexToHash(tx.TxHash)
dst.Owner = common.HexToAddress(tx.Owner)
dst.OrderStatus = types.OrderStatus(tx.OrderStatus)
dst.Nonce = tx.Nonce
return nil
}
func (s *RdsService) FindPendingOrderTx(txhash, orderhash common.Hash) (*OrderPendingTransaction, error) {
var tx OrderPendingTransaction
err := s.Db.Where("tx_hash=?", txhash.Hex()).Where("order_hash=?", orderhash.Hex()).First(&tx).Error
return &tx, err
}
func (s *RdsService) GetPendingOrderTxs(owner common.Address) ([]OrderPendingTransaction, error) {
var list []OrderPendingTransaction
err := s.Db.Where("owner=?", owner.Hex()).Find(&list).Error
return list, err
}
func (s *RdsService) GetPendingOrderTxSortedByNonce(owner common.Address, orderhash common.Hash) ([]OrderPendingTransaction, error) {
var list []OrderPendingTransaction
err := s.Db.Where("owner=?", owner.Hex()).Where("order_hash=?", orderhash.Hex()).Order("nonce DESC").Find(&list).Error
return list, err
}
func (s *RdsService) DelPendingOrderTx(owner common.Address, orderhash common.Hash, txhashlist []common.Hash) int64 {
if len(txhashlist) == 0 {
return 0
}
var list []string
for _, v := range txhashlist {
list = append(list, v.Hex())
}
return s.Db.Delete(&OrderPendingTransaction{}).
Where("owner=?", owner.Hash()).
Where("order_hash=?", orderhash.Hex()).
Where("tx_hash in (?)", list).
RowsAffected
}