-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.go
executable file
·202 lines (184 loc) · 8.13 KB
/
types.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
// Package types holds common types that are used across a variety of
// interfaces.
package types
import (
"encoding/json"
"errors"
"math/big"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/core/types"
"github.com/map-bgp/browserbook/browserbook-mesh/zeroex"
)
// Stats is the return value for core.GetStats. Also used in the browser interface.
type Stats struct {
Version string `json:"version"`
PubSubTopic string `json:"pubSubTopic"`
Rendezvous string `json:"rendezvous"`
SecondaryRendezvous []string `json:"secondaryRendezvous"`
PeerID string `json:"peerID"`
EthereumChainID int `json:"ethereumChainID"`
LatestBlock LatestBlock `json:"latestBlock"`
NumPeers int `json:"numPeers"`
NumOrders int `json:"numOrders"`
NumOrdersIncludingRemoved int `json:"numOrdersIncludingRemoved"`
NumPinnedOrders int `json:"numPinnedOrders"`
MaxExpirationTime *big.Int `json:"maxExpirationTime"`
StartOfCurrentUTCDay time.Time `json:"startOfCurrentUTCDay"`
EthRPCRequestsSentInCurrentUTCDay int `json:"ethRPCRequestsSentInCurrentUTCDay"`
EthRPCRateLimitExpiredRequests int64 `json:"ethRPCRateLimitExpiredRequests"`
}
// LatestBlock is the latest block processed by the Mesh node.
type LatestBlock struct {
Number *big.Int `json:"number"`
Hash common.Hash `json:"hash"`
}
// GetOrdersResponse is the return value for core.GetOrders. Also used in the
// browser interface.
type GetOrdersResponse struct {
Timestamp time.Time `json:"timestamp"`
OrdersInfos []*OrderInfo `json:"ordersInfos"`
}
// AddOrdersOpts is a set of options for core.AddOrders. Also used in the
// browser interface.
type AddOrdersOpts struct {
// Pinned determines whether or not the added orders should be pinned. Pinned
// orders will not be affected by any DDoS prevention or incentive mechanisms
// and will always stay in storage until they are no longer fillable. Defaults
// to true.
Pinned bool `json:"pinned"`
}
// OrderInfo represents an fillable order and how much it could be filled for.
type OrderInfo struct {
OrderHash common.Hash `json:"orderHash"`
SignedOrder *zeroex.SignedOrder `json:"signedOrder"`
FillableTakerAssetAmount *big.Int `json:"fillableTakerAssetAmount"`
}
type orderInfoJSON struct {
OrderHash string `json:"orderHash"`
SignedOrder *zeroex.SignedOrder `json:"signedOrder"`
FillableTakerAssetAmount string `json:"fillableTakerAssetAmount"`
}
// MarshalJSON is a custom Marshaler for OrderInfo
func (o OrderInfo) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"orderHash": o.OrderHash.Hex(),
"signedOrder": o.SignedOrder,
"fillableTakerAssetAmount": o.FillableTakerAssetAmount.String(),
})
}
// UnmarshalJSON implements a custom JSON unmarshaller for the OrderEvent type
func (o *OrderInfo) UnmarshalJSON(data []byte) error {
var orderInfoJSON orderInfoJSON
err := json.Unmarshal(data, &orderInfoJSON)
if err != nil {
return err
}
o.OrderHash = common.HexToHash(orderInfoJSON.OrderHash)
o.SignedOrder = orderInfoJSON.SignedOrder
var ok bool
o.FillableTakerAssetAmount, ok = math.ParseBig256(orderInfoJSON.FillableTakerAssetAmount)
if !ok {
return errors.New("Invalid uint256 number encountered for FillableTakerAssetAmount")
}
return nil
}
type OrderWithMetadata struct {
Hash common.Hash `json:"hash"`
ChainID *big.Int `json:"chainID"`
ExchangeAddress common.Address `json:"exchangeAddress"`
MakerAddress common.Address `json:"makerAddress"`
MakerAssetData []byte `json:"makerAssetData"`
MakerFeeAssetData []byte `json:"makerFeeAssetData"`
MakerAssetAmount *big.Int `json:"makerAssetAmount"`
MakerFee *big.Int `json:"makerFee"`
TakerAddress common.Address `json:"takerAddress"`
TakerAssetData []byte `json:"takerAssetData"`
TakerFeeAssetData []byte `json:"takerFeeAssetData"`
TakerAssetAmount *big.Int `json:"takerAssetAmount"`
TakerFee *big.Int `json:"takerFee"`
SenderAddress common.Address `json:"senderAddress"`
FeeRecipientAddress common.Address `json:"feeRecipientAddress"`
ExpirationTimeSeconds *big.Int `json:"expirationTimeSeconds"`
Salt *big.Int `json:"salt"`
Signature []byte `json:"signature"`
FillableTakerAssetAmount *big.Int `json:"fillableTakerAssetAmount"`
LastUpdated time.Time `json:"lastUpdated"`
// Was this order flagged for removal? Due to the possibility of block-reorgs, instead
// of immediately removing an order when FillableTakerAssetAmount becomes 0, we instead
// flag it for removal. After this order isn't updated for X time and has IsRemoved = true,
// the order can be permanently deleted.
IsRemoved bool `json:"isRemoved"`
// IsPinned indicates whether or not the order is pinned. Pinned orders are
// not removed from the database unless they become unfillable.
IsPinned bool `json:"isPinned"`
// JSON-encoded list of assetdatas contained in MakerAssetData. For non-MAP
// orders, the list contains only one element which is equal to MakerAssetData.
// For MAP orders, it contains each component assetdata.
ParsedMakerAssetData []*SingleAssetData `json:"parsedMakerAssetData"`
// Same as ParsedMakerAssetData but for MakerFeeAssetData instead of MakerAssetData.
ParsedMakerFeeAssetData []*SingleAssetData `json:"parsedMakerFeeAssetData"`
// LastValidatedBlockNumber is the block number at which the order was
// last validated.
LastValidatedBlockNumber *big.Int `json:"lastValidatedBlockNumber"`
// LastValidatedBlockHash is the hash of the block at which the order was
// last validated.
LastValidatedBlockHash common.Hash `json:"lastValidatedBlockHash"`
}
func (order OrderWithMetadata) SignedOrder() *zeroex.SignedOrder {
return &zeroex.SignedOrder{
Order: zeroex.Order{
ChainID: order.ChainID,
ExchangeAddress: order.ExchangeAddress,
MakerAddress: order.MakerAddress,
MakerAssetData: order.MakerAssetData,
MakerFeeAssetData: order.MakerFeeAssetData,
MakerAssetAmount: order.MakerAssetAmount,
MakerFee: order.MakerFee,
TakerAddress: order.TakerAddress,
TakerAssetData: order.TakerAssetData,
TakerFeeAssetData: order.TakerFeeAssetData,
TakerAssetAmount: order.TakerAssetAmount,
TakerFee: order.TakerFee,
SenderAddress: order.SenderAddress,
FeeRecipientAddress: order.FeeRecipientAddress,
ExpirationTimeSeconds: order.ExpirationTimeSeconds,
Salt: order.Salt,
},
Signature: order.Signature,
}
}
type SingleAssetData struct {
Address common.Address `json:"address"`
TokenID *big.Int `json:"tokenID"`
}
type MiniHeader struct {
Hash common.Hash `json:"hash"`
Parent common.Hash `json:"parent"`
Number *big.Int `json:"number"`
Timestamp time.Time `json:"timestamp"`
Logs []types.Log `json:"logs"`
}
type Metadata struct {
EthereumChainID int
EthRPCRequestsSentInCurrentUTCDay int
StartOfCurrentUTCDay time.Time
}
// HexToBytes converts the the given hex string (with or without the "0x" prefix)
// to a slice of bytes. If the string is "0x" it returns nil.
func HexToBytes(s string) []byte {
if s == "0x" {
return nil
}
return common.FromHex(s)
}
// BytesToHex converts the given slice of bytes to a hex string with a "0x" prefix.
// If b is nil or has length 0, it returns "0x".
func BytesToHex(b []byte) string {
if len(b) == 0 {
return "0x"
}
return hexutil.Encode(b)
}