/
token.go
146 lines (120 loc) · 3.39 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
package daos
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
"github.com/tomochain/dex-server/app"
"github.com/tomochain/dex-server/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"},
Unique: true,
}
err := db.Session.DB(dbName).C(collection).EnsureIndex(index)
if err != nil {
panic(err)
}
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 response []types.Token
err := db.Get(dao.dbName, dao.collectionName, bson.M{}, 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
}
// 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
}
// 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
}