-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathmodels.go
251 lines (221 loc) · 6.65 KB
/
models.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
// Copyright 2019 GitBitEx.com
//
// 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
//
// https://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 models
import (
"fmt"
"github.com/shopspring/decimal"
"time"
)
// 用于表示一笔订单或者交易的方向:买,卖
type Side string
func NewSideFromString(s string) (*Side, error) {
side := Side(s)
switch side {
case SideBuy:
case SideSell:
default:
return nil, fmt.Errorf("invalid side: %v", s)
}
return &side, nil
}
func (s Side) Opposite() Side {
if s == SideBuy {
return SideSell
}
return SideBuy
}
func (s Side) String() string {
return string(s)
}
// 订单类型
type OrderType string
func (t OrderType) String() string {
return string(t)
}
// 用于表示订单状态
type OrderStatus string
func NewOrderStatusFromString(s string) (*OrderStatus, error) {
status := OrderStatus(s)
switch status {
case OrderStatusNew:
case OrderStatusOpen:
case OrderStatusCancelling:
case OrderStatusCancelled:
case OrderStatusFilled:
default:
return nil, fmt.Errorf("invalid status: %v", s)
}
return &status, nil
}
func (t OrderStatus) String() string {
return string(t)
}
// 用于表示账单类型
type BillType string
// 用于表示一条fill完成的原因
type DoneReason string
type TransactionStatus string
const (
OrderTypeLimit = OrderType("limit")
OrderTypeMarket = OrderType("market")
SideBuy = Side("buy")
SideSell = Side("sell")
// 初始状态
OrderStatusNew = OrderStatus("new")
// 已经加入orderBook
OrderStatusOpen = OrderStatus("open")
// 中间状态,请求取消订单
OrderStatusCancelling = OrderStatus("cancelling")
// 订单已经被取消,部分成交的订单也是cancelled
OrderStatusCancelled = OrderStatus("cancelled")
// 订单完全成交
OrderStatusFilled = OrderStatus("filled")
BillTypeTrade = BillType("trade")
DoneReasonFilled = DoneReason("filled")
DoneReasonCancelled = DoneReason("cancelled")
TransactionStatusPending = TransactionStatus("pending")
TransactionStatusCompleted = TransactionStatus("completed")
)
type User struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
UserId int64
Email string
PasswordHash string
}
type Account struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
UserId int64 `gorm:"column:user_id;unique_index:idx_uid_currency"`
Currency string `gorm:"column:currency;unique_index:idx_uid_currency"`
Hold decimal.Decimal `gorm:"column:hold" sql:"type:decimal(32,16);"`
Available decimal.Decimal `gorm:"column:available" sql:"type:decimal(32,16);"`
}
type Bill struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
UserId int64
Currency string
Available decimal.Decimal `sql:"type:decimal(32,16);"`
Hold decimal.Decimal `sql:"type:decimal(32,16);"`
Type BillType
Settled bool
Notes string
}
type Product struct {
Id string `gorm:"column:id;primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
BaseCurrency string
QuoteCurrency string
BaseMinSize decimal.Decimal `sql:"type:decimal(32,16);"`
BaseMaxSize decimal.Decimal `sql:"type:decimal(32,16);"`
QuoteMinSize decimal.Decimal `sql:"type:decimal(32,16);"`
QuoteMaxSize decimal.Decimal `sql:"type:decimal(32,16);"`
BaseScale int32
QuoteScale int32
QuoteIncrement float64
}
type Order struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
ProductId string
UserId int64
ClientOid string
Size decimal.Decimal `sql:"type:decimal(32,16);"`
Funds decimal.Decimal `sql:"type:decimal(32,16);"`
FilledSize decimal.Decimal `sql:"type:decimal(32,16);"`
ExecutedValue decimal.Decimal `sql:"type:decimal(32,16);"`
Price decimal.Decimal `sql:"type:decimal(32,16);"`
FillFees decimal.Decimal `sql:"type:decimal(32,16);"`
Type OrderType
Side Side
TimeInForce string
Status OrderStatus
Settled bool
}
type Fill struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
TradeId int64
OrderId int64 `gorm:"unique_index:o_m"`
MessageSeq int64 `gorm:"unique_index:o_m"`
ProductId string
Size decimal.Decimal `sql:"type:decimal(32,16);"`
Price decimal.Decimal `sql:"type:decimal(32,16);"`
Funds decimal.Decimal `sql:"type:decimal(32,16);"`
Fee decimal.Decimal `sql:"type:decimal(32,16);"`
Liquidity string
Settled bool
Side Side
Done bool
DoneReason DoneReason
LogOffset int64
LogSeq int64
}
type Trade struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
ProductId string
TakerOrderId int64
MakerOrderId int64
Price decimal.Decimal `sql:"type:decimal(32,16);"`
Size decimal.Decimal `sql:"type:decimal(32,16);"`
Side Side
Time time.Time
LogOffset int64
LogSeq int64
}
type Tick struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
ProductId string `gorm:"unique_index:p_g_t"`
Granularity int64 `gorm:"unique_index:p_g_t"`
Time int64 `gorm:"unique_index:p_g_t"`
Open decimal.Decimal `sql:"type:decimal(32,16);"`
High decimal.Decimal `sql:"type:decimal(32,16);"`
Low decimal.Decimal `sql:"type:decimal(32,16);"`
Close decimal.Decimal `sql:"type:decimal(32,16);"`
Volume decimal.Decimal `sql:"type:decimal(32,16);"`
LogOffset int64
LogSeq int64
}
type Config struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
Key string
Value string
}
type Transaction struct {
Id int64 `gorm:"column:id;primary_key;AUTO_INCREMENT"`
CreatedAt time.Time
UpdatedAt time.Time
UserId int64
Currency string
BlockNum int
ConfirmNum int
Status TransactionStatus
FromAddress string
ToAddress string
Note string
TxId string
}