forked from ProofSuite/amp-matching-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pair.go
217 lines (170 loc) · 4.58 KB
/
pair.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
package daos
import (
"time"
"github.com/Proofsuite/amp-matching-engine/app"
"github.com/Proofsuite/amp-matching-engine/types"
"github.com/ethereum/go-ethereum/common"
mgo "github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
// PairDao contains:
// collectionName: MongoDB collection name
// dbName: name of mongodb to interact with
type PairDao struct {
collectionName string
dbName string
}
type PairDaoOption = func(*PairDao) error
func PairDaoDBOption(dbName string) func(dao *PairDao) error {
return func(dao *PairDao) error {
dao.dbName = dbName
return nil
}
}
// NewPairDao returns a new instance of AddressDao
func NewPairDao(options ...PairDaoOption) *PairDao {
dao := &PairDao{}
dao.collectionName = "pairs"
dao.dbName = app.Config.DBName
for _, op := range options {
err := op(dao)
if err != nil {
panic(err)
}
}
index := mgo.Index{
Key: []string{"baseTokenAddress", "quoteTokenAddress"},
Unique: true,
}
err := db.Session.DB(dao.dbName).C(dao.collectionName).EnsureIndex(index)
if err != nil {
panic(err)
}
return dao
}
// Create function performs the DB insertion task for pair collection
func (dao *PairDao) Create(pair *types.Pair) error {
pair.ID = bson.NewObjectId()
pair.CreatedAt = time.Now()
pair.UpdatedAt = time.Now()
err := db.Create(dao.dbName, dao.collectionName, pair)
return err
}
// GetAll function fetches all the pairs in the pair collection of mongodb.
func (dao *PairDao) GetAll() ([]types.Pair, error) {
var res []types.Pair
sort := []string{"-rank"}
err := db.GetAndSort(dao.dbName, dao.collectionName, bson.M{}, sort, 0, 0, &res)
if err != nil {
return nil, err
}
return res, nil
}
func (dao *PairDao) GetDefaultPairs() ([]types.Pair, error) {
var res []types.Pair
sort := []string{"-rank"}
query := bson.M{"active": true, "listed": true, "rank": bson.M{"$gte": 5}}
err := db.GetAndSort(dao.dbName, dao.collectionName, query, sort, 0, 0, &res)
if err != nil {
logger.Error(err)
return nil, err
}
if res == nil {
res = []types.Pair{}
}
return res, nil
}
func (dao *PairDao) GetListedPairs() ([]types.Pair, error) {
var res []types.Pair
sort := []string{"-rank"}
err := db.GetAndSort(dao.dbName, dao.collectionName, bson.M{"active": true, "listed": true}, sort, 0, 0, &res)
if err != nil {
logger.Error(err)
return nil, err
}
if res == nil {
res = []types.Pair{}
}
return res, nil
}
func (dao *PairDao) GetUnlistedPairs() ([]types.Pair, error) {
var res []types.Pair
sort := []string{"-rank"}
err := db.GetAndSort(dao.dbName, dao.collectionName, bson.M{"active": true, "listed": false}, sort, 0, 0, &res)
if err != nil {
logger.Error(err)
return nil, err
}
if res == nil {
res = []types.Pair{}
}
return res, nil
}
func (dao *PairDao) GetActivePairs() ([]types.Pair, error) {
var res []types.Pair
q := bson.M{"active": true}
sort := []string{"-rank"}
err := db.GetAndSort(dao.dbName, dao.collectionName, q, sort, 0, 0, &res)
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, nil
}
return res, nil
}
// GetByID function fetches details of a pair using pair's mongo ID.
func (dao *PairDao) GetByID(id bson.ObjectId) (*types.Pair, error) {
var response *types.Pair
err := db.GetByID(dao.dbName, dao.collectionName, id, &response)
return response, err
}
// GetByName function fetches details of a pair using pair's name.
// It makes CASE INSENSITIVE search query one pair's name
func (dao *PairDao) GetByName(name string) (*types.Pair, error) {
var res []*types.Pair
q := bson.M{"name": bson.RegEx{
Pattern: name,
Options: "i",
}}
err := db.Get(dao.dbName, dao.collectionName, q, 0, 1, &res)
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, nil
}
return res[0], nil
}
func (dao *PairDao) GetByTokenSymbols(baseTokenSymbol, quoteTokenSymbol string) (*types.Pair, error) {
var res []*types.Pair
q := bson.M{
"baseTokenSymbol": baseTokenSymbol,
"quoteTokenSymbol": quoteTokenSymbol,
}
err := db.Get(dao.dbName, dao.collectionName, q, 0, 1, &res)
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, nil
}
return res[0], nil
}
// GetByTokenAddress function fetches pair based on
// CONTRACT ADDRESS of base token and quote token
func (dao *PairDao) GetByTokenAddress(baseToken, quoteToken common.Address) (*types.Pair, error) {
var res []*types.Pair
q := bson.M{
"baseTokenAddress": baseToken.Hex(),
"quoteTokenAddress": quoteToken.Hex(),
}
err := db.Get(dao.dbName, dao.collectionName, q, 0, 1, &res)
if err != nil {
return nil, err
}
if len(res) == 0 {
return nil, nil
}
return res[0], nil
}