forked from 0xProject/0x-mesh
/
types.go
51 lines (44 loc) · 1.59 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
package rpc
import (
"encoding/json"
"errors"
"math/big"
"github.com/0xProject/0x-mesh/zeroex"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math"
)
// 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
}