This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
/
cutoffpair.go
101 lines (84 loc) · 3.42 KB
/
cutoffpair.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
/*
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 (
"github.com/Loopring/relay-lib/types"
"github.com/ethereum/go-ethereum/common"
"math/big"
)
type CutOffPairEvent struct {
ID int `gorm:"column:id;primary_key;"`
Protocol string `gorm:"column:contract_address;type:varchar(42)"`
DelegateAddress string `gorm:"column:delegate_address;type:varchar(42)"`
Owner string `gorm:"column:owner;type:varchar(42)"`
Token1 string `gorm:"column:token1;type:varchar(42)"`
Token2 string `gorm:"column:token2;type:varchar(42)"`
TxHash string `gorm:"column:tx_hash;type:varchar(82)"`
OrderHashList string `gorm:"column:order_hash_list;type:text"`
BlockNumber int64 `gorm:"column:block_number"`
LogIndex int64 `gorm:"column:log_index"`
Cutoff int64 `gorm:"column:cutoff"`
CreateTime int64 `gorm:"column:create_time"`
Status uint8 `gorm:"column:status"`
Fork bool `gorm:"column:fork"`
}
// convert types/cutoffPairEvent to dao/cutoffPairEvent
func (e *CutOffPairEvent) ConvertDown(src *types.CutoffPairEvent) error {
e.Owner = src.Owner.Hex()
e.Protocol = src.Protocol.Hex()
e.DelegateAddress = src.DelegateAddress.Hex()
e.TxHash = src.TxHash.Hex()
e.Token1 = src.Token1.Hex()
e.Token2 = src.Token2.Hex()
e.Cutoff = src.Cutoff.Int64()
e.LogIndex = src.TxLogIndex
e.BlockNumber = src.BlockNumber.Int64()
e.CreateTime = src.BlockTime
e.Status = uint8(src.Status)
e.OrderHashList = MarshalHashListToStr(src.OrderHashList)
return nil
}
// convert dao/cutoffPairEvent to types/cutoffPairEvent
func (e *CutOffPairEvent) ConvertUp(dst *types.CutoffPairEvent) error {
dst.Owner = common.HexToAddress(e.Owner)
dst.Protocol = common.HexToAddress(e.Protocol)
dst.DelegateAddress = common.HexToAddress(e.DelegateAddress)
dst.TxHash = common.HexToHash(e.TxHash)
dst.Token1 = common.HexToAddress(e.Token1)
dst.Token2 = common.HexToAddress(e.Token2)
dst.BlockNumber = big.NewInt(e.BlockNumber)
dst.Cutoff = big.NewInt(e.Cutoff)
dst.TxLogIndex = e.LogIndex
dst.BlockTime = e.CreateTime
dst.Status = types.TxStatus(e.Status)
dst.OrderHashList = UnmarshalStrToHashList(e.OrderHashList)
return nil
}
func (s *RdsService) GetCutoffPairEvent(txhash common.Hash) (CutOffPairEvent, error) {
var event CutOffPairEvent
err := s.Db.Where("tx_hash=?", txhash.Hex()).Where("fork=?", false).First(&event).Error
return event, err
}
func (s *RdsService) GetCutoffPairForkEvents(from, to int64) ([]CutOffPairEvent, error) {
var (
list []CutOffPairEvent
err error
)
err = s.Db.Where("block_number > ? and block_number <= ?", from, to).
Where("fork = ?", false).
Find(&list).Error
return list, err
}
func (s *RdsService) RollBackCutoffPair(from, to int64) error {
return s.Db.Model(&CutOffPairEvent{}).Where("block_number > ? and block_number <= ?", from, to).Update("fork", true).Error
}