-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtoken.go
256 lines (209 loc) · 6.11 KB
/
token.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
package daos
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/tomochain/tomox-sdk/app"
"github.com/tomochain/tomox-sdk/types"
)
// TokenDao contains:
// collectionName: MongoDB collection name
// dbName: name of mongodb to interact with
type TokenDao struct {
collectionName string
dbName string
}
// NewTokenDao returns a new instance of TokenDao.
func NewTokenDao() *TokenDao {
dbName := app.Config.DBName
collection := "tokens"
index := mgo.Index{
Key: []string{"contractAddress", "relayerAddress"},
Unique: true,
}
db.Session.DB(dbName).C(collection).EnsureIndex(index)
return &TokenDao{collection, dbName}
}
// NewLendingTokenDao lending dao
func NewLendingTokenDao() *TokenDao {
dbName := app.Config.DBName
collection := "lending_tokens"
index := mgo.Index{
Key: []string{"contractAddress", "relayerAddress"},
Unique: true,
}
db.Session.DB(dbName).C(collection).EnsureIndex(index)
return &TokenDao{collection, dbName}
}
// NewCollateralTokenDao lending dao
func NewCollateralTokenDao() *TokenDao {
dbName := app.Config.DBName
collection := "collateral_tokens"
index := mgo.Index{
Key: []string{"contractAddress", "relayerAddress"},
Unique: true,
}
db.Session.DB(dbName).C(collection).EnsureIndex(index)
return &TokenDao{collection, dbName}
}
// Create function performs the DB insertion task for token collection
func (dao *TokenDao) Create(token *types.Token) error {
if err := token.Validate(); err != nil {
logger.Error(err)
return err
}
token.ID = bson.NewObjectId()
token.CreatedAt = time.Now()
token.UpdatedAt = time.Now()
err := db.Create(dao.dbName, dao.collectionName, token)
if err != nil {
logger.Error(err)
return err
}
return nil
}
// GetAll function fetches all the tokens in the token collection of mongodb.
func (dao *TokenDao) GetAll() ([]types.Token, error) {
var res []types.Token
err := db.Get(dao.dbName, dao.collectionName, bson.M{}, 0, 0, &res)
if err != nil {
logger.Error(err)
return nil, err
}
ret := []types.Token{}
keys := make(map[string]bool)
for _, it := range res {
code := it.ContractAddress.Hex()
if _, value := keys[code]; !value {
keys[code] = true
ret = append(ret, it)
}
}
return ret, nil
}
func (dao *TokenDao) GetAllByCoinbase(addr common.Address) ([]types.Token, error) {
var response []types.Token
err := db.Get(dao.dbName, dao.collectionName, bson.M{"relayerAddress": addr.Hex()}, 0, 0, &response)
if err != nil {
logger.Error(err)
return nil, err
}
return response, nil
}
// GetQuote function fetches all the quote tokens in the token collection of mongodb.
func (dao *TokenDao) GetQuoteTokens() ([]types.Token, error) {
var response []types.Token
err := db.Get(dao.dbName, dao.collectionName, bson.M{"quote": true}, 0, 0, &response)
if err != nil {
logger.Error(err)
return nil, err
}
return response, nil
}
// GetBase function fetches all the base tokens in the token collection of mongodb.
func (dao *TokenDao) GetBaseTokens() ([]types.Token, error) {
var response []types.Token
err := db.Get(dao.dbName, dao.collectionName, bson.M{"quote": false}, 0, 0, &response)
if err != nil {
logger.Error(err)
return nil, err
}
return response, nil
}
// GetByID function fetches details of a token based on its mongo id
func (dao *TokenDao) GetByID(id bson.ObjectId) (*types.Token, error) {
var response *types.Token
err := db.GetByID(dao.dbName, dao.collectionName, id, &response)
if err != nil {
logger.Error(err)
return nil, err
}
return response, nil
}
func (dao *TokenDao) GetBySymbol(symbol string) (*types.Token, error) {
q := bson.M{"symbol": symbol}
var resp []types.Token
err := db.Get(dao.dbName, dao.collectionName, q, 0, 1, &resp)
if err != nil {
logger.Error(err)
return nil, err
}
if len(resp) == 0 {
return nil, nil
}
return &resp[0], nil
}
// GetByAddress function fetches details of a token based on its contract address
func (dao *TokenDao) GetByAddress(addr common.Address) (*types.Token, error) {
q := bson.M{"contractAddress": addr.Hex()}
var resp []types.Token
err := db.Get(dao.dbName, dao.collectionName, q, 0, 1, &resp)
if err != nil {
logger.Error(err)
return nil, err
}
if len(resp) == 0 {
return nil, nil
}
return &resp[0], nil
}
func (dao *TokenDao) UpdateFiatPriceBySymbol(symbol string, price float64) error {
q := bson.M{"symbol": symbol}
update := bson.M{"$set": bson.M{"usd": fmt.Sprintf("%f", price)}}
err := db.Update(dao.dbName, dao.collectionName, q, update)
if err != nil {
logger.Error(err)
return err
}
return nil
}
func (dao *TokenDao) UpdateByToken(addr common.Address, token *types.Token) error {
q := bson.M{"contractAddress": addr.Hex()}
update := bson.M{
"$set": bson.M{
"makeFee": token.MakeFee.String(),
"takeFee": token.TakeFee.String(),
},
}
err := db.Update(dao.dbName, dao.collectionName, q, update)
if err != nil {
logger.Error(err)
return err
}
return nil
}
func (dao *TokenDao) UpdateByTokenAndCoinbase(addr common.Address, coinbase common.Address, token *types.Token) error {
q := bson.M{"contractAddress": addr.Hex(), "relayerAddress": coinbase.Hex()}
update := bson.M{
"$set": bson.M{
"makeFee": token.MakeFee.String(),
"takeFee": token.TakeFee.String(),
},
}
err := db.Update(dao.dbName, dao.collectionName, q, update)
if err != nil {
logger.Error(err)
return err
}
return nil
}
// Drop drops all the order documents in the current database
func (dao *TokenDao) Drop() error {
err := db.DropCollection(dao.dbName, dao.collectionName)
if err != nil {
logger.Error(err)
return err
}
return nil
}
// DeleteByToken delete token by contract address
func (dao *TokenDao) DeleteByToken(contractAddress common.Address) error {
query := bson.M{"contractAddress": contractAddress.Hex()}
return db.RemoveItem(dao.dbName, dao.collectionName, query)
}
func (dao *TokenDao) DeleteByTokenAndCoinbase(contractAddress common.Address, addr common.Address) error {
query := bson.M{"contractAddress": contractAddress.Hex(), "relayerAddress": addr.Hex()}
return db.RemoveItem(dao.dbName, dao.collectionName, query)
}