/
trade.go
357 lines (305 loc) · 9.12 KB
/
trade.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package domain
import (
"time"
"github.com/google/uuid"
"github.com/shopspring/decimal"
)
var (
// SwapParserManager ...
SwapParserManager SwapParser
tradeTypes = map[TradeType]string{
TradeBuy: "BUY",
TradeSell: "SELL",
}
)
type TradeType int
func (t TradeType) String() string {
str, ok := tradeTypes[t]
if !ok {
str = "UNKNOWN"
}
return str
}
// Status represents the different statuses that a trade can assume.
type TradeStatus struct {
Code int
Failed bool
}
// Trade is the data structure representing a trade entity.
type Trade struct {
Id string
Type TradeType
MarketName string
MarketBaseAsset string
MarketQuoteAsset string
MarketPrice MarketPrice
MarketPercentageFee MarketFee
MarketFixedFee MarketFee
FeeAsset string
FeeAmount uint64
TraderPubkey []byte
Status TradeStatus
PsetBase64 string
TxId string
TxHex string
ExpiryTime int64
SettlementTime int64
SwapRequest *Swap
SwapAccept *Swap
SwapComplete *Swap
SwapFail *Swap
}
// NewTrade returns a trade with a new id and Empty status.
func NewTrade() *Trade {
return &Trade{Id: uuid.New().String(), Status: TradeStatus{Code: TradeStatusCodeUndefined}}
}
// Propose brings an Empty trade to the Propose status by first validating the
// provided arguments.
func (t *Trade) Propose(
tradeType TradeType, swapRequest SwapRequest,
mktName, mktBaseAsset, mktQuoteAsset string,
mktPercentageFee, mktFixedFee MarketFee,
traderPubkey []byte,
) (bool, error) {
if t.Status.Code >= TradeStatusCodeProposal {
return true, nil
}
if _, ok := tradeTypes[tradeType]; !ok {
return false, ErrTradeUnknownType
}
t.Type = tradeType
t.MarketName = mktName
t.MarketBaseAsset = mktBaseAsset
t.MarketQuoteAsset = mktQuoteAsset
t.TraderPubkey = traderPubkey
t.SwapRequest = &Swap{
Id: swapRequest.GetId(),
Timestamp: time.Now().Unix(),
}
t.PsetBase64 = swapRequest.GetTransaction()
t.Status.Code = TradeStatusCodeProposal
t.FeeAsset = swapRequest.FeeAsset
t.FeeAmount = swapRequest.FeeAmount
msg, errCode := SwapParserManager.SerializeRequest(swapRequest)
if errCode >= 0 {
t.Fail(swapRequest.GetId(), errCode)
return false, nil
}
price := calculateMarketMarketPrice(swapRequest, mktQuoteAsset)
t.SwapRequest.Message = msg
t.MarketPrice = price
t.MarketPercentageFee = mktPercentageFee
t.MarketFixedFee = mktFixedFee
return true, nil
}
// Accept brings a trade from the Proposal to the Accepted status by validating
// the provided argument against the SwapRequest message and sets its
// expiration time.
func (t *Trade) Accept(
psetBase64 string, unblindedIns []UnblindedInput, expiryTime int64,
) (bool, error) {
if t.Status.Code >= TradeStatusCodeAccepted {
return true, nil
}
if t.Status.Code != TradeStatusCodeProposal {
return false, ErrTradeMustBeProposal
}
if !time.Unix(expiryTime, 0).After(time.Unix(t.SwapRequest.Timestamp, 0)) {
return false, ErrTradeInvalidExpiryTime
}
swapAcceptID, swapAcceptMsg, errCode := SwapParserManager.SerializeAccept(
t.SwapRequest.Message, psetBase64, unblindedIns,
)
if errCode >= 0 {
t.Fail(t.SwapRequest.Id, errCode)
return false, nil
}
t.ExpiryTime = expiryTime
t.Status.Code = TradeStatusCodeAccepted
t.SwapAccept = &Swap{
Id: swapAcceptID,
Message: swapAcceptMsg,
Timestamp: time.Now().Unix(),
}
t.PsetBase64 = psetBase64
return true, nil
}
// Complete brings a trade from the Accepted to the Completed status by
// checking that the given PSET completes the one of the SwapAccept message
// and by finalizing it and extracting the raw tx in hex format. Complete must
// be called before the trade expires, otherwise it won't be possible to
// actually complete an accepted trade.
func (t *Trade) Complete(tx string) (bool, error) {
if t.Status.Code >= TradeStatusCodeCompleted {
return true, nil
}
if !t.IsAccepted() {
return false, ErrTradeMustBeAccepted
}
if t.IsExpired() {
if !t.Status.Failed {
t.Status.Failed = true
}
return false, ErrTradeExpired
}
txDetails, errCode := SwapParserManager.ParseSwapTransaction(tx)
if errCode >= 0 {
t.Fail(t.SwapAccept.Id, errCode)
return false, nil
}
swapCompleteId, swapCompleteMsg, errCode := SwapParserManager.SerializeComplete(
t.SwapAccept.Message,
tx,
)
if errCode >= 0 {
t.Fail(t.SwapAccept.Id, errCode)
return false, nil
}
t.SwapComplete = &Swap{
Id: swapCompleteId,
Message: swapCompleteMsg,
Timestamp: time.Now().Unix(),
}
t.Status.Code = TradeStatusCodeCompleted
t.TxHex = txDetails.TxHex
t.TxId = txDetails.Txid
if len(t.PsetBase64) > 0 {
t.PsetBase64 = txDetails.PsetBase64
}
return true, nil
}
// Settle brings the trade from the Completed to the Settled status, unsets the
// expiration time and adds the timestamp of the settlement (it must be a
// blocktime).
func (t *Trade) Settle(settlementTime int64) (bool, error) {
if t.Status.Code == TradeStatusCodeSettled {
return true, nil
}
if !(t.IsCompleted() || t.IsAccepted()) || t.Status.Failed {
return false, ErrTradeMustBeCompletedOrAccepted
}
t.ExpiryTime = 0
t.SettlementTime = settlementTime
t.Status.Code = TradeStatusCodeSettled
return true, nil
}
// Fail marks the current status of the trade as Failed and adds the SwapFail
// message.
func (t *Trade) Fail(swapID string, errCode int) {
if t.Status.Failed {
return
}
swapFailID, swapFailMsg := SwapParserManager.SerializeFail(swapID, errCode)
t.SwapFail = &Swap{
Id: swapFailID,
Message: swapFailMsg,
}
t.Status.Failed = true
}
// Expire brings the trade to the Expired status if its expiration date was
// previosly set. This infers that it must be in any of the Accepted, Completed,
// or related failed statuses. This method makes also sure that the expiration
// date has passed before changing the status.
func (t *Trade) Expire() (bool, error) {
if t.Status.Code == TradeStatusCodeExpired {
return true, nil
}
if t.ExpiryTime <= 0 {
return false, ErrTradeNullExpiryTime
}
if time.Now().Before(time.Unix(t.ExpiryTime, 0)) {
return false, ErrTradeExpiryTimeNotReached
}
t.Status.Code = TradeStatusCodeExpired
return true, nil
}
// IsEmpty returns whether the Trade is empty.
func (t *Trade) IsEmpty() bool {
return t.Status.Code == TradeStatusCodeUndefined
}
// IsProposal returns whether the trade is in Proposal status.
func (t *Trade) IsProposal() bool {
return t.Status.Code == TradeStatusCodeProposal
}
// IsAccepted returns whether the trade is in Accepted status.
func (t *Trade) IsAccepted() bool {
return t.Status.Code == TradeStatusCodeAccepted
}
// IsCompleted returns whether the trade is in Completed status.
func (t *Trade) IsCompleted() bool {
return t.Status.Code == TradeStatusCodeCompleted
}
// IsSettled returns whether the trade is in Settled status.
func (t *Trade) IsSettled() bool {
return t.Status.Code == TradeStatusCodeSettled
}
// IsRejected returns whether the trade has failed.
func (t *Trade) IsRejected() bool {
return t.Status.Failed
}
// IsExpired returns whether the trade is in Expired status, or if its
// expiration date has passed.
func (t *Trade) IsExpired() bool {
return t.Status.Code == TradeStatusCodeExpired ||
(t.ExpiryTime > 0 && time.Now().After(time.Unix(t.ExpiryTime, 0)))
}
// ContainsSwap returns whether a swap identified by its id belongs to the
// current trade.
func (t *Trade) ContainsSwap(swapID string) bool {
return swapID == t.SwapRequest.Id ||
swapID == t.SwapAccept.Id ||
swapID == t.SwapComplete.Id
}
// SwapRequestMessage returns the deserialized swap request message.
func (t *Trade) SwapRequestMessage() *SwapRequest {
if t.IsEmpty() {
return nil
}
return SwapParserManager.DeserializeRequest(
t.SwapRequest.Message, t.FeeAsset, t.FeeAmount,
)
}
// SwapAcceptMessage returns the deserialized swap accept message, if defined.
func (t *Trade) SwapAcceptMessage() *SwapAccept {
if t.IsEmpty() || t.IsProposal() {
return nil
}
return SwapParserManager.DeserializeAccept(t.SwapAccept.Message)
}
// SwapCompleteMessage returns the deserialized swap complete message, if defined.
func (t *Trade) SwapCompleteMessage() *SwapComplete {
if !t.IsCompleted() {
return nil
}
return SwapParserManager.DeserializeComplete(t.SwapComplete.Message)
}
// SwapFailMessage returns the deserialized swap fail message, if defined.
func (t *Trade) SwapFailMessage() *SwapFail {
if !t.IsRejected() {
return nil
}
return SwapParserManager.DeserializeFail(t.SwapFail.Message)
}
func calculateMarketMarketPrice(
swapRequest SwapRequest,
marketQuoteAsset string,
) (price MarketPrice) {
pricePR := decimal.NewFromInt(int64(swapRequest.GetAmountP())).Div(
decimal.NewFromInt(int64(swapRequest.GetAmountR())),
).Truncate(8)
priceRP := decimal.NewFromInt(int64(swapRequest.GetAmountR())).Div(
decimal.NewFromInt(int64(swapRequest.GetAmountP())),
).Truncate(8)
if swapRequest.GetAssetP() == marketQuoteAsset {
price = MarketPrice{
BasePrice: priceRP.String(),
QuotePrice: pricePR.String(),
}
} else {
price = MarketPrice{
BasePrice: pricePR.String(),
QuotePrice: priceRP.String(),
}
}
return
}