-
Notifications
You must be signed in to change notification settings - Fork 0
/
trade.go
147 lines (132 loc) · 5.62 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
package tomox
import (
"github.com/tomochain/tomochain/tomox/tomox_state"
"math/big"
"time"
"github.com/globalsign/mgo/bson"
"github.com/tomochain/tomochain/common"
"github.com/tomochain/tomochain/crypto/sha3"
)
const (
TradeStatusSuccess = "SUCCESS"
TradeTakerOrderHash = "takerOrderHash"
TradeMakerOrderHash = "makerOrderHash"
TradeTimestamp = "timestamp"
TradeQuantity = "quantity"
TradeMakerExchange = "makerExAddr"
TradeMaker = "uAddr"
TradeBaseToken = "bToken"
TradeQuoteToken = "qToken"
TradePrice = "tradedPrice"
MakerOrderType = "makerOrderType"
)
type Trade struct {
Taker common.Address `json:"taker" bson:"taker"`
Maker common.Address `json:"maker" bson:"maker"`
BaseToken common.Address `json:"baseToken" bson:"baseToken"`
QuoteToken common.Address `json:"quoteToken" bson:"quoteToken"`
MakerOrderHash common.Hash `json:"makerOrderHash" bson:"makerOrderHash"`
TakerOrderHash common.Hash `json:"takerOrderHash" bson:"takerOrderHash"`
MakerExchange common.Address `json:"makerExchange" bson:"makerExchange"`
TakerExchange common.Address `json:"takerExchange" bson:"takerExchange"`
Hash common.Hash `json:"hash" bson:"hash"`
TxHash common.Hash `json:"txHash" bson:"txHash"`
PairName string `json:"pairName" bson:"pairName"`
PricePoint *big.Int `json:"pricepoint" bson:"pricepoint"`
Amount *big.Int `json:"amount" bson:"amount"`
MakeFee *big.Int `json:"makeFee" bson:"makeFee"`
TakeFee *big.Int `json:"takeFee" bson:"takeFee"`
Status string `json:"status" bson:"status"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
TakerOrderSide string `json:"takerOrderSide" bson:"takerOrderSide"`
TakerOrderType string `json:"takerOrderType" bson:"takerOrderType"`
MakerOrderType string `json:"makerOrderType" bson:"makerOrderType"`
}
type TradeBSON struct {
Taker string `json:"taker" bson:"taker"`
Maker string `json:"maker" bson:"maker"`
BaseToken string `json:"baseToken" bson:"baseToken"`
QuoteToken string `json:"quoteToken" bson:"quoteToken"`
MakerOrderHash string `json:"makerOrderHash" bson:"makerOrderHash"`
TakerOrderHash string `json:"takerOrderHash" bson:"takerOrderHash"`
MakerExchange string `json:"makerExchange" bson:"makerExchange"`
TakerExchange string `json:"takerExchange" bson:"takerExchange"`
Hash string `json:"hash" bson:"hash"`
TxHash string `json:"txHash" bson:"txHash"`
PairName string `json:"pairName" bson:"pairName"`
Amount string `json:"amount" bson:"amount"`
MakeFee string `json:"makeFee" bson:"makeFee"`
TakeFee string `json:"takeFee" bson:"takeFee"`
PricePoint string `json:"pricepoint" bson:"pricepoint"`
Status string `json:"status" bson:"status"`
CreatedAt time.Time `json:"createdAt" bson:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" bson:"updatedAt"`
TakerOrderSide string `json:"takerOrderSide" bson:"takerOrderSide"`
TakerOrderType string `json:"takerOrderType" bson:"takerOrderType"`
MakerOrderType string `json:"makerOrderType" bson:"makerOrderType"`
}
func (t *Trade) GetBSON() (interface{}, error) {
tr := TradeBSON{
PairName: t.PairName,
Maker: t.Maker.Hex(),
Taker: t.Taker.Hex(),
BaseToken: t.BaseToken.Hex(),
QuoteToken: t.QuoteToken.Hex(),
MakerOrderHash: t.MakerOrderHash.Hex(),
TakerOrderHash: t.TakerOrderHash.Hex(),
MakerExchange: t.MakerExchange.Hex(),
TakerExchange: t.TakerExchange.Hex(),
Hash: t.Hash.Hex(),
TxHash: t.TxHash.Hex(),
CreatedAt: t.CreatedAt,
UpdatedAt: t.UpdatedAt,
PricePoint: t.PricePoint.String(),
Status: t.Status,
Amount: t.Amount.String(),
MakeFee: t.MakeFee.String(),
TakeFee: t.TakeFee.String(),
TakerOrderSide: t.TakerOrderSide,
TakerOrderType: t.TakerOrderType,
MakerOrderType: t.MakerOrderType,
}
return tr, nil
}
func (t *Trade) SetBSON(raw bson.Raw) error {
decoded := &TradeBSON{}
err := raw.Unmarshal(decoded)
if err != nil {
return err
}
t.PairName = decoded.PairName
t.Taker = common.HexToAddress(decoded.Taker)
t.Maker = common.HexToAddress(decoded.Maker)
t.BaseToken = common.HexToAddress(decoded.BaseToken)
t.QuoteToken = common.HexToAddress(decoded.QuoteToken)
t.MakerOrderHash = common.HexToHash(decoded.MakerOrderHash)
t.TakerOrderHash = common.HexToHash(decoded.TakerOrderHash)
t.MakerExchange = common.HexToAddress(decoded.MakerExchange)
t.TakerExchange = common.HexToAddress(decoded.TakerExchange)
t.Hash = common.HexToHash(decoded.Hash)
t.TxHash = common.HexToHash(decoded.TxHash)
t.Status = decoded.Status
t.Amount = tomox_state.ToBigInt(decoded.Amount)
t.PricePoint = tomox_state.ToBigInt(decoded.PricePoint)
t.MakeFee = tomox_state.ToBigInt(decoded.MakeFee)
t.TakeFee = tomox_state.ToBigInt(decoded.TakeFee)
t.CreatedAt = decoded.CreatedAt
t.UpdatedAt = decoded.UpdatedAt
t.TakerOrderSide = decoded.TakerOrderSide
t.TakerOrderType = decoded.TakerOrderType
t.MakerOrderType = decoded.MakerOrderType
return nil
}
// ComputeHash returns hashes the trade
// The OrderHash, Amount, Taker and TradeNonce attributes must be
// set before attempting to compute the trade hash
func (t *Trade) ComputeHash() common.Hash {
sha := sha3.NewKeccak256()
sha.Write(t.MakerOrderHash.Bytes())
sha.Write(t.TakerOrderHash.Bytes())
return common.BytesToHash(sha.Sum(nil))
}